001/*
002 *   Copyright (C) Christian Schulte, 2005-206
003 *   All rights reserved.
004 *
005 *   Redistribution and use in source and binary forms, with or without
006 *   modification, are permitted provided that the following conditions
007 *   are met:
008 *
009 *     o Redistributions of source code must retain the above copyright
010 *       notice, this list of conditions and the following disclaimer.
011 *
012 *     o Redistributions in binary form must reproduce the above copyright
013 *       notice, this list of conditions and the following disclaimer in
014 *       the documentation and/or other materials provided with the
015 *       distribution.
016 *
017 *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
018 *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
019 *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
020 *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
021 *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022 *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026 *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 *
028 *   $JOMC: ModulesTest.java 4613 2012-09-22 10:07:08Z schulte $
029 *
030 */
031package org.jomc.model.test;
032
033import java.util.logging.Level;
034import javax.xml.bind.JAXBContext;
035import javax.xml.bind.JAXBElement;
036import javax.xml.bind.JAXBException;
037import javax.xml.bind.util.JAXBSource;
038import org.jomc.model.Dependencies;
039import org.jomc.model.Dependency;
040import org.jomc.model.Implementation;
041import org.jomc.model.Implementations;
042import org.jomc.model.Instance;
043import org.jomc.model.Message;
044import org.jomc.model.Messages;
045import org.jomc.model.ModelObject;
046import org.jomc.model.Modules;
047import org.jomc.model.Properties;
048import org.jomc.model.Property;
049import org.jomc.model.Specification;
050import org.jomc.model.SpecificationReference;
051import org.jomc.model.Specifications;
052import org.jomc.model.Text;
053import org.jomc.model.Texts;
054import org.jomc.model.modlet.ModelHelper;
055import org.jomc.modlet.Model;
056import org.jomc.modlet.ModelContext;
057import org.jomc.modlet.ModelContextFactory;
058import org.jomc.modlet.ModelException;
059import org.jomc.modlet.ModelValidationReport;
060import org.junit.Assert;
061
062/**
063 * Test cases for class {@code org.jomc.model.Modules}.
064 *
065 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0
066 * @version $JOMC: ModulesTest.java 4613 2012-09-22 10:07:08Z schulte $
067 */
068public class ModulesTest
069{
070
071    /** Constant to prefix relative resource names with. */
072    private static final String ABSOLUTE_RESOURCE_NAME_PREFIX = "/org/jomc/model/test/";
073
074    /** The {@code TestSuite} holding the module tests to run. */
075    private TestSuite testSuite;
076
077    /** The {@code ModelContext} instance tests are performed with. */
078    private ModelContext modelContext;
079
080    /** Creates a new {@code ModulesTest} instance. */
081    public ModulesTest()
082    {
083        super();
084    }
085
086    /**
087     * Gets the {@code TestSuite} holding the module tests to run.
088     *
089     * @return The {@code TestSuite} holding the module tests to run.
090     *
091     * @see #newTestSuite()
092     */
093    public TestSuite getTestSuite()
094    {
095        if ( this.testSuite == null )
096        {
097            this.testSuite = this.newTestSuite();
098        }
099
100        return this.testSuite;
101    }
102
103    /**
104     * Creates a new {@code TestSuite} holding module tests to run.
105     *
106     * @return A new {@code TestSuite} holding module tests to run.
107     *
108     * @see #getTestSuite()
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     * Gets the {@code ModelContext} instance tests are performed with.
131     *
132     * @return The {@code ModelContext} instance tests are performed with.
133     *
134     * @see #newModelContext()
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     * Creates a new {@code ModelContext} instance tests are performed with.
163     *
164     * @return A new {@code ModelContext} instance tests are performed with.
165     *
166     * @see #getModelContext()
167     */
168    protected ModelContext newModelContext()
169    {
170        return ModelContextFactory.newInstance().newModelContext();
171    }
172
173    /**
174     * Runs a {@code ImplementationTestType} test.
175     *
176     * @param identifier The identifier of the {@code ImplementationTestType} to run.
177     *
178     * @throws Exception if running the test fails.
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     * Runs a {@code InstanceTestType} test.
250     *
251     * @param identifier The identifier of the {@code InstanceTestType} to run.
252     *
253     * @throws Exception if running the test fails.
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}