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 org.junit.Test;
23
24 import java.io.UnsupportedEncodingException;
25 import java.net.URL;
26 import java.net.URLDecoder;
27
28 import static org.junit.Assert.assertEquals;
29
30
31
32
33 public class TestUtils
34 {
35
36 @Test
37 public void testDirnameFunction_METATEST() throws UnsupportedEncodingException
38 {
39 String classname = getClass().getName().replace( '.', '/' ) + ".class";
40 String basedir = TestUtils.dirname( classname );
41
42 ClassLoader cl = Thread.currentThread().getContextClassLoader();
43 URL resource = cl.getResource( classname );
44
45 assertEquals( URLDecoder.decode( resource.getPath(), "UTF-8" ), basedir + classname );
46 }
47
48 public static String dirname( String file )
49 {
50 ClassLoader cl = Thread.currentThread().getContextClassLoader();
51 URL fileResource = cl.getResource( file );
52
53 String fullPath = fileResource.getPath();
54
55 String path = fullPath.substring( 0, fullPath.length() - file.length() );
56
57 try
58 {
59
60
61
62
63
64 return URLDecoder.decode( path, "UTF-8" );
65 }
66 catch ( UnsupportedEncodingException e )
67 {
68 throw new Error( "Broken JVM, UTF-8 must be supported", e );
69 }
70 }
71
72 }