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 4459 2012-03-27 23:55:31Z 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.ri.DefaultObjectManager;
43  import org.jomc.ri.test.support.TestScopeSpecification;
44  import org.jomc.ri.test.support.TestSpecification;
45  import org.jomc.ri.test.support.TestSpecificationMany;
46  import org.jomc.ri.test.support.TestSpecificationOne;
47  import org.jomc.ri.test.support.TestSpecificationOneMore;
48  import org.jomc.spi.Scope;
49  import org.junit.Test;
50  import static org.junit.Assert.assertNotNull;
51  import static org.junit.Assert.assertNull;
52  import static org.junit.Assert.assertTrue;
53  import static org.junit.Assert.fail;
54  
55  // SECTION-START[Documentation]
56  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
57  /**
58   * Test cases for the {@code DefaultObjectManager} class.
59   *
60   * <dl>
61   *   <dt><b>Identifier:</b></dt><dd>JOMC :: RI :: Tests :: Default Object Manager Test</dd>
62   *   <dt><b>Name:</b></dt><dd>JOMC RI</dd>
63   *   <dt><b>Abstract:</b></dt><dd>No</dd>
64   *   <dt><b>Final:</b></dt><dd>No</dd>
65   *   <dt><b>Stateless:</b></dt><dd>No</dd>
66   * </dl>
67   *
68   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
69   * @version 1.2.2
70   */
71  // </editor-fold>
72  // SECTION-END
73  // SECTION-START[Annotations]
74  // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
75  @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
76  // </editor-fold>
77  // SECTION-END
78  public class DefaultObjectManagerTest
79  {
80      // SECTION-START[DefaultObjectManagerTest]
81  
82      /** The {@code DefaultObjectManager} tests are performed with. */
83      private DefaultObjectManager objectManager;
84  
85      /**
86       * Gets the instance tests are performed with.
87       *
88       * @return The instance tests are performed with.
89       *
90       * @see #newObjectManager()
91       */
92      public DefaultObjectManager getObjectManager()
93      {
94          if ( this.objectManager == null )
95          {
96              this.objectManager = this.newObjectManager();
97          }
98  
99          return this.objectManager;
100     }
101 
102     /**
103      * Creates a new {@code DefaultObjectManager} instance to test.
104      *
105      * @return A new {@code DefaultObjectManager} instance to test.
106      *
107      * @see #getObjectManager()
108      */
109     protected DefaultObjectManager newObjectManager()
110     {
111         return new DefaultObjectManager();
112     }
113 
114     @Test
115     @SuppressWarnings( "deprecation" )
116     public final void testNullPointerException() throws Exception
117     {
118         try
119         {
120             DefaultObjectManager.getClassLoader( (Class) null );
121             fail( "Expected NullPointerException not thrown." );
122         }
123         catch ( final NullPointerException e )
124         {
125             assertNullPointerException( e );
126         }
127 
128         try
129         {
130             this.getObjectManager().getDefaultClassLoader( (Class<?>) null );
131             fail( "Expected NullPointerException not thrown." );
132         }
133         catch ( final NullPointerException e )
134         {
135             assertNullPointerException( e );
136         }
137 
138         try
139         {
140             this.getObjectManager().getDefaultLocator( null );
141             fail( "Expected NullPointerException not thrown." );
142         }
143         catch ( final NullPointerException e )
144         {
145             assertNullPointerException( e );
146         }
147 
148         try
149         {
150             this.getObjectManager().getDefaultScope( null );
151             fail( "Expected NullPointerException not thrown." );
152         }
153         catch ( final NullPointerException e )
154         {
155             assertNullPointerException( e );
156         }
157 
158         try
159         {
160             this.getObjectManager().getDependency( null, null );
161             fail( "Expected NullPointerException not thrown." );
162         }
163         catch ( final NullPointerException e )
164         {
165             assertNullPointerException( e );
166         }
167 
168         try
169         {
170             this.getObjectManager().getDependency( this, null );
171             fail( "Expected NullPointerException not thrown." );
172         }
173         catch ( final NullPointerException e )
174         {
175             assertNullPointerException( e );
176         }
177 
178         try
179         {
180             this.getObjectManager().getLocator( null, null );
181             fail( "Expected NullPointerException not thrown." );
182         }
183         catch ( final NullPointerException e )
184         {
185             assertNullPointerException( e );
186         }
187 
188         try
189         {
190             this.getObjectManager().getLocator( new URI( "TEST" ), null );
191             fail( "Expected NullPointerException not thrown." );
192         }
193         catch ( final NullPointerException e )
194         {
195             assertNullPointerException( e );
196         }
197 
198         try
199         {
200             this.getObjectManager().getLocator( null, this.getClass().getClassLoader() );
201             fail( "Expected NullPointerException not thrown." );
202         }
203         catch ( final NullPointerException e )
204         {
205             assertNullPointerException( e );
206         }
207 
208         try
209         {
210             this.getObjectManager().getMessage( null, null, null );
211             fail( "Expected NullPointerException not thrown." );
212         }
213         catch ( final NullPointerException e )
214         {
215             assertNullPointerException( e );
216         }
217 
218         try
219         {
220             this.getObjectManager().getMessage( this, null, null );
221             fail( "Expected NullPointerException not thrown." );
222         }
223         catch ( final NullPointerException e )
224         {
225             assertNullPointerException( e );
226         }
227 
228         try
229         {
230             this.getObjectManager().getMessage( this, "", null );
231             fail( "Expected NullPointerException not thrown." );
232         }
233         catch ( final NullPointerException e )
234         {
235             assertNullPointerException( e );
236         }
237 
238         try
239         {
240             this.getObjectManager().getObject( null );
241             fail( "Expected NullPointerException not thrown." );
242         }
243         catch ( final NullPointerException e )
244         {
245             assertNullPointerException( e );
246         }
247 
248         try
249         {
250             this.getObjectManager().getObject( (Scope) null, (Instance) null, null );
251             fail( "Expected NullPointerException not thrown." );
252         }
253         catch ( final NullPointerException e )
254         {
255             assertNullPointerException( e );
256         }
257 
258         try
259         {
260             this.getObjectManager().getObject( (Scope) null, new Instance(), null );
261             fail( "Expected NullPointerException not thrown." );
262         }
263         catch ( final NullPointerException e )
264         {
265             assertNullPointerException( e );
266         }
267 
268         try
269         {
270             this.getObjectManager().getObject( (Class<?>) null, null, null );
271             fail( "Expected NullPointerException not thrown." );
272         }
273         catch ( final NullPointerException e )
274         {
275             assertNullPointerException( e );
276         }
277 
278         try
279         {
280             this.getObjectManager().getObject( this.getClass(), null, null );
281             fail( "Expected NullPointerException not thrown." );
282         }
283         catch ( final NullPointerException e )
284         {
285             assertNullPointerException( e );
286         }
287 
288         try
289         {
290             this.getObjectManager().getObject( this.getClass(), new URI( "/" ), null );
291             fail( "Expected NullPointerException not thrown." );
292         }
293         catch ( final NullPointerException e )
294         {
295             assertNullPointerException( e );
296         }
297 
298         try
299         {
300             this.getObjectManager().getProperty( null, null );
301             fail( "Expected NullPointerException not thrown." );
302         }
303         catch ( final NullPointerException e )
304         {
305             assertNullPointerException( e );
306         }
307 
308         try
309         {
310             this.getObjectManager().getProperty( this, null );
311             fail( "Expected NullPointerException not thrown." );
312         }
313         catch ( final NullPointerException e )
314         {
315             assertNullPointerException( e );
316         }
317 
318         try
319         {
320             this.getObjectManager().getScope( null, null );
321             fail( "Expected NullPointerException not thrown." );
322         }
323         catch ( final NullPointerException e )
324         {
325             assertNullPointerException( e );
326         }
327 
328         try
329         {
330             this.getObjectManager().getScope( "TEST", null );
331             fail( "Expected NullPointerException not thrown." );
332         }
333         catch ( final NullPointerException e )
334         {
335             assertNullPointerException( e );
336         }
337 
338         try
339         {
340             this.getObjectManager().getScope( null, this.getClass().getClassLoader() );
341             fail( "Expected NullPointerException not thrown." );
342         }
343         catch ( final NullPointerException e )
344         {
345             assertNullPointerException( e );
346         }
347 
348         try
349         {
350             this.getObjectManager().log( null, null, null );
351             fail( "Expected NullPointerException not thrown." );
352         }
353         catch ( final NullPointerException e )
354         {
355             assertNullPointerException( e );
356         }
357 
358         try
359         {
360             this.getObjectManager().log( null, null, null, null );
361             fail( "Expected NullPointerException not thrown." );
362         }
363         catch ( final NullPointerException e )
364         {
365             assertNullPointerException( e );
366         }
367 
368         try
369         {
370             this.getObjectManager().log( this.getClass().getClassLoader(), null, null, null );
371             fail( "Expected NullPointerException not thrown." );
372         }
373         catch ( final NullPointerException e )
374         {
375             assertNullPointerException( e );
376         }
377     }
378 
379     @Test
380     public final void testNotNull() throws Exception
381     {
382         assertNotNull( this.getObjectManager().getListeners() );
383         assertNotNull( this.getObjectManager().getModules( this.getClass().getClassLoader() ) );
384         assertNotNull( this.getObjectManager().getObject( TestSpecificationOne.class ) );
385         assertNotNull( this.getObjectManager().getObject( TestSpecificationOne.class,
386                                                           "JOMC :: RI :: Tests :: Implementation Test" ) );
387 
388         assertNotNull( this.getObjectManager().getObject( TestSpecificationMany[].class ) );
389         assertNotNull( this.getObjectManager().getObject( TestSpecificationMany.class,
390                                                           "JOMC :: RI :: Tests :: Implementation Test" ) );
391 
392         assertNotNull( this.getObjectManager().getObject( TestScopeSpecification[].class ) );
393         assertNotNull( this.getObjectManager().getObject( TestScopeSpecification.class,
394                                                           "JOMC :: RI :: Tests :: Implementation Test" ) );
395 
396     }
397 
398     @Test
399     public final void testNull() throws Exception
400     {
401         assertNull( this.getObjectManager().getObject( Object.class ) );
402         assertNull( this.getObjectManager().getObject( TestSpecification.class ) );
403         assertNull( this.getObjectManager().getObject( TestSpecificationOne[].class ) );
404         assertNull( this.getObjectManager().getObject( TestSpecificationOneMore.class ) );
405         assertNull( this.getObjectManager().getObject( TestSpecificationMany.class ) );
406         assertNull( this.getObjectManager().getObject( ObjectManager.class, "DOES NOT EXIST" ) );
407         assertNull( this.getObjectManager().getObject( TestSpecification.class, "DOES NOT EXIST" ) );
408         assertNull( this.getObjectManager().getDependency( this, "DOES NOT EXIST" ) );
409         assertNull( this.getObjectManager().getProperty( this, "DOES NOT EXIST" ) );
410         assertNull( this.getObjectManager().getMessage( this, "DOES NOT EXIST", Locale.getDefault() ) );
411         assertNull( this.getObjectManager().getObject( TestSpecificationOneMore.class,
412                                                        "JOMC :: RI :: Tests :: Illegal Location Implementation" ) );
413 
414         assertNull( this.getObjectManager().getObject( TestScopeSpecification.class,
415                                                        "JOMC :: RI :: Tests :: Illegal Location Implementation" ) );
416 
417     }
418 
419     @Test
420     public final void testGetObjectManager() throws Exception
421     {
422         final ObjectManager first = DefaultObjectManager.getObjectManager( this.getClass().getClassLoader() );
423         final ObjectManager second = DefaultObjectManager.getObjectManager( this.getClass().getClassLoader() );
424         final ObjectManager third = DefaultObjectManager.getObjectManager( this.getClass().getClassLoader() );
425 
426         assertTrue( first == second );
427         assertTrue( first == third );
428         assertTrue( second == third );
429     }
430 
431     public static void assertNullPointerException( final NullPointerException e )
432     {
433         assertNotNull( e.getMessage() );
434         System.out.println( e.toString() );
435     }
436 
437     // SECTION-END
438     // SECTION-START[Constructors]
439     // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
440     /** Creates a new {@code DefaultObjectManagerTest} instance. */
441     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
442     public DefaultObjectManagerTest()
443     {
444         // SECTION-START[Default Constructor]
445         super();
446         // SECTION-END
447     }
448     // </editor-fold>
449     // SECTION-END
450     // SECTION-START[Dependencies]
451     // SECTION-END
452     // SECTION-START[Properties]
453     // SECTION-END
454     // SECTION-START[Messages]
455     // SECTION-END
456 }