Get jar files via URI objects in tests to prevent issue with spaces

This commit is contained in:
ljacqu 2016-06-12 20:54:39 +02:00
parent c3d391aeaf
commit 69100daba5

View File

@ -8,6 +8,8 @@ import java.io.File;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -31,8 +33,8 @@ public final class TestHelper {
* @return The project file * @return The project file
*/ */
public static File getJarFile(String path) { public static File getJarFile(String path) {
URL url = getUrlOrThrow(path); URI uri = getUriOrThrow(path);
return new File(url.getFile()); return new File(uri.getPath());
} }
/** /**
@ -42,7 +44,7 @@ public final class TestHelper {
* @return The Path object to the file * @return The Path object to the file
*/ */
public static Path getJarPath(String path) { public static Path getJarPath(String path) {
String sqlFilePath = getUrlOrThrow(path).getPath(); String sqlFilePath = getUriOrThrow(path).getPath();
// Windows preprends the path with a '/' or '\', which Paths cannot handle // Windows preprends the path with a '/' or '\', which Paths cannot handle
String appropriatePath = System.getProperty("os.name").contains("indow") String appropriatePath = System.getProperty("os.name").contains("indow")
? sqlFilePath.substring(1) ? sqlFilePath.substring(1)
@ -50,12 +52,16 @@ public final class TestHelper {
return Paths.get(appropriatePath); return Paths.get(appropriatePath);
} }
private static URL getUrlOrThrow(String path) { private static URI getUriOrThrow(String path) {
URL url = TestHelper.class.getResource(path); URL url = TestHelper.class.getResource(path);
if (url == null) { if (url == null) {
throw new IllegalStateException("File '" + path + "' could not be loaded"); throw new IllegalStateException("File '" + path + "' could not be loaded");
} }
return url; try {
return new URI(url.toString());
} catch (URISyntaxException e) {
throw new IllegalStateException("File '" + path + "' cannot be converted to a URI");
}
} }
/** /**