1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. 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,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
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 * @author jdcasey
31 */
32 public class TestUtils {
33
34 @Test
35 void testDirnameFunctionMETATEST() 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 * FIXME: URL encoding and HTML form encoding are not the same. Use FileUtils.toFile(URL) from plexus-utils
56 * once PLXUTILS-56 is released.
57 */
58 // necessary for JDK 1.5+, where spaces are escaped to %20
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 }