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  import static org.apache.maven.surefire.booter.Classpath.emptyClasspath;
28  
29  /**
30   * @author Kristian Rosenvold
31   */
32  public class ClasspathTest
33      extends TestCase
34  {
35      private static final String DUMMY_PROPERTY_NAME = "dummyProperty";
36  
37      private static final String DUMMY_URL_1 = "foo.jar";
38  
39      private static final String DUMMY_URL_2 = "bar.jar";
40  
41      public void testShouldWriteEmptyPropertyForEmptyClasspath()
42          throws Exception
43      {
44          Classpath classpath = Classpath.emptyClasspath();
45          classpath.writeToSystemProperty( DUMMY_PROPERTY_NAME );
46          assertEquals( "", System.getProperty( DUMMY_PROPERTY_NAME ) );
47      }
48  
49      public void testShouldWriteSeparatedElementsAsSystemProperty()
50          throws Exception
51      {
52          Classpath classpath = Classpath.emptyClasspath().addClassPathElementUrl( DUMMY_URL_1 ).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 =
61              emptyClasspath().addClassPathElementUrl( DUMMY_URL_1 ).addClassPathElementUrl( DUMMY_URL_1 );
62          assertClasspathConsistsOfElements( classpath, new String[]{ DUMMY_URL_1 } );
63      }
64  
65      public void testGetAsUrlList()
66          throws Exception
67      {
68          final List asUrlList = createClasspathWithTwoElements().getAsUrlList();
69          assertEquals( 2, asUrlList.size() );
70          assertTrue( asUrlList.get( 0 ).toString().endsWith( DUMMY_URL_1 ) );
71          assertTrue( asUrlList.get( 1 ).toString().endsWith( DUMMY_URL_2 ) );
72      }
73  
74      public void testShouldJoinTwoNullClasspaths()
75      {
76          Classpath joinedClasspath = Classpath.join( null, null );
77          assertEmptyClasspath( joinedClasspath );
78      }
79  
80      public void testShouldHaveAllElementsAfterJoiningTwoDifferentClasspaths()
81          throws Exception
82      {
83          Classpath firstClasspath = Classpath.emptyClasspath();
84          Classpath secondClasspath = firstClasspath.addClassPathElementUrl( DUMMY_URL_1 ).addClassPathElementUrl( DUMMY_URL_2 );
85          Classpath joinedClasspath = Classpath.join( firstClasspath, secondClasspath );
86          assertClasspathConsistsOfElements( joinedClasspath, new String[]{ DUMMY_URL_1, DUMMY_URL_2 } );
87      }
88  
89      public void testShouldNotHaveDuplicatesAfterJoiningTowClasspathsWithEqualElements()
90          throws Exception
91      {
92          Classpath firstClasspath = Classpath.emptyClasspath().addClassPathElementUrl( DUMMY_URL_1 );
93          Classpath secondClasspath = Classpath.emptyClasspath().addClassPathElementUrl( DUMMY_URL_1 );
94          Classpath joinedClasspath = Classpath.join( firstClasspath, secondClasspath );
95          assertClasspathConsistsOfElements( joinedClasspath, new String[]{ DUMMY_URL_1 } );
96      }
97  
98      public void testShouldNotBeAbleToRemoveElement()
99          throws Exception
100     {
101         try
102         {
103             Classpath classpath = createClasspathWithTwoElements();
104             classpath.getClassPath().remove( 0 );
105         }
106         catch ( java.lang.UnsupportedOperationException ignore )
107         {
108 
109         }
110     }
111 
112     private void assertClasspathConsistsOfElements( Classpath classpath, String[] elements )
113     {
114         List classpathElements = classpath.getClassPath();
115         for ( String element : elements )
116         {
117             assertTrue( "The element '" + element + " is missing.", classpathElements.contains( element ) );
118         }
119         assertEquals( "Wrong number of classpath elements.", elements.length, classpathElements.size() );
120     }
121 
122     private void assertEmptyClasspath( Classpath classpath )
123     {
124         List classpathElements = classpath.getClassPath();
125         assertEquals( "Wrong number of classpath elements.", 0, classpathElements.size() );
126     }
127 
128     private Classpath createClasspathWithTwoElements()
129     {
130         Classpath classpath = Classpath.emptyClasspath();
131         return classpath.addClassPathElementUrl( DUMMY_URL_1 ).addClassPathElementUrl( DUMMY_URL_2 );
132     }
133 
134     public void testShouldThrowIllegalArgumentExceptionWhenNullIsAddedAsClassPathElementUrl()
135         throws Exception
136     {
137         Classpath classpath = Classpath.emptyClasspath();
138         try
139         {
140             classpath.addClassPathElementUrl( null );
141             fail( "IllegalArgumentException not thrown." );
142         }
143         catch ( IllegalArgumentException expected )
144         {
145         }
146     }
147 
148     public void testShouldNotAddNullAsClassPathElementUrl()
149         throws Exception
150     {
151         Classpath classpath = Classpath.emptyClasspath();
152         try
153         {
154             classpath.addClassPathElementUrl( null );
155         }
156         catch ( IllegalArgumentException ignored )
157         {
158         }
159         assertEmptyClasspath( classpath );
160     }
161 }