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: ToolsModelValidatorTest.java 4200 2012-01-25 09:46:13Z schulte2005 $
029 *
030 */
031package org.jomc.tools.modlet.test;
032
033import java.util.List;
034import javax.xml.bind.util.JAXBSource;
035import org.jomc.model.Dependencies;
036import org.jomc.model.Dependency;
037import org.jomc.model.Implementation;
038import org.jomc.model.Implementations;
039import org.jomc.model.Message;
040import org.jomc.model.Messages;
041import org.jomc.model.ModelObject;
042import org.jomc.model.Module;
043import org.jomc.model.Modules;
044import org.jomc.model.Specification;
045import org.jomc.model.Specifications;
046import org.jomc.model.Text;
047import org.jomc.model.Texts;
048import org.jomc.model.modlet.ModelHelper;
049import org.jomc.modlet.Model;
050import org.jomc.modlet.ModelContext;
051import org.jomc.modlet.ModelContextFactory;
052import org.jomc.modlet.ModelValidationReport;
053import org.jomc.tools.model.SourceFileType;
054import org.jomc.tools.model.SourceFilesType;
055import org.jomc.tools.model.SourceSectionType;
056import org.jomc.tools.model.SourceSectionsType;
057import org.jomc.tools.modlet.ToolsModelValidator;
058import org.junit.Test;
059import static org.junit.Assert.assertNotNull;
060import static org.junit.Assert.assertTrue;
061import static org.junit.Assert.fail;
062
063/**
064 * Test cases for class {@code org.jomc.tools.modlet.ToolsModelValidator}.
065 *
066 * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
067 * @version $JOMC: ToolsModelValidatorTest.java 4200 2012-01-25 09:46:13Z schulte2005 $
068 */
069public class ToolsModelValidatorTest
070{
071
072    /** The {@code ToolsModelValidator} instance tests are performed with. */
073    private ToolsModelValidator toolsModelValidator;
074
075    /** Creates a new {@code ToolsModelValidatorTest} instance. */
076    public ToolsModelValidatorTest()
077    {
078        super();
079    }
080
081    /**
082     * Gets the {@code ToolsModelValidator} instance tests are performed with.
083     *
084     * @return The {@code ToolsModelValidator} instance tests are performed with.
085     *
086     * @see #newModelValidator()
087     */
088    public ToolsModelValidator getModelValidator()
089    {
090        if ( this.toolsModelValidator == null )
091        {
092            this.toolsModelValidator = this.newModelValidator();
093        }
094
095        return this.toolsModelValidator;
096    }
097
098    /**
099     * 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}