001    /*
002     *   Copyright (C) Christian Schulte, 2005-206
003     *   All rights reserved.
004     *
005     *   Redistribution and use in source and binary forms, with or without
006     *   modification, are permitted provided that the following conditions
007     *   are met:
008     *
009     *     o Redistributions of source code must retain the above copyright
010     *       notice, this list of conditions and the following disclaimer.
011     *
012     *     o Redistributions in binary form must reproduce the above copyright
013     *       notice, this list of conditions and the following disclaimer in
014     *       the documentation and/or other materials provided with the
015     *       distribution.
016     *
017     *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
018     *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
019     *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
020     *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
021     *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022     *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023     *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024     *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025     *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026     *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027     *
028     *   $JOMC: DefaultModelProviderTest.java 4200 2012-01-25 09:46:13Z schulte2005 $
029     *
030     */
031    package org.jomc.model.modlet.test;
032    
033    import org.jomc.model.ModelObject;
034    import org.jomc.model.modlet.DefaultModelProvider;
035    import org.jomc.modlet.Model;
036    import org.jomc.modlet.ModelContext;
037    import org.jomc.modlet.ModelContextFactory;
038    import org.junit.Test;
039    import static org.junit.Assert.assertEquals;
040    import static org.junit.Assert.assertFalse;
041    import static org.junit.Assert.assertNotNull;
042    import static org.junit.Assert.assertNull;
043    import static org.junit.Assert.assertTrue;
044    import static org.junit.Assert.fail;
045    
046    /**
047     * Test cases for class {@code org.jomc.model.modlet.DefaultModelProcessor}.
048     *
049     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
050     * @version $JOMC: DefaultModelProviderTest.java 4200 2012-01-25 09:46:13Z schulte2005 $
051     */
052    public class DefaultModelProviderTest
053    {
054    
055        /** The {@code DefaultModelProvider} instance tests are performed with. */
056        private DefaultModelProvider defaultModelProvider;
057    
058        /** Creates a new {@code DefaultModelProviderTest} instance. */
059        public DefaultModelProviderTest()
060        {
061            super();
062        }
063    
064        /**
065         * Gets the {@code DefaultModelProvider} instance tests are performed with.
066         *
067         * @return The {@code DefaultModelProvider} instance tests are performed with.
068         *
069         * @see #newModelProvider()
070         */
071        public DefaultModelProvider getModelProvider()
072        {
073            if ( this.defaultModelProvider == null )
074            {
075                this.defaultModelProvider = this.newModelProvider();
076            }
077    
078            return this.defaultModelProvider;
079        }
080    
081        /**
082         * Creates a new {@code DefaultModelProvider} instance to test.
083         *
084         * @return A new {@code DefaultModelProvider} instance to test.
085         *
086         * @see #getModelProvider()
087         */
088        protected DefaultModelProvider newModelProvider()
089        {
090            return new DefaultModelProvider();
091        }
092    
093        @Test
094        public final void testFindModules() throws Exception
095        {
096            final ModelContext context = ModelContextFactory.newInstance().newModelContext();
097    
098            try
099            {
100                this.getModelProvider().findModules( null, null, null );
101                fail( "Expected NullPointerException not thrown." );
102            }
103            catch ( final NullPointerException e )
104            {
105                assertNotNull( e.getMessage() );
106                System.out.println( e );
107            }
108    
109            try
110            {
111                this.getModelProvider().findModules( context, null, null );
112                fail( "Expected NullPointerException not thrown." );
113            }
114            catch ( final NullPointerException e )
115            {
116                assertNotNull( e.getMessage() );
117                System.out.println( e );
118            }
119    
120            try
121            {
122                this.getModelProvider().findModules( context, "TEST", null );
123                fail( "Expected NullPointerException not thrown." );
124            }
125            catch ( final NullPointerException e )
126            {
127                assertNotNull( e.getMessage() );
128                System.out.println( e );
129            }
130    
131            DefaultModelProvider.setDefaultModuleLocation( null );
132            this.getModelProvider().setModuleLocation( null );
133            assertEquals( 1, this.getModelProvider().findModules(
134                context, ModelObject.MODEL_PUBLIC_ID, DefaultModelProvider.getDefaultModuleLocation() ).getModule().size() );
135    
136            assertEquals( 1, this.getModelProvider().findModules(
137                context, ModelObject.MODEL_PUBLIC_ID, this.getModelProvider().getModuleLocation() ).getModule().size() );
138    
139            DefaultModelProvider.setDefaultModuleLocation( "DOES_NOT_EXIST" );
140            this.getModelProvider().setModuleLocation( "DOES_NOT_EXIST" );
141    
142            assertNull( this.getModelProvider().findModules(
143                context, ModelObject.MODEL_PUBLIC_ID, DefaultModelProvider.getDefaultModuleLocation() ) );
144    
145            assertNull( this.getModelProvider().findModules(
146                context, ModelObject.MODEL_PUBLIC_ID, this.getModelProvider().getModuleLocation() ) );
147    
148            DefaultModelProvider.setDefaultModuleLocation( null );
149            this.getModelProvider().setModuleLocation( null );
150        }
151    
152        @Test
153        public final void testFindModel() throws Exception
154        {
155            final ModelContext context = ModelContextFactory.newInstance().newModelContext();
156            final Model model = new Model();
157            model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
158    
159            try
160            {
161                this.getModelProvider().findModel( null, model );
162                fail( "Expected NullPointerException not thrown." );
163            }
164            catch ( final NullPointerException e )
165            {
166                assertNotNull( e.getMessage() );
167                System.out.println( e.toString() );
168            }
169    
170            try
171            {
172                this.getModelProvider().findModel( context, null );
173                fail( "Expected NullPointerException not thrown." );
174            }
175            catch ( final NullPointerException e )
176            {
177                assertNotNull( e.getMessage() );
178                System.out.println( e.toString() );
179            }
180    
181            assertNotNull( this.getModelProvider().findModel( context, model ) );
182        }
183    
184        @Test
185        public final void testDefaultEnabled() throws Exception
186        {
187            System.clearProperty( "org.jomc.model.DefaultModelProvider.defaultEnabled" );
188            System.clearProperty( "org.jomc.model.modlet.DefaultModelProvider.defaultEnabled" );
189            DefaultModelProvider.setDefaultEnabled( null );
190            assertTrue( DefaultModelProvider.isDefaultEnabled() );
191    
192            System.setProperty( "org.jomc.model.DefaultModelProvider.defaultEnabled", Boolean.toString( false ) );
193            DefaultModelProvider.setDefaultEnabled( null );
194            assertFalse( DefaultModelProvider.isDefaultEnabled() );
195            System.clearProperty( "org.jomc.model.DefaultModelProvider.defaultEnabled" );
196            DefaultModelProvider.setDefaultEnabled( null );
197            assertTrue( DefaultModelProvider.isDefaultEnabled() );
198    
199            System.setProperty( "org.jomc.model.modlet.DefaultModelProvider.defaultEnabled", Boolean.toString( false ) );
200            DefaultModelProvider.setDefaultEnabled( null );
201            assertFalse( DefaultModelProvider.isDefaultEnabled() );
202            System.clearProperty( "org.jomc.model.modlet.DefaultModelProvider.defaultEnabled" );
203            DefaultModelProvider.setDefaultEnabled( null );
204            assertTrue( DefaultModelProvider.isDefaultEnabled() );
205    
206            System.setProperty( "org.jomc.model.DefaultModelProvider.defaultEnabled", Boolean.toString( true ) );
207            System.setProperty( "org.jomc.model.modlet.DefaultModelProvider.defaultEnabled", Boolean.toString( false ) );
208            DefaultModelProvider.setDefaultEnabled( null );
209            assertFalse( DefaultModelProvider.isDefaultEnabled() );
210            System.clearProperty( "org.jomc.model.modlet.DefaultModelProvider.defaultEnabled" );
211            DefaultModelProvider.setDefaultEnabled( null );
212            assertTrue( DefaultModelProvider.isDefaultEnabled() );
213            System.clearProperty( "org.jomc.model.DefaultModelProvider.defaultEnabled" );
214            DefaultModelProvider.setDefaultEnabled( null );
215            assertTrue( DefaultModelProvider.isDefaultEnabled() );
216        }
217    
218        @Test
219        public final void testEnabled() throws Exception
220        {
221            final Model model = new Model();
222            model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
223    
224            DefaultModelProvider.setDefaultEnabled( null );
225            this.getModelProvider().setEnabled( null );
226            assertTrue( this.getModelProvider().isEnabled() );
227    
228            this.getModelProvider().findModel( ModelContextFactory.newInstance().newModelContext(), model );
229            DefaultModelProvider.setDefaultEnabled( false );
230            this.getModelProvider().setEnabled( null );
231            assertFalse( this.getModelProvider().isEnabled() );
232    
233            this.getModelProvider().findModel( ModelContextFactory.newInstance().newModelContext(), model );
234            DefaultModelProvider.setDefaultEnabled( null );
235            this.getModelProvider().setEnabled( null );
236        }
237    
238        @Test
239        public final void testDefaultModuleLocation() throws Exception
240        {
241            System.clearProperty( "org.jomc.model.DefaultModelProvider.defaultModuleLocation" );
242            System.clearProperty( "org.jomc.model.modlet.DefaultModelProvider.defaultModuleLocation" );
243            DefaultModelProvider.setDefaultModuleLocation( null );
244            assertEquals( "META-INF/jomc.xml", DefaultModelProvider.getDefaultModuleLocation() );
245    
246            System.setProperty( "org.jomc.model.DefaultModelProvider.defaultModuleLocation", "TEST" );
247            DefaultModelProvider.setDefaultModuleLocation( null );
248            assertEquals( "TEST", DefaultModelProvider.getDefaultModuleLocation() );
249            System.clearProperty( "org.jomc.model.DefaultModelProvider.defaultModuleLocation" );
250            DefaultModelProvider.setDefaultModuleLocation( null );
251            assertEquals( "META-INF/jomc.xml", DefaultModelProvider.getDefaultModuleLocation() );
252    
253            System.setProperty( "org.jomc.model.modlet.DefaultModelProvider.defaultModuleLocation", "TEST" );
254            DefaultModelProvider.setDefaultModuleLocation( null );
255            assertEquals( "TEST", DefaultModelProvider.getDefaultModuleLocation() );
256            System.clearProperty( "org.jomc.model.modlet.DefaultModelProvider.defaultModuleLocation" );
257            DefaultModelProvider.setDefaultModuleLocation( null );
258            assertEquals( "META-INF/jomc.xml", DefaultModelProvider.getDefaultModuleLocation() );
259    
260            System.setProperty( "org.jomc.model.DefaultModelProvider.defaultModuleLocation", "DEPRECATED" );
261            System.setProperty( "org.jomc.model.modlet.DefaultModelProvider.defaultModuleLocation", "TEST" );
262            DefaultModelProvider.setDefaultModuleLocation( null );
263            assertEquals( "TEST", DefaultModelProvider.getDefaultModuleLocation() );
264            System.clearProperty( "org.jomc.model.modlet.DefaultModelProvider.defaultModuleLocation" );
265            DefaultModelProvider.setDefaultModuleLocation( null );
266            assertEquals( "DEPRECATED", DefaultModelProvider.getDefaultModuleLocation() );
267            System.clearProperty( "org.jomc.model.DefaultModelProvider.defaultModuleLocation" );
268            DefaultModelProvider.setDefaultModuleLocation( null );
269            assertEquals( "META-INF/jomc.xml", DefaultModelProvider.getDefaultModuleLocation() );
270        }
271    
272        @Test
273        public final void testModuleLocation() throws Exception
274        {
275            DefaultModelProvider.setDefaultModuleLocation( null );
276            this.getModelProvider().setModuleLocation( null );
277            assertNotNull( this.getModelProvider().getModuleLocation() );
278    
279            DefaultModelProvider.setDefaultModuleLocation( "TEST" );
280            this.getModelProvider().setModuleLocation( null );
281            assertEquals( "TEST", this.getModelProvider().getModuleLocation() );
282    
283            DefaultModelProvider.setDefaultModuleLocation( null );
284            this.getModelProvider().setModuleLocation( null );
285        }
286    
287        @Test
288        public final void testDefaultValidating() throws Exception
289        {
290            System.clearProperty( "org.jomc.model.modlet.DefaultModelProvider.defaultValidating" );
291            DefaultModelProvider.setDefaultValidating( null );
292            assertTrue( DefaultModelProvider.isDefaultValidating() );
293            DefaultModelProvider.setDefaultValidating( null );
294            System.setProperty( "org.jomc.model.modlet.DefaultModelProvider.defaultValidating", "false" );
295            assertFalse( DefaultModelProvider.isDefaultValidating() );
296            System.clearProperty( "org.jomc.model.modlet.DefaultModelProvider.defaultValidating" );
297            DefaultModelProvider.setDefaultValidating( null );
298            assertTrue( DefaultModelProvider.isDefaultValidating() );
299        }
300    
301        @Test
302        public final void testValidating() throws Exception
303        {
304            DefaultModelProvider.setDefaultValidating( null );
305            this.getModelProvider().setValidating( null );
306            assertTrue( this.getModelProvider().isValidating() );
307    
308            DefaultModelProvider.setDefaultValidating( false );
309            this.getModelProvider().setValidating( null );
310            assertFalse( this.getModelProvider().isValidating() );
311    
312            DefaultModelProvider.setDefaultValidating( null );
313            this.getModelProvider().setValidating( null );
314        }
315    
316    }