1 package org.apache.maven.tools.plugin.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
59
60
61
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 }