Skip to content
Merged
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
80 changes: 80 additions & 0 deletions src/test/java/software/stephenson/trace/TraceClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,86 @@ void environment_disablesAndWinsOverTheServerWideFile(@TempDir Path plugins) thr
assertNull(TraceClient.builder(baseUrl(), "MyPlugin", "1.2.3").key("k").serverWideConfig(plugins.toFile()).build().disabledReason());
}

@Test
void environment_eitherVariableAloneTurnsReportingOffWhateverTheOtherSays() {
// Arrange: one variable opts out, the other says something that is
// not an opt-out. Neither can cancel the other.
String[][] pairs = {
{"on", "1"},
{"true", " Yes "},
{"off", "0"},
{"No", "false"},
};

for (String[] pair : pairs) {
environment.clear();
environment.put("TRACE_USAGE_REPORTING", pair[0]);
environment.put("DO_NOT_TRACK", pair[1]);

// Act
TraceClient client = TraceClient.builder(baseUrl(), "MyPlugin", "1.2.3").key("k").build();

// Assert
assertEquals(TraceClient.REASON_ENVIRONMENT, client.disabledReason(),
"TRACE_USAGE_REPORTING=" + pair[0] + ", DO_NOT_TRACK=" + pair[1]);
assertFalse(client.isEnabled());
}
}

@Test
void environment_valuesThatAreNotAnOptOutLeaveReportingOn() {
// Only the listed spellings opt out: a blank value, the opposite
// word, or anything else is not read as a wish to stop reporting.
for (String value : new String[] {"", " ", "on", "true", "yes", "1", "disabled", "offline"}) {
// Arrange
environment.clear();
environment.put("TRACE_USAGE_REPORTING", value);

// Act
TraceClient client = TraceClient.builder(baseUrl(), "MyPlugin", "1.2.3").key("k").build();

// Assert
assertNull(client.disabledReason(), "TRACE_USAGE_REPORTING=\"" + value + "\"");
client.close();
}
for (String value : new String[] {"", " ", "0", "false", "no", "off", "2", "y"}) {
// Arrange
environment.clear();
environment.put("DO_NOT_TRACK", value);

// Act
TraceClient client = TraceClient.builder(baseUrl(), "MyPlugin", "1.2.3").key("k").build();

// Assert
assertNull(client.disabledReason(), "DO_NOT_TRACK=\"" + value + "\"");
client.close();
}
}

@Test
void disabled_isOffForTheConfigReasonUnlessTheEnvironmentSaysSoFirst() {
// Act
TraceClient byDefault = TraceClient.disabled();

// Assert
assertFalse(byDefault.isEnabled());
assertEquals(TraceClient.REASON_CONFIG, byDefault.disabledReason());
assertNull(byDefault.installId(), "a disabled client has no install ID");

// Arrange: the environment is checked before the client's own switch,
// so it is the reason even here.
environment.put("DO_NOT_TRACK", "1");

// Act
TraceClient byEnvironment = TraceClient.disabled();

// Assert
assertFalse(byEnvironment.isEnabled());
assertEquals(TraceClient.REASON_ENVIRONMENT, byEnvironment.disabledReason());
byDefault.close();
byEnvironment.close();
}

@Test
void disabledReason_followsThePrecedenceEnvironmentThenServerWideThenConfigThenKey(@TempDir Path plugins) throws Exception {
// Arrange: everything says off at once.
Expand Down
Loading