View Javadoc
1   package org.codehaus.plexus.classworlds.launcher;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.FileNotFoundException;
22  import java.net.URL;
23  import java.util.Collection;
24  
25  import org.codehaus.plexus.classworlds.AbstractClassWorldsTestCase;
26  import org.codehaus.plexus.classworlds.ClassWorld;
27  import org.codehaus.plexus.classworlds.TestUtil;
28  import org.codehaus.plexus.classworlds.realm.ClassRealm;
29  import org.codehaus.plexus.classworlds.realm.DuplicateRealmException;
30  
31  public class ConfiguratorTest
32      extends AbstractClassWorldsTestCase
33  {
34      private Launcher launcher;
35      private Configurator configurator;
36  
37      public ConfiguratorTest( String name )
38      {
39          super( name );
40      }
41  
42      public void setUp()
43      {
44          this.launcher = new Launcher();
45          this.configurator = new Configurator( this.launcher );
46      }
47  
48      public void tearDown()
49      {
50          this.launcher = null;
51          this.configurator = null;
52          System.getProperties().remove( "set.using.existent" );
53          System.getProperties().remove( "set.using.default" );
54          System.getProperties().remove( "set.using.nonexistent" );
55          System.getProperties().remove( "set.using.nonexistent.default" );
56          System.getProperties().remove( "set.using.missing" );
57          System.getProperties().remove( "set.using.filtered.default" );
58      }
59  
60      public void testConfigure_Nonexistent()
61          throws Exception
62      {
63          try
64          {
65              this.configurator.configure( getConfigPath( "notfound.conf" ) );
66              fail( "throw FileNotFoundException" );
67          }
68          catch ( FileNotFoundException e )
69          {
70              // expected and correct
71          }
72      }
73  
74      public void testConfigure_DuplicateMain()
75          throws Exception
76      {
77          try
78          {
79              this.configurator.configure( getConfigPath( "dupe-main.conf" ) );
80              fail( "throw ConfigurationException" );
81          }
82          catch ( ConfigurationException e )
83          {
84              // expected and correct
85              assertTrue( e.getMessage().startsWith( "Duplicate main" ) );
86          }
87      }
88  
89      public void testConfigure_DuplicateRealm()
90          throws Exception
91      {
92          try
93          {
94              this.configurator.configure( getConfigPath( "dupe-realm.conf" ) );
95              fail( "throw DuplicateRealmException" );
96          }
97          catch ( DuplicateRealmException e )
98          {
99              // expected and correct
100             assertEquals( "dupe.realm", e.getId() );
101         }
102     }
103 
104     public void testConfigure_EarlyImport()
105         throws Exception
106     {
107         try
108         {
109             this.configurator.configure( getConfigPath( "early-import.conf" ) );
110             fail( "throw ConfigurationException" );
111         }
112         catch ( ConfigurationException e )
113         {
114             // expected and correct
115             assertTrue( e.getMessage().startsWith( "Unhandled import" ) );
116         }
117     }
118 
119     public void testConfigure_RealmSyntax()
120         throws Exception
121     {
122         try
123         {
124             this.configurator.configure( getConfigPath( "realm-syntax.conf" ) );
125             fail( "throw ConfigurationException" );
126         }
127         catch ( ConfigurationException e )
128         {
129             // expected and correct
130             assertTrue( e.getMessage().startsWith( "Invalid realm" ) );
131         }
132     }
133 
134     public void testConfigure_Valid()
135         throws Exception
136     {
137         this.configurator.configure( getConfigPath( "valid.conf" ) );
138 
139         assertEquals( "org.apache.maven.app.App", this.launcher.getMainClassName() );
140 
141         assertEquals( "maven", this.launcher.getMainRealmName() );
142 
143         ClassWorld world = this.launcher.getWorld();
144 
145         Collection<ClassRealm> realms = world.getRealms();
146 
147         assertEquals( 4, realms.size() );
148 
149         assertNotNull( world.getRealm( "ant" ) );
150         assertNotNull( world.getRealm( "maven" ) );
151         assertNotNull( world.getRealm( "xml" ) );
152 
153         ClassRealm antRealm = world.getRealm( "ant" );
154         ClassRealm mavenRealm = world.getRealm( "maven" );
155         ClassRealm xmlRealm = world.getRealm( "xml" );
156         ClassRealm globRealm = world.getRealm( "glob" );
157 
158         assertSame( null, antRealm.getImportClassLoader( "org.apache.tools.Ant" ) );
159 
160         assertSame( xmlRealm, antRealm.getImportClassLoader( "org.xml.sax.SAXException" ) );
161 
162         assertSame( null, mavenRealm.getImportClassLoader( "org.apache.maven.app.App" ) );
163 
164         assertSame( xmlRealm, mavenRealm.getImportClassLoader( "org.xml.sax.SAXException" ) );
165 
166         URL[] urls = globRealm.getURLs();
167 
168         String basedir = TestUtil.getBasedir();
169         assertArrayContains( urls, new File( basedir, "src/test/test-data/nested.jar" ).toURI().toURL() );
170         assertArrayContains( urls, new File( basedir, "src/test/test-data/a.jar" ).toURI().toURL() );
171         assertArrayContains( urls, new File( basedir, "src/test/test-data/b.jar" ).toURI().toURL() );
172         assertArrayContains( urls, new File( basedir, "src/test/test-data/c.jar" ).toURI().toURL() );
173     }
174 
175     public void testConfigure_Optionally_NonExistent()
176         throws Exception
177     {
178         this.configurator.configure( getConfigPath( "optionally-nonexistent.conf" ) );
179 
180         assertEquals( "org.apache.maven.app.App", this.launcher.getMainClassName() );
181 
182         assertEquals( "opt", this.launcher.getMainRealmName() );
183 
184         ClassWorld world = this.launcher.getWorld();
185 
186         Collection<ClassRealm> realms = world.getRealms();
187 
188         assertEquals( 1, realms.size() );
189 
190         assertNotNull( world.getRealm( "opt" ) );
191 
192         ClassRealm optRealm = world.getRealm( "opt" );
193 
194         URL[] urls = optRealm.getURLs();
195 
196         assertEquals( "no urls", 0, urls.length );
197     }
198 
199     public void testConfigure_Optionally_Existent()
200         throws Exception
201     {
202         this.configurator.configure( getConfigPath( "optionally-existent.conf" ) );
203 
204         assertEquals( "org.apache.maven.app.App", this.launcher.getMainClassName() );
205 
206         assertEquals( "opt", this.launcher.getMainRealmName() );
207 
208         ClassWorld world = this.launcher.getWorld();
209 
210         Collection<ClassRealm> realms = world.getRealms();
211 
212         assertEquals( 1, realms.size() );
213 
214         assertNotNull( world.getRealm( "opt" ) );
215 
216         ClassRealm optRealm = world.getRealm( "opt" );
217 
218         URL[] urls = optRealm.getURLs();
219 
220         assertEquals( "one url", 1, urls.length );
221 
222         assertSame( null, optRealm.getImportClassLoader( "org.xml.sax.SAXException" ) );
223     }
224 
225     public void testConfigure_Unhandled()
226         throws Exception
227     {
228         try
229         {
230             this.configurator.configure( getConfigPath( "unhandled.conf" ) );
231             fail( "throw ConfigurationException" );
232         }
233         catch ( ConfigurationException e )
234         {
235             // expected and correct
236             assertTrue( e.getMessage().startsWith( "Unhandled configuration" ) );
237         }
238     }
239 
240     public void testSet_Using_Existent()
241         throws Exception
242     {
243         assertNull( System.getProperty( "set.using.existent" ) );
244 
245         this.configurator.configure( getConfigPath( "set-using-existent.conf" ) );
246 
247         assertEquals( "testSet_Using_Existent", System.getProperty( "set.using.existent" ) );
248     }
249 
250     public void testSet_Using_NonExistent()
251         throws Exception
252     {
253         assertNull( System.getProperty( "set.using.nonexistent" ) );
254 
255         this.configurator.configure( getConfigPath( "set-using-nonexistent.conf" ) );
256 
257         assertNull( System.getProperty( "set.using.nonexistent" ) );
258     }
259 
260     public void testSet_Using_NonExistent_Default()
261         throws Exception
262     {
263         assertNull( System.getProperty( "set.using.nonexistent.default" ) );
264 
265         this.configurator.configure( getConfigPath( "set-using-nonexistent.conf" ) );
266 
267         assertEquals( "testSet_Using_NonExistent_Default", System.getProperty( "set.using.nonexistent.default" ) );
268     }
269 
270     public void testSet_Using_NonExistent_Override()
271         throws Exception
272     {
273         assertNull( System.getProperty( "set.using.default" ) );
274         System.setProperty( "set.using.default", "testSet_Using_NonExistent_Override" );
275 
276         this.configurator.configure( getConfigPath( "set-using-nonexistent.conf" ) );
277 
278         assertEquals( "testSet_Using_NonExistent_Override", System.getProperty( "set.using.default" ) );
279     }
280 
281     public void testSet_Using_Existent_Override()
282         throws Exception
283     {
284         assertNull( System.getProperty( "set.using.existent" ) );
285         System.setProperty( "set.using.existent", "testSet_Using_Existent_Override" );
286 
287         this.configurator.configure( getConfigPath( "set-using-existent.conf" ) );
288 
289         assertEquals( "testSet_Using_Existent_Override", System.getProperty( "set.using.existent" ) );
290     }
291 
292     public void testSet_Using_Existent_Default()
293         throws Exception
294     {
295         assertNull( System.getProperty( "set.using.default" ) );
296 
297         this.configurator.configure( getConfigPath( "set-using-existent.conf" ) );
298 
299         assertEquals( "testSet_Using_Existent_Default", System.getProperty( "set.using.default" ) );
300     }
301 
302     public void testSet_Using_Missing_Default()
303         throws Exception
304     {
305         assertNull( System.getProperty( "set.using.missing" ) );
306 
307         this.configurator.configure( getConfigPath( "set-using-missing.conf" ) );
308 
309         assertEquals( "testSet_Using_Missing_Default", System.getProperty( "set.using.missing" ) );
310     }
311 
312     public void testSet_Using_Missing_Override()
313         throws Exception
314     {
315         assertNull( System.getProperty( "set.using.missing" ) );
316         System.setProperty( "set.using.missing", "testSet_Using_Missing_Override" );
317 
318         this.configurator.configure( getConfigPath( "set-using-missing.conf" ) );
319 
320         assertEquals( "testSet_Using_Missing_Override", System.getProperty( "set.using.missing" ) );
321     }
322 
323     public void testSet_Using_Filtered_Default()
324         throws Exception
325     {
326         assertNull( System.getProperty( "set.using.filtered.default" ) );
327 
328         this.configurator.configure( getConfigPath( "set-using-missing.conf" ) );
329 
330         assertEquals( System.getProperty( "user.home" ) + "/m2", System.getProperty( "set.using.filtered.default" ) );
331     }
332 
333     private FileInputStream getConfigPath( String name )
334         throws Exception
335     {
336         return new FileInputStream(
337             new File( new File( TestUtil.getBasedir(), "src/test/test-data" ), name ) );
338     }
339 
340     private void assertArrayContains( URL[] array,
341                                       URL url )
342         throws Exception
343     {
344         for ( int i = 0; i < array.length; ++i )
345         {
346             if ( url.equals( array[i] ) )
347             {
348                 return;
349             }
350         }
351         fail( "URL (" + url + ") not found in array of URLs" );
352     }
353 }