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 org.junit.jupiter.api.Test;
23
24 import java.io.UnsupportedEncodingException;
25 import java.net.URL;
26 import java.net.URLDecoder;
27
28 import static org.junit.jupiter.api.Assertions.assertEquals;
29
30 /**
31 * @author jdcasey
32 */
33 public class TestUtils
34 {
35
36 @Test
37 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 * FIXME: URL encoding and HTML form encoding are not the same. Use FileUtils.toFile(URL) from plexus-utils
61 * once PLXUTILS-56 is released.
62 */
63 // necessary for JDK 1.5+, where spaces are escaped to %20
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 }