View Javadoc
1   package org.apache.maven.tools.plugin.util;
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 junit.framework.TestCase;
23  
24  import java.io.UnsupportedEncodingException;
25  import java.net.URL;
26  import java.net.URLDecoder;
27  
28  /**
29   * @author jdcasey
30   */
31  public class TestUtils
32      extends TestCase
33  {
34  
35      public void testDirnameFunction_METATEST() throws UnsupportedEncodingException
36      {
37          String classname = getClass().getName().replace( '.', '/' ) + ".class";
38          String basedir = TestUtils.dirname( classname );
39  
40          ClassLoader cl = Thread.currentThread().getContextClassLoader();
41          URL resource = cl.getResource( classname );
42  
43          assertEquals( URLDecoder.decode( resource.getPath(), "UTF-8" ), basedir + classname );
44      }
45  
46      public static String dirname( String file )
47      {
48          ClassLoader cl = Thread.currentThread().getContextClassLoader();
49          URL fileResource = cl.getResource( file );
50  
51          String fullPath = fileResource.getPath();
52  
53          String path = fullPath.substring( 0, fullPath.length() - file.length() );
54  
55          try
56          {
57              /*
58               * FIXME: URL encoding and HTML form encoding are not the same. Use FileUtils.toFile(URL) from plexus-utils
59               * once PLXUTILS-56 is released.
60               */
61              // necessary for JDK 1.5+, where spaces are escaped to %20
62              return URLDecoder.decode( path, "UTF-8" );
63          }
64          catch ( UnsupportedEncodingException e )
65          {
66              throw new Error( "Broken JVM, UTF-8 must be supported", e );
67          }
68      }
69  
70  }