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