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: ToolsModelProcessorTest.java 5043 2015-05-27 07:03:39Z schulte $
29   *
30   */
31  package org.jomc.tools.modlet.test;
32  
33  import org.jomc.model.Implementation;
34  import org.jomc.model.Implementations;
35  import org.jomc.model.ModelObject;
36  import org.jomc.model.Module;
37  import org.jomc.model.Modules;
38  import org.jomc.model.Specification;
39  import org.jomc.model.Specifications;
40  import org.jomc.model.modlet.ModelHelper;
41  import org.jomc.modlet.Model;
42  import org.jomc.modlet.ModelContext;
43  import org.jomc.modlet.ModelContextFactory;
44  import org.jomc.tools.model.SourceFileType;
45  import org.jomc.tools.model.SourceFilesType;
46  import org.jomc.tools.model.SourceSectionType;
47  import org.jomc.tools.model.SourceSectionsType;
48  import org.jomc.tools.modlet.ToolsModelProcessor;
49  import org.junit.Test;
50  import static org.junit.Assert.assertEquals;
51  import static org.junit.Assert.assertFalse;
52  import static org.junit.Assert.assertNotNull;
53  import static org.junit.Assert.assertNull;
54  import static org.junit.Assert.assertTrue;
55  import static org.junit.Assert.fail;
56  
57  /**
58   * Test cases for class {@code org.jomc.tools.modlet.ToolsModelProcessor}.
59   *
60   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0
61   * @version $JOMC: ToolsModelProcessorTest.java 5043 2015-05-27 07:03:39Z schulte $
62   */
63  public class ToolsModelProcessorTest
64  {
65  
66      /**
67       * The {@code ToolsModelProcessor} instance tests are performed with.
68       */
69      private ToolsModelProcessor toolsModelProcessor;
70  
71      /**
72       * Creates a new {@code ToolsModelProcessorTest} instance.
73       */
74      public ToolsModelProcessorTest()
75      {
76          super();
77      }
78  
79      /**
80       * Gets the {@code ToolsModelProcessor} instance tests are performed with.
81       *
82       * @return The {@code ToolsModelProcessor} instance tests are performed with.
83       *
84       * @see #newModelProcessor()
85       */
86      public ToolsModelProcessor getModelProcessor()
87      {
88          if ( this.toolsModelProcessor == null )
89          {
90              this.toolsModelProcessor = this.newModelProcessor();
91          }
92  
93          return this.toolsModelProcessor;
94      }
95  
96      /**
97       * Creates a new {@code ToolsModelProcessor} instance to test.
98       *
99       * @return A new {@code ToolsModelProcessor} instance to test.
100      *
101      * @see #getModelProcessor()
102      */
103     protected ToolsModelProcessor newModelProcessor()
104     {
105         return new ToolsModelProcessor();
106     }
107 
108     @Test
109     public final void testProcessModel() throws Exception
110     {
111         final ModelContext context = ModelContextFactory.newInstance().newModelContext();
112         Model model = new Model();
113         model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
114 
115         Modules modules = new Modules();
116         Module module = new Module();
117         module.setName( this.getClass().getName() );
118         module.setSpecifications( new Specifications() );
119         module.setImplementations( new Implementations() );
120 
121         Specification specification = new Specification();
122         specification.setClassDeclaration( true );
123         specification.setClazz( this.getClass().getName() );
124         specification.setIdentifier( this.getClass().getName() + " Specification" );
125 
126         Implementation implementation = new Implementation();
127         implementation.setClassDeclaration( true );
128         implementation.setClazz( this.getClass().getName() );
129         implementation.setIdentifier( this.getClass().getName() + " Implementation" );
130         implementation.setName( this.getClass().getName() + " Implementation" );
131 
132         module.getSpecifications().getSpecification().add( specification );
133         module.getImplementations().getImplementation().add( implementation );
134         modules.getModule().add( module );
135 
136         ModelHelper.setModules( model, modules );
137 
138         try
139         {
140             this.getModelProcessor().processModel( null, model );
141             fail( "Expected NullPointerException not thrown." );
142         }
143         catch ( final NullPointerException e )
144         {
145             assertNotNull( e.getMessage() );
146             System.out.println( e.toString() );
147         }
148 
149         try
150         {
151             this.getModelProcessor().processModel( context, null );
152             fail( "Expected NullPointerException not thrown." );
153         }
154         catch ( final NullPointerException e )
155         {
156             assertNotNull( e.getMessage() );
157             System.out.println( e.toString() );
158         }
159 
160         Model processed = this.getModelProcessor().processModel( context, model );
161         assertNotNull( processed );
162 
163         modules = ModelHelper.getModules( processed );
164         assertNotNull( modules );
165 
166         specification = modules.getSpecification( this.getClass().getName() + " Specification" );
167         assertNotNull( specification );
168 
169         implementation = modules.getImplementation( this.getClass().getName() + " Implementation" );
170         assertNotNull( implementation );
171 
172         SourceFileType ss = specification.getAnyObject( SourceFileType.class );
173         assertNull( ss );
174 
175         SourceFileType is = implementation.getAnyObject( SourceFileType.class );
176         assertNull( is );
177 
178         ss = new SourceFileType();
179         ss.setIdentifier( this.getClass().getName() + " Specification" );
180 
181         is = new SourceFileType();
182         is.setIdentifier( this.getClass().getName() + " Implementation" );
183 
184         specification.getAny().add( ss );
185         implementation.getAny().add( is );
186 
187         processed = this.getModelProcessor().processModel( context, processed );
188         assertNotNull( processed );
189 
190         modules = ModelHelper.getModules( processed );
191         assertNotNull( modules );
192 
193         specification = modules.getSpecification( this.getClass().getName() + " Specification" );
194         assertNotNull( specification );
195 
196         implementation = modules.getImplementation( this.getClass().getName() + " Implementation" );
197         assertNotNull( implementation );
198 
199         ss = specification.getAnyObject( SourceFileType.class );
200         assertNotNull( ss );
201         assertNotNull( ss.getLocation() );
202         assertNotNull( ss.getHeadComment() );
203 
204         is = implementation.getAnyObject( SourceFileType.class );
205         assertNotNull( is );
206         assertNotNull( is.getLocation() );
207         assertNotNull( is.getHeadComment() );
208 
209         specification.getAny().clear();
210         implementation.getAny().clear();
211 
212         SourceFilesType specificationSourceFiles = new SourceFilesType();
213         ss = new SourceFileType();
214         ss.setIdentifier( this.getClass().getSimpleName() );
215         ss.setSourceSections( new SourceSectionsType() );
216         specificationSourceFiles.getSourceFile().add( ss );
217         specification.getAny().add( specificationSourceFiles );
218 
219         SourceFilesType implementationSourceFiles = new SourceFilesType();
220         is = new SourceFileType();
221         is.setIdentifier( this.getClass().getSimpleName() );
222         is.setSourceSections( new SourceSectionsType() );
223         implementationSourceFiles.getSourceFile().add( is );
224         implementation.getAny().add( implementationSourceFiles );
225 
226         SourceSectionType sourceSection = new SourceSectionType();
227         sourceSection.setName( "License Header" );
228 
229         ss.getSourceSections().getSourceSection().add( sourceSection );
230         is.getSourceSections().getSourceSection().add( sourceSection );
231 
232         sourceSection = new SourceSectionType();
233         sourceSection.setName( "Annotations" );
234 
235         ss.getSourceSections().getSourceSection().add( sourceSection );
236         is.getSourceSections().getSourceSection().add( sourceSection );
237 
238         sourceSection = new SourceSectionType();
239         sourceSection.setName( "Documentation" );
240 
241         ss.getSourceSections().getSourceSection().add( sourceSection );
242         is.getSourceSections().getSourceSection().add( sourceSection );
243 
244         sourceSection = new SourceSectionType();
245         sourceSection.setName( this.getClass().getSimpleName() );
246 
247         ss.getSourceSections().getSourceSection().add( sourceSection );
248         is.getSourceSections().getSourceSection().add( sourceSection );
249 
250         sourceSection = new SourceSectionType();
251         sourceSection.setName( "Constructors" );
252 
253         is.getSourceSections().getSourceSection().add( sourceSection );
254 
255         sourceSection = new SourceSectionType();
256         sourceSection.setName( "Default Constructor" );
257 
258         is.getSourceSections().getSourceSection().add( sourceSection );
259 
260         sourceSection = new SourceSectionType();
261         sourceSection.setName( "Dependencies" );
262 
263         is.getSourceSections().getSourceSection().add( sourceSection );
264 
265         sourceSection = new SourceSectionType();
266         sourceSection.setName( "Properties" );
267 
268         is.getSourceSections().getSourceSection().add( sourceSection );
269 
270         sourceSection = new SourceSectionType();
271         sourceSection.setName( "Messages" );
272 
273         is.getSourceSections().getSourceSection().add( sourceSection );
274 
275         processed = this.getModelProcessor().processModel( context, processed );
276         assertNotNull( processed );
277 
278         modules = ModelHelper.getModules( processed );
279         assertNotNull( modules );
280 
281         specification = modules.getSpecification( this.getClass().getName() + " Specification" );
282         assertNotNull( specification );
283 
284         implementation = modules.getImplementation( this.getClass().getName() + " Implementation" );
285         assertNotNull( implementation );
286 
287         specificationSourceFiles = specification.getAnyObject( SourceFilesType.class );
288         assertNotNull( specificationSourceFiles );
289 
290         ss = specificationSourceFiles.getSourceFile( this.getClass().getSimpleName() );
291         assertNotNull( ss );
292         assertNotNull( ss.getHeadComment() );
293         assertNotNull( ss.getLocation() );
294         assertNotNull( ss.getTemplate() );
295 
296         implementationSourceFiles = implementation.getAnyObject( SourceFilesType.class );
297         assertNotNull( implementationSourceFiles );
298         is = implementationSourceFiles.getSourceFile( this.getClass().getSimpleName() );
299         assertNotNull( is );
300         assertNotNull( is.getHeadComment() );
301         assertNotNull( is.getLocation() );
302         assertNotNull( is.getTemplate() );
303 
304         sourceSection = ss.getSourceSections().getSourceSection( "License Header" );
305         assertNotNull( sourceSection );
306         assertTrue( sourceSection.isOptional() );
307         assertNotNull( sourceSection.getHeadTemplate() );
308 
309         sourceSection = is.getSourceSections().getSourceSection( "License Header" );
310         assertNotNull( sourceSection );
311         assertTrue( sourceSection.isOptional() );
312         assertNotNull( sourceSection.getHeadTemplate() );
313 
314         sourceSection = ss.getSourceSections().getSourceSection( "Annotations" );
315         assertNotNull( sourceSection );
316         assertNotNull( sourceSection.getHeadTemplate() );
317 
318         sourceSection = is.getSourceSections().getSourceSection( "Annotations" );
319         assertNotNull( sourceSection );
320         assertNotNull( sourceSection.getHeadTemplate() );
321 
322         sourceSection = ss.getSourceSections().getSourceSection( "Documentation" );
323         assertNotNull( sourceSection );
324         assertTrue( sourceSection.isOptional() );
325         assertNotNull( sourceSection.getHeadTemplate() );
326 
327         sourceSection = is.getSourceSections().getSourceSection( "Documentation" );
328         assertNotNull( sourceSection );
329         assertTrue( sourceSection.isOptional() );
330         assertNotNull( sourceSection.getHeadTemplate() );
331 
332         sourceSection = ss.getSourceSections().getSourceSection( this.getClass().getSimpleName() );
333         assertNotNull( sourceSection );
334         assertTrue( sourceSection.isEditable() );
335         assertEquals( 1, sourceSection.getIndentationLevel() );
336 
337         sourceSection = is.getSourceSections().getSourceSection( this.getClass().getSimpleName() );
338         assertNotNull( sourceSection );
339         assertTrue( sourceSection.isEditable() );
340         assertEquals( 1, sourceSection.getIndentationLevel() );
341 
342         sourceSection = is.getSourceSections().getSourceSection( "Constructors" );
343         assertNotNull( sourceSection );
344         assertNotNull( sourceSection.getHeadTemplate() );
345         assertNotNull( sourceSection.getTailTemplate() );
346         assertEquals( 1, sourceSection.getIndentationLevel() );
347         assertTrue( sourceSection.isOptional() );
348 
349         sourceSection = is.getSourceSections().getSourceSection( "Default Constructor" );
350         assertNotNull( sourceSection );
351         assertNotNull( sourceSection.getHeadTemplate() );
352         assertEquals( 2, sourceSection.getIndentationLevel() );
353         assertTrue( sourceSection.isEditable() );
354 
355         sourceSection = is.getSourceSections().getSourceSection( "Dependencies" );
356         assertNotNull( sourceSection );
357         assertNotNull( sourceSection.getHeadTemplate() );
358         assertEquals( 1, sourceSection.getIndentationLevel() );
359         assertTrue( sourceSection.isOptional() );
360 
361         sourceSection = is.getSourceSections().getSourceSection( "Properties" );
362         assertNotNull( sourceSection );
363         assertNotNull( sourceSection.getHeadTemplate() );
364         assertEquals( 1, sourceSection.getIndentationLevel() );
365         assertTrue( sourceSection.isOptional() );
366 
367         sourceSection = is.getSourceSections().getSourceSection( "Messages" );
368         assertNotNull( sourceSection );
369         assertNotNull( sourceSection.getHeadTemplate() );
370         assertEquals( 1, sourceSection.getIndentationLevel() );
371         assertTrue( sourceSection.isOptional() );
372 
373         sourceSection = is.getSourceSections().getSourceSection( this.getClass().getSimpleName() );
374         assertNotNull( sourceSection );
375         assertEquals( 1, sourceSection.getIndentationLevel() );
376         assertTrue( sourceSection.isEditable() );
377     }
378 
379     @Test
380     public final void testDefaultEnabled() throws Exception
381     {
382         System.clearProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultEnabled" );
383         ToolsModelProcessor.setDefaultEnabled( null );
384         assertTrue( ToolsModelProcessor.isDefaultEnabled() );
385 
386         System.setProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultEnabled", Boolean.toString( false ) );
387         ToolsModelProcessor.setDefaultEnabled( null );
388         assertFalse( ToolsModelProcessor.isDefaultEnabled() );
389         System.clearProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultEnabled" );
390         ToolsModelProcessor.setDefaultEnabled( null );
391         assertTrue( ToolsModelProcessor.isDefaultEnabled() );
392 
393         System.setProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultEnabled", Boolean.toString( true ) );
394         ToolsModelProcessor.setDefaultEnabled( null );
395         assertTrue( ToolsModelProcessor.isDefaultEnabled() );
396         System.clearProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultEnabled" );
397         ToolsModelProcessor.setDefaultEnabled( null );
398         assertTrue( ToolsModelProcessor.isDefaultEnabled() );
399     }
400 
401     @Test
402     public final void testEnabled() throws Exception
403     {
404         final Model model = new Model();
405         model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
406 
407         ToolsModelProcessor.setDefaultEnabled( null );
408         this.getModelProcessor().setEnabled( null );
409         assertTrue( this.getModelProcessor().isEnabled() );
410 
411         this.getModelProcessor().processModel( ModelContextFactory.newInstance().newModelContext(), model );
412         ToolsModelProcessor.setDefaultEnabled( false );
413         this.getModelProcessor().setEnabled( null );
414         assertFalse( this.getModelProcessor().isEnabled() );
415 
416         this.getModelProcessor().processModel( ModelContextFactory.newInstance().newModelContext(), model );
417         ToolsModelProcessor.setDefaultEnabled( null );
418         this.getModelProcessor().setEnabled( null );
419     }
420 
421     @Test
422     public final void testDefaultHeadComment() throws Exception
423     {
424         System.clearProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultHeadComment" );
425         ToolsModelProcessor.setDefaultHeadComment( null );
426         assertEquals( "//", ToolsModelProcessor.getDefaultHeadComment() );
427 
428         System.setProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultHeadComment", "/*" );
429         ToolsModelProcessor.setDefaultHeadComment( null );
430         assertEquals( "/*", ToolsModelProcessor.getDefaultHeadComment() );
431         System.clearProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultHeadComment" );
432         ToolsModelProcessor.setDefaultHeadComment( null );
433         assertEquals( "//", ToolsModelProcessor.getDefaultHeadComment() );
434     }
435 
436     @Test
437     public final void testHeadComment() throws Exception
438     {
439         ToolsModelProcessor.setDefaultHeadComment( null );
440         this.getModelProcessor().setHeadComment( null );
441         assertEquals( "//", this.getModelProcessor().getHeadComment() );
442 
443         ToolsModelProcessor.setDefaultHeadComment( "/*" );
444         this.getModelProcessor().setHeadComment( null );
445         assertEquals( "/*", this.getModelProcessor().getHeadComment() );
446 
447         ToolsModelProcessor.setDefaultHeadComment( null );
448         this.getModelProcessor().setHeadComment( null );
449         assertEquals( "//", this.getModelProcessor().getHeadComment() );
450     }
451 
452     @Test
453     public final void testDefaultTailComment() throws Exception
454     {
455         System.clearProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultTailComment" );
456         ToolsModelProcessor.setDefaultTailComment( null );
457         assertNull( ToolsModelProcessor.getDefaultTailComment() );
458 
459         System.setProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultTailComment", "*/" );
460         ToolsModelProcessor.setDefaultTailComment( null );
461         assertEquals( "*/", ToolsModelProcessor.getDefaultTailComment() );
462 
463         System.clearProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultTailComment" );
464         ToolsModelProcessor.setDefaultTailComment( null );
465         assertNull( ToolsModelProcessor.getDefaultTailComment() );
466     }
467 
468     @Test
469     public final void testTailComment() throws Exception
470     {
471         ToolsModelProcessor.setDefaultTailComment( null );
472         this.getModelProcessor().setTailComment( null );
473         assertNull( this.getModelProcessor().getTailComment() );
474 
475         ToolsModelProcessor.setDefaultTailComment( "*/" );
476         this.getModelProcessor().setTailComment( null );
477         assertEquals( "*/", this.getModelProcessor().getTailComment() );
478 
479         ToolsModelProcessor.setDefaultTailComment( null );
480         this.getModelProcessor().setTailComment( null );
481         assertNull( this.getModelProcessor().getTailComment() );
482     }
483 
484 }