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