Skip to content

Commit

Permalink
Tests: add a TestUtil test, remove useless I/O check in SVGGeneratorTest
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosame committed Jul 15, 2024
1 parent 0954d22 commit a39a097
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public URL getResource(Class<?> cl, final String resourceName) {
return java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<URL>() {
@Override
public URL run() {
return cl.getClassLoader().getResource(resourceName);
ClassLoader loader = cl.getClassLoader();
return loader != null ? loader.getResource(resourceName) : null;
}
});
}
Expand Down
38 changes: 36 additions & 2 deletions echosvg-test/src/main/java/io/sf/carte/echosvg/test/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit a39a097

Please sign in to comment.