From a39a097aab617dab0cddaf5cca8bff6835d93c30 Mon Sep 17 00:00:00 2001 From: Carlos Amengual Date: Mon, 15 Jul 2024 13:38:35 +0200 Subject: [PATCH] Tests: add a TestUtil test, remove useless I/O check in SVGGeneratorTest --- .../carte/echosvg/test/SMResourceLoader.java | 3 +- .../io/sf/carte/echosvg/test/TestUtil.java | 38 +++++++++- .../echosvg/svggen/test/SVGGeneratorTest.java | 8 +-- .../carte/echosvg/test/TestLocationsTest.java | 3 +- .../sf/carte/echosvg/test/TestUtilTest.java | 69 +++++++++++++++++++ 5 files changed, 110 insertions(+), 11 deletions(-) create mode 100644 echosvg-test/src/test/java/io/sf/carte/echosvg/test/TestUtilTest.java diff --git a/echosvg-test/src/main/java/io/sf/carte/echosvg/test/SMResourceLoader.java b/echosvg-test/src/main/java/io/sf/carte/echosvg/test/SMResourceLoader.java index 3f2d38237..d925ce11f 100644 --- a/echosvg-test/src/main/java/io/sf/carte/echosvg/test/SMResourceLoader.java +++ b/echosvg-test/src/main/java/io/sf/carte/echosvg/test/SMResourceLoader.java @@ -44,7 +44,8 @@ public URL getResource(Class cl, final String resourceName) { return java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() { @Override public URL run() { - return cl.getClassLoader().getResource(resourceName); + ClassLoader loader = cl.getClassLoader(); + return loader != null ? loader.getResource(resourceName) : null; } }); } diff --git a/echosvg-test/src/main/java/io/sf/carte/echosvg/test/TestUtil.java b/echosvg-test/src/main/java/io/sf/carte/echosvg/test/TestUtil.java index dd82b4318..0e1c0e0da 100644 --- a/echosvg-test/src/main/java/io/sf/carte/echosvg/test/TestUtil.java +++ b/echosvg-test/src/main/java/io/sf/carte/echosvg/test/TestUtil.java @@ -45,6 +45,14 @@ public class TestUtil { private TestUtil() { } + /** + * Get the URL of the root project directory. + * + * @param cl a class provided by the project. + * @param projectDirname the directory name of the subproject from which this is + * executed. + * @return the URL. + */ public static String getRootProjectURL(Class cl, String projectDirname) { String resName = cl.getName().replace(".", "/") + ".class"; URL url = ResourceLoader.getInstance().getResource(cl, resName); @@ -67,13 +75,39 @@ private static URL cwdURL() { } } + /** + * Get the URL of the Gradle-style project build directory. + * + * @param cl a class provided by the project. + * @param projectDirname the directory name of the subproject from which this is + * executed. + * @return the URL. + */ public static String getProjectBuildURL(Class cl, String projectDirname) { String resName = cl.getName().replace(".", "/") + ".class"; URL url = ResourceLoader.getInstance().getResource(cl, resName); + String classUrl; if (url == null) { - return null; + url = cwdURL(); + File f = new File(url.getFile(), projectDirname); + if (f.exists()) { + // CWD is root directory + try { + url = new URL(url.getProtocol(), url.getHost(), url.getPort(), f.getAbsolutePath()); + } catch (MalformedURLException e) { + return null; + } + classUrl = url.toExternalForm(); + } else { + // CWD is the project directory instead of root + classUrl = url.toExternalForm(); + if (classUrl.lastIndexOf(projectDirname) == -1) { + return null; + } + } + } else { + classUrl = url.toExternalForm(); } - String classUrl = url.toExternalForm(); int testDirIdx = classUrl.lastIndexOf(projectDirname); String buildDir = classUrl.substring(5, testDirIdx + projectDirname.length()) + "/build/"; return buildDir; diff --git a/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/SVGGeneratorTest.java b/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/SVGGeneratorTest.java index 968ad2969..a3b8a0af3 100644 --- a/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/SVGGeneratorTest.java +++ b/echosvg-test/src/test/java/io/sf/carte/echosvg/svggen/test/SVGGeneratorTest.java @@ -59,8 +59,6 @@ public class SVGGeneratorTest { private static final String[] VARIATION_PLATFORMS = io.sf.carte.echosvg.test.svg.PreconfiguredRenderingTest.DEFAULT_VARIATION_PLATFORMS; - private static final String ACCEPTED_REF_DIR = "accepted-ref"; - private static final String CANDIDATE_VARIATION_DIR = "candidate-variation"; private static final String PLATFORM_VARIATION_SUFFIX = io.sf.carte.echosvg.test.svg.PreconfiguredRenderingTest.PLATFORM_VARIATION_SUFFIX; @@ -364,11 +362,7 @@ static String getNonQualifiedClassName(Painter painter) { static URL getReferenceURL(Painter painter, String prefix) throws MalformedURLException { String suffix = prefix + getNonQualifiedClassName(painter) + SVG_EXTENSION; - URL refUrl = new URL(GENERATOR_REFERENCE_BASE + ACCEPTED_REF_DIR + '/' + suffix); - File acceptedReference = new File(refUrl.getFile()); - if (!acceptedReference.exists()) { - refUrl = new URL(GENERATOR_REFERENCE_BASE + suffix); - } + URL refUrl = new URL(GENERATOR_REFERENCE_BASE + suffix); return refUrl; } diff --git a/echosvg-test/src/test/java/io/sf/carte/echosvg/test/TestLocationsTest.java b/echosvg-test/src/test/java/io/sf/carte/echosvg/test/TestLocationsTest.java index b46c41f6b..dbec88222 100644 --- a/echosvg-test/src/test/java/io/sf/carte/echosvg/test/TestLocationsTest.java +++ b/echosvg-test/src/test/java/io/sf/carte/echosvg/test/TestLocationsTest.java @@ -40,7 +40,8 @@ public void testProjectRoot() throws IOException { public void testGetTestProjectBuildPath() throws IOException { String path = TestLocations.getTestProjectBuildURL(); assertNotNull(path); - assertTrue(path.length() > 8); + assertTrue(path.contains(TestLocations.TEST_DIRNAME), "Path must contain " + + TestLocations.TEST_DIRNAME); } } diff --git a/echosvg-test/src/test/java/io/sf/carte/echosvg/test/TestUtilTest.java b/echosvg-test/src/test/java/io/sf/carte/echosvg/test/TestUtilTest.java new file mode 100644 index 000000000..7a8bf2bba --- /dev/null +++ b/echosvg-test/src/test/java/io/sf/carte/echosvg/test/TestUtilTest.java @@ -0,0 +1,69 @@ +/* + * + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package io.sf.carte.echosvg.test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; + +import org.junit.jupiter.api.Test; + +/** + * Detect testing issues early by checking test util. + */ +public class TestUtilTest { + + @Test + public void testGetRootProjectURL() throws IOException { + String path = TestUtil.getRootProjectURL(getClass(), TestLocations.TEST_DIRNAME); + assertNotNull(path); + assertTrue(path.contains("echosvg")); + } + + @Test + public void testGetRootProjectURLWrongClass() throws IOException { + /* + * We pass a class that isn't provided by EchoSVG so it must use CWD + */ + String path = TestUtil.getRootProjectURL(String.class, TestLocations.TEST_DIRNAME); + assertNotNull(path); + assertTrue(path.contains("echosvg")); + } + + @Test + public void testGetProjectBuildURL() throws IOException { + String path = TestUtil.getProjectBuildURL(getClass(), TestLocations.TEST_DIRNAME); + assertNotNull(path); + assertTrue(path.contains(TestLocations.TEST_DIRNAME), "Path must contain " + + TestLocations.TEST_DIRNAME); + } + + @Test + public void testGetProjectBuildURLWrongClass() throws IOException { + /* + * We pass a class that isn't provided by EchoSVG so it must use CWD + */ + String path = TestUtil.getProjectBuildURL(String.class, TestLocations.TEST_DIRNAME); + assertNotNull(path); + assertTrue(path.contains(TestLocations.TEST_DIRNAME), "Path must contain " + + TestLocations.TEST_DIRNAME); + } + +}