Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/main/java/software/stephenson/trace/TraceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,19 @@ static final class ServerWideConfig {
* nor stop the host program.
*/
private ServerWideConfig readServerWideConfig(File pluginsDirectory) {
Path file = new File(pluginsDirectory, SERVER_WIDE_CONFIG_PATH).toPath();
File location = new File(pluginsDirectory, SERVER_WIDE_CONFIG_PATH);
try {
// Inside the try: toPath() throws InvalidPathException for a path
// the file system cannot represent, and build() never throws.
Path file = location.toPath();
if (!Files.exists(file)) {
Files.createDirectories(file.getParent());
Files.write(file, SERVER_WIDE_CONFIG_CONTENT.getBytes(StandardCharsets.UTF_8));
// just written: enabled: true, and the tags example commented out
}
return parseServerWideConfig(Files.readAllLines(file, StandardCharsets.UTF_8));
} catch (IOException | RuntimeException failure) {
log("could not read server-wide config " + file + ": " + failure);
log("could not read server-wide config " + location + ": " + failure);
return ServerWideConfig.NONE;
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/software/stephenson/trace/TraceClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,29 @@ void serverWideConfig_ioFailureIsLoggedFineAndTreatedAsEnabled(@TempDir Path scr
client.close();
}

@Test
void serverWideConfig_aPathThatCannotBeConvertedIsLoggedFineAndTreatedAsEnabled() throws Exception {
// Arrange
// A NUL in the name makes File.toPath() throw InvalidPathException --
// a RuntimeException that build() must not let out.
File unconvertible = new File("plugins\u0000");
RecordingHandler log = new RecordingHandler();
Logger logger = Logger.getLogger("TraceClientTest.serverWideInvalidPath");
logger.setLevel(Level.ALL);
logger.addHandler(log);

// Act
TraceClient client = assertDoesNotThrow(() -> TraceClient.builder(baseUrl(), "MyPlugin", "1.2.3").key("k")
.serverWideConfig(unconvertible).logger(logger).build());

// Assert
assertTrue(client.isEnabled(), "a switch file that cannot be located must not turn reporting off");
assertTrue(log.await(1, TimeUnit.SECONDS), "the failure should be mentioned at FINE");
assertEquals(Level.FINE, log.records.get(0).getLevel());
assertTrue(log.records.get(0).getMessage().contains("server-wide config"), log.records.get(0).getMessage());
client.close();
}

private static Path writeServerWideConfig(Path plugins, String content) throws java.io.IOException {
Path file = plugins.resolve("trace").resolve("config.yml");
Files.createDirectories(file.getParent());
Expand Down
Loading