View Javadoc

1   /*
2    *  jDTAUS Core RI Client Container
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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   * Tests the {@code DefaultContainer} implementation.
31   *
32   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
33   * @version $JDTAUS: DefaultContainerTest.java 8709 2012-10-02 21:07:40Z schulte $
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       * Tests the {@link Container#getObject(Class, String)} method to return the
48       * correctly initialized instance of {@link TestImplementation}.
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       * Tests the {@link Container#getObject(Class, String)} method to return the
63       * same instance of {@link TestImplementation} for successive calls.
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       * Tests that model properties are correctly provided to implementations.
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      * Checks the given {@code TestSpecification} to provide the correct
116      * property values.
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      * Checks that dependency cycles are detected correctly by throwing a
141      * corresponding {@link DependencyCycleException}.
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      * Tests the {@link Container#getObject(Class, String)} method to corectly
162      * detect non-serializable implementation classes in context scope.
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      * Tests the {@link Container#getObject(Class, String)} method to return an
175      * array of correctly initialized instances of {@link TestImplementation}
176      * when given a {@code null} value for the implementation name argument.
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 }