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: ToolsModelValidatorTest.java 4200 2012-01-25 09:46:13Z schulte2005 $
29   *
30   */
31  package org.jomc.tools.modlet.test;
32  
33  import java.util.List;
34  import javax.xml.bind.util.JAXBSource;
35  import org.jomc.model.Dependencies;
36  import org.jomc.model.Dependency;
37  import org.jomc.model.Implementation;
38  import org.jomc.model.Implementations;
39  import org.jomc.model.Message;
40  import org.jomc.model.Messages;
41  import org.jomc.model.ModelObject;
42  import org.jomc.model.Module;
43  import org.jomc.model.Modules;
44  import org.jomc.model.Specification;
45  import org.jomc.model.Specifications;
46  import org.jomc.model.Text;
47  import org.jomc.model.Texts;
48  import org.jomc.model.modlet.ModelHelper;
49  import org.jomc.modlet.Model;
50  import org.jomc.modlet.ModelContext;
51  import org.jomc.modlet.ModelContextFactory;
52  import org.jomc.modlet.ModelValidationReport;
53  import org.jomc.tools.model.SourceFileType;
54  import org.jomc.tools.model.SourceFilesType;
55  import org.jomc.tools.model.SourceSectionType;
56  import org.jomc.tools.model.SourceSectionsType;
57  import org.jomc.tools.modlet.ToolsModelValidator;
58  import org.junit.Test;
59  import static org.junit.Assert.assertNotNull;
60  import static org.junit.Assert.assertTrue;
61  import static org.junit.Assert.fail;
62  
63  /**
64   * Test cases for class {@code org.jomc.tools.modlet.ToolsModelValidator}.
65   *
66   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
67   * @version $JOMC: ToolsModelValidatorTest.java 4200 2012-01-25 09:46:13Z schulte2005 $
68   */
69  public class ToolsModelValidatorTest
70  {
71  
72      /** The {@code ToolsModelValidator} instance tests are performed with. */
73      private ToolsModelValidator toolsModelValidator;
74  
75      /** Creates a new {@code ToolsModelValidatorTest} instance. */
76      public ToolsModelValidatorTest()
77      {
78          super();
79      }
80  
81      /**
82       * Gets the {@code ToolsModelValidator} instance tests are performed with.
83       *
84       * @return The {@code ToolsModelValidator} instance tests are performed with.
85       *
86       * @see #newModelValidator()
87       */
88      public ToolsModelValidator getModelValidator()
89      {
90          if ( this.toolsModelValidator == null )
91          {
92              this.toolsModelValidator = this.newModelValidator();
93          }
94  
95          return this.toolsModelValidator;
96      }
97  
98      /**
99       * Create a new {@code ToolsModelValidator} instance to test.
100      *
101      * @return A new {@code ToolsModelValidator} instance to test.
102      *
103      * @see #getModelValidator()
104      */
105     protected ToolsModelValidator newModelValidator()
106     {
107         return new ToolsModelValidator();
108     }
109 
110     @Test
111     public final void testValidateModel() throws Exception
112     {
113         final ModelContext modelContext = ModelContextFactory.newInstance().newModelContext();
114 
115         try
116         {
117             this.getModelValidator().validateModel( modelContext, null );
118             fail( "Expected NullPointerException not thrown." );
119         }
120         catch ( final NullPointerException e )
121         {
122             assertNotNull( e.getMessage() );
123             System.out.println( e.toString() );
124         }
125 
126         try
127         {
128             this.getModelValidator().validateModel( null, new Model() );
129             fail( "Expected NullPointerException not thrown." );
130         }
131         catch ( final NullPointerException e )
132         {
133             assertNotNull( e.getMessage() );
134             System.out.println( e.toString() );
135         }
136 
137         ModelValidationReport report = this.getModelValidator().validateModel( modelContext, new Model() );
138         assertNotNull( report );
139         assertTrue( report.isModelValid() );
140 
141         final Model model = new Model();
142         model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
143 
144         final SourceFileType sourceFile1 = new SourceFileType();
145         sourceFile1.setIdentifier( this.getClass().getSimpleName() + " 1" );
146 
147         final SourceFileType sourceFile2 = new SourceFileType();
148         sourceFile2.setIdentifier( this.getClass().getSimpleName() + " 2" );
149 
150         final SourceFilesType sourceFiles1 = new SourceFilesType();
151         sourceFiles1.getSourceFile().add( sourceFile1 );
152         sourceFiles1.getSourceFile().add( sourceFile2 );
153 
154         final SourceFilesType sourceFiles2 = new SourceFilesType();
155         sourceFiles2.getSourceFile().add( sourceFile1 );
156         sourceFiles2.getSourceFile().add( sourceFile2 );
157 
158         final SourceSectionType sourceSection1 = new SourceSectionType();
159         sourceSection1.setName( this.getClass().getSimpleName() + " 1" );
160 
161         final SourceSectionType sourceSection2 = new SourceSectionType();
162         sourceSection2.setName( this.getClass().getSimpleName() + " 2" );
163 
164         final SourceSectionsType sourceSections1 = new SourceSectionsType();
165         sourceSections1.getSourceSection().add( sourceSection1 );
166         sourceSections1.getSourceSection().add( sourceSection2 );
167 
168         final SourceSectionsType sourceSections2 = new SourceSectionsType();
169         sourceSections2.getSourceSection().add( sourceSection1 );
170         sourceSections2.getSourceSection().add( sourceSection2 );
171 
172         model.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFile( sourceFile1 ) );
173         model.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFile( sourceFile2 ) );
174         model.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFiles( sourceFiles1 ) );
175         model.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFiles( sourceFiles2 ) );
176         model.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFiles( new SourceFilesType() ) );
177         model.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSection( sourceSection1 ) );
178         model.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSection( sourceSection2 ) );
179         model.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSections( sourceSections1 ) );
180         model.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSections( sourceSections2 ) );
181         model.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSections( new SourceSectionsType() ) );
182 
183         final Modules modules = new Modules();
184         ModelHelper.setModules( model, modules );
185 
186         final Module module = new Module();
187         modules.getModule().add( module );
188         module.setSpecifications( new Specifications() );
189         module.setImplementations( new Implementations() );
190         module.setName( this.getClass().getSimpleName() );
191 
192         module.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFile( sourceFile1 ) );
193         module.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFile( sourceFile2 ) );
194         module.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFiles( sourceFiles1 ) );
195         module.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFiles( sourceFiles2 ) );
196         module.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFiles( new SourceFilesType() ) );
197         module.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSection( sourceSection1 ) );
198         module.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSection( sourceSection2 ) );
199         module.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSections( sourceSections1 ) );
200         module.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSections( sourceSections2 ) );
201         module.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSections( new SourceSectionsType() ) );
202         module.setMessages( new Messages() );
203 
204         final Specification specification = new Specification();
205         specification.setIdentifier( this.getClass().getSimpleName() );
206         specification.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFile( sourceFile1 ) );
207         specification.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFile( sourceFile2 ) );
208         specification.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFiles( sourceFiles1 ) );
209         specification.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFiles( sourceFiles2 ) );
210         specification.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFiles( new SourceFilesType() ) );
211         specification.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSection( sourceSection1 ) );
212         specification.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSection( sourceSection2 ) );
213         specification.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSections( sourceSections1 ) );
214         specification.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSections( sourceSections2 ) );
215         specification.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSections(
216             new SourceSectionsType() ) );
217 
218         final Implementation implementation = new Implementation();
219         implementation.setIdentifier( this.getClass().getSimpleName() );
220         implementation.setName( this.getClass().getSimpleName() );
221         implementation.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFile( sourceFile1 ) );
222         implementation.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFile( sourceFile2 ) );
223         implementation.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFiles( sourceFiles1 ) );
224         implementation.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFiles( sourceFiles2 ) );
225         implementation.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFiles( new SourceFilesType() ) );
226         implementation.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSection( sourceSection1 ) );
227         implementation.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSection( sourceSection2 ) );
228         implementation.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSections( sourceSections1 ) );
229         implementation.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSections( sourceSections2 ) );
230         implementation.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSections(
231             new SourceSectionsType() ) );
232 
233         implementation.setDependencies( new Dependencies() );
234         implementation.setMessages( new Messages() );
235 
236         final Dependency dependency = new Dependency();
237         dependency.setName( this.getClass().getSimpleName() );
238         dependency.setIdentifier( this.getClass().getSimpleName() );
239         dependency.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFile( sourceFile1 ) );
240         dependency.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFile( sourceFile2 ) );
241         dependency.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFiles( sourceFiles1 ) );
242         dependency.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFiles( sourceFiles2 ) );
243         dependency.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFiles( new SourceFilesType() ) );
244         dependency.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSection( sourceSection1 ) );
245         dependency.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSection( sourceSection2 ) );
246         dependency.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSections( sourceSections1 ) );
247         dependency.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSections( sourceSections2 ) );
248         dependency.getAny().add(
249             new org.jomc.tools.model.ObjectFactory().createSourceSections( new SourceSectionsType() ) );
250 
251         final Message message = new Message();
252         message.setName( this.getClass().getSimpleName() );
253         message.setTemplate( new Texts() );
254         message.getTemplate().setDefaultLanguage( "en" );
255 
256         final Text text = new Text();
257         text.setLanguage( "en" );
258         message.getTemplate().getText().add( text );
259 
260         message.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFile( sourceFile1 ) );
261         message.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFile( sourceFile2 ) );
262         message.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFiles( sourceFiles1 ) );
263         message.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFiles( sourceFiles2 ) );
264         message.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFiles( new SourceFilesType() ) );
265         message.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSection( sourceSection1 ) );
266         message.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSection( sourceSection2 ) );
267         message.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSections( sourceSections1 ) );
268         message.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSections( sourceSections2 ) );
269         message.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceSections( new SourceSectionsType() ) );
270 
271         implementation.getDependencies().getDependency().add( dependency );
272         implementation.getMessages().getMessage().add( message );
273 
274         module.getImplementations().getImplementation().add( implementation );
275         module.getMessages().getMessage().add( message );
276         module.getSpecifications().getSpecification().add( specification );
277 
278         final Specification deprecatedSpecification = new Specification();
279         deprecatedSpecification.setIdentifier( this.getClass().getSimpleName() + " 2" );
280         deprecatedSpecification.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFile( sourceFile1 ) );
281 
282         final Implementation deprecatedImplementation = new Implementation();
283         deprecatedImplementation.setIdentifier( this.getClass().getSimpleName() + " 2" );
284         deprecatedImplementation.setName( this.getClass().getSimpleName() );
285         deprecatedImplementation.getAny().add( new org.jomc.tools.model.ObjectFactory().createSourceFile( sourceFile1 ) );
286 
287         module.getSpecifications().getSpecification().add( deprecatedSpecification );
288         module.getImplementations().getImplementation().add( deprecatedImplementation );
289 
290         final JAXBSource jaxbSource = new JAXBSource( modelContext.createMarshaller(
291             ModelObject.MODEL_PUBLIC_ID ), new org.jomc.modlet.ObjectFactory().createModel( model ) );
292 
293         report = modelContext.validateModel( ModelObject.MODEL_PUBLIC_ID, jaxbSource );
294         assertValidModel( report );
295 
296         report = this.getModelValidator().validateModel( modelContext, model );
297         assertInvalidModel( report );
298         assertModelValidationReportDetail( report, "MODEL_SOURCE_FILE_CONSTRAINT", 6 );
299         assertModelValidationReportDetail( report, "MODEL_SOURCE_FILES_CONSTRAINT", 1 );
300         assertModelValidationReportDetail( report, "MODEL_SOURCE_SECTION_CONSTRAINT", 6 );
301         assertModelValidationReportDetail( report, "MODEL_SOURCE_SECTIONS_CONSTRAINT", 1 );
302         assertModelValidationReportDetail( report, "MODULE_SOURCE_FILE_CONSTRAINT", 6 );
303         assertModelValidationReportDetail( report, "MODULE_SOURCE_FILES_CONSTRAINT", 1 );
304         assertModelValidationReportDetail( report, "MODULE_SOURCE_SECTION_CONSTRAINT", 6 );
305         assertModelValidationReportDetail( report, "MODULE_SOURCE_SECTIONS_CONSTRAINT", 1 );
306         assertModelValidationReportDetail( report, "IMPLEMENTATION_SOURCE_FILE_MULTIPLICITY_CONSTRAINT", 1 );
307         assertModelValidationReportDetail( report, "IMPLEMENTATION_SOURCE_FILES_MULTIPLICITY_CONSTRAINT", 1 );
308         assertModelValidationReportDetail( report, "IMPLEMENTATION_SOURCE_SECTION_CONSTRAINT", 6 );
309         assertModelValidationReportDetail( report, "IMPLEMENTATION_SOURCE_SECTIONS_CONSTRAINT", 1 );
310         assertModelValidationReportDetail( report, "IMPLEMENTATION_SOURCE_FILE_INFORMATION", 1 );
311         assertModelValidationReportDetail( report, "IMPLEMENTATION_DEPENDENCY_SOURCE_FILE_CONSTRAINT", 6 );
312         assertModelValidationReportDetail( report, "IMPLEMENTATION_DEPENDENCY_SOURCE_FILES_CONSTRAINT", 1 );
313         assertModelValidationReportDetail( report, "IMPLEMENTATION_DEPENDENCY_SOURCE_SECTION_CONSTRAINT", 6 );
314         assertModelValidationReportDetail( report, "IMPLEMENTATION_DEPENDENCY_SOURCE_SECTIONS_CONSTRAINT", 1 );
315         assertModelValidationReportDetail( report, "IMPLEMENTATION_MESSAGE_SOURCE_FILE_CONSTRAINT", 6 );
316         assertModelValidationReportDetail( report, "IMPLEMENTATION_MESSAGE_SOURCE_FILES_CONSTRAINT", 1 );
317         assertModelValidationReportDetail( report, "IMPLEMENTATION_MESSAGE_SOURCE_SECTION_CONSTRAINT", 6 );
318         assertModelValidationReportDetail( report, "IMPLEMENTATION_MESSAGE_SOURCE_SECTIONS_CONSTRAINT", 1 );
319         assertModelValidationReportDetail( report, "SPECIFICATION_SOURCE_FILE_MULTIPLICITY_CONSTRAINT", 1 );
320         assertModelValidationReportDetail( report, "SPECIFICATION_SOURCE_FILES_MULTIPLICITY_CONSTRAINT", 1 );
321         assertModelValidationReportDetail( report, "SPECIFICATION_SOURCE_SECTION_CONSTRAINT", 6 );
322         assertModelValidationReportDetail( report, "SPECIFICATION_SOURCE_SECTIONS_CONSTRAINT", 1 );
323         assertModelValidationReportDetail( report, "SPECIFICATION_SOURCE_FILE_INFORMATION", 1 );
324     }
325 
326     private static void assertValidModel( final ModelValidationReport report )
327     {
328         assertNotNull( report );
329 
330         if ( !report.isModelValid() )
331         {
332             System.out.println( ">>>Unexpected invalid model:" );
333             logModelValidationReport( report );
334             fail( report.toString() );
335         }
336         else
337         {
338             System.out.println( ">>>Valid model:" );
339             logModelValidationReport( report );
340         }
341     }
342 
343     private static void assertInvalidModel( final ModelValidationReport report )
344     {
345         assertNotNull( report );
346 
347         if ( report.isModelValid() )
348         {
349             System.out.println( ">>>Unexpected valid model:" );
350             logModelValidationReport( report );
351             fail( report.toString() );
352         }
353         else
354         {
355             System.out.println( ">>>Invalid model:" );
356             logModelValidationReport( report );
357         }
358     }
359 
360     private static void assertModelValidationReportDetail( final ModelValidationReport report, final String identifier,
361                                                            final Number count )
362     {
363         final List<ModelValidationReport.Detail> details = report.getDetails( identifier );
364 
365         if ( details.size() != count )
366         {
367             System.out.println( ">>>Unexpected number of '" + identifier + "' details. Expected " + count + " - found "
368                                 + details.size() + "." );
369 
370             logModelValidationReport( report );
371             fail( report.toString() );
372         }
373     }
374 
375     private static void logModelValidationReport( final ModelValidationReport report )
376     {
377         for ( ModelValidationReport.Detail d : report.getDetails() )
378         {
379             System.out.println( "\t" + d );
380         }
381     }
382 
383 }