001/*
002 *  jDTAUS Core RI Client Container
003 *  Copyright (C) 2005 Christian Schulte
004 *  <cs@schulte.it>
005 *
006 *  This library is free software; you can redistribute it and/or
007 *  modify it under the terms of the GNU Lesser General Public
008 *  License as published by the Free Software Foundation; either
009 *  version 2.1 of the License, or any later version.
010 *
011 *  This library is distributed in the hope that it will be useful,
012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 *  Lesser General Public License for more details.
015 *
016 *  You should have received a copy of the GNU Lesser General Public
017 *  License along with this library; if not, write to the Free Software
018 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
019 *
020 */
021package org.jdtaus.core.container.ri.client.test;
022
023import java.net.URL;
024import java.util.Iterator;
025import java.util.Map;
026import java.util.logging.Level;
027import java.util.logging.Logger;
028import junit.framework.Assert;
029import junit.framework.TestCase;
030import org.jdtaus.core.container.IncompatibleImplementationException;
031import org.jdtaus.core.container.ModelError;
032import org.jdtaus.core.container.ModelFactory;
033
034/**
035 * Tests the {@code DefaultModel} implementation.
036 *
037 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
038 * @version $JDTAUS: DefaultModelTest.java 8709 2012-10-02 21:07:40Z schulte $
039 */
040public class DefaultModelTest extends TestCase
041{
042
043    private static final String MODEL_LOCATION = "META-INF/jdtaus/module.xml";
044
045    private static final String TRANSFORMATION_LOCATION =
046        "META-INF/jdtaus/container.xslt";
047
048    /**
049     * Name of the system property controlling the use of the context
050     * classloader.
051     */
052    private static final String SYS_ENABLE_CONTEXT_CLASSLOADER =
053        "org.jdtaus.core.container.ClassLoaderFactory.enableContextClassloader";
054
055    public void testBackwardsCompatibility() throws Exception
056    {
057        this.assertValidModel(
058            new URL[]
059            {
060                this.getClass().getResource( "Specifications-1.0.xml" ),
061                this.getClass().getResource( "Implementation-1.0.xml" ),
062            } );
063
064        this.assertValidModel(
065            new URL[]
066            {
067                this.getClass().getResource( "Specifications-1.1.xml" ),
068                this.getClass().getResource( "Implementation-1.1.xml" ),
069            } );
070
071        this.assertValidModel(
072            new URL[]
073            {
074                this.getClass().getResource( "Specifications-1.2.xml" ),
075                this.getClass().getResource( "Implementation-1.2.xml" ),
076            } );
077
078    }
079
080    public void testCompatibilityDetection() throws Exception
081    {
082        this.assertValidModel(
083            new URL[]
084            {
085                this.getClass().getResource( "CompatibilityTestcase1.xml" )
086            } );
087
088        this.assertValidModel(
089            new URL[]
090            {
091                this.getClass().getResource( "CompatibilityTestcase2.xml" )
092            } );
093
094        this.assertValidModel(
095            new URL[]
096            {
097                this.getClass().getResource( "CompatibilityTestcase3.xml" )
098            } );
099
100        this.assertValidModel(
101            new URL[]
102            {
103                this.getClass().getResource( "CompatibilityTestcase4.xml" )
104            } );
105
106        this.assertValidModel(
107            new URL[]
108            {
109                this.getClass().getResource( "CompatibilityTestcase5.xml" )
110            } );
111
112        this.assertValidModel(
113            new URL[]
114            {
115                this.getClass().getResource(
116                "ImplementationCompatibilityTestcase1.xml" )
117            } );
118
119        this.assertValidModel(
120            new URL[]
121            {
122                this.getClass().getResource(
123                "ImplementationCompatibilityTestcase2.xml" )
124            } );
125
126        this.assertValidModel(
127            new URL[]
128            {
129                this.getClass().getResource(
130                "ImplementationCompatibilityTestcase3.xml" )
131            } );
132
133        this.assertValidModel(
134            new URL[]
135            {
136                this.getClass().getResource(
137                "ImplementationCompatibilityTestcase4.xml" )
138            } );
139
140        this.assertValidModel(
141            new URL[]
142            {
143                this.getClass().getResource(
144                "ImplementationCompatibilityTestcase5.xml" )
145            } );
146
147        this.assertValidModel(
148            new URL[]
149            {
150                this.getClass().getResource(
151                "ImplementationCompatibilityTestcase6.xml" )
152            } );
153
154    }
155
156    public void testIncompatibilityDetection() throws Exception
157    {
158        this.assertIncompatibleImplementation(
159            new URL[]
160            {
161                this.getClass().getResource( "IncompatibilityTestcase1.xml" )
162            } );
163
164        this.assertIncompatibleImplementation(
165            new URL[]
166            {
167                this.getClass().getResource( "IncompatibilityTestcase2.xml" )
168            } );
169
170        this.assertIncompatibleImplementation(
171            new URL[]
172            {
173                this.getClass().getResource( "IncompatibilityTestcase3.xml" )
174            } );
175
176        this.assertIncompatibleImplementation(
177            new URL[]
178            {
179                this.getClass().getResource( "IncompatibilityTestcase4.xml" )
180            } );
181
182        this.assertIncompatibleImplementation(
183            new URL[]
184            {
185                this.getClass().getResource( "IncompatibilityTestcase5.xml" )
186            } );
187
188        this.assertIncompatibleImplementation(
189            new URL[]
190            {
191                this.getClass().getResource(
192                "ImplementationIncompatibilityTestcase1.xml" )
193            } );
194
195        this.assertIncompatibleImplementation(
196            new URL[]
197            {
198                this.getClass().getResource(
199                "ImplementationIncompatibilityTestcase2.xml" )
200            } );
201
202        this.assertIncompatibleImplementation(
203            new URL[]
204            {
205                this.getClass().getResource(
206                "ImplementationIncompatibilityTestcase3.xml" )
207            } );
208
209        this.assertIncompatibleImplementation(
210            new URL[]
211            {
212                this.getClass().getResource(
213                "ImplementationIncompatibilityTestcase4.xml" )
214            } );
215
216        this.assertIncompatibleImplementation(
217            new URL[]
218            {
219                this.getClass().getResource(
220                "ImplementationIncompatibilityTestcase5.xml" )
221            } );
222
223        this.assertIncompatibleImplementation(
224            new URL[]
225            {
226                this.getClass().getResource(
227                "ImplementationIncompatibilityTestcase6.xml" )
228            } );
229
230    }
231
232    public void testMessages() throws Exception
233    {
234        this.assertValidModel(
235            new URL[]
236            {
237                this.getClass().getResource(
238                "MessagesTestcase1.xml" )
239            } );
240
241        this.assertValidModel(
242            new URL[]
243            {
244                this.getClass().getResource(
245                "MessagesTestcase2.xml" )
246            } );
247
248    }
249
250    public void testIllegalMessages() throws Exception
251    {
252        this.assertInvalidModel(
253            new URL[]
254            {
255                this.getClass().getResource(
256                "MessagesTestcase3.xml" )
257            } );
258
259        this.assertInvalidModel(
260            new URL[]
261            {
262                this.getClass().getResource(
263                "MessagesTestcase4.xml" )
264            } );
265
266        this.assertInvalidModel(
267            new URL[]
268            {
269                this.getClass().getResource(
270                "MessagesTestcase5.xml" )
271            } );
272
273    }
274
275    public void testSystemProperties() throws Exception
276    {
277        for ( Iterator it = System.getProperties().entrySet().iterator();
278              it.hasNext(); )
279        {
280            final Map.Entry e = (Map.Entry) it.next();
281            Logger.getLogger( this.getClass().getName() ).
282                log( Level.CONFIG, e.getKey() + ":" + e.getValue() );
283
284        }
285    }
286
287    protected void assertIncompatibleImplementation( final URL[] resources )
288    {
289        try
290        {
291            this.assertValidModel( resources );
292            Assert.fail( "Model with incompatible versions allowed." );
293        }
294        catch ( IncompatibleImplementationException e )
295        {
296            Assert.assertNotNull( e.getMessage() );
297            System.out.println( e.getMessage() );
298        }
299    }
300
301    protected void assertValidModel( final URL[] resources )
302    {
303        final ClassLoader currentLoader =
304            Thread.currentThread().getContextClassLoader();
305
306        try
307        {
308            final ResourceLoader resourceLoader =
309                new ResourceLoader( this.getClass().getClassLoader() );
310
311            resourceLoader.addResources( MODEL_LOCATION, resources );
312            resourceLoader.addResources( TRANSFORMATION_LOCATION, new URL[]
313                {
314                    this.getClass().getResource( "noop.xslt" )
315                } );
316
317            Thread.currentThread().setContextClassLoader( resourceLoader );
318            System.setProperty( SYS_ENABLE_CONTEXT_CLASSLOADER,
319                                Boolean.toString( true ) );
320
321            ModelFactory.newModel();
322        }
323        finally
324        {
325            System.setProperty( SYS_ENABLE_CONTEXT_CLASSLOADER,
326                                Boolean.toString( false ) );
327
328            Thread.currentThread().setContextClassLoader( currentLoader );
329        }
330    }
331
332    protected void assertInvalidModel( final URL[] resources )
333    {
334        try
335        {
336            this.assertValidModel( resources );
337        }
338        catch ( ModelError e )
339        {
340            Assert.assertNotNull( e.getMessage() );
341            System.out.println( e.toString() );
342        }
343    }
344
345}