View Javadoc
1   package org.codehaus.plexus.classworlds;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.net.MalformedURLException;
21  import java.net.URL;
22  
23  /**
24   * @author <a href="bwalding@jakarta.org">Ben Walding</a>
25   */
26  public class TestUtil
27  {
28  
29      public static URL getTestResourceUrl( String resourceName )
30      {
31          File baseDir = new File( getBasedir() );
32  
33          File testDir = new File( baseDir, "src/test/test-data" );
34  
35          File resourceFile = new File( testDir, resourceName );
36  
37          try
38          {
39              return resourceFile.toURI().toURL();
40          }
41          catch ( MalformedURLException e )
42          {
43              throw new RuntimeException( e );
44          }
45      }
46  
47      public static String getBasedir()
48      {
49          String basedir = System.getProperty( "basedir" );
50  
51          /* do our best if we are not running from surefire */
52          if ( basedir == null || basedir.length() <= 0 )
53          {
54              basedir = ( new File( "" ) ).getAbsolutePath();
55          }
56  
57          return basedir;
58      }
59  
60  }