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