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      {
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      {
50          Classpath classpath = Classpath.emptyClasspath().addClassPathElementUrl( DUMMY_URL_1 ).addClassPathElementUrl( DUMMY_URL_2 );
51          classpath.writeToSystemProperty( DUMMY_PROPERTY_NAME );
52          assertEquals( DUMMY_URL_1 + File.pathSeparatorChar + DUMMY_URL_2 + File.pathSeparatorChar,
53                        System.getProperty( DUMMY_PROPERTY_NAME ) );
54      }
55  
56      public void testShouldAddNoDuplicateElements()
57      {
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      {
65          Classpath joinedClasspath = Classpath.join( null, null );
66          assertEmptyClasspath( joinedClasspath );
67      }
68  
69      public void testShouldHaveAllElementsAfterJoiningTwoDifferentClasspaths()
70      {
71          Classpath firstClasspath = Classpath.emptyClasspath();
72          Classpath secondClasspath = firstClasspath.addClassPathElementUrl( DUMMY_URL_1 ).addClassPathElementUrl( DUMMY_URL_2 );
73          Classpath joinedClasspath = Classpath.join( firstClasspath, secondClasspath );
74          assertClasspathConsistsOfElements( joinedClasspath, new String[]{ DUMMY_URL_1, DUMMY_URL_2 } );
75      }
76  
77      public void testShouldNotHaveDuplicatesAfterJoiningTowClasspathsWithEqualElements()
78      {
79          Classpath firstClasspath = Classpath.emptyClasspath().addClassPathElementUrl( DUMMY_URL_1 );
80          Classpath secondClasspath = Classpath.emptyClasspath().addClassPathElementUrl( DUMMY_URL_1 );
81          Classpath joinedClasspath = Classpath.join( firstClasspath, secondClasspath );
82          assertClasspathConsistsOfElements( joinedClasspath, new String[]{ DUMMY_URL_1 } );
83      }
84  
85      public void testShouldNotBeAbleToRemoveElement()
86      {
87          try
88          {
89              Classpath classpath = createClasspathWithTwoElements();
90              classpath.getClassPath().remove( 0 );
91          }
92          catch ( java.lang.UnsupportedOperationException ignore )
93          {
94  
95          }
96      }
97  
98      private void assertClasspathConsistsOfElements( Classpath classpath, String[] elements )
99      {
100         List classpathElements = classpath.getClassPath();
101         for ( String element : elements )
102         {
103             assertTrue( "The element '" + element + " is missing.", classpathElements.contains( element ) );
104         }
105         assertEquals( "Wrong number of classpath elements.", elements.length, classpathElements.size() );
106     }
107 
108     private void assertEmptyClasspath( Classpath classpath )
109     {
110         List classpathElements = classpath.getClassPath();
111         assertEquals( "Wrong number of classpath elements.", 0, classpathElements.size() );
112     }
113 
114     private Classpath createClasspathWithTwoElements()
115     {
116         Classpath classpath = Classpath.emptyClasspath();
117         return classpath.addClassPathElementUrl( DUMMY_URL_1 ).addClassPathElementUrl( DUMMY_URL_2 );
118     }
119 
120     public void testShouldThrowIllegalArgumentExceptionWhenNullIsAddedAsClassPathElementUrl()
121     {
122         Classpath classpath = Classpath.emptyClasspath();
123         try
124         {
125             classpath.addClassPathElementUrl( null );
126             fail( "IllegalArgumentException not thrown." );
127         }
128         catch ( IllegalArgumentException expected )
129         {
130         }
131     }
132 
133     public void testShouldNotAddNullAsClassPathElementUrl()
134     {
135         Classpath classpath = Classpath.emptyClasspath();
136         try
137         {
138             classpath.addClassPathElementUrl( null );
139         }
140         catch ( IllegalArgumentException ignored )
141         {
142         }
143         assertEmptyClasspath( classpath );
144     }
145 }