1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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.model.TemplateParameterType;
59 import org.jomc.tools.modlet.ToolsModelValidator;
60 import org.junit.Test;
61 import static org.junit.Assert.assertNotNull;
62 import static org.junit.Assert.assertTrue;
63 import static org.junit.Assert.fail;
64
65
66
67
68
69
70
71 public class ToolsModelValidatorTest
72 {
73
74
75
76
77 private ToolsModelValidator toolsModelValidator;
78
79
80
81
82 public ToolsModelValidatorTest()
83 {
84 super();
85 }
86
87
88
89
90
91
92
93
94 public ToolsModelValidator getModelValidator()
95 {
96 if ( this.toolsModelValidator == null )
97 {
98 this.toolsModelValidator = this.newModelValidator();
99 }
100
101 return this.toolsModelValidator;
102 }
103
104
105
106
107
108
109
110
111 protected ToolsModelValidator newModelValidator()
112 {
113 return new ToolsModelValidator();
114 }
115
116 @Test
117 public final void testValidateModel() throws Exception
118 {
119 final ModelContext modelContext = ModelContextFactory.newInstance().newModelContext();
120
121 try
122 {
123 this.getModelValidator().validateModel( modelContext, null );
124 fail( "Expected NullPointerException not thrown." );
125 }
126 catch ( final NullPointerException e )
127 {
128 assertNotNull( e.getMessage() );
129 System.out.println( e.toString() );
130 }
131
132 try
133 {
134 this.getModelValidator().validateModel( null, new Model() );
135 fail( "Expected NullPointerException not thrown." );
136 }
137 catch ( final NullPointerException e )
138 {
139 assertNotNull( e.getMessage() );
140 System.out.println( e.toString() );
141 }
142
143 ModelValidationReport report = this.getModelValidator().validateModel( modelContext, new Model() );
144 assertNotNull( report );
145 assertTrue( report.isModelValid() );
146
147 final Model model = new Model();
148 model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
149
150 final SourceFileType sourceFile1 = new SourceFileType();
151 sourceFile1.setIdentifier( this.getClass().getSimpleName() + " 1" );
152 sourceFile1.getTemplateParameter().add( new TemplateParameterType() );
153 sourceFile1.getTemplateParameter().get( 0 ).setName( "SourceFile1" );
154 sourceFile1.getTemplateParameter().get( 0 ).setType( "DOES_NOT_EXIST" );
155 sourceFile1.getTemplateParameter().get( 0 ).setValue( "TEST" );
156
157 final SourceFileType sourceFile2 = new SourceFileType();
158 sourceFile2.setIdentifier( this.getClass().getSimpleName() + " 2" );
159 sourceFile2.getTemplateParameter().add( new TemplateParameterType() );
160 sourceFile2.getTemplateParameter().get( 0 ).setName( "SourceFile2" );
161 sourceFile2.getTemplateParameter().get( 0 ).setType( "DOES_NOT_EXIST" );
162 sourceFile2.getTemplateParameter().get( 0 ).setValue( "TEST" );
163
164 final SourceFilesType sourceFiles1 = new SourceFilesType();
165 sourceFiles1.getSourceFile().add( sourceFile1 );
166 sourceFiles1.getSourceFile().add( sourceFile2 );
167
168 final SourceFilesType sourceFiles2 = new SourceFilesType();
169 sourceFiles2.getSourceFile().add( sourceFile1 );
170 sourceFiles2.getSourceFile().add( sourceFile2 );
171
172 final SourceSectionType sourceSection1 = new SourceSectionType();
173 sourceSection1.setName( this.getClass().getSimpleName() + " 1" );
174 sourceSection1.getTemplateParameter().add( new TemplateParameterType() );
175 sourceSection1.getTemplateParameter().get( 0 ).setName( "SourceSection1" );
176 sourceSection1.getTemplateParameter().get( 0 ).setType( "DOES_NOT_EXIST" );
177 sourceSection1.getTemplateParameter().get( 0 ).setValue( "TEST" );
178 sourceSection1.setSourceSections( new SourceSectionsType() );
179 sourceSection1.getSourceSections().getSourceSection().add( sourceSection1.clone() );
180 sourceSection1.getSourceSections().getSourceSection().get( 0 ).setName( "SourceSection1 - nested" );
181
182 final SourceSectionType sourceSection2 = new SourceSectionType();
183 sourceSection2.setName( this.getClass().getSimpleName() + " 2" );
184 sourceSection2.getTemplateParameter().add( new TemplateParameterType() );
185 sourceSection2.getTemplateParameter().get( 0 ).setName( "SourceSection2" );
186 sourceSection2.getTemplateParameter().get( 0 ).setType( "DOES_NOT_EXIST" );
187 sourceSection2.getTemplateParameter().get( 0 ).setValue( "TEST" );
188 sourceSection2.setSourceSections( new SourceSectionsType() );
189 sourceSection2.getSourceSections().getSourceSection().add( sourceSection2.clone() );
190 sourceSection2.getSourceSections().getSourceSection().get( 0 ).setName( "SourceSection2 - nested" );
191
192 final SourceSectionsType sourceSections1 = new SourceSectionsType();
193 sourceSections1.getSourceSection().add( sourceSection1 );
194 sourceSections1.getSourceSection().add( sourceSection2 );
195
196 final SourceSectionsType sourceSections2 = new SourceSectionsType();
197 sourceSections2.getSourceSection().add( sourceSection1 );
198 sourceSections2.getSourceSection().add( sourceSection2 );
199
200 sourceFile1.setSourceSections( sourceSections1 );
201 sourceFile2.setSourceSections( sourceSections2 );
202
203 model.getAny().add( new ObjectFactory().createSourceFile( sourceFile1 ) );
204 model.getAny().add( new ObjectFactory().createSourceFile( sourceFile2 ) );
205 model.getAny().add( new ObjectFactory().createSourceFiles( sourceFiles1 ) );
206 model.getAny().add( new ObjectFactory().createSourceFiles( sourceFiles2 ) );
207 model.getAny().add( new ObjectFactory().createSourceFiles( new SourceFilesType() ) );
208 model.getAny().add( new ObjectFactory().createSourceSection( sourceSection1 ) );
209 model.getAny().add( new ObjectFactory().createSourceSection( sourceSection2 ) );
210 model.getAny().add( new ObjectFactory().createSourceSections( sourceSections1 ) );
211 model.getAny().add( new ObjectFactory().createSourceSections( sourceSections2 ) );
212 model.getAny().add( new ObjectFactory().createSourceSections( new SourceSectionsType() ) );
213
214 final Modules modules = new Modules();
215 ModelHelper.setModules( model, modules );
216
217 final Module module = new Module();
218 modules.getModule().add( module );
219 module.setSpecifications( new Specifications() );
220 module.setImplementations( new Implementations() );
221 module.setName( this.getClass().getSimpleName() );
222
223 module.getAny().add( new ObjectFactory().createSourceFile( sourceFile1 ) );
224 module.getAny().add( new ObjectFactory().createSourceFile( sourceFile2 ) );
225 module.getAny().add( new ObjectFactory().createSourceFiles( sourceFiles1 ) );
226 module.getAny().add( new ObjectFactory().createSourceFiles( sourceFiles2 ) );
227 module.getAny().add( new ObjectFactory().createSourceFiles( new SourceFilesType() ) );
228 module.getAny().add( new ObjectFactory().createSourceSection( sourceSection1 ) );
229 module.getAny().add( new ObjectFactory().createSourceSection( sourceSection2 ) );
230 module.getAny().add( new ObjectFactory().createSourceSections( sourceSections1 ) );
231 module.getAny().add( new ObjectFactory().createSourceSections( sourceSections2 ) );
232 module.getAny().add( new ObjectFactory().createSourceSections( new SourceSectionsType() ) );
233 module.setMessages( new Messages() );
234
235 final Specification specification = new Specification();
236 specification.setIdentifier( this.getClass().getSimpleName() );
237 specification.getAny().add( new ObjectFactory().createSourceFile( sourceFile1 ) );
238 specification.getAny().add( new ObjectFactory().createSourceFile( sourceFile2 ) );
239 specification.getAny().add( new ObjectFactory().createSourceFiles( sourceFiles1 ) );
240 specification.getAny().add( new ObjectFactory().createSourceFiles( sourceFiles2 ) );
241 specification.getAny().add( new ObjectFactory().createSourceFiles( new SourceFilesType() ) );
242 specification.getAny().add( new ObjectFactory().createSourceSection( sourceSection1 ) );
243 specification.getAny().add( new ObjectFactory().createSourceSection( sourceSection2 ) );
244 specification.getAny().add( new ObjectFactory().createSourceSections( sourceSections1 ) );
245 specification.getAny().add( new ObjectFactory().createSourceSections( sourceSections2 ) );
246 specification.getAny().add( new ObjectFactory().createSourceSections( new SourceSectionsType() ) );
247
248 final Implementation implementation = new Implementation();
249 implementation.setIdentifier( this.getClass().getSimpleName() );
250 implementation.setName( this.getClass().getSimpleName() );
251 implementation.getAny().add( new ObjectFactory().createSourceFile( sourceFile1 ) );
252 implementation.getAny().add( new ObjectFactory().createSourceFile( sourceFile2 ) );
253 implementation.getAny().add( new ObjectFactory().createSourceFiles( sourceFiles1 ) );
254 implementation.getAny().add( new ObjectFactory().createSourceFiles( sourceFiles2 ) );
255 implementation.getAny().add( new ObjectFactory().createSourceFiles( new SourceFilesType() ) );
256 implementation.getAny().add( new ObjectFactory().createSourceSection( sourceSection1 ) );
257 implementation.getAny().add( new ObjectFactory().createSourceSection( sourceSection2 ) );
258 implementation.getAny().add( new ObjectFactory().createSourceSections( sourceSections1 ) );
259 implementation.getAny().add( new ObjectFactory().createSourceSections( sourceSections2 ) );
260 implementation.getAny().add( new ObjectFactory().createSourceSections( new SourceSectionsType() ) );
261
262 implementation.setDependencies( new Dependencies() );
263 implementation.setMessages( new Messages() );
264
265 final Dependency dependency = new Dependency();
266 dependency.setName( this.getClass().getSimpleName() );
267 dependency.setIdentifier( this.getClass().getSimpleName() );
268 dependency.getAny().add( new ObjectFactory().createSourceFile( sourceFile1 ) );
269 dependency.getAny().add( new ObjectFactory().createSourceFile( sourceFile2 ) );
270 dependency.getAny().add( new ObjectFactory().createSourceFiles( sourceFiles1 ) );
271 dependency.getAny().add( new ObjectFactory().createSourceFiles( sourceFiles2 ) );
272 dependency.getAny().add( new ObjectFactory().createSourceFiles( new SourceFilesType() ) );
273 dependency.getAny().add( new ObjectFactory().createSourceSection( sourceSection1 ) );
274 dependency.getAny().add( new ObjectFactory().createSourceSection( sourceSection2 ) );
275 dependency.getAny().add( new ObjectFactory().createSourceSections( sourceSections1 ) );
276 dependency.getAny().add( new ObjectFactory().createSourceSections( sourceSections2 ) );
277 dependency.getAny().add( new ObjectFactory().createSourceSections( new SourceSectionsType() ) );
278
279 final Message message = new Message();
280 message.setName( this.getClass().getSimpleName() );
281 message.setTemplate( new Texts() );
282 message.getTemplate().setDefaultLanguage( "en" );
283
284 final Text text = new Text();
285 text.setLanguage( "en" );
286 message.getTemplate().getText().add( text );
287
288 message.getAny().add( new ObjectFactory().createSourceFile( sourceFile1 ) );
289 message.getAny().add( new ObjectFactory().createSourceFile( sourceFile2 ) );
290 message.getAny().add( new ObjectFactory().createSourceFiles( sourceFiles1 ) );
291 message.getAny().add( new ObjectFactory().createSourceFiles( sourceFiles2 ) );
292 message.getAny().add( new ObjectFactory().createSourceFiles( new SourceFilesType() ) );
293 message.getAny().add( new ObjectFactory().createSourceSection( sourceSection1 ) );
294 message.getAny().add( new ObjectFactory().createSourceSection( sourceSection2 ) );
295 message.getAny().add( new ObjectFactory().createSourceSections( sourceSections1 ) );
296 message.getAny().add( new ObjectFactory().createSourceSections( sourceSections2 ) );
297 message.getAny().add( new ObjectFactory().createSourceSections( new SourceSectionsType() ) );
298
299 implementation.getDependencies().getDependency().add( dependency );
300 implementation.getMessages().getMessage().add( message );
301
302 module.getImplementations().getImplementation().add( implementation );
303 module.getMessages().getMessage().add( message );
304 module.getSpecifications().getSpecification().add( specification );
305
306 final Specification deprecatedSpecification = new Specification();
307 deprecatedSpecification.setIdentifier( this.getClass().getSimpleName() + " 2" );
308 deprecatedSpecification.getAny().add( new ObjectFactory().createSourceFile( sourceFile1 ) );
309
310 final Implementation deprecatedImplementation = new Implementation();
311 deprecatedImplementation.setIdentifier( this.getClass().getSimpleName() + " 2" );
312 deprecatedImplementation.setName( this.getClass().getSimpleName() );
313 deprecatedImplementation.getAny().add( new ObjectFactory().createSourceFile( sourceFile1 ) );
314
315 module.getSpecifications().getSpecification().add( deprecatedSpecification );
316 module.getImplementations().getImplementation().add( deprecatedImplementation );
317
318 final JAXBSource jaxbSource = new JAXBSource( modelContext.createMarshaller(
319 ModelObject.MODEL_PUBLIC_ID ), new org.jomc.modlet.ObjectFactory().createModel( model ) );
320
321 report = modelContext.validateModel( ModelObject.MODEL_PUBLIC_ID, jaxbSource );
322 assertValidModel( report );
323
324 report = this.getModelValidator().validateModel( modelContext, model );
325 assertInvalidModel( report );
326 assertModelValidationReportDetail( report, "MODEL_SOURCE_FILE_CONSTRAINT", 6 );
327 assertModelValidationReportDetail( report, "MODEL_SOURCE_FILES_CONSTRAINT", 1 );
328 assertModelValidationReportDetail( report, "MODEL_SOURCE_SECTION_CONSTRAINT", 6 );
329 assertModelValidationReportDetail( report, "MODEL_SOURCE_SECTIONS_CONSTRAINT", 1 );
330 assertModelValidationReportDetail( report, "MODULE_SOURCE_FILE_CONSTRAINT", 6 );
331 assertModelValidationReportDetail( report, "MODULE_SOURCE_FILES_CONSTRAINT", 1 );
332 assertModelValidationReportDetail( report, "MODULE_SOURCE_SECTION_CONSTRAINT", 6 );
333 assertModelValidationReportDetail( report, "MODULE_SOURCE_SECTIONS_CONSTRAINT", 1 );
334 assertModelValidationReportDetail( report, "IMPLEMENTATION_SOURCE_FILE_MULTIPLICITY_CONSTRAINT", 1 );
335 assertModelValidationReportDetail( report, "IMPLEMENTATION_SOURCE_FILES_MULTIPLICITY_CONSTRAINT", 1 );
336 assertModelValidationReportDetail( report, "IMPLEMENTATION_SOURCE_SECTION_CONSTRAINT", 6 );
337 assertModelValidationReportDetail( report, "IMPLEMENTATION_SOURCE_SECTIONS_CONSTRAINT", 1 );
338 assertModelValidationReportDetail( report, "IMPLEMENTATION_SOURCE_FILE_INFORMATION", 1 );
339 assertModelValidationReportDetail( report, "IMPLEMENTATION_DEPENDENCY_SOURCE_FILE_CONSTRAINT", 6 );
340 assertModelValidationReportDetail( report, "IMPLEMENTATION_DEPENDENCY_SOURCE_FILES_CONSTRAINT", 1 );
341 assertModelValidationReportDetail( report, "IMPLEMENTATION_DEPENDENCY_SOURCE_SECTION_CONSTRAINT", 6 );
342 assertModelValidationReportDetail( report, "IMPLEMENTATION_DEPENDENCY_SOURCE_SECTIONS_CONSTRAINT", 1 );
343 assertModelValidationReportDetail( report, "IMPLEMENTATION_MESSAGE_SOURCE_FILE_CONSTRAINT", 6 );
344 assertModelValidationReportDetail( report, "IMPLEMENTATION_MESSAGE_SOURCE_FILES_CONSTRAINT", 1 );
345 assertModelValidationReportDetail( report, "IMPLEMENTATION_MESSAGE_SOURCE_SECTION_CONSTRAINT", 6 );
346 assertModelValidationReportDetail( report, "IMPLEMENTATION_MESSAGE_SOURCE_SECTIONS_CONSTRAINT", 1 );
347 assertModelValidationReportDetail( report, "SPECIFICATION_SOURCE_FILE_MULTIPLICITY_CONSTRAINT", 1 );
348 assertModelValidationReportDetail( report, "SPECIFICATION_SOURCE_FILES_MULTIPLICITY_CONSTRAINT", 1 );
349 assertModelValidationReportDetail( report, "SPECIFICATION_SOURCE_SECTION_CONSTRAINT", 6 );
350 assertModelValidationReportDetail( report, "SPECIFICATION_SOURCE_SECTIONS_CONSTRAINT", 1 );
351 assertModelValidationReportDetail( report, "SPECIFICATION_SOURCE_FILE_INFORMATION", 1 );
352 assertModelValidationReportDetail( report, "MODEL_SOURCE_FILE_TEMPLATE_PARAMETER_JAVA_VALUE_CONSTRAINT", 6 );
353 assertModelValidationReportDetail( report, "MODEL_SOURCE_FILE_SECTION_TEMPLATE_PARAMETER_JAVA_VALUE_CONSTRAINT",
354 24 );
355
356 assertModelValidationReportDetail( report, "MODEL_SOURCE_SECTION_TEMPLATE_PARAMETER_JAVA_VALUE_CONSTRAINT",
357 12 );
358
359 assertModelValidationReportDetail( report, "MODULE_SOURCE_FILE_TEMPLATE_PARAMETER_JAVA_VALUE_CONSTRAINT", 6 );
360
361 assertModelValidationReportDetail( report,
362 "MODULE_SOURCE_FILE_SECTION_TEMPLATE_PARAMETER_JAVA_VALUE_CONSTRAINT",
363 24 );
364
365 assertModelValidationReportDetail( report, "MODULE_SOURCE_SECTION_TEMPLATE_PARAMETER_JAVA_VALUE_CONSTRAINT",
366 12 );
367
368 assertModelValidationReportDetail( report,
369 "IMPLEMENTATION_SOURCE_FILE_TEMPLATE_PARAMETER_JAVA_VALUE_CONSTRAINT",
370 7 );
371
372 assertModelValidationReportDetail( report,
373 "IMPLEMENTATION_SOURCE_FILE_SECTION_TEMPLATE_PARAMETER_JAVA_VALUE_CONSTRAINT",
374 28 );
375
376 assertModelValidationReportDetail( report,
377 "IMPLEMENTATION_SOURCE_SECTION_TEMPLATE_PARAMETER_JAVA_VALUE_CONSTRAINT",
378 12 );
379
380 assertModelValidationReportDetail( report,
381 "IMPLEMENTATION_DEPENDENCY_SOURCE_FILE_TEMPLATE_PARAMETER_JAVA_VALUE_CONSTRAINT",
382 6 );
383
384 assertModelValidationReportDetail( report,
385 "IMPLEMENTATION_DEPENDENCY_SOURCE_FILE_SECTION_TEMPLATE_PARAMETER_JAVA_VALUE_CONSTRAINT",
386 24 );
387
388 assertModelValidationReportDetail( report,
389 "IMPLEMENTATION_DEPENDENCY_SOURCE_SECTION_TEMPLATE_PARAMETER_JAVA_VALUE_CONSTRAINT",
390 12 );
391
392 assertModelValidationReportDetail( report,
393 "IMPLEMENTATION_MESSAGE_SOURCE_FILE_TEMPLATE_PARAMETER_JAVA_VALUE_CONSTRAINT",
394 6 );
395
396 assertModelValidationReportDetail( report,
397 "IMPLEMENTATION_MESSAGE_SOURCE_FILE_SECTION_TEMPLATE_PARAMETER_JAVA_VALUE_CONSTRAINT",
398 24 );
399
400 assertModelValidationReportDetail( report,
401 "IMPLEMENTATION_MESSAGE_SOURCE_SECTION_TEMPLATE_PARAMETER_JAVA_VALUE_CONSTRAINT",
402 12 );
403
404 assertModelValidationReportDetail( report,
405 "SPECIFICATION_SOURCE_FILE_TEMPLATE_PARAMETER_JAVA_VALUE_CONSTRAINT",
406 7 );
407
408 assertModelValidationReportDetail( report,
409 "SPECIFICATION_SOURCE_FILE_SECTION_TEMPLATE_PARAMETER_JAVA_VALUE_CONSTRAINT",
410 28 );
411
412 assertModelValidationReportDetail( report,
413 "SPECIFICATION_SOURCE_SECTION_TEMPLATE_PARAMETER_JAVA_VALUE_CONSTRAINT",
414 12 );
415 }
416
417 private static void assertValidModel( final ModelValidationReport report )
418 {
419 assertNotNull( report );
420
421 if ( !report.isModelValid() )
422 {
423 System.out.println( ">>>Unexpected invalid model:" );
424 logModelValidationReport( report );
425 fail( report.toString() );
426 }
427 else
428 {
429 System.out.println( ">>>Valid model:" );
430 logModelValidationReport( report );
431 }
432 }
433
434 private static void assertInvalidModel( final ModelValidationReport report )
435 {
436 assertNotNull( report );
437
438 if ( report.isModelValid() )
439 {
440 System.out.println( ">>>Unexpected valid model:" );
441 logModelValidationReport( report );
442 fail( report.toString() );
443 }
444 else
445 {
446 System.out.println( ">>>Invalid model:" );
447 logModelValidationReport( report );
448 }
449 }
450
451 private static void assertModelValidationReportDetail( final ModelValidationReport report, final String identifier,
452 final int count )
453 {
454 final List<ModelValidationReport.Detail> details = report.getDetails( identifier );
455
456 if ( details.size() != count )
457 {
458 System.out.println( ">>>Unexpected number of '" + identifier + "' details. Expected " + count + " - found "
459 + details.size() + "." );
460
461 logModelValidationReport( report );
462 fail( report.toString() );
463 }
464 }
465
466 private static void logModelValidationReport( final ModelValidationReport report )
467 {
468 for ( final ModelValidationReport.Detail d : report.getDetails() )
469 {
470 System.out.println( "\t" + d );
471 }
472 }
473
474 }