Skip to content

Commit e2f465b

Browse files
committed
Add testing of Class.getResourceAsStream(String) to mx hellomodule
1 parent 72f31c1 commit e2f465b

File tree

1 file changed

+28
-14
lines changed
  • substratevm/src/native-image-module-tests/hello.app/src/main/java/hello

1 file changed

+28
-14
lines changed

substratevm/src/native-image-module-tests/hello.app/src/main/java/hello/Main.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,7 @@ public static void main(String[] args) throws NoSuchMethodException, InvocationT
6161
// testBootLayer(helloAppModule, helloLibModule); // TODO: Re-enable when fixing GR-34444
6262

6363
System.out.println("Now testing resources in modules");
64-
String helloAppModuleResourceContents;
65-
String sameResourcePathName = "resource-file.txt";
66-
try (Scanner s = new Scanner(helloAppModule.getResourceAsStream(sameResourcePathName))) {
67-
helloAppModuleResourceContents = s.nextLine();
68-
} catch (IOException e) {
69-
throw new AssertionError("Unable to access resource " + sameResourcePathName + " from " + helloAppModule);
70-
}
71-
String helloLibModuleResourceContents;
72-
try (Scanner s = new Scanner(helloLibModule.getResourceAsStream(sameResourcePathName))) {
73-
helloLibModuleResourceContents = s.nextLine();
74-
} catch (IOException e) {
75-
throw new AssertionError("Unable to access resource " + sameResourcePathName + " from " + helloLibModule);
76-
}
77-
assert !helloAppModuleResourceContents.equals(helloLibModuleResourceContents) : sameResourcePathName + " not recognized as different resources";
64+
testResourceAccess(helloAppModule, helloLibModule);
7865
}
7966

8067
private static void testModuleObjects(Module helloAppModule, Module helloLibModule) {
@@ -129,4 +116,31 @@ private static void testBootLayer(Module helloAppModule, Module helloLibModule)
129116
assert ModuleLayer.boot().modules().contains(helloAppModule);
130117
assert ModuleLayer.boot().modules().contains(helloLibModule);
131118
}
119+
120+
private static void testResourceAccess(Module helloAppModule, Module helloLibModule) {
121+
String helloAppModuleResourceContents;
122+
String sameResourcePathName = "resource-file.txt";
123+
124+
// Test Module.getResourceAsStream(String)
125+
try (Scanner s = new Scanner(helloAppModule.getResourceAsStream(sameResourcePathName))) {
126+
helloAppModuleResourceContents = s.nextLine();
127+
} catch (IOException e) {
128+
throw new AssertionError("Unable to access resource " + sameResourcePathName + " from " + helloAppModule);
129+
}
130+
String helloLibModuleResourceContents;
131+
try (Scanner s = new Scanner(helloLibModule.getResourceAsStream(sameResourcePathName))) {
132+
helloLibModuleResourceContents = s.nextLine();
133+
} catch (IOException e) {
134+
throw new AssertionError("Unable to access resource " + sameResourcePathName + " from " + helloLibModule);
135+
}
136+
assert !helloAppModuleResourceContents.equals(helloLibModuleResourceContents) : sameResourcePathName + " not recognized as different resources";
137+
138+
// Test Class.getResourceAsStream(String)
139+
try (Scanner s = new Scanner(Main.class.getResourceAsStream("/" + sameResourcePathName))) {
140+
assert helloAppModuleResourceContents.equals(s.nextLine()) : "Class.getResourceAsStream(String) result differs from Module.getResourceAsStream(String) result";
141+
}
142+
try (Scanner s = new Scanner(Greeter.class.getResourceAsStream("/" + sameResourcePathName))) {
143+
assert helloLibModuleResourceContents.equals(s.nextLine()) : "Class.getResourceAsStream(String) result differs from Module.getResourceAsStream(String) result";
144+
}
145+
}
132146
}

0 commit comments

Comments
 (0)