View Javadoc

1   /*
2    *   Copyright (C) Christian Schulte, 2005-206
3    *   All rights reserved.
4    *
5    *   Redistribution and use in source and binary forms, with or without
6    *   modification, are permitted provided that the following conditions
7    *   are met:
8    *
9    *     o Redistributions of source code must retain the above copyright
10   *       notice, this list of conditions and the following disclaimer.
11   *
12   *     o Redistributions in binary form must reproduce the above copyright
13   *       notice, this list of conditions and the following disclaimer in
14   *       the documentation and/or other materials provided with the
15   *       distribution.
16   *
17   *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18   *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19   *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20   *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21   *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   *
28   *   $JOMC: DefaultModelProviderTest.java 4613 2012-09-22 10:07:08Z schulte $
29   *
30   */
31  package org.jomc.model.modlet.test;
32  
33  import org.jomc.model.ModelObject;
34  import org.jomc.model.modlet.DefaultModelProvider;
35  import org.jomc.modlet.Model;
36  import org.jomc.modlet.ModelContext;
37  import org.jomc.modlet.ModelContextFactory;
38  import org.junit.Test;
39  import static org.junit.Assert.assertEquals;
40  import static org.junit.Assert.assertFalse;
41  import static org.junit.Assert.assertNotNull;
42  import static org.junit.Assert.assertNull;
43  import static org.junit.Assert.assertTrue;
44  import static org.junit.Assert.fail;
45  
46  /**
47   * Test cases for class {@code org.jomc.model.modlet.DefaultModelProcessor}.
48   *
49   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0
50   * @version $JOMC: DefaultModelProviderTest.java 4613 2012-09-22 10:07:08Z schulte $
51   */
52  public class DefaultModelProviderTest
53  {
54  
55      /** The {@code DefaultModelProvider} instance tests are performed with. */
56      private DefaultModelProvider defaultModelProvider;
57  
58      /** Creates a new {@code DefaultModelProviderTest} instance. */
59      public DefaultModelProviderTest()
60      {
61          super();
62      }
63  
64      /**
65       * Gets the {@code DefaultModelProvider} instance tests are performed with.
66       *
67       * @return The {@code DefaultModelProvider} instance tests are performed with.
68       *
69       * @see #newModelProvider()
70       */
71      public DefaultModelProvider getModelProvider()
72      {
73          if ( this.defaultModelProvider == null )
74          {
75              this.defaultModelProvider = this.newModelProvider();
76          }
77  
78          return this.defaultModelProvider;
79      }
80  
81      /**
82       * Creates a new {@code DefaultModelProvider} instance to test.
83       *
84       * @return A new {@code DefaultModelProvider} instance to test.
85       *
86       * @see #getModelProvider()
87       */
88      protected DefaultModelProvider newModelProvider()
89      {
90          return new DefaultModelProvider();
91      }
92  
93      @Test
94      public final void testFindModules() throws Exception
95      {
96          final ModelContext context = ModelContextFactory.newInstance().newModelContext();
97  
98          try
99          {
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 }