View Javadoc

1   // SECTION-START[License Header]
2   // <editor-fold defaultstate="collapsed" desc=" Generated License ">
3   /*
4    *   Java Object Management and Configuration
5    *   Copyright (C) Christian Schulte, 2005-206
6    *   All rights reserved.
7    *
8    *   Redistribution and use in source and binary forms, with or without
9    *   modification, are permitted provided that the following conditions
10   *   are met:
11   *
12   *     o Redistributions of source code must retain the above copyright
13   *       notice, this list of conditions and the following disclaimer.
14   *
15   *     o Redistributions in binary form must reproduce the above copyright
16   *       notice, this list of conditions and the following disclaimer in
17   *       the documentation and/or other materials provided with the
18   *       distribution.
19   *
20   *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
21   *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22   *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23   *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
24   *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25   *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26   *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27   *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28   *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29   *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   *   $JOMC: DefaultObjectManagerTest.java 4588 2012-06-03 06:01:30Z schulte2005 $
32   *
33   */
34  // </editor-fold>
35  // SECTION-END
36  package org.jomc.ri.test;
37  
38  import java.net.URI;
39  import java.util.Locale;
40  import org.jomc.ObjectManager;
41  import org.jomc.model.Instance;
42  import org.jomc.model.Modules;
43  import org.jomc.ri.DefaultObjectManager;
44  import org.jomc.ri.test.support.TestScopeSpecification;
45  import org.jomc.ri.test.support.TestSpecification;
46  import org.jomc.ri.test.support.TestSpecificationMany;
47  import org.jomc.ri.test.support.TestSpecificationOne;
48  import org.jomc.ri.test.support.TestSpecificationOneMore;
49  import org.jomc.spi.Scope;
50  import org.junit.Test;
51  import static org.junit.Assert.assertNotNull;
52  import static org.junit.Assert.assertNull;
53  import static org.junit.Assert.assertTrue;
54  import static org.junit.Assert.fail;
55  
56  // SECTION-START[Documentation]
57  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
58  /**
59   * Test cases for the {@code DefaultObjectManager} class.
60   *
61   * <dl>
62   *   <dt><b>Identifier:</b></dt><dd>JOMC :: RI :: Tests :: Default Object Manager Test</dd>
63   *   <dt><b>Name:</b></dt><dd>JOMC RI</dd>
64   *   <dt><b>Abstract:</b></dt><dd>No</dd>
65   *   <dt><b>Final:</b></dt><dd>No</dd>
66   *   <dt><b>Stateless:</b></dt><dd>No</dd>
67   * </dl>
68   *
69   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
70   * @version 1.3
71   */
72  // </editor-fold>
73  // SECTION-END
74  // SECTION-START[Annotations]
75  // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
76  @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.3", comments = "See http://jomc.sourceforge.net/jomc/1.3/jomc-tools-1.3" )
77  // </editor-fold>
78  // SECTION-END
79  public class DefaultObjectManagerTest
80  {
81      // SECTION-START[DefaultObjectManagerTest]
82  
83      /** The {@code DefaultObjectManager} tests are performed with. */
84      private DefaultObjectManager objectManager;
85  
86      /**
87       * Gets the instance tests are performed with.
88       *
89       * @return The instance tests are performed with.
90       *
91       * @see #newObjectManager()
92       */
93      public DefaultObjectManager getObjectManager()
94      {
95          if ( this.objectManager == null )
96          {
97              this.objectManager = this.newObjectManager();
98          }
99  
100         return this.objectManager;
101     }
102 
103     /**
104      * Creates a new {@code DefaultObjectManager} instance to test.
105      *
106      * @return A new {@code DefaultObjectManager} instance to test.
107      *
108      * @see #getObjectManager()
109      */
110     protected DefaultObjectManager newObjectManager()
111     {
112         return new DefaultObjectManager();
113     }
114 
115     @Test
116     @SuppressWarnings( "deprecation" )
117     public final void testNullPointerException() throws Exception
118     {
119         try
120         {
121             DefaultObjectManager.getClassLoader( (Class) null );
122             fail( "Expected NullPointerException not thrown." );
123         }
124         catch ( final NullPointerException e )
125         {
126             assertNullPointerException( e );
127         }
128 
129         try
130         {
131             this.getObjectManager().getDefaultClassLoader( (Class<?>) null );
132             fail( "Expected NullPointerException not thrown." );
133         }
134         catch ( final NullPointerException e )
135         {
136             assertNullPointerException( e );
137         }
138 
139         try
140         {
141             this.getObjectManager().getDefaultLocator( null );
142             fail( "Expected NullPointerException not thrown." );
143         }
144         catch ( final NullPointerException e )
145         {
146             assertNullPointerException( e );
147         }
148         try
149         {
150             this.getObjectManager().getDefaultLocator( null, new URI( "file:///tmp" ) );
151             fail( "Expected NullPointerException not thrown." );
152         }
153         catch ( final NullPointerException e )
154         {
155             assertNullPointerException( e );
156         }
157 
158         try
159         {
160             this.getObjectManager().getDefaultScope( null );
161             fail( "Expected NullPointerException not thrown." );
162         }
163         catch ( final NullPointerException e )
164         {
165             assertNullPointerException( e );
166         }
167         try
168         {
169             this.getObjectManager().getDefaultScope( new Modules(), null );
170             fail( "Expected NullPointerException not thrown." );
171         }
172         catch ( final NullPointerException e )
173         {
174             assertNullPointerException( e );
175         }
176         try
177         {
178             this.getObjectManager().getDefaultScope( null, "Singleton" );
179             fail( "Expected NullPointerException not thrown." );
180         }
181         catch ( final NullPointerException e )
182         {
183             assertNullPointerException( e );
184         }
185 
186         try
187         {
188             this.getObjectManager().getDependency( null, null );
189             fail( "Expected NullPointerException not thrown." );
190         }
191         catch ( final NullPointerException e )
192         {
193             assertNullPointerException( e );
194         }
195 
196         try
197         {
198             this.getObjectManager().getDependency( this, null );
199             fail( "Expected NullPointerException not thrown." );
200         }
201         catch ( final NullPointerException e )
202         {
203             assertNullPointerException( e );
204         }
205 
206         try
207         {
208             this.getObjectManager().getLocator( null, null );
209             fail( "Expected NullPointerException not thrown." );
210         }
211         catch ( final NullPointerException e )
212         {
213             assertNullPointerException( e );
214         }
215 
216         try
217         {
218             this.getObjectManager().getLocator( new URI( "TEST" ), null );
219             fail( "Expected NullPointerException not thrown." );
220         }
221         catch ( final NullPointerException e )
222         {
223             assertNullPointerException( e );
224         }
225 
226         try
227         {
228             this.getObjectManager().getLocator( null, this.getClass().getClassLoader() );
229             fail( "Expected NullPointerException not thrown." );
230         }
231         catch ( final NullPointerException e )
232         {
233             assertNullPointerException( e );
234         }
235 
236         try
237         {
238             this.getObjectManager().getMessage( null, null, null );
239             fail( "Expected NullPointerException not thrown." );
240         }
241         catch ( final NullPointerException e )
242         {
243             assertNullPointerException( e );
244         }
245 
246         try
247         {
248             this.getObjectManager().getMessage( this, null, null );
249             fail( "Expected NullPointerException not thrown." );
250         }
251         catch ( final NullPointerException e )
252         {
253             assertNullPointerException( e );
254         }
255 
256         try
257         {
258             this.getObjectManager().getMessage( this, "", null );
259             fail( "Expected NullPointerException not thrown." );
260         }
261         catch ( final NullPointerException e )
262         {
263             assertNullPointerException( e );
264         }
265 
266         try
267         {
268             this.getObjectManager().getObject( null );
269             fail( "Expected NullPointerException not thrown." );
270         }
271         catch ( final NullPointerException e )
272         {
273             assertNullPointerException( e );
274         }
275 
276         try
277         {
278             this.getObjectManager().getObject( (Scope) null, (Instance) null, null );
279             fail( "Expected NullPointerException not thrown." );
280         }
281         catch ( final NullPointerException e )
282         {
283             assertNullPointerException( e );
284         }
285 
286         try
287         {
288             this.getObjectManager().getObject( (Scope) null, new Instance(), null );
289             fail( "Expected NullPointerException not thrown." );
290         }
291         catch ( final NullPointerException e )
292         {
293             assertNullPointerException( e );
294         }
295 
296         try
297         {
298             this.getObjectManager().getObject( (Class<?>) null, null, null );
299             fail( "Expected NullPointerException not thrown." );
300         }
301         catch ( final NullPointerException e )
302         {
303             assertNullPointerException( e );
304         }
305 
306         try
307         {
308             this.getObjectManager().getObject( this.getClass(), null, null );
309             fail( "Expected NullPointerException not thrown." );
310         }
311         catch ( final NullPointerException e )
312         {
313             assertNullPointerException( e );
314         }
315 
316         try
317         {
318             this.getObjectManager().getObject( this.getClass(), new URI( "/" ), null );
319             fail( "Expected NullPointerException not thrown." );
320         }
321         catch ( final NullPointerException e )
322         {
323             assertNullPointerException( e );
324         }
325 
326         try
327         {
328             this.getObjectManager().getProperty( null, null );
329             fail( "Expected NullPointerException not thrown." );
330         }
331         catch ( final NullPointerException e )
332         {
333             assertNullPointerException( e );
334         }
335 
336         try
337         {
338             this.getObjectManager().getProperty( this, null );
339             fail( "Expected NullPointerException not thrown." );
340         }
341         catch ( final NullPointerException e )
342         {
343             assertNullPointerException( e );
344         }
345 
346         try
347         {
348             this.getObjectManager().getScope( null, null );
349             fail( "Expected NullPointerException not thrown." );
350         }
351         catch ( final NullPointerException e )
352         {
353             assertNullPointerException( e );
354         }
355 
356         try
357         {
358             this.getObjectManager().getScope( "TEST", null );
359             fail( "Expected NullPointerException not thrown." );
360         }
361         catch ( final NullPointerException e )
362         {
363             assertNullPointerException( e );
364         }
365 
366         try
367         {
368             this.getObjectManager().getScope( null, this.getClass().getClassLoader() );
369             fail( "Expected NullPointerException not thrown." );
370         }
371         catch ( final NullPointerException e )
372         {
373             assertNullPointerException( e );
374         }
375 
376         try
377         {
378             this.getObjectManager().log( null, null, null );
379             fail( "Expected NullPointerException not thrown." );
380         }
381         catch ( final NullPointerException e )
382         {
383             assertNullPointerException( e );
384         }
385 
386         try
387         {
388             this.getObjectManager().log( null, null, null, null );
389             fail( "Expected NullPointerException not thrown." );
390         }
391         catch ( final NullPointerException e )
392         {
393             assertNullPointerException( e );
394         }
395 
396         try
397         {
398             this.getObjectManager().log( this.getClass().getClassLoader(), null, null, null );
399             fail( "Expected NullPointerException not thrown." );
400         }
401         catch ( final NullPointerException e )
402         {
403             assertNullPointerException( e );
404         }
405     }
406 
407     @Test
408     public final void testNotNull() throws Exception
409     {
410         assertNotNull( this.getObjectManager().getListeners() );
411         assertNotNull( this.getObjectManager().getModules( this.getClass().getClassLoader() ) );
412         assertNotNull( this.getObjectManager().getObject( TestSpecificationOne.class ) );
413         assertNotNull( this.getObjectManager().getObject( TestSpecificationOne.class,
414                                                           "JOMC :: RI :: Tests :: Implementation Test" ) );
415 
416         assertNotNull( this.getObjectManager().getObject( TestSpecificationMany[].class ) );
417         assertNotNull( this.getObjectManager().getObject( TestSpecificationMany.class,
418                                                           "JOMC :: RI :: Tests :: Implementation Test" ) );
419 
420         assertNotNull( this.getObjectManager().getObject( TestScopeSpecification[].class ) );
421         assertNotNull( this.getObjectManager().getObject( TestScopeSpecification.class,
422                                                           "JOMC :: RI :: Tests :: Implementation Test" ) );
423 
424     }
425 
426     @Test
427     public final void testNull() throws Exception
428     {
429         assertNull( this.getObjectManager().getObject( Object.class ) );
430         assertNull( this.getObjectManager().getObject( TestSpecification.class ) );
431         assertNull( this.getObjectManager().getObject( TestSpecificationOne[].class ) );
432         assertNull( this.getObjectManager().getObject( TestSpecificationOneMore.class ) );
433         assertNull( this.getObjectManager().getObject( TestSpecificationMany.class ) );
434         assertNull( this.getObjectManager().getObject( ObjectManager.class, "DOES NOT EXIST" ) );
435         assertNull( this.getObjectManager().getObject( TestSpecification.class, "DOES NOT EXIST" ) );
436         assertNull( this.getObjectManager().getDependency( this, "DOES NOT EXIST" ) );
437         assertNull( this.getObjectManager().getProperty( this, "DOES NOT EXIST" ) );
438         assertNull( this.getObjectManager().getMessage( this, "DOES NOT EXIST", Locale.getDefault() ) );
439         assertNull( this.getObjectManager().getObject( TestSpecificationOneMore.class,
440                                                        "JOMC :: RI :: Tests :: Illegal Location Implementation" ) );
441 
442         assertNull( this.getObjectManager().getObject( TestScopeSpecification.class,
443                                                        "JOMC :: RI :: Tests :: Illegal Location Implementation" ) );
444 
445     }
446 
447     @Test
448     public final void testGetObjectManager() throws Exception
449     {
450         final ObjectManager first = DefaultObjectManager.getObjectManager( this.getClass().getClassLoader() );
451         final ObjectManager second = DefaultObjectManager.getObjectManager( this.getClass().getClassLoader() );
452         final ObjectManager third = DefaultObjectManager.getObjectManager( this.getClass().getClassLoader() );
453 
454         assertTrue( first == second );
455         assertTrue( first == third );
456         assertTrue( second == third );
457     }
458 
459     public static void assertNullPointerException( final NullPointerException e )
460     {
461         assertNotNull( e.getMessage() );
462         System.out.println( e.toString() );
463     }
464 
465     // SECTION-END
466     // SECTION-START[Constructors]
467     // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
468     /** Creates a new {@code DefaultObjectManagerTest} instance. */
469     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.3", comments = "See http://jomc.sourceforge.net/jomc/1.3/jomc-tools-1.3" )
470     public DefaultObjectManagerTest()
471     {
472         // SECTION-START[Default Constructor]
473         super();
474         // SECTION-END
475     }
476     // </editor-fold>
477     // SECTION-END
478     // SECTION-START[Dependencies]
479     // SECTION-END
480     // SECTION-START[Properties]
481     // SECTION-END
482     // SECTION-START[Messages]
483     // SECTION-END
484 }