|
8 | 8 | import static java.util.Collections.emptyList; |
9 | 9 | import static java.util.Collections.emptyMap; |
10 | 10 | import static java.util.Collections.singletonList; |
| 11 | +import static java.util.Objects.requireNonNull; |
11 | 12 | import static org.junit.jupiter.api.Assertions.assertEquals; |
12 | 13 | import static org.junit.jupiter.api.Assumptions.assumeTrue; |
13 | 14 | import static org.junit.jupiter.params.provider.Arguments.arguments; |
14 | 15 |
|
15 | 16 | import datadog.environment.CommandLineHelper.Result; |
| 17 | +import java.io.BufferedReader; |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.InputStream; |
| 20 | +import java.io.InputStreamReader; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Collection; |
16 | 23 | import java.util.HashMap; |
17 | 24 | import java.util.List; |
18 | 25 | import java.util.Map; |
19 | 26 | import java.util.stream.Stream; |
| 27 | +import org.junit.jupiter.api.Assertions; |
20 | 28 | import org.junit.jupiter.params.ParameterizedTest; |
21 | 29 | import org.junit.jupiter.params.provider.Arguments; |
22 | 30 | import org.junit.jupiter.params.provider.MethodSource; |
@@ -122,12 +130,37 @@ private static Stream<Arguments> procFsCmdLine() { |
122 | 130 | arguments( |
123 | 131 | "Java with JAR and options", |
124 | 132 | new String[]{"java", "-Xmx512m", "-Xms256m", "-jar", "app.jar"}, |
125 | | - asList("-Xmx512m", "-Xms256m", "-jar", "app.jar") |
| 133 | + asList("-Xmx512m", "-Xms256m") |
126 | 134 | ), |
127 | 135 | arguments( |
128 | 136 | "Java from class and options", |
129 | 137 | new String[]{"java", "-Xmx512m", "-Xms256m", "-cp", "app.jar", "Main"}, |
130 | | - asList("-Xmx512m", "-Xms256m", "-cp", "app.jar", "Main") |
| 138 | + asList("-Xmx512m", "-Xms256m", "-cp", "app.jar") |
| 139 | + ), |
| 140 | + arguments( |
| 141 | + "Java from class and options, mixed", |
| 142 | + new String[]{"java", "-Xms256m", "-cp", "app.jar", "-Xmx512m", "Main"}, |
| 143 | + asList("-Xms256m", "-cp", "app.jar", "-Xmx512m") |
| 144 | + ), |
| 145 | + arguments( |
| 146 | + "Args from file", |
| 147 | + new String[]{"java", "-Dargfile.prop=test", "-verbose:class", argFile("carriage-return-separated"), "-jar", "app.jar"}, |
| 148 | + flatten("-Dargfile.prop=test", "-verbose:class", expectedArsFromArgFile("carriage-return-separated")) |
| 149 | + ), |
| 150 | + arguments( |
| 151 | + "Args from file", |
| 152 | + new String[]{"java", "-Dargfile.prop=test", "-verbose:class", argFile("new-line-separated"), "-jar", "app.jar"}, |
| 153 | + flatten("-Dargfile.prop=test", "-verbose:class", expectedArsFromArgFile("new-line-separated")) |
| 154 | + ), |
| 155 | + arguments( |
| 156 | + "Args from file", |
| 157 | + new String[]{"java", "-Dargfile.prop=test", "-verbose:class", argFile("space-separated"), "-jar", "app.jar"}, |
| 158 | + flatten("-Dargfile.prop=test", "-verbose:class", expectedArsFromArgFile("space-separated")) |
| 159 | + ), |
| 160 | + arguments( |
| 161 | + "Args from file", |
| 162 | + new String[]{"java", "-Dargfile.prop=test", "-verbose:class", argFile("tab-separated"), "-jar", "app.jar"}, |
| 163 | + flatten("-Dargfile.prop=test", "-verbose:class", expectedArsFromArgFile("tab-separated")) |
131 | 164 | )); |
132 | 165 | // spotless:on |
133 | 166 | } |
@@ -157,4 +190,36 @@ private static Map<String, String> env(String... keysAndValues) { |
157 | 190 | } |
158 | 191 | return env; |
159 | 192 | } |
| 193 | + |
| 194 | + private static String argFile(String name) { |
| 195 | + return "@src/test/resources/argfiles/" + name + ".txt"; |
| 196 | + } |
| 197 | + |
| 198 | + private static List<String> expectedArsFromArgFile(String name) { |
| 199 | + List<String> arguments = new ArrayList<>(); |
| 200 | + try (InputStream stream = |
| 201 | + requireNonNull( |
| 202 | + CommandLineTest.class.getResourceAsStream("/argfiles/" + name + "-expected.txt")); |
| 203 | + BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) { |
| 204 | + String line; |
| 205 | + while ((line = reader.readLine()) != null) { |
| 206 | + arguments.add(line); |
| 207 | + } |
| 208 | + } catch (IOException e) { |
| 209 | + Assertions.fail("Failed to read expected args from " + name + "argfile", e); |
| 210 | + } |
| 211 | + return arguments; |
| 212 | + } |
| 213 | + |
| 214 | + private static List<String> flatten(Object... values) { |
| 215 | + List<String> result = new ArrayList<>(); |
| 216 | + for (Object value : values) { |
| 217 | + if (value instanceof Collection) { |
| 218 | + result.addAll((Collection<? extends String>) value); |
| 219 | + } else { |
| 220 | + result.add(value.toString()); |
| 221 | + } |
| 222 | + } |
| 223 | + return result; |
| 224 | + } |
160 | 225 | } |
0 commit comments