Skip to content

Commit 436f857

Browse files
committed
Add native unit test to test resource inclusion from path with space
1 parent 3eedd42 commit 436f857

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed

substratevm/mx.substratevm/suite.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,24 @@
946946
"jacoco" : "exclude",
947947
},
948948

949+
"com.oracle.svm.test.with.space": {
950+
"subDir": "src",
951+
"sourceDirs": ["src"],
952+
"dependencies": [
953+
"mx:JUNIT_TOOL",
954+
"sdk:NATIVEIMAGE",
955+
],
956+
"checkstyle": "com.oracle.svm.test",
957+
"workingSets": "SVM",
958+
"annotationProcessors": [
959+
"compiler:GRAAL_PROCESSOR",
960+
"SVM_PROCESSOR",
961+
],
962+
"javaCompliance" : "21+",
963+
"spotbugs": "false",
964+
"jacoco" : "exclude",
965+
},
966+
949967
"com.oracle.svm.configure.test": {
950968
"subDir": "src",
951969
"sourceDirs": ["src"],
@@ -1992,6 +2010,19 @@
19922010
"testDistribution" : True,
19932011
},
19942012

2013+
"SVM_TESTS WITH SPACE" : {
2014+
"subDir": "src",
2015+
"relpath" : True,
2016+
"dependencies" : [
2017+
"com.oracle.svm.test.with.space",
2018+
],
2019+
"distDependencies": [
2020+
"sdk:NATIVEIMAGE",
2021+
"mx:JUNIT_TOOL",
2022+
],
2023+
"testDistribution" : True,
2024+
},
2025+
19952026
"POLYGLOT_NATIVE_API" : {
19962027
"subDir": "src",
19972028
"dependencies": [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Args= \
2+
--initialize-at-run-time=com.oracle.svm.test.with.space \
3+
--features=com.oracle.svm.test.with.space.NativeImageResourceUtils$TestFeature
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package com.oracle.svm.test.with.space;
27+
28+
import static com.oracle.svm.test.with.space.NativeImageResourceUtils.RESOURCE_FILE_IN_JAR_WITH_SPACE;
29+
import static com.oracle.svm.test.with.space.NativeImageResourceUtils.resourceNameToPath;
30+
import static com.oracle.svm.test.with.space.NativeImageResourceUtils.resourceNameToURL;
31+
32+
import java.net.URL;
33+
import java.nio.file.Path;
34+
35+
import org.junit.Assert;
36+
import org.junit.Test;
37+
38+
public class NativeImageResourceTest {
39+
40+
@Test
41+
@SuppressWarnings("deprecation")
42+
public void accessFileFromJarWithSpaceInName() {
43+
URL url = resourceNameToURL(RESOURCE_FILE_IN_JAR_WITH_SPACE, true);
44+
Assert.assertNotNull("URL is null!", url);
45+
Path path = resourceNameToPath(RESOURCE_FILE_IN_JAR_WITH_SPACE, true);
46+
Assert.assertNotNull("Path is null!", path);
47+
}
48+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package com.oracle.svm.test.with.space;
27+
28+
import java.net.URI;
29+
import java.net.URISyntaxException;
30+
import java.net.URL;
31+
import java.nio.file.Path;
32+
import java.nio.file.Paths;
33+
34+
import org.graalvm.nativeimage.hosted.Feature;
35+
import org.graalvm.nativeimage.hosted.RuntimeResourceAccess;
36+
import org.junit.Assert;
37+
38+
public class NativeImageResourceUtils {
39+
40+
public static final String ROOT_DIRECTORY = "/";
41+
public static final String RESOURCE_DIR = "/resources";
42+
public static final String RESOURCE_FILE_IN_JAR_WITH_SPACE = RESOURCE_DIR + "/resource-in-jar-with-space.txt";
43+
44+
// Register resources.
45+
public static final class TestFeature implements Feature {
46+
@Override
47+
public void beforeAnalysis(BeforeAnalysisAccess access) {
48+
// Remove leading / for the resource patterns
49+
Module resourceModule = TestFeature.class.getModule();
50+
RuntimeResourceAccess.addResource(resourceModule, RESOURCE_DIR.substring(1));
51+
RuntimeResourceAccess.addResource(resourceModule, RESOURCE_FILE_IN_JAR_WITH_SPACE.substring(1));
52+
}
53+
}
54+
55+
public static URL resourceNameToURL(String resourceName, boolean failIfNotExists) {
56+
URL resource = NativeImageResourceUtils.class.getResource(resourceName);
57+
Assert.assertFalse("Resource " + resourceName + " is not found!", resource == null && failIfNotExists);
58+
return resource;
59+
}
60+
61+
public static URI resourceNameToURI(String resourceName, boolean failIfNotExists) {
62+
try {
63+
URL url = resourceNameToURL(resourceName, failIfNotExists);
64+
return url != null ? url.toURI() : null;
65+
} catch (URISyntaxException e) {
66+
Assert.fail("Bad URI syntax!");
67+
}
68+
return null;
69+
}
70+
71+
public static Path resourceNameToPath(String resourceName, boolean failIfNotExists) {
72+
URI uri = resourceNameToURI(resourceName, failIfNotExists);
73+
return uri != null ? Paths.get(uri) : null;
74+
}
75+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I am in a jar with spaces in its name!

0 commit comments

Comments
 (0)