1   package org.codehaus.classworlds;
2   
3   import junit.framework.TestCase;
4   
5   import java.io.File;
6   import java.io.InputStream;
7   import java.net.URL;
8   import java.util.Enumeration;
9   
10  // jars within jars
11  // hierarchy vs graph
12  
13  public class RealmClassLoaderTest
14      extends TestCase
15  {
16      private ClassWorld world;
17      
18      private ClassRealm realm;
19  
20      private RealmClassLoader classLoader;
21  
22      public void setUp()
23          throws Exception
24      {
25          this.world = new ClassWorld();
26  
27          this.realm = this.world.newRealm( "realm" );
28  
29          this.classLoader = (RealmClassLoader) this.realm.getClassLoader();
30          
31          classLoader.addConstituent( getJarUrl( "component0-1.0.jar" ) );
32      }
33  
34      public void testLoadingOfApplicationClass()
35          throws Exception
36      {
37          Class c = classLoader.loadClass( "org.codehaus.plexus.Component0" );
38  
39          assertNotNull( c );
40      }
41  
42      public void testLoadingOfApplicationClassThenDoingItAgain()
43          throws Exception
44      {
45          Class c;
46  
47          c = classLoader.loadClass( "org.codehaus.plexus.Component0" );
48  
49          assertNotNull( c );
50  
51          c = classLoader.loadClass( "org.codehaus.plexus.Component0" );
52  
53          assertNotNull( c );
54      }
55  
56  
57      public void testLoadingOfSystemClass()
58          throws Exception
59      {
60          Class c = classLoader.loadClass( "java.lang.Object" );
61  
62          assertNotNull( c );
63      }
64  
65      public void testLoadingOfNonExistentClass()
66          throws Exception
67      {
68          try
69          {
70              classLoader.loadClass( "org.codehaus.plexus.NonExistentComponent" );
71  
72              fail( "Should have thrown a ClassNotFoundException!" );
73          }
74          catch ( ClassNotFoundException e )
75          {
76              // do nothing
77          }
78      }
79  
80      public void testGetApplicationResource()
81          throws Exception
82      {
83          URL resource = classLoader.getResource( "META-INF/plexus/components.xml" );
84  
85          assertNotNull( resource );
86  
87          String content = getContent( resource.openStream() );
88  
89          assertTrue( content.startsWith( "<component-set>" ) );
90      }
91  
92      public void testGetSystemResource()
93          throws Exception
94      {
95          URL resource = classLoader.getResource( "java/lang/Object.class" );
96  
97          assertNotNull( resource );
98      }
99  
100 
101     public void testGetResources()
102         throws Exception
103     {
104         classLoader.addConstituent( getJarUrl( "component1-1.0.jar" ) );
105 
106         Enumeration e = classLoader.getResources( "META-INF/plexus/components.xml" );
107 
108         assertNotNull( e );
109 
110         int resourceCount = 0;
111 
112         for ( Enumeration resources = e; resources.hasMoreElements(); )
113         {
114             resources.nextElement();
115 
116             resourceCount++;
117         }
118 
119         assertEquals( 2, resourceCount );
120     }
121 
122 
123     public void testGetResourceAsStream()
124         throws Exception
125     {
126         InputStream is = classLoader.getResourceAsStream( "META-INF/plexus/components.xml" );
127 
128         assertNotNull( is );
129 
130         String content = getContent( is );
131 
132         assertTrue( content.startsWith( "<component-set>" ) );
133     }
134 
135 
136     protected URL getJarUrl( String jarName )
137         throws Exception
138     {
139         File jarFile = new File( System.getProperty( "basedir" ), "src/test-jars/" + jarName );
140 
141         return jarFile.toURL();
142     }
143 
144     protected String getContent( InputStream in )
145         throws Exception
146     {
147         byte[] buffer = new byte[1024];
148 
149         int read = 0;
150 
151         StringBuffer content = new StringBuffer();
152 
153         while ( ( read = in.read( buffer, 0, 1024 ) ) >= 0 )
154         {
155             content.append( new String( buffer, 0, read ) );
156         }
157 
158         return content.toString();
159     }
160 }