|
22 | 22 | import java.io.BufferedReader;
|
23 | 23 | import java.io.IOException;
|
24 | 24 | import java.io.StringReader;
|
| 25 | +import java.nio.file.Files; |
| 26 | +import java.nio.file.NoSuchFileException; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.nio.file.StandardOpenOption; |
25 | 29 | import java.util.Arrays;
|
26 | 30 | import java.util.Collections;
|
27 | 31 | import java.util.HashMap;
|
|
31 | 35 | import java.util.concurrent.atomic.AtomicBoolean;
|
32 | 36 |
|
33 | 37 | import static org.hamcrest.Matchers.contains;
|
| 38 | +import static org.hamcrest.Matchers.empty; |
34 | 39 | import static org.hamcrest.Matchers.equalTo;
|
| 40 | +import static org.hamcrest.Matchers.hasKey; |
| 41 | +import static org.hamcrest.Matchers.hasSize; |
35 | 42 | import static org.junit.Assert.assertFalse;
|
36 | 43 | import static org.junit.Assert.assertNull;
|
37 | 44 | import static org.junit.Assert.assertThat;
|
@@ -149,6 +156,113 @@ public void testComplexOptions() throws IOException {
|
149 | 156 | }
|
150 | 157 | }
|
151 | 158 |
|
| 159 | + public void testMissingRootJvmOptions() throws IOException, JvmOptionsParser.JvmOptionsFileParserException { |
| 160 | + final Path config = newTempDir(); |
| 161 | + try { |
| 162 | + final JvmOptionsParser parser = new JvmOptionsParser(); |
| 163 | + parser.readJvmOptionsFiles(config); |
| 164 | + fail("expected no such file exception, the root jvm.options file does not exist"); |
| 165 | + } catch (final NoSuchFileException expected) { |
| 166 | + // this is expected, the root JVM options file must exist |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + public void testReadRootJvmOptions() throws IOException, JvmOptionsParser.JvmOptionsFileParserException { |
| 171 | + final Path config = newTempDir(); |
| 172 | + final Path rootJvmOptions = config.resolve("jvm.options"); |
| 173 | + Files.write( |
| 174 | + rootJvmOptions, |
| 175 | + Arrays.asList("# comment", "-Xms256m", "-Xmx256m"), |
| 176 | + StandardOpenOption.CREATE_NEW, |
| 177 | + StandardOpenOption.APPEND |
| 178 | + ); |
| 179 | + if (randomBoolean()) { |
| 180 | + // an empty jvm.options.d directory should be irrelevant |
| 181 | + Files.createDirectory(config.resolve("jvm.options.d")); |
| 182 | + } |
| 183 | + final JvmOptionsParser parser = new JvmOptionsParser(); |
| 184 | + final List<String> jvmOptions = parser.readJvmOptionsFiles(config); |
| 185 | + assertThat(jvmOptions, contains("-Xms256m", "-Xmx256m")); |
| 186 | + } |
| 187 | + |
| 188 | + public void testReadJvmOptionsDirectory() throws IOException, JvmOptionsParser.JvmOptionsFileParserException { |
| 189 | + final Path config = newTempDir(); |
| 190 | + Files.createDirectory(config.resolve("jvm.options.d")); |
| 191 | + Files.write( |
| 192 | + config.resolve("jvm.options"), |
| 193 | + Arrays.asList("# comment", "-Xms256m", "-Xmx256m"), |
| 194 | + StandardOpenOption.CREATE_NEW, |
| 195 | + StandardOpenOption.APPEND |
| 196 | + ); |
| 197 | + Files.write( |
| 198 | + config.resolve("jvm.options.d").resolve("heap.options"), |
| 199 | + Arrays.asList("# comment", "-Xms384m", "-Xmx384m"), |
| 200 | + StandardOpenOption.CREATE_NEW, |
| 201 | + StandardOpenOption.APPEND |
| 202 | + ); |
| 203 | + final JvmOptionsParser parser = new JvmOptionsParser(); |
| 204 | + final List<String> jvmOptions = parser.readJvmOptionsFiles(config); |
| 205 | + assertThat(jvmOptions, contains("-Xms256m", "-Xmx256m", "-Xms384m", "-Xmx384m")); |
| 206 | + } |
| 207 | + |
| 208 | + public void testReadJvmOptionsDirectoryInOrder() throws IOException, JvmOptionsParser.JvmOptionsFileParserException { |
| 209 | + final Path config = newTempDir(); |
| 210 | + Files.createDirectory(config.resolve("jvm.options.d")); |
| 211 | + Files.write( |
| 212 | + config.resolve("jvm.options"), |
| 213 | + Arrays.asList("# comment", "-Xms256m", "-Xmx256m"), |
| 214 | + StandardOpenOption.CREATE_NEW, |
| 215 | + StandardOpenOption.APPEND |
| 216 | + ); |
| 217 | + Files.write( |
| 218 | + config.resolve("jvm.options.d").resolve("first.options"), |
| 219 | + Arrays.asList("# comment", "-Xms384m", "-Xmx384m"), |
| 220 | + StandardOpenOption.CREATE_NEW, |
| 221 | + StandardOpenOption.APPEND |
| 222 | + ); |
| 223 | + Files.write( |
| 224 | + config.resolve("jvm.options.d").resolve("second.options"), |
| 225 | + Arrays.asList("# comment", "-Xms512m", "-Xmx512m"), |
| 226 | + StandardOpenOption.CREATE_NEW, |
| 227 | + StandardOpenOption.APPEND |
| 228 | + ); |
| 229 | + final JvmOptionsParser parser = new JvmOptionsParser(); |
| 230 | + final List<String> jvmOptions = parser.readJvmOptionsFiles(config); |
| 231 | + assertThat(jvmOptions, contains("-Xms256m", "-Xmx256m", "-Xms384m", "-Xmx384m", "-Xms512m", "-Xmx512m")); |
| 232 | + } |
| 233 | + |
| 234 | + public void testReadJvmOptionsDirectoryIgnoresFilesNotNamedOptions() throws IOException, |
| 235 | + JvmOptionsParser.JvmOptionsFileParserException { |
| 236 | + final Path config = newTempDir(); |
| 237 | + Files.createFile(config.resolve("jvm.options")); |
| 238 | + Files.createDirectory(config.resolve("jvm.options.d")); |
| 239 | + Files.write( |
| 240 | + config.resolve("jvm.options.d").resolve("heap.not-named-options"), |
| 241 | + Arrays.asList("# comment", "-Xms256m", "-Xmx256m"), |
| 242 | + StandardOpenOption.CREATE_NEW, |
| 243 | + StandardOpenOption.APPEND |
| 244 | + ); |
| 245 | + final JvmOptionsParser parser = new JvmOptionsParser(); |
| 246 | + final List<String> jvmOptions = parser.readJvmOptionsFiles(config); |
| 247 | + assertThat(jvmOptions, empty()); |
| 248 | + } |
| 249 | + |
| 250 | + public void testFileContainsInvalidLinesThrowsParserException() throws IOException { |
| 251 | + final Path config = newTempDir(); |
| 252 | + final Path rootJvmOptions = config.resolve("jvm.options"); |
| 253 | + Files.write(rootJvmOptions, Arrays.asList("XX:+UseG1GC"), StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND); |
| 254 | + try { |
| 255 | + final JvmOptionsParser parser = new JvmOptionsParser(); |
| 256 | + parser.readJvmOptionsFiles(config); |
| 257 | + fail("expected JVM options file parser exception, XX:+UseG1GC is improperly formatted"); |
| 258 | + } catch (final JvmOptionsParser.JvmOptionsFileParserException expected) { |
| 259 | + assertThat(expected.jvmOptionsFile(), equalTo(rootJvmOptions)); |
| 260 | + assertThat(expected.invalidLines().entrySet(), hasSize(1)); |
| 261 | + assertThat(expected.invalidLines(), hasKey(1)); |
| 262 | + assertThat(expected.invalidLines().get(1), equalTo("XX:+UseG1GC")); |
| 263 | + } |
| 264 | + } |
| 265 | + |
152 | 266 | private void assertExpectedJvmOptions(final int javaMajorVersion, final BufferedReader br, final List<String> expectedJvmOptions)
|
153 | 267 | throws IOException {
|
154 | 268 | final Map<String, AtomicBoolean> seenJvmOptions = new HashMap<>();
|
|
0 commit comments