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: ToolsModelProcessorTest.java 4200 2012-01-25 09:46:13Z schulte2005 $
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:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
61   * @version $JOMC: ToolsModelProcessorTest.java 4200 2012-01-25 09:46:13Z schulte2005 $
62   */
63  public class ToolsModelProcessorTest
64  {
65  
66      /** The {@code ToolsModelProcessor} instance tests are performed with. */
67      private ToolsModelProcessor toolsModelProcessor;
68  
69      /** Creates a new {@code ToolsModelProcessorTest} instance. */
70      public ToolsModelProcessorTest()
71      {
72          super();
73      }
74  
75      /**
76       * Gets the {@code ToolsModelProcessor} instance tests are performed with.
77       *
78       * @return The {@code ToolsModelProcessor} instance tests are performed with.
79       *
80       * @see #newModelProcessor()
81       */
82      public ToolsModelProcessor getModelProcessor()
83      {
84          if ( this.toolsModelProcessor == null )
85          {
86              this.toolsModelProcessor = this.newModelProcessor();
87          }
88  
89          return this.toolsModelProcessor;
90      }
91  
92      /**
93       * Creates a new {@code ToolsModelProcessor} instance to test.
94       *
95       * @return A new {@code ToolsModelProcessor} instance to test.
96       *
97       * @see #getModelProcessor()
98       */
99      protected ToolsModelProcessor newModelProcessor()
100     {
101         return new ToolsModelProcessor();
102     }
103 
104     @Test
105     public final void testProcessModel() throws Exception
106     {
107         final ModelContext context = ModelContextFactory.newInstance().newModelContext();
108         Model model = new Model();
109         model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
110 
111         Modules modules = new Modules();
112         Module module = new Module();
113         module.setName( this.getClass().getName() );
114         module.setSpecifications( new Specifications() );
115         module.setImplementations( new Implementations() );
116 
117         Specification specification = new Specification();
118         specification.setClassDeclaration( true );
119         specification.setClazz( this.getClass().getName() );
120         specification.setIdentifier( this.getClass().getName() + " Specification" );
121 
122         Implementation implementation = new Implementation();
123         implementation.setClassDeclaration( true );
124         implementation.setClazz( this.getClass().getName() );
125         implementation.setIdentifier( this.getClass().getName() + " Implementation" );
126         implementation.setName( this.getClass().getName() + " Implementation" );
127 
128         module.getSpecifications().getSpecification().add( specification );
129         module.getImplementations().getImplementation().add( implementation );
130         modules.getModule().add( module );
131 
132         ModelHelper.setModules( model, modules );
133 
134         try
135         {
136             this.getModelProcessor().processModel( null, model );
137             fail( "Expected NullPointerException not thrown." );
138         }
139         catch ( final NullPointerException e )
140         {
141             assertNotNull( e.getMessage() );
142             System.out.println( e.toString() );
143         }
144 
145         try
146         {
147             this.getModelProcessor().processModel( context, null );
148             fail( "Expected NullPointerException not thrown." );
149         }
150         catch ( final NullPointerException e )
151         {
152             assertNotNull( e.getMessage() );
153             System.out.println( e.toString() );
154         }
155 
156         Model processed = this.getModelProcessor().processModel( context, model );
157         assertNotNull( processed );
158 
159         modules = ModelHelper.getModules( processed );
160         assertNotNull( modules );
161 
162         specification = modules.getSpecification( this.getClass().getName() + " Specification" );
163         assertNotNull( specification );
164 
165         implementation = modules.getImplementation( this.getClass().getName() + " Implementation" );
166         assertNotNull( implementation );
167 
168         SourceFileType ss = specification.getAnyObject( SourceFileType.class );
169         assertNull( ss );
170 
171         SourceFileType is = implementation.getAnyObject( SourceFileType.class );
172         assertNull( is );
173 
174         ss = new SourceFileType();
175         ss.setIdentifier( this.getClass().getName() + " Specification" );
176 
177         is = new SourceFileType();
178         is.setIdentifier( this.getClass().getName() + " Implementation" );
179 
180         specification.getAny().add( ss );
181         implementation.getAny().add( is );
182 
183         processed = this.getModelProcessor().processModel( context, processed );
184         assertNotNull( processed );
185 
186         modules = ModelHelper.getModules( processed );
187         assertNotNull( modules );
188 
189         specification = modules.getSpecification( this.getClass().getName() + " Specification" );
190         assertNotNull( specification );
191 
192         implementation = modules.getImplementation( this.getClass().getName() + " Implementation" );
193         assertNotNull( implementation );
194 
195         ss = specification.getAnyObject( SourceFileType.class );
196         assertNotNull( ss );
197         assertNotNull( ss.getLocation() );
198         assertNotNull( ss.getHeadComment() );
199 
200         is = implementation.getAnyObject( SourceFileType.class );
201         assertNotNull( is );
202         assertNotNull( is.getLocation() );
203         assertNotNull( is.getHeadComment() );
204 
205         specification.getAny().clear();
206         implementation.getAny().clear();
207 
208         SourceFilesType specificationSourceFiles = new SourceFilesType();
209         ss = new SourceFileType();
210         ss.setIdentifier( this.getClass().getSimpleName() );
211         ss.setSourceSections( new SourceSectionsType() );
212         specificationSourceFiles.getSourceFile().add( ss );
213         specification.getAny().add( specificationSourceFiles );
214 
215         SourceFilesType implementationSourceFiles = new SourceFilesType();
216         is = new SourceFileType();
217         is.setIdentifier( this.getClass().getSimpleName() );
218         is.setSourceSections( new SourceSectionsType() );
219         implementationSourceFiles.getSourceFile().add( is );
220         implementation.getAny().add( implementationSourceFiles );
221 
222         SourceSectionType sourceSection = new SourceSectionType();
223         sourceSection.setName( "License Header" );
224 
225         ss.getSourceSections().getSourceSection().add( sourceSection );
226         is.getSourceSections().getSourceSection().add( sourceSection );
227 
228         sourceSection = new SourceSectionType();
229         sourceSection.setName( "Annotations" );
230 
231         ss.getSourceSections().getSourceSection().add( sourceSection );
232         is.getSourceSections().getSourceSection().add( sourceSection );
233 
234         sourceSection = new SourceSectionType();
235         sourceSection.setName( "Documentation" );
236 
237         ss.getSourceSections().getSourceSection().add( sourceSection );
238         is.getSourceSections().getSourceSection().add( sourceSection );
239 
240         sourceSection = new SourceSectionType();
241         sourceSection.setName( this.getClass().getSimpleName() );
242 
243         ss.getSourceSections().getSourceSection().add( sourceSection );
244         is.getSourceSections().getSourceSection().add( sourceSection );
245 
246         sourceSection = new SourceSectionType();
247         sourceSection.setName( "Constructors" );
248 
249         is.getSourceSections().getSourceSection().add( sourceSection );
250 
251         sourceSection = new SourceSectionType();
252         sourceSection.setName( "Default Constructor" );
253 
254         is.getSourceSections().getSourceSection().add( sourceSection );
255 
256         sourceSection = new SourceSectionType();
257         sourceSection.setName( "Dependencies" );
258 
259         is.getSourceSections().getSourceSection().add( sourceSection );
260 
261         sourceSection = new SourceSectionType();
262         sourceSection.setName( "Properties" );
263 
264         is.getSourceSections().getSourceSection().add( sourceSection );
265 
266         sourceSection = new SourceSectionType();
267         sourceSection.setName( "Messages" );
268 
269         is.getSourceSections().getSourceSection().add( sourceSection );
270 
271         processed = this.getModelProcessor().processModel( context, processed );
272         assertNotNull( processed );
273 
274         modules = ModelHelper.getModules( processed );
275         assertNotNull( modules );
276 
277         specification = modules.getSpecification( this.getClass().getName() + " Specification" );
278         assertNotNull( specification );
279 
280         implementation = modules.getImplementation( this.getClass().getName() + " Implementation" );
281         assertNotNull( implementation );
282 
283         specificationSourceFiles = specification.getAnyObject( SourceFilesType.class );
284         assertNotNull( specificationSourceFiles );
285 
286         ss = specificationSourceFiles.getSourceFile( this.getClass().getSimpleName() );
287         assertNotNull( ss );
288         assertNotNull( ss.getHeadComment() );
289         assertNotNull( ss.getLocation() );
290         assertNotNull( ss.getTemplate() );
291 
292         implementationSourceFiles = implementation.getAnyObject( SourceFilesType.class );
293         assertNotNull( implementationSourceFiles );
294         is = implementationSourceFiles.getSourceFile( this.getClass().getSimpleName() );
295         assertNotNull( is );
296         assertNotNull( is.getHeadComment() );
297         assertNotNull( is.getLocation() );
298         assertNotNull( is.getTemplate() );
299 
300         sourceSection = ss.getSourceSections().getSourceSection( "License Header" );
301         assertNotNull( sourceSection );
302         assertTrue( sourceSection.isOptional() );
303         assertNotNull( sourceSection.getHeadTemplate() );
304 
305         sourceSection = is.getSourceSections().getSourceSection( "License Header" );
306         assertNotNull( sourceSection );
307         assertTrue( sourceSection.isOptional() );
308         assertNotNull( sourceSection.getHeadTemplate() );
309 
310         sourceSection = ss.getSourceSections().getSourceSection( "Annotations" );
311         assertNotNull( sourceSection );
312         assertNotNull( sourceSection.getHeadTemplate() );
313 
314         sourceSection = is.getSourceSections().getSourceSection( "Annotations" );
315         assertNotNull( sourceSection );
316         assertNotNull( sourceSection.getHeadTemplate() );
317 
318         sourceSection = ss.getSourceSections().getSourceSection( "Documentation" );
319         assertNotNull( sourceSection );
320         assertTrue( sourceSection.isOptional() );
321         assertNotNull( sourceSection.getHeadTemplate() );
322 
323         sourceSection = is.getSourceSections().getSourceSection( "Documentation" );
324         assertNotNull( sourceSection );
325         assertTrue( sourceSection.isOptional() );
326         assertNotNull( sourceSection.getHeadTemplate() );
327 
328         sourceSection = ss.getSourceSections().getSourceSection( this.getClass().getSimpleName() );
329         assertNotNull( sourceSection );
330         assertTrue( sourceSection.isEditable() );
331         assertEquals( 1, sourceSection.getIndentationLevel() );
332 
333         sourceSection = is.getSourceSections().getSourceSection( this.getClass().getSimpleName() );
334         assertNotNull( sourceSection );
335         assertTrue( sourceSection.isEditable() );
336         assertEquals( 1, sourceSection.getIndentationLevel() );
337 
338         sourceSection = is.getSourceSections().getSourceSection( "Constructors" );
339         assertNotNull( sourceSection );
340         assertNotNull( sourceSection.getHeadTemplate() );
341         assertNotNull( sourceSection.getTailTemplate() );
342         assertEquals( 1, sourceSection.getIndentationLevel() );
343         assertTrue( sourceSection.isOptional() );
344 
345         sourceSection = is.getSourceSections().getSourceSection( "Default Constructor" );
346         assertNotNull( sourceSection );
347         assertNotNull( sourceSection.getHeadTemplate() );
348         assertEquals( 2, sourceSection.getIndentationLevel() );
349         assertTrue( sourceSection.isEditable() );
350 
351         sourceSection = is.getSourceSections().getSourceSection( "Dependencies" );
352         assertNotNull( sourceSection );
353         assertNotNull( sourceSection.getHeadTemplate() );
354         assertEquals( 1, sourceSection.getIndentationLevel() );
355         assertTrue( sourceSection.isOptional() );
356 
357         sourceSection = is.getSourceSections().getSourceSection( "Properties" );
358         assertNotNull( sourceSection );
359         assertNotNull( sourceSection.getHeadTemplate() );
360         assertEquals( 1, sourceSection.getIndentationLevel() );
361         assertTrue( sourceSection.isOptional() );
362 
363         sourceSection = is.getSourceSections().getSourceSection( "Messages" );
364         assertNotNull( sourceSection );
365         assertNotNull( sourceSection.getHeadTemplate() );
366         assertEquals( 1, sourceSection.getIndentationLevel() );
367         assertTrue( sourceSection.isOptional() );
368 
369         sourceSection = is.getSourceSections().getSourceSection( this.getClass().getSimpleName() );
370         assertNotNull( sourceSection );
371         assertEquals( 1, sourceSection.getIndentationLevel() );
372         assertTrue( sourceSection.isEditable() );
373     }
374 
375     @Test
376     public final void testDefaultEnabled() throws Exception
377     {
378         System.clearProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultEnabled" );
379         ToolsModelProcessor.setDefaultEnabled( null );
380         assertTrue( ToolsModelProcessor.isDefaultEnabled() );
381 
382         System.setProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultEnabled", Boolean.toString( false ) );
383         ToolsModelProcessor.setDefaultEnabled( null );
384         assertFalse( ToolsModelProcessor.isDefaultEnabled() );
385         System.clearProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultEnabled" );
386         ToolsModelProcessor.setDefaultEnabled( null );
387         assertTrue( ToolsModelProcessor.isDefaultEnabled() );
388 
389         System.setProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultEnabled", Boolean.toString( true ) );
390         ToolsModelProcessor.setDefaultEnabled( null );
391         assertTrue( ToolsModelProcessor.isDefaultEnabled() );
392         System.clearProperty( "org.jomc.tools.modlet.ToolsModelProcessor.defaultEnabled" );
393         ToolsModelProcessor.setDefaultEnabled( null );
394         assertTrue( ToolsModelProcessor.isDefaultEnabled() );
395     }
396 
397     @Test
398     public final void testEnabled() throws Exception
399     {
400         final Model model = new Model();
401         model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
402 
403         ToolsModelProcessor.setDefaultEnabled( null );
404         this.getModelProcessor().setEnabled( null );
405         assertTrue( this.getModelProcessor().isEnabled() );
406 
407         this.getModelProcessor().processModel( ModelContextFactory.newInstance().newModelContext(), model );
408         ToolsModelProcessor.setDefaultEnabled( false );
409         this.getModelProcessor().setEnabled( null );
410         assertFalse( this.getModelProcessor().isEnabled() );
411 
412         this.getModelProcessor().processModel( ModelContextFactory.newInstance().newModelContext(), model );
413         ToolsModelProcessor.setDefaultEnabled( null );
414         this.getModelProcessor().setEnabled( null );
415     }
416 
417 }