View Javadoc
1   package org.codehaus.plexus.classworlds;
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.net.URL;
20  import java.net.URLClassLoader;
21  import java.util.Enumeration;
22  
23  import org.codehaus.plexus.classworlds.realm.ClassRealm;
24  import org.codehaus.plexus.classworlds.realm.DuplicateRealmException;
25  import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
26  
27  public class ClassWorldTest
28      extends AbstractClassWorldsTestCase
29  {
30      private ClassWorld world;
31  
32      public ClassWorldTest( String name )
33      {
34          super( name );
35      }
36  
37      public void setUp()
38      {
39          this.world = new ClassWorld();
40      }
41  
42      public void tearDown()
43      {
44          this.world = null;
45      }
46  
47      public void testEmpty()
48      {
49          assertTrue( this.world.getRealms().isEmpty() );
50      }
51  
52      public void testNewRealm()
53          throws Exception
54      {
55          ClassRealm realm = this.world.newRealm( "foo" );
56  
57          assertNotNull( realm );
58      }
59  
60      public void testGetRealm()
61          throws Exception
62      {
63          ClassRealm realm = this.world.newRealm( "foo" );
64  
65          assertSame( realm, this.world.getRealm( "foo" ) );
66      }
67  
68      public void testNewRealm_Duplicate()
69          throws Exception
70      {
71          try
72          {
73              this.world.newRealm( "foo" );
74              this.world.newRealm( "foo" );
75  
76              fail( "throw DuplicateRealmException" );
77          }
78          catch ( DuplicateRealmException e )
79          {
80              // expected and correct
81  
82              assertSame( this.world, e.getWorld() );
83  
84              assertEquals( "foo", e.getId() );
85          }
86      }
87  
88      public void testGetRealm_NoSuch()
89          throws Exception
90      {
91          try
92          {
93              this.world.getRealm( "foo" );
94              fail( "throw NoSuchRealmException" );
95          }
96          catch ( NoSuchRealmException e )
97          {
98              // expected and correct
99  
100             assertSame( this.world, e.getWorld() );
101 
102             assertEquals( "foo", e.getId() );
103         }
104     }
105 
106     public void testGetRealms()
107         throws Exception
108     {
109         assertTrue( this.world.getRealms().isEmpty() );
110 
111         ClassRealm foo = this.world.newRealm( "foo" );
112 
113         assertEquals( 1, this.world.getRealms().size() );
114 
115         assertTrue( this.world.getRealms().contains( foo ) );
116 
117         ClassRealm bar = this.world.newRealm( "bar" );
118 
119         assertEquals( 2, this.world.getRealms().size() );
120 
121         assertTrue( this.world.getRealms().contains( bar ) );
122     }
123     
124     public void testPLX334() 
125         throws Exception 
126     {
127         ClassLoader loader = new URLClassLoader( new URL[] { getJarUrl( "component1-1.0.jar" ) } );
128         world.newRealm( "netbeans", loader );
129         ClassRealm plexus = world.newRealm( "plexus" );
130         plexus.importFrom( "netbeans", "META-INF/plexus" );
131         plexus.importFrom( "netbeans", "org.codehaus.plexus" );
132         Enumeration<URL> e = plexus.getResources( "META-INF/plexus/components.xml" );
133         assertNotNull( e );
134         int resourceCount = 0;
135         for ( Enumeration<URL> resources = e; resources.hasMoreElements(); )
136         {
137             URL obj = resources.nextElement();
138             assertTrue( obj.getPath().indexOf( "/component1-1.0.jar!/META-INF/plexus/components.xml" ) >= 0 );
139             resourceCount++;
140         }
141 //        assertEquals( 2, resourceCount );
142 // for some reason surefire-plugin 2.3 returned 2 items there:        
143 //        for example:
144 //jar:file:/home/mkleint/.m2/repository/org/codehaus/plexus/plexus-archiver/1.0-alpha-7/plexus-archiver-1.0-alpha-7.jar!/META-INF/plexus/components.xml
145 //jar:file:/home/mkleint/src/plexus-trunk/plexus-classworlds/src/test-jars/component1-1.0.jar!/META-INF/plexus/components.xml
146 //    However only 1 is correct, which is actually returned by the 2.4 surefire-plugin
147         
148         assertEquals( 1, resourceCount );
149         Class<?> c = plexus.loadClass( "org.codehaus.plexus.Component1" );
150         assertNotNull( c );
151         
152     }
153 
154 }