1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.tools.plugin.util;
20
21 import java.io.UnsupportedEncodingException;
22 import java.net.URL;
23 import java.net.URLDecoder;
24
25 import org.junit.jupiter.api.Test;
26
27 import static org.junit.jupiter.api.Assertions.assertEquals;
28
29
30
31
32 public class TestUtils {
33
34 @Test
35 void testDirnameFunction_METATEST() throws UnsupportedEncodingException {
36 String classname = getClass().getName().replace('.', '/') + ".class";
37 String basedir = TestUtils.dirname(classname);
38
39 ClassLoader cl = Thread.currentThread().getContextClassLoader();
40 URL resource = cl.getResource(classname);
41
42 assertEquals(URLDecoder.decode(resource.getPath(), "UTF-8"), basedir + classname);
43 }
44
45 public static String dirname(String file) {
46 ClassLoader cl = Thread.currentThread().getContextClassLoader();
47 URL fileResource = cl.getResource(file);
48
49 String fullPath = fileResource.getPath();
50
51 String path = fullPath.substring(0, fullPath.length() - file.length());
52
53 try {
54
55
56
57
58
59 return URLDecoder.decode(path, "UTF-8");
60 } catch (UnsupportedEncodingException e) {
61 throw new Error("Broken JVM, UTF-8 must be supported", e);
62 }
63 }
64 }