77import static java .util .Objects .requireNonNull ;
88import static org .junit .jupiter .api .Assertions .assertEquals ;
99import static org .junit .jupiter .api .Assumptions .assumeFalse ;
10- import static org .junit .jupiter .api .Assumptions .assumeTrue ;
1110import static org .junit .jupiter .params .provider .Arguments .arguments ;
1211
1312import java .io .BufferedReader ;
2524
2625class CommandLineTest {
2726 private static final String JVM_OPTIONS_MARKER = "-- JVM OPTIONS --" ;
27+ private static final String MAIN_CLASS_MARKER = "-- MAIN CLASS --" ;
2828 private static final String CMD_ARGUMENTS_MARKER = "-- CMD ARGUMENTS --" ;
2929 private static final String REAL_CMD_ARGUMENTS_MARKER = "-- REAL CMD ARGUMENTS --" ;
30+ private static final String TEST_PROCESS_CLASS_NAME = CommandLineTestProcess .class .getName ();
3031
3132 static Stream <Arguments > data () {
3233 // spotless:off
@@ -48,10 +49,22 @@ static Stream<Arguments> data() {
4849 "Both JVM options and command arguments" ,
4950 of (asList ("-Xmx128m" , "-XX:+UseG1GC" , "-Dtest.property=value" ), asList ("arg1" , "arg2" )),
5051 of (asList ("-Xmx128m" , "-XX:+UseG1GC" , "-Dtest.property=value" ), asList ("arg1" , "arg2" ))),
52+ arguments (
53+ "JVM options from argfile" ,
54+ of (asList ("-Dtest.property=value" , argFile ("carriage-return-separated" )), asList ("arg1" , "arg2" )),
55+ of (flatten ("-Dtest.property=value" , expectedArsFromArgFile ("carriage-return-separated" )), asList ("arg1" , "arg2" ))),
56+ arguments (
57+ "JVM options from argfile" ,
58+ of (asList ("-Dtest.property=value" , argFile ("new-line-separated" )), asList ("arg1" , "arg2" )),
59+ of (flatten ("-Dtest.property=value" , expectedArsFromArgFile ("new-line-separated" )), asList ("arg1" , "arg2" ))),
5160 arguments (
5261 "JVM options from argfile" ,
5362 of (asList ("-Dtest.property=value" , argFile ("space-separated" )), asList ("arg1" , "arg2" )),
54- of (flatten ("-Dtest.property=value" , expectedArsFromArgFile ("space-separated" )), asList ("arg1" , "arg2" )))
63+ of (flatten ("-Dtest.property=value" , expectedArsFromArgFile ("space-separated" )), asList ("arg1" , "arg2" ))),
64+ arguments (
65+ "JVM options from argfile" ,
66+ of (asList ("-Dtest.property=value" , argFile ("tab-separated" )), asList ("arg1" , "arg2" )),
67+ of (flatten ("-Dtest.property=value" , expectedArsFromArgFile ("tab-separated" )), asList ("arg1" , "arg2" )))
5568 );
5669 // spotless:on
5770 }
@@ -60,51 +73,17 @@ static Stream<Arguments> data() {
6073 @ MethodSource ("data" )
6174 void testGetVmArguments (String useCase , RunArguments arguments , RunArguments expectedArguments )
6275 throws Exception {
63- if (useCase .contains ("argfile" )) {
64- System .out .println (">>> java.home" + System .getProperty ("java.home" ));
65- System .err .println (">>> java.home" + System .getProperty ("java.home" ));
66- }
6776 // Skip unsupported test cases
6877 skipArgFileTestOnJava8 (arguments );
69- // keepDisabledArgFileOnLinuxOnly(arguments);
7078 // Run test process
7179 Result result = forkAndRunWithArgs (CommandLineTestProcess .class , arguments );
7280 // Check results
7381 assertEquals (expectedArguments .jvmOptions , result .jvmOptions , "Failed to get JVM options" );
82+ assertEquals (TEST_PROCESS_CLASS_NAME , result .mainClass (), "Failed to get main class" );
7483 assertEquals (result .realCmdArgs , result .cmdArgs , "Failed to get command arguments" );
7584 assertEquals (result .realCmdArgs , expectedArguments .cmdArgs , "Unexpected command arguments" );
7685 }
7786
78- // @Test
79- // // Disable the test for Java 8. Using -PtestJvm will set Java HOME to the JVM to use to run
80- // this
81- // // test.
82- // @DisabledIfSystemProperty(named = "java.home", matches = ".*[-/]8\\..*")
83- // public void testGetVmArgumentsFromArgFile() throws Exception {
84- // List<String> jvmOptions = asList("-Dproperty1=value1", argFile("space-separated"));
85- // List<String> expectedJvmOptions = flatten("-Dproperty1=value1",
86- // expectedArsFromArgFile("space-separated"));
87- // Result result = forkAndRunWithArgs(CommandLineTestProcess.class, of(jvmOptions,
88- // emptyList()));
89- // assertEquals(expectedJvmOptions, result.jvmOptions, "Failed to get JVM options");
90- // // TODO CMD ARGS
91- // }
92- //
93- // @Test
94- // // Enable only for Java 20+: https://bugs.openjdk.org/browse/JDK-8297258
95- // // Using -PtestJvm will set Java HOME to the JVM to use to run this test.
96- // @EnabledIfSystemProperty(named = "java.home", matches = ".*[-/]21\\..*")
97- // public void testGetVmArgumentsFromDisabledArgFile() throws Exception {
98- // List<String> jvmArgs = asList("-Dproperty1=value1", "--disable-@files");
99- // List<String> cmdArgs = asList("arg1", argFile("space-separated"), "arg2");
100- // // --disable-@files won't be reported
101- // List<String> expectedJvmOptions = singletonList("-Dproperty1=value1");
102- // List<String> expectedCmdArgs = flatten(CommandLineTestProcess.class.getName(), cmdArgs);
103- // Result result = forkAndRunWithArgs(CommandLineTestProcess.class, of(jvmArgs, cmdArgs));
104- // assertEquals(expectedJvmOptions, result.jvmOptions, "Failed to get JVM options");
105- // assertEquals(expectedCmdArgs, result.cmdArgs, "Failed to get command arguments");
106- // }
107-
10887 private static void skipArgFileTestOnJava8 (RunArguments arguments ) {
10988 boolean useArgFile = false ;
11089 for (String jvmOption : arguments .jvmOptions ) {
@@ -126,19 +105,6 @@ private static void skipArgFileTestOnJava8(RunArguments arguments) {
126105 }
127106 }
128107
129- private static void keepDisabledArgFileOnLinuxOnly (RunArguments arguments ) {
130- boolean disableArgFile = false ;
131- for (String jvmOptions : arguments .jvmOptions ) {
132- if (jvmOptions .startsWith ("--disable-@files" )) {
133- disableArgFile = true ;
134- break ;
135- }
136- }
137- if (disableArgFile ) {
138- assumeTrue (OperatingSystem .isLinux ());
139- }
140- }
141-
142108 private static String argFile (String name ) {
143109 return "@src/test/resources/argfiles/" + name + ".txt" ;
144110 }
@@ -182,6 +148,8 @@ private Result forkAndRunWithArgs(Class<?> clazz, RunArguments arguments)
182148 while ((line = reader .readLine ()) != null ) {
183149 if (JVM_OPTIONS_MARKER .equals (line )) {
184150 current = result .jvmOptions ;
151+ } else if (MAIN_CLASS_MARKER .equals (line )) {
152+ current = result .mainClasses ;
185153 } else if (CMD_ARGUMENTS_MARKER .equals (line )) {
186154 current = result .cmdArgs ;
187155 } else if (REAL_CMD_ARGUMENTS_MARKER .equals (line )) {
@@ -240,7 +208,11 @@ static RunArguments of(List<String> jvmArgs, List<String> cmdArgs) {
240208
241209 static class Result extends CommandLineTest .RunArguments {
242210 List <String > realCmdArgs = new ArrayList <>();
243- String command ; // TODO
211+ List <String > mainClasses = new ArrayList <>();
212+
213+ String mainClass () {
214+ return String .join ("," , this .mainClasses );
215+ }
244216 }
245217
246218 // This class will be executed in the subprocess
@@ -251,6 +223,9 @@ public static void main(String[] args) {
251223 for (String option : JavaVirtualMachine .getVmOptions ()) {
252224 System .out .println (option );
253225 }
226+ // Print main class
227+ System .out .println (MAIN_CLASS_MARKER );
228+ System .out .println (JavaVirtualMachine .getMainClass ());
254229 // Print each command argument on a new line
255230 System .out .println (CMD_ARGUMENTS_MARKER );
256231 for (String arg : JavaVirtualMachine .getCommandArguments ()) {
0 commit comments