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.model.test;
32
33 import java.util.logging.Level;
34 import javax.xml.bind.JAXBContext;
35 import javax.xml.bind.JAXBElement;
36 import javax.xml.bind.JAXBException;
37 import javax.xml.bind.util.JAXBSource;
38 import org.jomc.model.Dependencies;
39 import org.jomc.model.Dependency;
40 import org.jomc.model.Implementation;
41 import org.jomc.model.Implementations;
42 import org.jomc.model.Instance;
43 import org.jomc.model.Message;
44 import org.jomc.model.Messages;
45 import org.jomc.model.ModelObject;
46 import org.jomc.model.Modules;
47 import org.jomc.model.Properties;
48 import org.jomc.model.Property;
49 import org.jomc.model.Specification;
50 import org.jomc.model.SpecificationReference;
51 import org.jomc.model.Specifications;
52 import org.jomc.model.Text;
53 import org.jomc.model.Texts;
54 import org.jomc.model.modlet.ModelHelper;
55 import org.jomc.modlet.Model;
56 import org.jomc.modlet.ModelContext;
57 import org.jomc.modlet.ModelContextFactory;
58 import org.jomc.modlet.ModelException;
59 import org.jomc.modlet.ModelValidationReport;
60 import org.junit.Assert;
61
62
63
64
65
66
67
68 public class ModulesTest
69 {
70
71
72 private static final String ABSOLUTE_RESOURCE_NAME_PREFIX = "/org/jomc/model/test/";
73
74
75 private TestSuite testSuite;
76
77
78 private ModelContext modelContext;
79
80
81 public ModulesTest()
82 {
83 super();
84 }
85
86
87
88
89
90
91
92
93 public TestSuite getTestSuite()
94 {
95 if ( this.testSuite == null )
96 {
97 this.testSuite = this.newTestSuite();
98 }
99
100 return this.testSuite;
101 }
102
103
104
105
106
107
108
109
110 protected TestSuite newTestSuite()
111 {
112 try
113 {
114 return ( (JAXBElement<TestSuite>) this.getModelContext().createUnmarshaller(
115 ModelObject.MODEL_PUBLIC_ID ).unmarshal( this.getClass().getResource(
116 ABSOLUTE_RESOURCE_NAME_PREFIX + "ModulesTestSuite.xml" ) ) ).getValue();
117
118 }
119 catch ( final JAXBException e )
120 {
121 throw new AssertionError( e );
122 }
123 catch ( final ModelException e )
124 {
125 throw new AssertionError( e );
126 }
127 }
128
129
130
131
132
133
134
135
136 public ModelContext getModelContext()
137 {
138 if ( this.modelContext == null )
139 {
140 this.modelContext = this.newModelContext();
141 this.modelContext.getListeners().add( new ModelContext.Listener()
142 {
143
144 @Override
145 public void onLog( final Level level, String message, Throwable t )
146 {
147 super.onLog( level, message, t );
148 System.out.println( "[" + level.getLocalizedName() + "] " + message );
149 if ( t != null )
150 {
151 t.printStackTrace( System.out );
152 }
153 }
154
155 } );
156 }
157
158 return this.modelContext;
159 }
160
161
162
163
164
165
166
167
168 protected ModelContext newModelContext()
169 {
170 return ModelContextFactory.newInstance().newModelContext();
171 }
172
173
174
175
176
177
178
179
180 public final void testImplementation( final String identifier ) throws Exception
181 {
182 Assert.assertNotNull( "identifier", identifier );
183
184 ImplementationTestType test = null;
185
186 for ( ImplementationTestType candidate : this.getTestSuite().getImplementationTest() )
187 {
188 if ( identifier.equals( candidate.getIdentifier() ) )
189 {
190 test = candidate;
191 break;
192 }
193 }
194
195 Assert.assertNotNull( "Implementation test '" + identifier + "' not found.", test );
196
197 final JAXBContext jaxbContext = this.getModelContext().createContext( ModelObject.MODEL_PUBLIC_ID );
198
199 System.out.println( "ImplementationTest: " + test.getIdentifier() );
200
201 final JAXBElement<Modules> modules = (JAXBElement<Modules>) test.getModules().getAny();
202 final Model model = new Model();
203 model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
204 ModelHelper.setModules( model, modules.getValue() );
205
206 final ModelValidationReport modulesReport = this.getModelContext().validateModel( model );
207
208 if ( !modulesReport.isModelValid() )
209 {
210 log( modulesReport );
211 }
212
213 Assert.assertTrue( "[" + test.getIdentifier() + "] Unexpected invalid modules.",
214 modulesReport.isModelValid() );
215
216 final JAXBElement<Implementation> expected =
217 (JAXBElement<Implementation>) test.getImplementation().getAny();
218
219 final ModelValidationReport implementationReport = this.getModelContext().validateModel(
220 ModelObject.MODEL_PUBLIC_ID, new JAXBSource( jaxbContext, expected ) );
221
222 if ( !implementationReport.isModelValid() )
223 {
224 log( implementationReport );
225 }
226
227 Assert.assertTrue( "[" + test.getIdentifier() + "] Unexpected invalid implementation.",
228 implementationReport.isModelValid() );
229
230 final Implementation i = modules.getValue().getImplementation( expected.getValue().getIdentifier() );
231
232 Assert.assertNotNull( i );
233 assertEquals( expected.getValue(), i );
234 assertEquals( expected.getValue().getDependencies(),
235 modules.getValue().getDependencies( expected.getValue().getIdentifier() ) );
236
237 assertEquals( expected.getValue().getMessages(),
238 modules.getValue().getMessages( expected.getValue().getIdentifier() ) );
239
240 assertEquals( expected.getValue().getProperties(),
241 modules.getValue().getProperties( expected.getValue().getIdentifier() ) );
242
243 assertEquals( expected.getValue().getSpecifications(),
244 modules.getValue().getSpecifications( expected.getValue().getIdentifier() ) );
245
246 }
247
248
249
250
251
252
253
254
255 public final void testInstance( final String identifier ) throws Exception
256 {
257 Assert.assertNotNull( "identifier", identifier );
258
259 InstanceTestType test = null;
260
261 for ( InstanceTestType candidate : this.getTestSuite().getInstanceTest() )
262 {
263 if ( identifier.equals( candidate.getIdentifier() ) )
264 {
265 test = candidate;
266 break;
267 }
268 }
269
270 Assert.assertNotNull( "Instance test '" + identifier + "' not found.", test );
271
272 System.out.println( "InstanceTest: " + test.getIdentifier() );
273
274 final JAXBElement<Modules> modules = (JAXBElement<Modules>) test.getModules().getAny();
275 final Model model = new Model();
276 model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
277 ModelHelper.setModules( model, modules.getValue() );
278
279 ModelValidationReport validationReport = this.getModelContext().validateModel( model );
280
281 if ( !validationReport.isModelValid() )
282 {
283 log( validationReport );
284 }
285
286 Assert.assertTrue( "[" + test.getIdentifier() + "] Unexpected invalid modules.",
287 validationReport.isModelValid() );
288
289 final JAXBElement<Instance> expected = (JAXBElement<Instance>) test.getInstance().getAny();
290 validationReport = this.getModelContext().validateModel(
291 ModelObject.MODEL_PUBLIC_ID,
292 new JAXBSource( this.getModelContext().createContext( ModelObject.MODEL_PUBLIC_ID ), expected ) );
293
294 if ( !validationReport.isModelValid() )
295 {
296 log( validationReport );
297 }
298
299 Assert.assertTrue( "[" + test.getIdentifier() + "] Unexpected invalid instance.",
300 validationReport.isModelValid() );
301
302 Instance instance = null;
303
304 if ( test.getDependencyName() != null )
305 {
306 final Dependencies dependencies =
307 modules.getValue().getDependencies( test.getImplementationIdentifier() );
308
309 Assert.assertNotNull( "[" + test.getIdentifier() + "] No dependencies for implementation '"
310 + test.getImplementationIdentifier() + "' not found.", dependencies );
311
312 final Dependency d = dependencies.getDependency( test.getDependencyName() );
313 Assert.assertNotNull( "[" + test.getIdentifier() + "] Dependency '" + test.getDependencyName()
314 + "' not found.", d );
315
316 Assert.assertNotNull( "[" + test.getIdentifier() + "] Expected implementation name of dependency '"
317 + test.getDependencyName() + "' not set.", d.getImplementationName() );
318
319 final Implementations implementations = modules.getValue().getImplementations( d.getIdentifier() );
320 Assert.assertNotNull( "[" + test.getIdentifier() + "] Expected implementations of dependency '"
321 + test.getDependencyName() + "' not found.", implementations );
322
323 final Implementation i = implementations.getImplementationByName( d.getImplementationName() );
324 Assert.assertNotNull( "[" + test.getIdentifier() + "] Expected '" + d.getImplementationName()
325 + "' implementation not found.", i );
326
327 instance = modules.getValue().getInstance( i.getIdentifier(), d );
328 }
329 else
330 {
331 instance = modules.getValue().getInstance( test.getImplementationIdentifier() );
332 }
333
334 Assert.assertNotNull( "[" + test.getIdentifier() + "] Expected instance not found.", instance );
335 assertEquals( expected.getValue(), instance );
336 }
337
338 public static void assertEquals( final ModelObject expected, final ModelObject computed ) throws Exception
339 {
340 if ( expected != null )
341 {
342 Assert.assertNotNull( computed );
343 Assert.assertEquals( expected.getCreateDate(), computed.getCreateDate() );
344 Assert.assertEquals( expected.getModelVersion(), computed.getModelVersion() );
345 Assert.assertEquals( expected.isDeprecated(), computed.isDeprecated() );
346 }
347 else
348 {
349 Assert.assertNull( computed );
350 }
351 }
352
353 public static void assertEquals( final Instance expected, final Instance computed ) throws Exception
354 {
355 if ( expected != null )
356 {
357 Assert.assertNotNull( computed );
358 assertEquals( (ModelObject) expected, (ModelObject) computed );
359 Assert.assertEquals( expected.getClazz(), computed.getClazz() );
360 assertEquals( expected.getDependencies(), computed.getDependencies() );
361 Assert.assertEquals( expected.getIdentifier(), computed.getIdentifier() );
362 assertEquals( expected.getMessages(), computed.getMessages() );
363 Assert.assertEquals( expected.getName(), computed.getName() );
364 assertEquals( expected.getProperties(), computed.getProperties() );
365 assertEquals( expected.getSpecifications(), computed.getSpecifications() );
366 }
367 else
368 {
369 Assert.assertNull( computed );
370 }
371 }
372
373 public static void assertEquals( final Implementations expected, final Implementations computed ) throws Exception
374 {
375 if ( expected != null )
376 {
377 Assert.assertNotNull( computed );
378 assertEquals( (ModelObject) expected, (ModelObject) computed );
379
380 for ( Implementation i : expected.getImplementation() )
381 {
382 assertEquals( i, computed.getImplementation( i.getIdentifier() ) );
383 }
384 }
385 else
386 {
387 Assert.assertNull( computed );
388 }
389 }
390
391 public static void assertEquals( final Implementation expected, final Implementation computed ) throws Exception
392 {
393 if ( expected != null )
394 {
395 Assert.assertNotNull( computed );
396 assertEquals( (ModelObject) expected, (ModelObject) computed );
397 Assert.assertEquals( expected.getClazz(), computed.getClazz() );
398 Assert.assertEquals( expected.getIdentifier(), computed.getIdentifier() );
399 Assert.assertEquals( expected.getLocation(), computed.getLocation() );
400 Assert.assertEquals( expected.getName(), computed.getName() );
401 Assert.assertEquals( expected.getVendor(), computed.getVendor() );
402 Assert.assertEquals( expected.getVersion(), computed.getVersion() );
403 Assert.assertEquals( expected.isAbstract(), computed.isAbstract() );
404 Assert.assertEquals( expected.isFinal(), computed.isFinal() );
405 Assert.assertEquals( expected.isStateless(), computed.isStateless() );
406 }
407 else
408 {
409 Assert.assertNull( computed );
410 }
411 }
412
413 public static void assertEquals( final Specifications expected, final Specifications computed ) throws Exception
414 {
415 if ( expected != null )
416 {
417 Assert.assertNotNull( computed );
418 assertEquals( (ModelObject) expected, (ModelObject) computed );
419
420 for ( Specification s : expected.getSpecification() )
421 {
422 assertEquals( s, computed.getSpecification( s.getIdentifier() ) );
423 }
424
425 for ( SpecificationReference r : expected.getReference() )
426 {
427 assertEquals( r, computed.getReference( r.getIdentifier() ) );
428 }
429 }
430 else
431 {
432 Assert.assertNull( computed );
433 }
434 }
435
436 public static void assertEquals( final SpecificationReference expected, final SpecificationReference computed )
437 throws Exception
438 {
439 if ( expected != null )
440 {
441 Assert.assertNotNull( computed );
442 assertEquals( (ModelObject) expected, (ModelObject) computed );
443
444 Assert.assertEquals( expected.getIdentifier(), computed.getIdentifier() );
445 Assert.assertEquals( expected.getVersion(), computed.getVersion() );
446 Assert.assertEquals( expected.isDeprecated(), computed.isDeprecated() );
447 Assert.assertEquals( expected.isFinal(), computed.isFinal() );
448 Assert.assertEquals( expected.isOverride(), computed.isOverride() );
449 }
450 else
451 {
452 Assert.assertNull( computed );
453 }
454 }
455
456 public static void assertEquals( final Specification expected, final Specification computed ) throws Exception
457 {
458 if ( expected != null )
459 {
460 Assert.assertNotNull( computed );
461 assertEquals( (ModelObject) expected, (ModelObject) computed );
462
463 Assert.assertEquals( expected.getClazz(), computed.getClazz() );
464 Assert.assertEquals( expected.getIdentifier(), computed.getIdentifier() );
465 Assert.assertEquals( expected.getMultiplicity(), computed.getMultiplicity() );
466 assertEquals( expected.getProperties(), computed.getProperties() );
467 Assert.assertEquals( expected.getScope(), computed.getScope() );
468 Assert.assertEquals( expected.getVendor(), computed.getVendor() );
469 Assert.assertEquals( expected.getVersion(), computed.getVersion() );
470 }
471 else
472 {
473 Assert.assertNull( computed );
474 }
475 }
476
477 public static void assertEquals( final Dependencies expected, final Dependencies computed ) throws Exception
478 {
479 if ( expected != null )
480 {
481 Assert.assertNotNull( computed );
482
483 for ( Dependency d : expected.getDependency() )
484 {
485 assertEquals( d, computed.getDependency( d.getName() ) );
486 }
487 }
488 else
489 {
490 Assert.assertNull( computed );
491 }
492 }
493
494 public static void assertEquals( final Dependency expected, final Dependency computed ) throws Exception
495 {
496 if ( expected != null )
497 {
498 Assert.assertNotNull( computed );
499 Assert.assertEquals( expected.getIdentifier(), computed.getIdentifier() );
500 Assert.assertEquals( expected.getImplementationName(), computed.getImplementationName() );
501 Assert.assertEquals( expected.getName(), computed.getName() );
502 Assert.assertEquals( expected.isDeprecated(), computed.isDeprecated() );
503 Assert.assertEquals( expected.isFinal(), computed.isFinal() );
504 Assert.assertEquals( expected.isOverride(), computed.isOverride() );
505 Assert.assertEquals( expected.isBound(), computed.isBound() );
506 Assert.assertEquals( expected.isOptional(), computed.isOptional() );
507 Assert.assertEquals( expected.getVersion(), computed.getVersion() );
508 assertEquals( expected.getDependencies(), computed.getDependencies() );
509 assertEquals( expected.getMessages(), computed.getMessages() );
510 assertEquals( expected.getProperties(), computed.getProperties() );
511 }
512 else
513 {
514 Assert.assertNull( computed );
515 }
516 }
517
518 public static void assertEquals( final Messages expected, final Messages computed ) throws Exception
519 {
520 if ( expected != null )
521 {
522 Assert.assertNotNull( computed );
523
524 for ( Message m : expected.getMessage() )
525 {
526 assertEquals( m, computed.getMessage( m.getName() ) );
527 }
528 }
529 else
530 {
531 Assert.assertNull( computed );
532 }
533 }
534
535 public static void assertEquals( final Message expected, final Message computed ) throws Exception
536 {
537 if ( expected != null )
538 {
539 Assert.assertNotNull( computed );
540 Assert.assertEquals( expected.getName(), computed.getName() );
541 Assert.assertEquals( expected.isDeprecated(), computed.isDeprecated() );
542 Assert.assertEquals( expected.isFinal(), computed.isFinal() );
543 Assert.assertEquals( expected.isOverride(), computed.isOverride() );
544 assertEquals( expected.getTemplate(), computed.getTemplate() );
545 }
546 else
547 {
548 Assert.assertNull( computed );
549 }
550 }
551
552 public static void assertEquals( final Texts expected, final Texts computed ) throws Exception
553 {
554 if ( expected != null )
555 {
556 Assert.assertNotNull( computed );
557 Assert.assertEquals( expected.getDefaultLanguage(), computed.getDefaultLanguage() );
558
559 for ( Text t : expected.getText() )
560 {
561 Assert.assertNotNull( computed.getText( t.getLanguage() ) );
562 Assert.assertEquals( t.getValue(), computed.getText( t.getLanguage() ).getValue() );
563 }
564
565 for ( Text t : computed.getText() )
566 {
567 Assert.assertNotNull( expected.getText( t.getLanguage() ) );
568 }
569 }
570 else
571 {
572 Assert.assertNull( computed );
573 }
574 }
575
576 public static void assertEquals( final Properties expected, final Properties computed ) throws Exception
577 {
578 if ( expected != null )
579 {
580 Assert.assertNotNull( computed );
581
582 for ( Property p : expected.getProperty() )
583 {
584 assertEquals( p, computed.getProperty( p.getName() ) );
585 }
586 }
587 else
588 {
589 Assert.assertNull( computed );
590 }
591 }
592
593 public static void assertEquals( final Property expected, final Property computed ) throws Exception
594 {
595 if ( expected != null )
596 {
597 Assert.assertNotNull( computed );
598 Assert.assertEquals( expected.getJavaValue( ModulesTest.class.getClassLoader() ),
599 computed.getJavaValue( ModulesTest.class.getClassLoader() ) );
600
601 Assert.assertEquals( expected.getName(), computed.getName() );
602 Assert.assertEquals( expected.getType(), computed.getType() );
603 Assert.assertEquals( expected.getValue(), computed.getValue() );
604 Assert.assertEquals( expected.isDeprecated(), computed.isDeprecated() );
605 Assert.assertEquals( expected.isFinal(), computed.isFinal() );
606 Assert.assertEquals( expected.isOverride(), computed.isOverride() );
607 }
608 else
609 {
610 Assert.assertNull( computed );
611 }
612 }
613
614 private static void log( final ModelValidationReport report )
615 {
616 for ( ModelValidationReport.Detail d : report.getDetails() )
617 {
618 System.out.println( "\t" + d.toString() );
619 }
620 }
621
622 }