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: DefaultModelProcessorTest.java 4613 2012-09-22 10:07:08Z schulte $
029 *
030 */
031package org.jomc.model.modlet.test;
032
033import org.jomc.model.ModelObject;
034import org.jomc.model.Modules;
035import org.jomc.model.modlet.DefaultModelProcessor;
036import org.jomc.model.modlet.ModelHelper;
037import org.jomc.modlet.Model;
038import org.jomc.modlet.ModelContext;
039import org.jomc.modlet.ModelContextFactory;
040import org.junit.Test;
041import static org.junit.Assert.assertEquals;
042import static org.junit.Assert.assertFalse;
043import static org.junit.Assert.assertNotNull;
044import static org.junit.Assert.assertNull;
045import static org.junit.Assert.assertTrue;
046import static org.junit.Assert.fail;
047
048/**
049 * Test cases for class {@code org.jomc.model.modlet.DefaultModelProcessor}.
050 *
051 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0
052 * @version $JOMC: DefaultModelProcessorTest.java 4613 2012-09-22 10:07:08Z schulte $
053 */
054public class DefaultModelProcessorTest
055{
056
057    /** The {@code DefaultModelProcessor} instance tests are performed with. */
058    private DefaultModelProcessor defaultModelProcessor;
059
060    /** Creates a new {@code DefaultModelProcessorTest} instance. */
061    public DefaultModelProcessorTest()
062    {
063        super();
064    }
065
066    /**
067     * Gets the {@code DefaultModelProcessor} instance tests are performed with.
068     *
069     * @return The {@code DefaultModelProcessor} instance tests are performed with.
070     *
071     * @see #newModelProcessor()
072     */
073    public DefaultModelProcessor getModelProcessor()
074    {
075        if ( this.defaultModelProcessor == null )
076        {
077            this.defaultModelProcessor = this.newModelProcessor();
078        }
079
080        return this.defaultModelProcessor;
081    }
082
083    /**
084     * Creates a new {@code DefaultModelProcessor} instance to test.
085     *
086     * @return A new {@code DefaultModelProcessor} instance to test.
087     *
088     * @see #getModelProcessor()
089     */
090    protected DefaultModelProcessor newModelProcessor()
091    {
092        return new DefaultModelProcessor();
093    }
094
095    @Test
096    public final void testFindTransformers() throws Exception
097    {
098        final ModelContext context = ModelContextFactory.newInstance().newModelContext();
099
100        try
101        {
102            this.getModelProcessor().findTransformers( context, null );
103            fail( "Expected NullPointerException not thrown." );
104        }
105        catch ( final NullPointerException e )
106        {
107            assertNotNull( e.getMessage() );
108            System.out.println( e );
109        }
110
111        try
112        {
113            this.getModelProcessor().findTransformers( null, "" );
114            fail( "Expected NullPointerException not thrown." );
115        }
116        catch ( final NullPointerException e )
117        {
118            assertNotNull( e.getMessage() );
119            System.out.println( e );
120        }
121
122        DefaultModelProcessor.setDefaultTransformerLocation( null );
123        this.getModelProcessor().setTransformerLocation( null );
124        assertEquals( 1, this.getModelProcessor().findTransformers(
125            context, DefaultModelProcessor.getDefaultTransformerLocation() ).size() );
126
127        DefaultModelProcessor.setDefaultTransformerLocation( "DOES_NOT_EXIST" );
128        this.getModelProcessor().setTransformerLocation( "DOES_NOT_EXIST" );
129
130        assertNull( this.getModelProcessor().findTransformers(
131            context, DefaultModelProcessor.getDefaultTransformerLocation() ) );
132
133        assertNull( this.getModelProcessor().findTransformers(
134            context, this.getModelProcessor().getTransformerLocation() ) );
135
136        DefaultModelProcessor.setDefaultTransformerLocation( null );
137        this.getModelProcessor().setTransformerLocation( null );
138    }
139
140    @Test
141    public final void testProcessModel() throws Exception
142    {
143        final ModelContext context = ModelContextFactory.newInstance().newModelContext();
144        final Model model = new Model();
145        model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
146
147        try
148        {
149            this.getModelProcessor().processModel( null, model );
150            fail( "Expected NullPointerException not thrown." );
151        }
152        catch ( final NullPointerException e )
153        {
154            assertNotNull( e.getMessage() );
155            System.out.println( e.toString() );
156        }
157
158        try
159        {
160            this.getModelProcessor().processModel( context, null );
161            fail( "Expected NullPointerException not thrown." );
162        }
163        catch ( final NullPointerException e )
164        {
165            assertNotNull( e.getMessage() );
166            System.out.println( e.toString() );
167        }
168
169        assertNotNull( this.getModelProcessor().processModel( context, model ) );
170
171        this.getModelProcessor().setTransformerLocation(
172            this.getClass().getPackage().getName().replace( '.', '/' ) + "/system-property-test.xsl" );
173
174        final Model processedSystemProperty = this.getModelProcessor().processModel( context, model );
175        assertNotNull( processedSystemProperty );
176
177        final Modules processedSystemPropertyModules = ModelHelper.getModules( processedSystemProperty );
178        assertNotNull( processedSystemPropertyModules );
179        assertNotNull( processedSystemPropertyModules.getModule( System.getProperty( "user.home" ) ) );
180
181        this.getModelProcessor().setTransformerLocation(
182            this.getClass().getPackage().getName().replace( '.', '/' ) + "/relative-uri-test.xsl" );
183
184        final Model processedRelativeUri = this.getModelProcessor().processModel( context, model );
185        assertNotNull( processedRelativeUri );
186
187        final Modules processedRelativeUriModules = ModelHelper.getModules( processedRelativeUri );
188        assertNotNull( processedRelativeUriModules );
189        assertNotNull( processedRelativeUriModules.getModule( System.getProperty( "os.name" ) ) );
190
191        this.getModelProcessor().setTransformerLocation( null );
192    }
193
194    @Test
195    public final void testDefaultEnabled() throws Exception
196    {
197        System.clearProperty( "org.jomc.model.DefaultModelProcessor.defaultEnabled" );
198        System.clearProperty( "org.jomc.model.modlet.DefaultModelProcessor.defaultEnabled" );
199        DefaultModelProcessor.setDefaultEnabled( null );
200        assertTrue( DefaultModelProcessor.isDefaultEnabled() );
201
202        System.setProperty( "org.jomc.model.DefaultModelProcessor.defaultEnabled", Boolean.toString( false ) );
203        DefaultModelProcessor.setDefaultEnabled( null );
204        assertFalse( DefaultModelProcessor.isDefaultEnabled() );
205        System.clearProperty( "org.jomc.model.DefaultModelProcessor.defaultEnabled" );
206        DefaultModelProcessor.setDefaultEnabled( null );
207        assertTrue( DefaultModelProcessor.isDefaultEnabled() );
208
209        System.setProperty( "org.jomc.model.modlet.DefaultModelProcessor.defaultEnabled", Boolean.toString( false ) );
210        DefaultModelProcessor.setDefaultEnabled( null );
211        assertFalse( DefaultModelProcessor.isDefaultEnabled() );
212        System.clearProperty( "org.jomc.model.modlet.DefaultModelProcessor.defaultEnabled" );
213        DefaultModelProcessor.setDefaultEnabled( null );
214        assertTrue( DefaultModelProcessor.isDefaultEnabled() );
215
216        System.setProperty( "org.jomc.model.DefaultModelProcessor.defaultEnabled", Boolean.toString( true ) );
217        System.setProperty( "org.jomc.model.modlet.DefaultModelProcessor.defaultEnabled", Boolean.toString( false ) );
218        DefaultModelProcessor.setDefaultEnabled( null );
219        assertFalse( DefaultModelProcessor.isDefaultEnabled() );
220        System.clearProperty( "org.jomc.model.modlet.DefaultModelProcessor.defaultEnabled" );
221        DefaultModelProcessor.setDefaultEnabled( null );
222        assertTrue( DefaultModelProcessor.isDefaultEnabled() );
223        System.clearProperty( "org.jomc.model.DefaultModelProcessor.defaultEnabled" );
224        DefaultModelProcessor.setDefaultEnabled( null );
225        assertTrue( DefaultModelProcessor.isDefaultEnabled() );
226    }
227
228    @Test
229    public final void testEnabled() throws Exception
230    {
231        final Model model = new Model();
232        model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
233
234        DefaultModelProcessor.setDefaultEnabled( null );
235        this.getModelProcessor().setEnabled( null );
236        assertTrue( this.getModelProcessor().isEnabled() );
237
238        this.getModelProcessor().processModel( ModelContextFactory.newInstance().newModelContext(), model );
239        DefaultModelProcessor.setDefaultEnabled( false );
240        this.getModelProcessor().setEnabled( null );
241        assertFalse( this.getModelProcessor().isEnabled() );
242
243        this.getModelProcessor().processModel( ModelContextFactory.newInstance().newModelContext(), model );
244        DefaultModelProcessor.setDefaultEnabled( null );
245        this.getModelProcessor().setEnabled( null );
246    }
247
248    @Test
249    public final void testDefaultTransformerLocation() throws Exception
250    {
251        System.clearProperty( "org.jomc.model.DefaultModelProcessor.defaultTransformerLocation" );
252        System.clearProperty( "org.jomc.model.modlet.DefaultModelProcessor.defaultTransformerLocation" );
253        DefaultModelProcessor.setDefaultTransformerLocation( null );
254        assertEquals( "META-INF/jomc.xsl", DefaultModelProcessor.getDefaultTransformerLocation() );
255
256        System.setProperty( "org.jomc.model.DefaultModelProcessor.defaultTransformerLocation", "TEST" );
257        DefaultModelProcessor.setDefaultTransformerLocation( null );
258        assertEquals( "TEST", DefaultModelProcessor.getDefaultTransformerLocation() );
259        System.clearProperty( "org.jomc.model.DefaultModelProcessor.defaultTransformerLocation" );
260        DefaultModelProcessor.setDefaultTransformerLocation( null );
261        assertEquals( "META-INF/jomc.xsl", DefaultModelProcessor.getDefaultTransformerLocation() );
262
263        System.setProperty( "org.jomc.model.modlet.DefaultModelProcessor.defaultTransformerLocation", "TEST" );
264        DefaultModelProcessor.setDefaultTransformerLocation( null );
265        assertEquals( "TEST", DefaultModelProcessor.getDefaultTransformerLocation() );
266        System.clearProperty( "org.jomc.model.modlet.DefaultModelProcessor.defaultTransformerLocation" );
267        DefaultModelProcessor.setDefaultTransformerLocation( null );
268        assertEquals( "META-INF/jomc.xsl", DefaultModelProcessor.getDefaultTransformerLocation() );
269
270        System.setProperty( "org.jomc.model.DefaultModelProcessor.defaultTransformerLocation", "DEPRECATED" );
271        System.setProperty( "org.jomc.model.modlet.DefaultModelProcessor.defaultTransformerLocation", "TEST" );
272        DefaultModelProcessor.setDefaultTransformerLocation( null );
273        assertEquals( "TEST", DefaultModelProcessor.getDefaultTransformerLocation() );
274        System.clearProperty( "org.jomc.model.modlet.DefaultModelProcessor.defaultTransformerLocation" );
275        DefaultModelProcessor.setDefaultTransformerLocation( null );
276        assertEquals( "DEPRECATED", DefaultModelProcessor.getDefaultTransformerLocation() );
277        System.clearProperty( "org.jomc.model.DefaultModelProcessor.defaultTransformerLocation" );
278        DefaultModelProcessor.setDefaultTransformerLocation( null );
279        assertEquals( "META-INF/jomc.xsl", DefaultModelProcessor.getDefaultTransformerLocation() );
280    }
281
282    @Test
283    public final void testTransformerLocation() throws Exception
284    {
285        DefaultModelProcessor.setDefaultTransformerLocation( null );
286        this.getModelProcessor().setTransformerLocation( null );
287        assertNotNull( this.getModelProcessor().getTransformerLocation() );
288
289        DefaultModelProcessor.setDefaultTransformerLocation( "TEST" );
290        this.getModelProcessor().setTransformerLocation( null );
291        assertEquals( "TEST", this.getModelProcessor().getTransformerLocation() );
292
293        DefaultModelProcessor.setDefaultTransformerLocation( null );
294        this.getModelProcessor().setTransformerLocation( null );
295    }
296
297}