View Javadoc

1   /*
2    *  jDTAUS Core RI Client Container
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package org.jdtaus.core.container.ri.client.test;
22  
23  import java.net.URL;
24  import java.util.Iterator;
25  import java.util.Map;
26  import java.util.logging.Level;
27  import java.util.logging.Logger;
28  import junit.framework.Assert;
29  import junit.framework.TestCase;
30  import org.jdtaus.core.container.IncompatibleImplementationException;
31  import org.jdtaus.core.container.ModelError;
32  import org.jdtaus.core.container.ModelFactory;
33  
34  /**
35   * Tests the {@code DefaultModel} implementation.
36   *
37   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
38   * @version $JDTAUS: DefaultModelTest.java 8709 2012-10-02 21:07:40Z schulte $
39   */
40  public class DefaultModelTest extends TestCase
41  {
42  
43      private static final String MODEL_LOCATION = "META-INF/jdtaus/module.xml";
44  
45      private static final String TRANSFORMATION_LOCATION =
46          "META-INF/jdtaus/container.xslt";
47  
48      /**
49       * Name of the system property controlling the use of the context
50       * classloader.
51       */
52      private static final String SYS_ENABLE_CONTEXT_CLASSLOADER =
53          "org.jdtaus.core.container.ClassLoaderFactory.enableContextClassloader";
54  
55      public void testBackwardsCompatibility() throws Exception
56      {
57          this.assertValidModel(
58              new URL[]
59              {
60                  this.getClass().getResource( "Specifications-1.0.xml" ),
61                  this.getClass().getResource( "Implementation-1.0.xml" ),
62              } );
63  
64          this.assertValidModel(
65              new URL[]
66              {
67                  this.getClass().getResource( "Specifications-1.1.xml" ),
68                  this.getClass().getResource( "Implementation-1.1.xml" ),
69              } );
70  
71          this.assertValidModel(
72              new URL[]
73              {
74                  this.getClass().getResource( "Specifications-1.2.xml" ),
75                  this.getClass().getResource( "Implementation-1.2.xml" ),
76              } );
77  
78      }
79  
80      public void testCompatibilityDetection() throws Exception
81      {
82          this.assertValidModel(
83              new URL[]
84              {
85                  this.getClass().getResource( "CompatibilityTestcase1.xml" )
86              } );
87  
88          this.assertValidModel(
89              new URL[]
90              {
91                  this.getClass().getResource( "CompatibilityTestcase2.xml" )
92              } );
93  
94          this.assertValidModel(
95              new URL[]
96              {
97                  this.getClass().getResource( "CompatibilityTestcase3.xml" )
98              } );
99  
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 }