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.util.List;
24  
25  import junit.framework.TestCase;
26  
27  /**
28   * @author Kristian Rosenvold
29   */
30  public class ClasspathTest
31      extends TestCase
32  {
33      private static final String DUMMY_PROPERTY_NAME = "dummyProperty";
34  
35      private static final String DUMMY_URL_1 = "foo.jar";
36  
37      private static final String DUMMY_URL_2 = "bar.jar";
38  
39      public void testShouldWriteEmptyPropertyForEmptyClasspath()
40          throws Exception
41      {
42          Classpath classpath = new Classpath();
43          classpath.writeToSystemProperty( DUMMY_PROPERTY_NAME );
44          assertEquals( "", System.getProperty( DUMMY_PROPERTY_NAME ) );
45      }
46  
47      public void testShouldWriteSeparatedElementsAsSystemProperty()
48          throws Exception
49      {
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 ) );
56      }
57  
58      public void testShouldAddNoDuplicateElements()
59      {
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 } );
64      }
65  
66      public void testGetAsUrlList()
67          throws Exception
68      {
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 ) );
73      }
74  
75      public void testShouldJoinTwoNullClasspaths()
76      {
77          Classpath joinedClasspath = Classpath.join( null, null );
78          assertEmptyClasspath( joinedClasspath );
79      }
80  
81      public void testShouldHaveAllElementsAfterJoiningTwoDifferentClasspaths()
82          throws Exception
83      {
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 } );
90      }
91  
92      public void testShouldNotHaveDuplicatesAfterJoiningTowClasspathsWithEqualElements()
93          throws Exception
94      {
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 } );
101     }
102 
103     public void testShouldNotBeAbleToRemoveElement()
104         throws Exception
105     {
106         try {
107         Classpath classpath = createClasspathWithTwoElements();
108         classpath.getClassPath().remove( 0 );
109         } catch (java.lang.UnsupportedOperationException ignore){
110 
111         }
112     }
113 
114     private void assertClasspathConsistsOfElements( Classpath classpath, String[] elements )
115     {
116         List classpathElements = classpath.getClassPath();
117         for ( int i = 0; i < elements.length; ++i )
118         {
119             assertTrue( "The element '" + elements[i] + " is missing.", classpathElements.contains( elements[i] ) );
120         }
121         assertEquals( "Wrong number of classpath elements.", elements.length, classpathElements.size() );
122     }
123 
124     private void assertEmptyClasspath( Classpath classpath )
125     {
126         List classpathElements = classpath.getClassPath();
127         assertEquals( "Wrong number of classpath elements.", 0, classpathElements.size() );
128     }
129 
130     private Classpath createClasspathWithTwoElements()
131     {
132         Classpath classpath = new Classpath();
133         classpath.addClassPathElementUrl( DUMMY_URL_1 );
134         classpath.addClassPathElementUrl( DUMMY_URL_2 );
135         return classpath;
136     }
137 
138     public void testShouldThrowIllegalArgumentExceptionWhenNullIsAddedAsClassPathElementUrl()
139         throws Exception
140     {
141         Classpath classpath = new Classpath();
142         try
143         {
144             classpath.addClassPathElementUrl( null );
145             fail("IllegalArgumentException not thrown.");
146         }
147         catch (IllegalArgumentException expected)
148         {
149         }
150     }
151 
152     public void testShouldNotAddNullAsClassPathElementUrl()
153         throws Exception
154     {
155         Classpath classpath = new Classpath();
156         try
157         {
158             classpath.addClassPathElementUrl( null );
159         }
160         catch (IllegalArgumentException ignored)
161         {
162         }
163         assertEmptyClasspath( classpath );
164     }
165 }