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: DefaultModelProcessorTest.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.Modules;
35  import org.jomc.model.modlet.DefaultModelProcessor;
36  import org.jomc.model.modlet.ModelHelper;
37  import org.jomc.modlet.Model;
38  import org.jomc.modlet.ModelContext;
39  import org.jomc.modlet.ModelContextFactory;
40  import org.junit.Test;
41  import static org.junit.Assert.assertEquals;
42  import static org.junit.Assert.assertFalse;
43  import static org.junit.Assert.assertNotNull;
44  import static org.junit.Assert.assertNull;
45  import static org.junit.Assert.assertTrue;
46  import static org.junit.Assert.fail;
47  
48  /**
49   * Test cases for class {@code org.jomc.model.modlet.DefaultModelProcessor}.
50   *
51   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0
52   * @version $JOMC: DefaultModelProcessorTest.java 4613 2012-09-22 10:07:08Z schulte $
53   */
54  public class DefaultModelProcessorTest
55  {
56  
57      /** The {@code DefaultModelProcessor} instance tests are performed with. */
58      private DefaultModelProcessor defaultModelProcessor;
59  
60      /** Creates a new {@code DefaultModelProcessorTest} instance. */
61      public DefaultModelProcessorTest()
62      {
63          super();
64      }
65  
66      /**
67       * Gets the {@code DefaultModelProcessor} instance tests are performed with.
68       *
69       * @return The {@code DefaultModelProcessor} instance tests are performed with.
70       *
71       * @see #newModelProcessor()
72       */
73      public DefaultModelProcessor getModelProcessor()
74      {
75          if ( this.defaultModelProcessor == null )
76          {
77              this.defaultModelProcessor = this.newModelProcessor();
78          }
79  
80          return this.defaultModelProcessor;
81      }
82  
83      /**
84       * Creates a new {@code DefaultModelProcessor} instance to test.
85       *
86       * @return A new {@code DefaultModelProcessor} instance to test.
87       *
88       * @see #getModelProcessor()
89       */
90      protected DefaultModelProcessor newModelProcessor()
91      {
92          return new DefaultModelProcessor();
93      }
94  
95      @Test
96      public final void testFindTransformers() throws Exception
97      {
98          final ModelContext context = ModelContextFactory.newInstance().newModelContext();
99  
100         try
101         {
102             this.getModelProcessor().findTransformers( context, null );
103             fail( "Expected NullPointerException not thrown." );
104         }
105         catch ( final NullPointerException e )
106         {
107             assertNotNull( e.getMessage() );
108             System.out.println( e );
109         }
110 
111         try
112         {
113             this.getModelProcessor().findTransformers( null, "" );
114             fail( "Expected NullPointerException not thrown." );
115         }
116         catch ( final NullPointerException e )
117         {
118             assertNotNull( e.getMessage() );
119             System.out.println( e );
120         }
121 
122         DefaultModelProcessor.setDefaultTransformerLocation( null );
123         this.getModelProcessor().setTransformerLocation( null );
124         assertEquals( 1, this.getModelProcessor().findTransformers(
125             context, DefaultModelProcessor.getDefaultTransformerLocation() ).size() );
126 
127         DefaultModelProcessor.setDefaultTransformerLocation( "DOES_NOT_EXIST" );
128         this.getModelProcessor().setTransformerLocation( "DOES_NOT_EXIST" );
129 
130         assertNull( this.getModelProcessor().findTransformers(
131             context, DefaultModelProcessor.getDefaultTransformerLocation() ) );
132 
133         assertNull( this.getModelProcessor().findTransformers(
134             context, this.getModelProcessor().getTransformerLocation() ) );
135 
136         DefaultModelProcessor.setDefaultTransformerLocation( null );
137         this.getModelProcessor().setTransformerLocation( null );
138     }
139 
140     @Test
141     public final void testProcessModel() throws Exception
142     {
143         final ModelContext context = ModelContextFactory.newInstance().newModelContext();
144         final Model model = new Model();
145         model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
146 
147         try
148         {
149             this.getModelProcessor().processModel( null, model );
150             fail( "Expected NullPointerException not thrown." );
151         }
152         catch ( final NullPointerException e )
153         {
154             assertNotNull( e.getMessage() );
155             System.out.println( e.toString() );
156         }
157 
158         try
159         {
160             this.getModelProcessor().processModel( context, null );
161             fail( "Expected NullPointerException not thrown." );
162         }
163         catch ( final NullPointerException e )
164         {
165             assertNotNull( e.getMessage() );
166             System.out.println( e.toString() );
167         }
168 
169         assertNotNull( this.getModelProcessor().processModel( context, model ) );
170 
171         this.getModelProcessor().setTransformerLocation(
172             this.getClass().getPackage().getName().replace( '.', '/' ) + "/system-property-test.xsl" );
173 
174         final Model processedSystemProperty = this.getModelProcessor().processModel( context, model );
175         assertNotNull( processedSystemProperty );
176 
177         final Modules processedSystemPropertyModules = ModelHelper.getModules( processedSystemProperty );
178         assertNotNull( processedSystemPropertyModules );
179         assertNotNull( processedSystemPropertyModules.getModule( System.getProperty( "user.home" ) ) );
180 
181         this.getModelProcessor().setTransformerLocation(
182             this.getClass().getPackage().getName().replace( '.', '/' ) + "/relative-uri-test.xsl" );
183 
184         final Model processedRelativeUri = this.getModelProcessor().processModel( context, model );
185         assertNotNull( processedRelativeUri );
186 
187         final Modules processedRelativeUriModules = ModelHelper.getModules( processedRelativeUri );
188         assertNotNull( processedRelativeUriModules );
189         assertNotNull( processedRelativeUriModules.getModule( System.getProperty( "os.name" ) ) );
190 
191         this.getModelProcessor().setTransformerLocation( null );
192     }
193 
194     @Test
195     public final void testDefaultEnabled() throws Exception
196     {
197         System.clearProperty( "org.jomc.model.DefaultModelProcessor.defaultEnabled" );
198         System.clearProperty( "org.jomc.model.modlet.DefaultModelProcessor.defaultEnabled" );
199         DefaultModelProcessor.setDefaultEnabled( null );
200         assertTrue( DefaultModelProcessor.isDefaultEnabled() );
201 
202         System.setProperty( "org.jomc.model.DefaultModelProcessor.defaultEnabled", Boolean.toString( false ) );
203         DefaultModelProcessor.setDefaultEnabled( null );
204         assertFalse( DefaultModelProcessor.isDefaultEnabled() );
205         System.clearProperty( "org.jomc.model.DefaultModelProcessor.defaultEnabled" );
206         DefaultModelProcessor.setDefaultEnabled( null );
207         assertTrue( DefaultModelProcessor.isDefaultEnabled() );
208 
209         System.setProperty( "org.jomc.model.modlet.DefaultModelProcessor.defaultEnabled", Boolean.toString( false ) );
210         DefaultModelProcessor.setDefaultEnabled( null );
211         assertFalse( DefaultModelProcessor.isDefaultEnabled() );
212         System.clearProperty( "org.jomc.model.modlet.DefaultModelProcessor.defaultEnabled" );
213         DefaultModelProcessor.setDefaultEnabled( null );
214         assertTrue( DefaultModelProcessor.isDefaultEnabled() );
215 
216         System.setProperty( "org.jomc.model.DefaultModelProcessor.defaultEnabled", Boolean.toString( true ) );
217         System.setProperty( "org.jomc.model.modlet.DefaultModelProcessor.defaultEnabled", Boolean.toString( false ) );
218         DefaultModelProcessor.setDefaultEnabled( null );
219         assertFalse( DefaultModelProcessor.isDefaultEnabled() );
220         System.clearProperty( "org.jomc.model.modlet.DefaultModelProcessor.defaultEnabled" );
221         DefaultModelProcessor.setDefaultEnabled( null );
222         assertTrue( DefaultModelProcessor.isDefaultEnabled() );
223         System.clearProperty( "org.jomc.model.DefaultModelProcessor.defaultEnabled" );
224         DefaultModelProcessor.setDefaultEnabled( null );
225         assertTrue( DefaultModelProcessor.isDefaultEnabled() );
226     }
227 
228     @Test
229     public final void testEnabled() throws Exception
230     {
231         final Model model = new Model();
232         model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
233 
234         DefaultModelProcessor.setDefaultEnabled( null );
235         this.getModelProcessor().setEnabled( null );
236         assertTrue( this.getModelProcessor().isEnabled() );
237 
238         this.getModelProcessor().processModel( ModelContextFactory.newInstance().newModelContext(), model );
239         DefaultModelProcessor.setDefaultEnabled( false );
240         this.getModelProcessor().setEnabled( null );
241         assertFalse( this.getModelProcessor().isEnabled() );
242 
243         this.getModelProcessor().processModel( ModelContextFactory.newInstance().newModelContext(), model );
244         DefaultModelProcessor.setDefaultEnabled( null );
245         this.getModelProcessor().setEnabled( null );
246     }
247 
248     @Test
249     public final void testDefaultTransformerLocation() throws Exception
250     {
251         System.clearProperty( "org.jomc.model.DefaultModelProcessor.defaultTransformerLocation" );
252         System.clearProperty( "org.jomc.model.modlet.DefaultModelProcessor.defaultTransformerLocation" );
253         DefaultModelProcessor.setDefaultTransformerLocation( null );
254         assertEquals( "META-INF/jomc.xsl", DefaultModelProcessor.getDefaultTransformerLocation() );
255 
256         System.setProperty( "org.jomc.model.DefaultModelProcessor.defaultTransformerLocation", "TEST" );
257         DefaultModelProcessor.setDefaultTransformerLocation( null );
258         assertEquals( "TEST", DefaultModelProcessor.getDefaultTransformerLocation() );
259         System.clearProperty( "org.jomc.model.DefaultModelProcessor.defaultTransformerLocation" );
260         DefaultModelProcessor.setDefaultTransformerLocation( null );
261         assertEquals( "META-INF/jomc.xsl", DefaultModelProcessor.getDefaultTransformerLocation() );
262 
263         System.setProperty( "org.jomc.model.modlet.DefaultModelProcessor.defaultTransformerLocation", "TEST" );
264         DefaultModelProcessor.setDefaultTransformerLocation( null );
265         assertEquals( "TEST", DefaultModelProcessor.getDefaultTransformerLocation() );
266         System.clearProperty( "org.jomc.model.modlet.DefaultModelProcessor.defaultTransformerLocation" );
267         DefaultModelProcessor.setDefaultTransformerLocation( null );
268         assertEquals( "META-INF/jomc.xsl", DefaultModelProcessor.getDefaultTransformerLocation() );
269 
270         System.setProperty( "org.jomc.model.DefaultModelProcessor.defaultTransformerLocation", "DEPRECATED" );
271         System.setProperty( "org.jomc.model.modlet.DefaultModelProcessor.defaultTransformerLocation", "TEST" );
272         DefaultModelProcessor.setDefaultTransformerLocation( null );
273         assertEquals( "TEST", DefaultModelProcessor.getDefaultTransformerLocation() );
274         System.clearProperty( "org.jomc.model.modlet.DefaultModelProcessor.defaultTransformerLocation" );
275         DefaultModelProcessor.setDefaultTransformerLocation( null );
276         assertEquals( "DEPRECATED", DefaultModelProcessor.getDefaultTransformerLocation() );
277         System.clearProperty( "org.jomc.model.DefaultModelProcessor.defaultTransformerLocation" );
278         DefaultModelProcessor.setDefaultTransformerLocation( null );
279         assertEquals( "META-INF/jomc.xsl", DefaultModelProcessor.getDefaultTransformerLocation() );
280     }
281 
282     @Test
283     public final void testTransformerLocation() throws Exception
284     {
285         DefaultModelProcessor.setDefaultTransformerLocation( null );
286         this.getModelProcessor().setTransformerLocation( null );
287         assertNotNull( this.getModelProcessor().getTransformerLocation() );
288 
289         DefaultModelProcessor.setDefaultTransformerLocation( "TEST" );
290         this.getModelProcessor().setTransformerLocation( null );
291         assertEquals( "TEST", this.getModelProcessor().getTransformerLocation() );
292 
293         DefaultModelProcessor.setDefaultTransformerLocation( null );
294         this.getModelProcessor().setTransformerLocation( null );
295     }
296 
297 }