View Javadoc
1   /*
2    *   Copyright (C) Christian Schulte <cs@schulte.it>, 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: ToolsModelProviderTest.java 5043 2015-05-27 07:03:39Z schulte $
29   *
30   */
31  package org.jomc.tools.modlet.test;
32  
33  import java.util.HashSet;
34  import java.util.Set;
35  import java.util.logging.Level;
36  import org.jomc.model.Implementation;
37  import org.jomc.model.Implementations;
38  import org.jomc.model.ModelObject;
39  import org.jomc.model.Module;
40  import org.jomc.model.Modules;
41  import org.jomc.model.Specification;
42  import org.jomc.model.SpecificationReference;
43  import org.jomc.model.Specifications;
44  import org.jomc.model.modlet.ModelHelper;
45  import org.jomc.modlet.Model;
46  import org.jomc.modlet.ModelContext;
47  import org.jomc.modlet.ModelContextFactory;
48  import org.jomc.tools.model.SourceFileType;
49  import org.jomc.tools.model.SourceFilesType;
50  import org.jomc.tools.model.SourceSectionType;
51  import org.jomc.tools.model.SourceSectionsType;
52  import org.jomc.tools.modlet.ToolsModelProvider;
53  import org.junit.Test;
54  import static org.junit.Assert.assertEquals;
55  import static org.junit.Assert.assertFalse;
56  import static org.junit.Assert.assertNotNull;
57  import static org.junit.Assert.assertNull;
58  import static org.junit.Assert.assertTrue;
59  import static org.junit.Assert.fail;
60  
61  /**
62   * Test cases for class {@code org.jomc.tools.modlet.ToolsModelProvider}.
63   *
64   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0
65   * @version $JOMC: ToolsModelProviderTest.java 5043 2015-05-27 07:03:39Z schulte $
66   */
67  public class ToolsModelProviderTest
68  {
69  
70      /**
71       * The {@code ToolsModelProvider} instance tests are performed with.
72       */
73      private ToolsModelProvider toolsModelProvider;
74  
75      /**
76       * Creates a new {@code ToolsModelProviderTest} instance.
77       */
78      public ToolsModelProviderTest()
79      {
80          super();
81      }
82  
83      /**
84       * Gets the {@code ToolsModelProvider} instance tests are performed with.
85       *
86       * @return The {@code ToolsModelProvider} instance tests are performed with.
87       *
88       * @see #newModelProvider()
89       */
90      public ToolsModelProvider getModelProvider()
91      {
92          if ( this.toolsModelProvider == null )
93          {
94              this.toolsModelProvider = this.newModelProvider();
95          }
96  
97          return this.toolsModelProvider;
98      }
99  
100     /**
101      * Creates a new {@code ToolsModelProvider} instance to test.
102      *
103      * @return A new {@code ToolsModelProvider} instance to test.
104      *
105      * @see #getModelProvider()
106      */
107     protected ToolsModelProvider newModelProvider()
108     {
109         return new ToolsModelProvider();
110     }
111 
112     @Test
113     public final void testFindModel() throws Exception
114     {
115         final ModelContext context = ModelContextFactory.newInstance().newModelContext();
116         context.setLogLevel( Level.ALL );
117         context.getListeners().add( new ModelContext.Listener()
118         {
119 
120             @Override
121             public void onLog( final Level level, final String message, final Throwable t )
122             {
123                 super.onLog( level, message, t );
124                 System.out.println( "[" + level.getLocalizedName() + "] " + message );
125 
126                 if ( t != null )
127                 {
128                     t.printStackTrace( System.out );
129                 }
130             }
131 
132         } );
133 
134         Model model = new Model();
135         model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
136 
137         Modules modules = new Modules();
138         Module module = new Module();
139         module.setName( this.getClass().getName() );
140         module.setSpecifications( new Specifications() );
141         module.setImplementations( new Implementations() );
142 
143         Specification specification = new Specification();
144         specification.setClassDeclaration( true );
145         specification.setClazz( "specification.Documentation" );
146         specification.setIdentifier( this.getClass().getName() + " Specification" );
147 
148         Implementation implementation = new Implementation();
149         implementation.setClassDeclaration( true );
150         implementation.setClazz( "implementation.Documentation" );
151         implementation.setIdentifier( this.getClass().getName() + " Implementation" );
152         implementation.setName( this.getClass().getName() + " Implementation" );
153         implementation.setSpecifications( new Specifications() );
154         implementation.getSpecifications().getReference().add( new SpecificationReference() );
155         implementation.getSpecifications().getReference().get( 0 ).setIdentifier( specification.getIdentifier() );
156 
157         module.getSpecifications().getSpecification().add( specification );
158         module.getImplementations().getImplementation().add( implementation );
159         modules.getModule().add( module );
160 
161         ModelHelper.setModules( model, modules );
162 
163         try
164         {
165             this.getModelProvider().findModel( null, model );
166             fail( "Expected NullPointerException not thrown." );
167         }
168         catch ( final NullPointerException e )
169         {
170             assertNotNull( e.getMessage() );
171             System.out.println( e.toString() );
172         }
173 
174         try
175         {
176             this.getModelProvider().findModel( context, null );
177             fail( "Expected NullPointerException not thrown." );
178         }
179         catch ( final NullPointerException e )
180         {
181             assertNotNull( e.getMessage() );
182             System.out.println( e.toString() );
183         }
184 
185         Model found = this.getModelProvider().findModel( context, model );
186         assertNotNull( found );
187 
188         modules = ModelHelper.getModules( found );
189         assertNotNull( modules );
190 
191         specification = modules.getSpecification( this.getClass().getName() + " Specification" );
192         assertNotNull( specification );
193 
194         implementation = modules.getImplementation( this.getClass().getName() + " Implementation" );
195         assertNotNull( implementation );
196 
197         final SourceFilesType specificationSourceFiles = specification.getAnyObject( SourceFilesType.class );
198         final SourceFilesType implementationSourceFiles = implementation.getAnyObject( SourceFilesType.class );
199 
200         assertNotNull( specificationSourceFiles );
201         assertNotNull( implementationSourceFiles );
202 
203         final SourceFileType specificationSourceFile = specificationSourceFiles.getSourceFile( "Default" );
204         final SourceFileType implementationSourceFile = implementationSourceFiles.getSourceFile( "Default" );
205 
206         assertNotNull( specificationSourceFile );
207         assertSectionNameUniqueness( specificationSourceFile.getSourceSections() );
208         assertNotNull( implementationSourceFile );
209         assertSectionNameUniqueness( implementationSourceFile.getSourceSections() );
210 
211         this.getModelProvider().setEnabled( false );
212 
213         found = this.getModelProvider().findModel( context, model );
214         assertNull( found );
215 
216         this.getModelProvider().setEnabled( true );
217     }
218 
219     @Test
220     public final void testDefaultEnabled() throws Exception
221     {
222         System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultEnabled" );
223         ToolsModelProvider.setDefaultEnabled( null );
224         assertTrue( ToolsModelProvider.isDefaultEnabled() );
225 
226         System.setProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultEnabled", Boolean.toString( false ) );
227         ToolsModelProvider.setDefaultEnabled( null );
228         assertFalse( ToolsModelProvider.isDefaultEnabled() );
229         System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultEnabled" );
230         ToolsModelProvider.setDefaultEnabled( null );
231         assertTrue( ToolsModelProvider.isDefaultEnabled() );
232 
233         System.setProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultEnabled", Boolean.toString( true ) );
234         ToolsModelProvider.setDefaultEnabled( null );
235         assertTrue( ToolsModelProvider.isDefaultEnabled() );
236         System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultEnabled" );
237         ToolsModelProvider.setDefaultEnabled( null );
238         assertTrue( ToolsModelProvider.isDefaultEnabled() );
239     }
240 
241     @Test
242     public final void testEnabled() throws Exception
243     {
244         final Model model = new Model();
245         model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
246 
247         ToolsModelProvider.setDefaultEnabled( null );
248         this.getModelProvider().setEnabled( null );
249         assertTrue( this.getModelProvider().isEnabled() );
250 
251         this.getModelProvider().findModel( ModelContextFactory.newInstance().newModelContext(), model );
252         ToolsModelProvider.setDefaultEnabled( false );
253         this.getModelProvider().setEnabled( null );
254         assertFalse( this.getModelProvider().isEnabled() );
255 
256         this.getModelProvider().findModel( ModelContextFactory.newInstance().newModelContext(), model );
257         ToolsModelProvider.setDefaultEnabled( null );
258         this.getModelProvider().setEnabled( null );
259     }
260 
261     @Test
262     public final void testDefaultHeadComment() throws Exception
263     {
264         System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultHeadComment" );
265         ToolsModelProvider.setDefaultHeadComment( null );
266         assertEquals( "//", ToolsModelProvider.getDefaultHeadComment() );
267 
268         System.setProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultHeadComment", "/*" );
269         ToolsModelProvider.setDefaultHeadComment( null );
270         assertEquals( "/*", ToolsModelProvider.getDefaultHeadComment() );
271         System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultHeadComment" );
272         ToolsModelProvider.setDefaultHeadComment( null );
273         assertEquals( "//", ToolsModelProvider.getDefaultHeadComment() );
274     }
275 
276     @Test
277     public final void testHeadComment() throws Exception
278     {
279         ToolsModelProvider.setDefaultHeadComment( null );
280         this.getModelProvider().setHeadComment( null );
281         assertEquals( "//", this.getModelProvider().getHeadComment() );
282 
283         ToolsModelProvider.setDefaultHeadComment( "/*" );
284         this.getModelProvider().setHeadComment( null );
285         assertEquals( "/*", this.getModelProvider().getHeadComment() );
286 
287         ToolsModelProvider.setDefaultHeadComment( null );
288         this.getModelProvider().setHeadComment( null );
289         assertEquals( "//", this.getModelProvider().getHeadComment() );
290     }
291 
292     @Test
293     public final void testDefaultTailComment() throws Exception
294     {
295         System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultTailComment" );
296         ToolsModelProvider.setDefaultTailComment( null );
297         assertNull( ToolsModelProvider.getDefaultTailComment() );
298 
299         System.setProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultTailComment", "*/" );
300         ToolsModelProvider.setDefaultTailComment( null );
301         assertEquals( "*/", ToolsModelProvider.getDefaultTailComment() );
302 
303         System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultTailComment" );
304         ToolsModelProvider.setDefaultTailComment( null );
305         assertNull( ToolsModelProvider.getDefaultTailComment() );
306     }
307 
308     @Test
309     public final void testTailComment() throws Exception
310     {
311         ToolsModelProvider.setDefaultTailComment( null );
312         this.getModelProvider().setTailComment( null );
313         assertNull( this.getModelProvider().getTailComment() );
314 
315         ToolsModelProvider.setDefaultTailComment( "*/" );
316         this.getModelProvider().setTailComment( null );
317         assertEquals( "*/", this.getModelProvider().getTailComment() );
318 
319         ToolsModelProvider.setDefaultTailComment( null );
320         this.getModelProvider().setTailComment( null );
321         assertNull( this.getModelProvider().getTailComment() );
322     }
323 
324     private static void assertSectionNameUniqueness( final SourceSectionsType sections )
325     {
326         if ( sections != null )
327         {
328             final Set<String> sectionNames = new HashSet<String>( sections.getSourceSection().size() );
329 
330             for ( int i = 0, s0 = sections.getSourceSection().size(); i < s0; i++ )
331             {
332                 final SourceSectionType section = sections.getSourceSection().get( i );
333                 assertTrue( "Multiple '" + section.getName() + "' sections.", sectionNames.add( section.getName() ) );
334                 assertSectionNameUniqueness( section.getSourceSections() );
335             }
336         }
337     }
338 
339 }