1
package org.apache.maven.surefire.booter;
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
12
* http://www.apache.org/licenses/LICENSE-2.0
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
23
import java.util.List;
25
import junit.framework.TestCase;
28
* @author Kristian Rosenvold
30
public class ClasspathTest
33
private static final String DUMMY_PROPERTY_NAME = "dummyProperty";
35
private static final String DUMMY_URL_1 = "foo.jar";
37
private static final String DUMMY_URL_2 = "bar.jar";
39
public void testShouldWriteEmptyPropertyForEmptyClasspath()
42
Classpath classpath = new Classpath();
43
classpath.writeToSystemProperty( DUMMY_PROPERTY_NAME );
44
assertEquals( "", System.getProperty( DUMMY_PROPERTY_NAME ) );
47
public void testShouldWriteSeparatedElementsAsSystemProperty()
50
Classpath classpath = new Classpath();
51
classpath.addClassPathElementUrl( DUMMY_URL_1 );
52
classpath.addClassPathElementUrl( DUMMY_URL_2 );
53
classpath.writeToSystemProperty( DUMMY_PROPERTY_NAME );
54
assertEquals( DUMMY_URL_1 + File.pathSeparatorChar + DUMMY_URL_2 + File.pathSeparatorChar,
55
System.getProperty( DUMMY_PROPERTY_NAME ) );
58
public void testShouldAddNoDuplicateElements()
60
Classpath classpath = new Classpath();
61
classpath.addClassPathElementUrl( DUMMY_URL_1 );
62
classpath.addClassPathElementUrl( DUMMY_URL_1 );
63
assertClasspathConsistsOfElements( classpath, new String[] { DUMMY_URL_1 } );
66
public void testGetAsUrlList()
69
final List asUrlList = createClasspathWithTwoElements().getAsUrlList();
70
assertEquals( 2, asUrlList.size() );
71
assertTrue( asUrlList.get( 0 ).toString().endsWith( DUMMY_URL_1 ) );
72
assertTrue( asUrlList.get( 1 ).toString().endsWith( DUMMY_URL_2 ) );
75
public void testShouldJoinTwoNullClasspaths()
77
Classpath joinedClasspath = Classpath.join( null, null );
78
assertEmptyClasspath( joinedClasspath );
81
public void testShouldHaveAllElementsAfterJoiningTwoDifferentClasspaths()
84
Classpath firstClasspath = new Classpath();
85
firstClasspath.addClassPathElementUrl( DUMMY_URL_1 );
86
Classpath secondClasspath = new Classpath();
87
secondClasspath.addClassPathElementUrl( DUMMY_URL_2 );
88
Classpath joinedClasspath = Classpath.join( firstClasspath, secondClasspath );
89
assertClasspathConsistsOfElements( joinedClasspath, new String[] { DUMMY_URL_1, DUMMY_URL_2 } );
92
public void testShouldNotHaveDuplicatesAfterJoiningTowClasspathsWithEqualElements()
95
Classpath firstClasspath = new Classpath();
96
firstClasspath.addClassPathElementUrl( DUMMY_URL_1 );
97
Classpath secondClasspath = new Classpath();
98
secondClasspath.addClassPathElementUrl( DUMMY_URL_1 );
99
Classpath joinedClasspath = Classpath.join( firstClasspath, secondClasspath );
100
assertClasspathConsistsOfElements( joinedClasspath, new String[] { DUMMY_URL_1 } );
103
public void testShouldNotBeAbleToRemoveElement()
107
Classpath classpath = createClasspathWithTwoElements();
108
classpath.getClassPath().remove( 0 );
109
} catch (java.lang.UnsupportedOperationException ignore){
114
private void assertClasspathConsistsOfElements( Classpath classpath, String[] elements )
116
List classpathElements = classpath.getClassPath();
117
for ( int i = 0; i < elements.length; ++i )
119
assertTrue( "The element '" + elements[i] + " is missing.", classpathElements.contains( elements[i] ) );
121
assertEquals( "Wrong number of classpath elements.", elements.length, classpathElements.size() );
124
private void assertEmptyClasspath( Classpath classpath )
126
List classpathElements = classpath.getClassPath();
127
assertEquals( "Wrong number of classpath elements.", 0, classpathElements.size() );
130
private Classpath createClasspathWithTwoElements()
132
Classpath classpath = new Classpath();
133
classpath.addClassPathElementUrl( DUMMY_URL_1 );
134
classpath.addClassPathElementUrl( DUMMY_URL_2 );
138
public void testShouldThrowIllegalArgumentExceptionWhenNullIsAddedAsClassPathElementUrl()
141
Classpath classpath = new Classpath();
144
classpath.addClassPathElementUrl( null );
145
fail("IllegalArgumentException not thrown.");
147
catch (IllegalArgumentException expected)
152
public void testShouldNotAddNullAsClassPathElementUrl()
155
Classpath classpath = new Classpath();
158
classpath.addClassPathElementUrl( null );
160
catch (IllegalArgumentException ignored)
163
assertEmptyClasspath( classpath );