1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.jdtaus.core.container.ri.client.test;
22
23 import junit.framework.Assert;
24 import junit.framework.TestCase;
25 import org.jdtaus.core.container.Container;
26 import org.jdtaus.core.container.ContainerFactory;
27 import org.jdtaus.core.container.DependencyCycleException;
28
29
30
31
32
33
34
35 public class DefaultContainerTest extends TestCase
36 {
37
38 private static final String MODULE_NAME =
39 "jDTAUS Core ⁑ RI Client Container";
40
41 public Container getContainer()
42 {
43 return ContainerFactory.getContainer();
44 }
45
46
47
48
49
50 public void testInstantiation() throws Exception
51 {
52 assert this.getContainer() != null;
53
54 final TestSpecification spec =
55 (TestSpecification) this.getContainer().
56 getObject( TestSpecification.class.getName(), MODULE_NAME );
57
58 Assert.assertTrue( spec.isInitialized() );
59 }
60
61
62
63
64
65 public void testSingleton() throws Exception
66 {
67 assert this.getContainer() != null;
68
69 final TestSpecification spec1 =
70 (TestSpecification) this.getContainer().
71 getObject( TestSpecification.class.getName(), MODULE_NAME );
72
73 final TestSpecification spec2 =
74 (TestSpecification) this.getContainer().
75 getObject( TestSpecification.class.getName(), MODULE_NAME );
76
77 final TestSpecification spec3 =
78 (TestSpecification) this.getContainer().
79 getObject( TestSpecification.class.getName(), MODULE_NAME );
80
81 final TestSpecification child =
82 (TestSpecification) this.getContainer().
83 getObject( TestSpecification.class.getName(),
84 MODULE_NAME + " - Child" );
85
86 Assert.assertTrue( spec1 == spec2 );
87 Assert.assertTrue( spec2 == spec3 );
88 Assert.assertTrue( spec3 == spec1 );
89 Assert.assertFalse( child == spec1 );
90 Assert.assertFalse( child == spec2 );
91 Assert.assertFalse( child == spec3 );
92 }
93
94
95
96
97 public void testProperties()
98 {
99 final TestSpecification spec1 =
100 (TestSpecification) this.getContainer().
101 getObject( TestSpecification.class.getName(), MODULE_NAME );
102
103 final MultitonSpecification child =
104 (MultitonSpecification) this.getContainer().
105 getObject( MultitonSpecification.class.getName(),
106 MODULE_NAME + " - Child" );
107
108 this.assertValidProperties( spec1 );
109 this.assertValidProperties( child );
110 this.assertValidProperties( spec1.getDependency() );
111 this.assertValidProperties( child.getDependency() );
112 }
113
114
115
116
117
118 private void assertValidProperties( final TestSpecification s )
119 {
120 Assert.assertEquals( s.isBoolean(), true );
121 Assert.assertEquals( s.getByte(), (byte) 1 );
122 Assert.assertEquals( s.getChar(), 'X' );
123 Assert.assertTrue( s.getDouble() == 0.1D );
124 Assert.assertTrue( s.getFloat() == 0.1F );
125 Assert.assertEquals( s.getInt(), 1 );
126 Assert.assertEquals( s.getLong(), 1L );
127 Assert.assertEquals( s.getShort(), (short) 1 );
128 Assert.assertEquals( s.isBooleanObject(), Boolean.TRUE );
129 Assert.assertEquals( s.getByteObject(), new Byte( (byte) 1 ) );
130 Assert.assertEquals( s.getCharacterObject(), new Character( 'X' ) );
131 Assert.assertEquals( s.getDoubleObject(), new Double( 0.1D ) );
132 Assert.assertEquals( s.getFloatObject(), new Float( 0.1F ) );
133 Assert.assertEquals( s.getIntegerObject(), new Integer( 1 ) );
134 Assert.assertEquals( s.getLongObject(), new Long( 1L ) );
135 Assert.assertEquals( s.getShortObject(), new Short( (short) 1 ) );
136 Assert.assertEquals( s.getStringObject(), "TEST" );
137 }
138
139
140
141
142
143 public void testDependencyCycleDetection() throws Exception
144 {
145 try
146 {
147 this.getContainer().getObject(
148 CycleTestSpecification.class.getName(), MODULE_NAME +
149 " - Cycle Test" );
150
151 Assert.fail( "Dependency cycle not detected." );
152 }
153 catch ( DependencyCycleException e )
154 {
155 Assert.assertNotNull( e.getMessage() );
156 System.out.println( e.toString() );
157 }
158 }
159
160
161
162
163
164 public void testSerializable() throws Exception
165 {
166 assert this.getContainer() != null;
167
168 Assert.assertNotNull( this.getContainer().getObject(
169 ContextTestSpecification.class.getName(), MODULE_NAME ) );
170
171 }
172
173
174
175
176
177
178 public void testInstantiationNullImplementationName() throws Exception
179 {
180 assert this.getContainer() != null;
181
182 final TestSpecification[] spec =
183 (TestSpecification[]) this.getContainer().
184 getObject( TestSpecification.class.getName(), null );
185
186 for ( int i = spec.length - 1; i >= 0; i-- )
187 {
188 Assert.assertTrue( spec[i].isInitialized() );
189 }
190 }
191
192 }