|
| 1 | +/* |
| 2 | + * |
| 3 | + * See the NOTICE file distributed with this work for additional |
| 4 | + * information regarding copyright ownership. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + */ |
| 19 | +package io.sf.carte.echosvg.test; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.net.MalformedURLException; |
| 23 | +import java.net.URL; |
| 24 | +import java.nio.file.Paths; |
| 25 | + |
| 26 | +public class TestUtil { |
| 27 | + |
| 28 | + private TestUtil() { |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Get the URL of the root project directory. |
| 33 | + * |
| 34 | + * @param cl a class provided by the project. |
| 35 | + * @param projectDirname the directory name of the subproject from which this is |
| 36 | + * executed. |
| 37 | + * @return the URL. |
| 38 | + */ |
| 39 | + public static String getRootProjectURL(Class<?> cl, String projectDirname) { |
| 40 | + String resName = cl.getName().replace(".", "/") + ".class"; |
| 41 | + URL url = ResourceLoader.getInstance().getResource(cl, resName); |
| 42 | + if (url == null) { |
| 43 | + url = cwdURL(); |
| 44 | + } |
| 45 | + String sUrl = url.toExternalForm(); |
| 46 | + int testDirIdx = sUrl.lastIndexOf(projectDirname); |
| 47 | + if (testDirIdx != -1) { |
| 48 | + sUrl = sUrl.substring(0, testDirIdx); |
| 49 | + } // If no projectDirname, we probably got the root via CWD |
| 50 | + return sUrl; |
| 51 | + } |
| 52 | + |
| 53 | + private static URL cwdURL() { |
| 54 | + try { |
| 55 | + return Paths.get(".").toAbsolutePath().normalize().toUri().toURL(); |
| 56 | + } catch (MalformedURLException e) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Get the URL of the Gradle-style project build directory. |
| 63 | + * |
| 64 | + * @param cl a class provided by the project. |
| 65 | + * @param projectDirname the directory name of the subproject from which this is |
| 66 | + * executed. |
| 67 | + * @return the URL. |
| 68 | + */ |
| 69 | + public static String getProjectBuildURL(Class<?> cl, String projectDirname) { |
| 70 | + String resName = cl.getName().replace(".", "/") + ".class"; |
| 71 | + URL url = ResourceLoader.getInstance().getResource(cl, resName); |
| 72 | + String classUrl; |
| 73 | + if (url == null) { |
| 74 | + url = cwdURL(); |
| 75 | + File f = new File(url.getFile(), projectDirname); |
| 76 | + if (f.exists()) { |
| 77 | + // CWD is root directory |
| 78 | + try { |
| 79 | + url = new URL(url.getProtocol(), url.getHost(), url.getPort(), f.getAbsolutePath()); |
| 80 | + } catch (MalformedURLException e) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + classUrl = url.toExternalForm(); |
| 84 | + } else { |
| 85 | + // CWD is the project directory instead of root |
| 86 | + classUrl = url.toExternalForm(); |
| 87 | + if (classUrl.lastIndexOf(projectDirname) == -1) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + } |
| 91 | + } else { |
| 92 | + classUrl = url.toExternalForm(); |
| 93 | + } |
| 94 | + int testDirIdx = classUrl.lastIndexOf(projectDirname); |
| 95 | + String buildDir = classUrl.substring(5, testDirIdx + projectDirname.length()) + "/build/"; |
| 96 | + return buildDir; |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments