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: ToolsModelProviderTest.java 4833 2014-01-04 05:07:36Z schulte $
029 *
030 */
031package org.jomc.tools.modlet.test;
032
033import java.util.HashSet;
034import java.util.Set;
035import java.util.logging.Level;
036import org.jomc.model.Implementation;
037import org.jomc.model.Implementations;
038import org.jomc.model.ModelObject;
039import org.jomc.model.Module;
040import org.jomc.model.Modules;
041import org.jomc.model.Specification;
042import org.jomc.model.SpecificationReference;
043import org.jomc.model.Specifications;
044import org.jomc.model.modlet.ModelHelper;
045import org.jomc.modlet.Model;
046import org.jomc.modlet.ModelContext;
047import org.jomc.modlet.ModelContextFactory;
048import org.jomc.tools.model.SourceFileType;
049import org.jomc.tools.model.SourceFilesType;
050import org.jomc.tools.model.SourceSectionType;
051import org.jomc.tools.model.SourceSectionsType;
052import org.jomc.tools.modlet.ToolsModelProvider;
053import org.junit.Test;
054import static org.junit.Assert.assertEquals;
055import static org.junit.Assert.assertFalse;
056import static org.junit.Assert.assertNotNull;
057import static org.junit.Assert.assertNull;
058import static org.junit.Assert.assertTrue;
059import static org.junit.Assert.fail;
060
061/**
062 * Test cases for class {@code org.jomc.tools.modlet.ToolsModelProvider}.
063 *
064 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0
065 * @version $JOMC: ToolsModelProviderTest.java 4833 2014-01-04 05:07:36Z schulte $
066 */
067public class ToolsModelProviderTest
068{
069
070    /** The {@code ToolsModelProvider} instance tests are performed with. */
071    private ToolsModelProvider toolsModelProvider;
072
073    /** Creates a new {@code ToolsModelProviderTest} instance. */
074    public ToolsModelProviderTest()
075    {
076        super();
077    }
078
079    /**
080     * Gets the {@code ToolsModelProvider} instance tests are performed with.
081     *
082     * @return The {@code ToolsModelProvider} instance tests are performed with.
083     *
084     * @see #newModelProvider()
085     */
086    public ToolsModelProvider getModelProvider()
087    {
088        if ( this.toolsModelProvider == null )
089        {
090            this.toolsModelProvider = this.newModelProvider();
091        }
092
093        return this.toolsModelProvider;
094    }
095
096    /**
097     * Creates a new {@code ToolsModelProvider} instance to test.
098     *
099     * @return A new {@code ToolsModelProvider} instance to test.
100     *
101     * @see #getModelProvider()
102     */
103    protected ToolsModelProvider newModelProvider()
104    {
105        return new ToolsModelProvider();
106    }
107
108    @Test
109    public final void testFindModel() throws Exception
110    {
111        final ModelContext context = ModelContextFactory.newInstance().newModelContext();
112        context.setLogLevel( Level.ALL );
113        context.getListeners().add( new ModelContext.Listener()
114        {
115
116            @Override
117            public void onLog( final Level level, final String message, final Throwable t )
118            {
119                super.onLog( level, message, t );
120                System.out.println( "[" + level.getLocalizedName() + "] " + message );
121
122                if ( t != null )
123                {
124                    t.printStackTrace( System.out );
125                }
126            }
127
128        } );
129
130        Model model = new Model();
131        model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
132
133        Modules modules = new Modules();
134        Module module = new Module();
135        module.setName( this.getClass().getName() );
136        module.setSpecifications( new Specifications() );
137        module.setImplementations( new Implementations() );
138
139        Specification specification = new Specification();
140        specification.setClassDeclaration( true );
141        specification.setClazz( "specification.Documentation" );
142        specification.setIdentifier( this.getClass().getName() + " Specification" );
143
144        Implementation implementation = new Implementation();
145        implementation.setClassDeclaration( true );
146        implementation.setClazz( "implementation.Documentation" );
147        implementation.setIdentifier( this.getClass().getName() + " Implementation" );
148        implementation.setName( this.getClass().getName() + " Implementation" );
149        implementation.setSpecifications( new Specifications() );
150        implementation.getSpecifications().getReference().add( new SpecificationReference() );
151        implementation.getSpecifications().getReference().get( 0 ).setIdentifier( specification.getIdentifier() );
152
153        module.getSpecifications().getSpecification().add( specification );
154        module.getImplementations().getImplementation().add( implementation );
155        modules.getModule().add( module );
156
157        ModelHelper.setModules( model, modules );
158
159        try
160        {
161            this.getModelProvider().findModel( null, model );
162            fail( "Expected NullPointerException not thrown." );
163        }
164        catch ( final NullPointerException e )
165        {
166            assertNotNull( e.getMessage() );
167            System.out.println( e.toString() );
168        }
169
170        try
171        {
172            this.getModelProvider().findModel( context, null );
173            fail( "Expected NullPointerException not thrown." );
174        }
175        catch ( final NullPointerException e )
176        {
177            assertNotNull( e.getMessage() );
178            System.out.println( e.toString() );
179        }
180
181        Model found = this.getModelProvider().findModel( context, model );
182        assertNotNull( found );
183
184        modules = ModelHelper.getModules( found );
185        assertNotNull( modules );
186
187        specification = modules.getSpecification( this.getClass().getName() + " Specification" );
188        assertNotNull( specification );
189
190        implementation = modules.getImplementation( this.getClass().getName() + " Implementation" );
191        assertNotNull( implementation );
192
193        final SourceFilesType specificationSourceFiles = specification.getAnyObject( SourceFilesType.class );
194        final SourceFilesType implementationSourceFiles = implementation.getAnyObject( SourceFilesType.class );
195
196        assertNotNull( specificationSourceFiles );
197        assertNotNull( implementationSourceFiles );
198
199        final SourceFileType specificationSourceFile = specificationSourceFiles.getSourceFile( "Default" );
200        final SourceFileType implementationSourceFile = implementationSourceFiles.getSourceFile( "Default" );
201
202        assertNotNull( specificationSourceFile );
203        assertSectionNameUniqueness( specificationSourceFile.getSourceSections() );
204        assertNotNull( implementationSourceFile );
205        assertSectionNameUniqueness( implementationSourceFile.getSourceSections() );
206
207        this.getModelProvider().setEnabled( false );
208
209        found = this.getModelProvider().findModel( context, model );
210        assertNull( found );
211
212        this.getModelProvider().setEnabled( true );
213    }
214
215    @Test
216    public final void testDefaultEnabled() throws Exception
217    {
218        System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultEnabled" );
219        ToolsModelProvider.setDefaultEnabled( null );
220        assertTrue( ToolsModelProvider.isDefaultEnabled() );
221
222        System.setProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultEnabled", Boolean.toString( false ) );
223        ToolsModelProvider.setDefaultEnabled( null );
224        assertFalse( ToolsModelProvider.isDefaultEnabled() );
225        System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultEnabled" );
226        ToolsModelProvider.setDefaultEnabled( null );
227        assertTrue( ToolsModelProvider.isDefaultEnabled() );
228
229        System.setProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultEnabled", Boolean.toString( true ) );
230        ToolsModelProvider.setDefaultEnabled( null );
231        assertTrue( ToolsModelProvider.isDefaultEnabled() );
232        System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultEnabled" );
233        ToolsModelProvider.setDefaultEnabled( null );
234        assertTrue( ToolsModelProvider.isDefaultEnabled() );
235    }
236
237    @Test
238    public final void testEnabled() throws Exception
239    {
240        final Model model = new Model();
241        model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
242
243        ToolsModelProvider.setDefaultEnabled( null );
244        this.getModelProvider().setEnabled( null );
245        assertTrue( this.getModelProvider().isEnabled() );
246
247        this.getModelProvider().findModel( ModelContextFactory.newInstance().newModelContext(), model );
248        ToolsModelProvider.setDefaultEnabled( false );
249        this.getModelProvider().setEnabled( null );
250        assertFalse( this.getModelProvider().isEnabled() );
251
252        this.getModelProvider().findModel( ModelContextFactory.newInstance().newModelContext(), model );
253        ToolsModelProvider.setDefaultEnabled( null );
254        this.getModelProvider().setEnabled( null );
255    }
256
257    @Test
258    public final void testDefaultHeadComment() throws Exception
259    {
260        System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultHeadComment" );
261        ToolsModelProvider.setDefaultHeadComment( null );
262        assertEquals( "//", ToolsModelProvider.getDefaultHeadComment() );
263
264        System.setProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultHeadComment", "/*" );
265        ToolsModelProvider.setDefaultHeadComment( null );
266        assertEquals( "/*", ToolsModelProvider.getDefaultHeadComment() );
267        System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultHeadComment" );
268        ToolsModelProvider.setDefaultHeadComment( null );
269        assertEquals( "//", ToolsModelProvider.getDefaultHeadComment() );
270    }
271
272    @Test
273    public final void testHeadComment() throws Exception
274    {
275        ToolsModelProvider.setDefaultHeadComment( null );
276        this.getModelProvider().setHeadComment( null );
277        assertEquals( "//", this.getModelProvider().getHeadComment() );
278
279        ToolsModelProvider.setDefaultHeadComment( "/*" );
280        this.getModelProvider().setHeadComment( null );
281        assertEquals( "/*", this.getModelProvider().getHeadComment() );
282
283        ToolsModelProvider.setDefaultHeadComment( null );
284        this.getModelProvider().setHeadComment( null );
285        assertEquals( "//", this.getModelProvider().getHeadComment() );
286    }
287
288    @Test
289    public final void testDefaultTailComment() throws Exception
290    {
291        System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultTailComment" );
292        ToolsModelProvider.setDefaultTailComment( null );
293        assertNull( ToolsModelProvider.getDefaultTailComment() );
294
295        System.setProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultTailComment", "*/" );
296        ToolsModelProvider.setDefaultTailComment( null );
297        assertEquals( "*/", ToolsModelProvider.getDefaultTailComment() );
298
299        System.clearProperty( "org.jomc.tools.modlet.ToolsModelProvider.defaultTailComment" );
300        ToolsModelProvider.setDefaultTailComment( null );
301        assertNull( ToolsModelProvider.getDefaultTailComment() );
302    }
303
304    @Test
305    public final void testTailComment() throws Exception
306    {
307        ToolsModelProvider.setDefaultTailComment( null );
308        this.getModelProvider().setTailComment( null );
309        assertNull( this.getModelProvider().getTailComment() );
310
311        ToolsModelProvider.setDefaultTailComment( "*/" );
312        this.getModelProvider().setTailComment( null );
313        assertEquals( "*/", this.getModelProvider().getTailComment() );
314
315        ToolsModelProvider.setDefaultTailComment( null );
316        this.getModelProvider().setTailComment( null );
317        assertNull( this.getModelProvider().getTailComment() );
318    }
319
320    private static void assertSectionNameUniqueness( final SourceSectionsType sections )
321    {
322        if ( sections != null )
323        {
324            final Set<String> sectionNames = new HashSet<String>( sections.getSourceSection().size() );
325
326            for ( int i = 0, s0 = sections.getSourceSection().size(); i < s0; i++ )
327            {
328                final SourceSectionType section = sections.getSourceSection().get( i );
329                assertTrue( "Multiple '" + section.getName() + "' sections.", sectionNames.add( section.getName() ) );
330                assertSectionNameUniqueness( section.getSourceSections() );
331            }
332        }
333    }
334
335}