3737import java .nio .file .Files ;
3838import java .nio .file .Path ;
3939import java .nio .file .Paths ;
40+ import java .util .Arrays ;
4041import java .util .ArrayList ;
4142import java .util .List ;
4243import java .util .spi .ToolProvider ;
4344import java .util .stream .Stream ;
4445
4546import jdk .test .lib .process .ProcessTools ;
47+ import org .junit .jupiter .api .Test ;
48+ import org .junit .jupiter .api .Disabled ;
49+ import org .junit .jupiter .api .BeforeAll ;
50+ import static org .junit .jupiter .api .Assertions .*;
4651
47- import org .testng .annotations .Test ;
48- import static org .testng .Assert .*;
49-
50- @ Test
51- public class SetDefaultProvider {
52+ class SetDefaultProvider {
5253
5354 private static final String SET_DEFAULT_FSP =
54- "-Djava.nio.file.spi.DefaultFileSystemProvider=TestProvider" ;
55+ "-Djava.nio.file.spi.DefaultFileSystemProvider=testfsp. TestProvider" ;
5556
5657 private static final ToolProvider JAR_TOOL = ToolProvider .findFirst ("jar" )
5758 .orElseThrow (() ->
5859 new RuntimeException ("jar tool not found" )
5960 );
6061
61- private static Path createTempDirectory (String prefix ) throws IOException {
62- Path testDir = Paths .get (System .getProperty ("test.dir" , "." ));
63- return Files .createTempDirectory (testDir , prefix );
62+ private static final String TESTFSP = "testfsp" ;
63+ private static final String TESTAPP = "testapp" ;
64+ private static final String TESTAPP_MAIN = TESTAPP + ".Main" ;
65+
66+ // directory containing testfsp class files
67+ private static String TESTFSP_CLASSES ;
68+
69+ // directory containing testapp class files
70+ private static String TESTAPP_CLASSES ;
71+
72+ @ BeforeAll
73+ static void setup () {
74+ TESTFSP_CLASSES = classes (TESTFSP );
75+ TESTAPP_CLASSES = classes (TESTAPP );
6476 }
6577
6678 /**
67- * Test override of default FileSystemProvider with the main application
68- * on the class path.
79+ * Test file system provider exploded on the class path.
6980 */
70- public void testClassPath () throws Exception {
71- String moduleClasses = moduleClasses ();
72- String testClasses = System .getProperty ("test.classes" );
73- String classpath = moduleClasses + File .pathSeparator + testClasses ;
74- int exitValue = exec (SET_DEFAULT_FSP , "-cp" , classpath , "p.Main" );
75- assertEquals (exitValue , 0 );
81+ @ Test
82+ void testFspOnClassPath1 () throws Exception {
83+ exec (SET_DEFAULT_FSP ,
84+ "-cp" , ofClasspath (TESTFSP_CLASSES , TESTAPP_CLASSES ),
85+ TESTAPP_MAIN );
7686 }
7787
7888 /**
79- * Test override of default FileSystemProvider with a
80- * FileSystemProvider jar and the main application on the class path.
89+ * Test file system provider in JAR file on the class path.
8190 */
82- public void testClassPathWithFileSystemProviderJar () throws Exception {
83- String testClasses = System .getProperty ("test.classes" );
84- Path jar = Path .of ("testFileSystemProvider.jar" );
85- Files .deleteIfExists (jar );
86- createFileSystemProviderJar (jar , Path .of (testClasses ));
87- String classpath = jar + File .pathSeparator + testClasses
88- + File .separator + "modules" + File .separator + "m" ;
89- int exitValue = exec (SET_DEFAULT_FSP , "-cp" , classpath , "p.Main" );
90- assertEquals (exitValue , 0 );
91+ @ Test
92+ void testFspOnClassPath2 () throws Exception {
93+ String jarFile = createJar ("fsp.jar" , TESTFSP_CLASSES );
94+ exec (SET_DEFAULT_FSP ,
95+ "-cp" , ofClasspath (jarFile , TESTAPP_CLASSES ),
96+ TESTAPP_MAIN );
9197 }
9298
9399 /**
94- * Creates a JAR containing the FileSystemProvider used to override the
95- * default FileSystemProvider
100+ * Test file system provider in exploded module on the module path.
96101 */
97102 private void createFileSystemProviderJar (Path jar , Path dir ) throws IOException {
98103
99- List <String > args = new ArrayList <>();
100- args .add ("--create" );
101- args .add ("--file=" + jar );
102- try (Stream <Path > stream = Files .list (dir )) {
103- List <String > paths = stream
104- .map (path -> path .getFileName ().toString ())
105- .filter (f -> f .startsWith ("TestProvider" ))
106- .toList ();
107- for (var p : paths ) {
108- args .add ("-C" );
109- args .add (dir .toString ());
110- args .add (p );
111- }
112- }
113- int ret = JAR_TOOL .run (System .out , System .out , args .toArray (new String [0 ]));
114- assertEquals (ret , 0 );
104+ /**
105+ * Test file system provider in modular JAR on the module path.
106+ */
107+ @ Test
108+ void testFspOnModulePath2 () throws Exception {
109+ String jarFile = createJar ("fsp.jar" , TESTFSP_CLASSES );
110+ exec (SET_DEFAULT_FSP ,
111+ "-p" , jarFile ,
112+ "--add-modules" , TESTFSP ,
113+ "-cp" , TESTAPP_CLASSES ,
114+ TESTAPP_MAIN );
115115 }
116116
117117 /**
118- * Test override of default FileSystemProvider with the main application
119- * on the class path and a SecurityManager enabled.
118+ * Test file system provider linked into run-time image.
120119 */
121120 public void testClassPathWithSecurityManager () throws Exception {
122121 String moduleClasses = moduleClasses ();
@@ -131,95 +130,108 @@ public void testClassPathWithSecurityManager() throws Exception {
131130 }
132131
133132 /**
134- * Test override of default FileSystemProvider with the main application
135- * on the module path as an exploded module.
133+ * Test file system provider on class path, application in exploded module on module path.
136134 */
137- public void testExplodedModule () throws Exception {
138- String modulePath = System .getProperty ("jdk.module.path" );
139- int exitValue = exec (SET_DEFAULT_FSP , "-p" , modulePath , "-m" , "m/p.Main" );
140- assertEquals (exitValue , 0 );
135+ @ Test
136+ void testAppOnModulePath1 () throws Exception {
137+ exec (SET_DEFAULT_FSP ,
138+ "-p" , TESTAPP_CLASSES ,
139+ "-cp" , TESTFSP_CLASSES ,
140+ "-m" , TESTAPP + "/" + TESTAPP_MAIN );
141141 }
142142
143143 /**
144- * Test override of default FileSystemProvider with the main application
145- * on the module path as a modular JAR.
144+ * Test file system provider on class path, application in modular JAR on module path.
146145 */
147- public void testModularJar () throws Exception {
148- String jarFile = createModularJar ();
149- int exitValue = exec (SET_DEFAULT_FSP , "-p" , jarFile , "-m" , "m/p.Main" );
150- assertEquals (exitValue , 0 );
146+ @ Test
147+ void testAppOnModulePath2 () throws Exception {
148+ String jarFile = createJar ("testapp.jar" , TESTAPP_CLASSES );
149+ exec (SET_DEFAULT_FSP ,
150+ "-cp" , TESTFSP_CLASSES ,
151+ "-p" , jarFile ,
152+ "-m" , TESTAPP + "/" + TESTAPP_MAIN );
151153 }
152154
153155 /**
154- * Test override of default FileSystemProvider where the main application
155- * is a module that is patched by an exploded patch.
156+ * Test file system provider on class path, application in modular JAR on module path
157+ * that is patched with exploded patch.
156158 */
157- public void testExplodedModuleWithExplodedPatch () throws Exception {
159+ @ Test
160+ void testPatchedAppOnModulePath1 () throws Exception {
158161 Path patchdir = createTempDirectory ("patch" );
159- String modulePath = System . getProperty ( "jdk.module.path" );
160- int exitValue = exec (SET_DEFAULT_FSP ,
161- "--patch-module" , "m =" + patchdir ,
162- "-p" , modulePath ,
163- "-m " , "m/p.Main" );
164- assertEquals ( exitValue , 0 );
162+ Files . createFile ( patchdir . resolve ( "aoo.properties" ) );
163+ exec (SET_DEFAULT_FSP ,
164+ "--patch-module" , TESTAPP + " =" + patchdir ,
165+ "-p" , TESTAPP_CLASSES ,
166+ "-cp " , TESTFSP_CLASSES ,
167+ "-m" , TESTAPP + "/" + TESTAPP_MAIN );
165168 }
166169
167170 /**
168- * Test override of default FileSystemProvider where the main application
169- * is a module that is patched by an exploded patch .
171+ * Test file system provider on class path, application in modular JAR on module path
172+ * that is patched with patch in JAR file .
170173 */
171- public void testExplodedModuleWithJarPatch () throws Exception {
174+ @ Test
175+ void testPatchedAppOnModulePath2 () throws Exception {
172176 Path patchdir = createTempDirectory ("patch" );
173- Files .createDirectory (patchdir .resolve ("m.properties" ));
174- Path patch = createJarFile (patchdir );
175- String modulePath = System .getProperty ("jdk.module.path" );
176- int exitValue = exec (SET_DEFAULT_FSP ,
177- "--patch-module" , "m=" + patch ,
178- "-p" , modulePath ,
179- "-m" , "m/p.Main" );
180- assertEquals (exitValue , 0 );
177+ Files .createFile (patchdir .resolve ("app.properties" ));
178+ String jarFile = createJar ("patch.jar" , patchdir .toString ());
179+ exec (SET_DEFAULT_FSP ,
180+ "--patch-module" , TESTAPP + "=" + jarFile ,
181+ "-p" , TESTAPP_CLASSES ,
182+ "-cp" , TESTFSP_CLASSES ,
183+ "-m" , TESTAPP + "/" + TESTAPP_MAIN );
181184 }
182185
183186 /**
184- * Returns the directory containing the classes for module "m" .
187+ * Returns the directory containing the classes for the given module .
185188 */
186- private String moduleClasses ( ) {
189+ private static String classes ( String mn ) {
187190 String mp = System .getProperty ("jdk.module.path" );
188- for ( String dir : mp .split (File .pathSeparator )) {
189- Path m = Paths . get ( dir , "m" );
190- if ( Files . exists ( m )) return m . toString ();
191- }
192- fail ();
193- return null ;
191+ return Arrays . stream ( mp .split (File .pathSeparator ))
192+ . map ( e -> Path . of ( e , mn ))
193+ . filter ( Files :: isDirectory )
194+ . findAny ()
195+ . map ( Path :: toString )
196+ . orElseThrow () ;
194197 }
195198
196199 /**
197- * Creates a modular JAR containing module "m" .
200+ * Returns a class path from the given paths .
198201 */
199- private String createModularJar () throws Exception {
200- Path dir = Paths .get (moduleClasses ());
201- Path jar = createJarFile (dir );
202- return jar .toString ();
202+ private String ofClasspath (String ... paths ) {
203+ return String .join (File .pathSeparator , paths );
203204 }
204205
205206 /**
206- * Creates a JAR file containing the entries in the given file tree .
207+ * Creates a JAR file from the contains of the given directory .
207208 */
208- private Path createJarFile (Path dir ) throws Exception {
209- Path jar = createTempDirectory ("tmp" ).resolve ("m.jar" );
210- String [] args = { "--create" , "--file=" + jar , "-C" , dir .toString (), "." };
211- int ret = JAR_TOOL .run (System .out , System .out , args );
209+ private String createJar (String jar , String dir ) throws IOException {
210+ List <String > args = new ArrayList <>();
211+ args .add ("--create" );
212+ args .add ("--file=" + jar );
213+ args .add ("-C" );
214+ args .add (dir );
215+ args .add ("." );
216+ int ret = JAR_TOOL .run (System .err , System .err , args .toArray (new String [0 ]));
212217 assertEquals (ret , 0 );
213218 return jar ;
214219 }
215220
216221 /**
217- * Invokes the java launcher with the given arguments, returning the exit code.
222+ * Create a temporary directory with the given prefix in the current directory.
223+ */
224+ private static Path createTempDirectory (String prefix ) throws IOException {
225+ return Files .createTempDirectory (Path .of ("." ), prefix );
226+ }
227+
228+ /**
229+ * Invokes the java launcher with the given arguments, throws if the non-0 is returned.
218230 */
219- private int exec (String ... args ) throws Exception {
220- return ProcessTools .executeTestJava (args )
221- .outputTo (System .out )
222- .errorTo (System .out )
223- .getExitValue ( );
231+ private void exec (String ... args ) throws Exception {
232+ ProcessTools .executeTestJava (args )
233+ .outputTo (System .err )
234+ .errorTo (System .err )
235+ .shouldHaveExitValue ( 0 );
224236 }
225237}
0 commit comments