@@ -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