View Javadoc
1   package org.apache.maven.surefire.booter;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.net.URL;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import junit.framework.TestCase;
28  import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
29  
30  import static org.apache.maven.surefire.booter.Classpath.emptyClasspath;
31  
32  /**
33   * @author Kristian Rosenvold
34   */
35  public class ClasspathTest
36      extends TestCase
37  {
38      private static final String DUMMY_PROPERTY_NAME = "dummyProperty";
39  
40      private static final String DUMMY_URL_1 = "foo.jar";
41  
42      private static final String DUMMY_URL_2 = "bar.jar";
43  
44      public void testShouldWriteEmptyPropertyForEmptyClasspath()
45      {
46          Classpath classpath = Classpath.emptyClasspath();
47          classpath.writeToSystemProperty( DUMMY_PROPERTY_NAME );
48          assertEquals( "", System.getProperty( DUMMY_PROPERTY_NAME ) );
49      }
50  
51      public void testShouldWriteSeparatedElementsAsSystemProperty()
52      {
53          Classpath classpath = Classpath.emptyClasspath()
54                  .addClassPathElementUrl( DUMMY_URL_1 )
55                  .addClassPathElementUrl( DUMMY_URL_2 );
56          classpath.writeToSystemProperty( DUMMY_PROPERTY_NAME );
57          assertEquals( DUMMY_URL_1 + File.pathSeparatorChar + DUMMY_URL_2 + File.pathSeparatorChar,
58                        System.getProperty( DUMMY_PROPERTY_NAME ) );
59      }
60  
61      public void testShouldAddNoDuplicateElements()
62      {
63          Classpath classpath = emptyClasspath()
64                  .addClassPathElementUrl( DUMMY_URL_1 )
65                  .addClassPathElementUrl( DUMMY_URL_1 );
66          assertClasspathConsistsOfElements( classpath, new String[]{ DUMMY_URL_1 } );
67      }
68  
69      public void testShouldJoinTwoNullClasspaths()
70      {
71          Classpath joinedClasspath = Classpath.join( null, null );
72          assertEmptyClasspath( joinedClasspath );
73      }
74  
75      public void testShouldHaveAllElementsAfterJoiningTwoDifferentClasspaths()
76      {
77          Classpath firstClasspath = Classpath.emptyClasspath();
78          Classpath secondClasspath = firstClasspath.addClassPathElementUrl( DUMMY_URL_1 )
79                  .addClassPathElementUrl( DUMMY_URL_2 );
80          Classpath joinedClasspath = Classpath.join( firstClasspath, secondClasspath );
81          assertClasspathConsistsOfElements( joinedClasspath, new String[]{ DUMMY_URL_1, DUMMY_URL_2 } );
82      }
83  
84      public void testShouldNotHaveDuplicatesAfterJoiningTowClasspathsWithEqualElements()
85      {
86          Classpath firstClasspath = Classpath.emptyClasspath().addClassPathElementUrl( DUMMY_URL_1 );
87          Classpath secondClasspath = Classpath.emptyClasspath().addClassPathElementUrl( DUMMY_URL_1 );
88          Classpath joinedClasspath = Classpath.join( firstClasspath, secondClasspath );
89          assertClasspathConsistsOfElements( joinedClasspath, new String[]{ DUMMY_URL_1 } );
90      }
91  
92      public void testShouldNotBeAbleToRemoveElement()
93      {
94          try
95          {
96              Classpath classpath = createClasspathWithTwoElements();
97              classpath.getClassPath().remove( 0 );
98          }
99          catch ( java.lang.UnsupportedOperationException ignore )
100         {
101 
102         }
103     }
104 
105     private void assertClasspathConsistsOfElements( Classpath classpath, String[] elements )
106     {
107         List<String> classpathElements = classpath.getClassPath();
108         for ( String element : elements )
109         {
110             assertTrue( "The element '" + element + " is missing.", classpathElements.contains( element ) );
111         }
112         assertEquals( "Wrong number of classpath elements.", elements.length, classpathElements.size() );
113     }
114 
115     private void assertEmptyClasspath( Classpath classpath )
116     {
117         List<String> classpathElements = classpath.getClassPath();
118         assertEquals( "Wrong number of classpath elements.", 0, classpathElements.size() );
119     }
120 
121     private Classpath createClasspathWithTwoElements()
122     {
123         Classpath classpath = Classpath.emptyClasspath();
124         return classpath.addClassPathElementUrl( DUMMY_URL_1 ).addClassPathElementUrl( DUMMY_URL_2 );
125     }
126 
127     public void testShouldThrowIllegalArgumentExceptionWhenNullIsAddedAsClassPathElementUrl()
128     {
129         Classpath classpath = Classpath.emptyClasspath();
130         try
131         {
132             classpath.addClassPathElementUrl( null );
133             fail( "IllegalArgumentException not thrown." );
134         }
135         catch ( IllegalArgumentException expected )
136         {
137         }
138     }
139 
140     public void testShouldNotAddNullAsClassPathElementUrl()
141     {
142         Classpath classpath = Classpath.emptyClasspath();
143         try
144         {
145             classpath.addClassPathElementUrl( null );
146         }
147         catch ( IllegalArgumentException ignored )
148         {
149         }
150         assertEmptyClasspath( classpath );
151     }
152 
153     public void testCloneShouldBeEqual()
154     {
155         Classpath classpath = Classpath.emptyClasspath()
156             .addClassPathElementUrl( DUMMY_URL_1 )
157             .addClassPathElementUrl( DUMMY_URL_2 );
158 
159         assertEquals( classpath, classpath );
160         assertFalse( classpath.equals( null ) );
161 
162         assertEquals( 2, classpath.getClassPath().size() );
163         assertEquals( classpath, classpath.clone() );
164         assertEquals( classpath.hashCode(), classpath.clone().hashCode() );
165     }
166 
167     public void testIterator()
168     {
169         Classpath classpath = Classpath.emptyClasspath()
170             .addClassPathElementUrl( DUMMY_URL_1 )
171             .addClassPathElementUrl( DUMMY_URL_2 );
172         Iterator<String> it = classpath.iterator();
173         String url1 = it.hasNext() ? it.next() : null;
174         String url2 = it.hasNext() ? it.next() : null;
175         assertEquals( DUMMY_URL_1, url1 );
176         assertEquals( DUMMY_URL_2, url2 );
177     }
178 
179     public void testLog()
180     {
181         Classpath classpath = Classpath.emptyClasspath()
182             .addClassPathElementUrl( DUMMY_URL_1 )
183             .addClassPathElementUrl( DUMMY_URL_2 );
184         String log = classpath.getLogMessage( "classpath:" );
185         assertEquals( "classpath:  " + DUMMY_URL_1 + "  " + DUMMY_URL_2, log );
186     }
187 
188     public void testCompactLog()
189     {
190         Classpath classpath = Classpath.emptyClasspath()
191             .addClassPathElementUrl( "root" + File.separatorChar + DUMMY_URL_1 )
192             .addClassPathElementUrl( "root" + File.separatorChar + DUMMY_URL_2 );
193         String log = classpath.getCompactLogMessage( "classpath:" );
194         assertEquals( "classpath:  " + DUMMY_URL_1 + "  " + DUMMY_URL_2, log );
195     }
196 
197     public void testLoadInNewClassLoader() throws Exception
198     {
199         Class<?> target = ConsoleLogger.class;
200         String thisPath = "/" + target.getName().replace( '.', '/' ) + ".class";
201         URL url = target.getResource( thisPath );
202         assertTrue( url.toString().endsWith( thisPath ) );
203         String s = url.toString().replace( thisPath, "" ).replace( "!", "" ).replace( "jar:file:", "file:" );
204         String oneClasspath = new URL( s ).getFile();
205         assertTrue( new File( oneClasspath ).exists() );
206         Classpath classpath = Classpath.emptyClasspath();
207         ClassLoader classLoader = classpath.addClassPathElementUrl( new File( oneClasspath ).getCanonicalPath() )
208             .createClassLoader( false, true, "" );
209         Class<?> cls = classLoader.loadClass( target.getName() );
210         assertNotNull( cls );
211         assertEquals( cls.getName(), target.getName() );
212         assertNotSame( cls, target );
213     }
214 
215     public void testDontLoadInNewClassLoader()
216     {
217         Class<?> target = ConsoleLogger.class;
218         String thisPath = "/" + target.getName().replace( '.', '/' ) + ".class";
219         URL url = target.getResource( thisPath );
220         assertTrue( url.toString().endsWith( thisPath ) );
221         try
222         {
223             Classpath.emptyClasspath()
224                 .addClassPathElementUrl( "\u0000" )
225                 .createClassLoader( false, true, "" );
226             fail();
227         }
228         catch ( SurefireExecutionException e )
229         {
230             // expected
231         }
232     }
233 }