001    // SECTION-START[License Header]
002    // <editor-fold defaultstate="collapsed" desc=" Generated License ">
003    /*
004     *   Java Object Management and Configuration
005     *   Copyright (C) Christian Schulte, 2005-206
006     *   All rights reserved.
007     *
008     *   Redistribution and use in source and binary forms, with or without
009     *   modification, are permitted provided that the following conditions
010     *   are met:
011     *
012     *     o Redistributions of source code must retain the above copyright
013     *       notice, this list of conditions and the following disclaimer.
014     *
015     *     o Redistributions in binary form must reproduce the above copyright
016     *       notice, this list of conditions and the following disclaimer in
017     *       the documentation and/or other materials provided with the
018     *       distribution.
019     *
020     *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
021     *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
022     *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
023     *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
024     *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
025     *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
026     *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
027     *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
028     *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
029     *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030     *
031     *   $JOMC: DefaultObjectManagerTest.java 4459 2012-03-27 23:55:31Z schulte2005 $
032     *
033     */
034    // </editor-fold>
035    // SECTION-END
036    package org.jomc.ri.test;
037    
038    import java.net.URI;
039    import java.util.Locale;
040    import org.jomc.ObjectManager;
041    import org.jomc.model.Instance;
042    import org.jomc.ri.DefaultObjectManager;
043    import org.jomc.ri.test.support.TestScopeSpecification;
044    import org.jomc.ri.test.support.TestSpecification;
045    import org.jomc.ri.test.support.TestSpecificationMany;
046    import org.jomc.ri.test.support.TestSpecificationOne;
047    import org.jomc.ri.test.support.TestSpecificationOneMore;
048    import org.jomc.spi.Scope;
049    import org.junit.Test;
050    import static org.junit.Assert.assertNotNull;
051    import static org.junit.Assert.assertNull;
052    import static org.junit.Assert.assertTrue;
053    import static org.junit.Assert.fail;
054    
055    // SECTION-START[Documentation]
056    // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
057    /**
058     * Test cases for the {@code DefaultObjectManager} class.
059     *
060     * <dl>
061     *   <dt><b>Identifier:</b></dt><dd>JOMC :: RI :: Tests :: Default Object Manager Test</dd>
062     *   <dt><b>Name:</b></dt><dd>JOMC RI</dd>
063     *   <dt><b>Abstract:</b></dt><dd>No</dd>
064     *   <dt><b>Final:</b></dt><dd>No</dd>
065     *   <dt><b>Stateless:</b></dt><dd>No</dd>
066     * </dl>
067     *
068     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
069     * @version 1.2.2
070     */
071    // </editor-fold>
072    // SECTION-END
073    // SECTION-START[Annotations]
074    // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
075    @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" )
076    // </editor-fold>
077    // SECTION-END
078    public class DefaultObjectManagerTest
079    {
080        // SECTION-START[DefaultObjectManagerTest]
081    
082        /** The {@code DefaultObjectManager} tests are performed with. */
083        private DefaultObjectManager objectManager;
084    
085        /**
086         * Gets the instance tests are performed with.
087         *
088         * @return The instance tests are performed with.
089         *
090         * @see #newObjectManager()
091         */
092        public DefaultObjectManager getObjectManager()
093        {
094            if ( this.objectManager == null )
095            {
096                this.objectManager = this.newObjectManager();
097            }
098    
099            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    }