Skip to content

Commit

Permalink
Use SystemStubsExtension for system property unit tests (#1868)
Browse files Browse the repository at this point in the history
Use SystemStubsExtension for system property unit tests

See https://github.com/webcompere/system-stubs & https://www.baeldung.com/java-system-stubs
  • Loading branch information
schlosna authored Mar 6, 2024
1 parent d787bbb commit 9a2bc77
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 77 deletions.
2 changes: 2 additions & 0 deletions tritium-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'org.junit.jupiter:junit-jupiter-api'
testImplementation 'org.mockito:mockito-core'
testImplementation 'uk.org.webcompere:system-stubs-core'
testImplementation 'uk.org.webcompere:system-stubs-jupiter'
testImplementation project(':tritium-test')
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@
import java.util.function.BooleanSupplier;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.properties.SystemProperties;

@ExtendWith(SystemStubsExtension.class)
final class AbstractInvocationEventHandlerTest {

@SystemStub
private SystemProperties systemProperties;

@BeforeEach
void before() {
System.clearProperty("instrument");
systemProperties.remove("instrument");
System.getProperties()
.entrySet()
.removeIf(entry -> entry.getKey().toString().startsWith("instrument"));
Expand All @@ -42,32 +50,32 @@ void testSystemPropertySupplierEnabledByDefault() {

@Test
void testSystemPropertySupplierInstrumentFalse() {
System.setProperty("instrument", "false");
systemProperties.set("instrument", "false");
BooleanSupplier supplier =
AbstractInvocationEventHandler.getSystemPropertySupplier(CompositeInvocationEventHandler.class);
assertThat(supplier.getAsBoolean()).isFalse();
}

@Test
void testSystemPropertySupplierInstrumentTrue() {
System.setProperty("instrument", "true");
systemProperties.set("instrument", "true");
BooleanSupplier supplier =
AbstractInvocationEventHandler.getSystemPropertySupplier(CompositeInvocationEventHandler.class);
assertThat(supplier.getAsBoolean()).isTrue();
}

@Test
void testSystemPropertySupplierInstrumentClassFalse() {
System.setProperty("instrument." + CompositeInvocationEventHandler.class.getName(), "false");
systemProperties.set("instrument." + CompositeInvocationEventHandler.class.getName(), "false");
BooleanSupplier supplier =
AbstractInvocationEventHandler.getSystemPropertySupplier(CompositeInvocationEventHandler.class);
assertThat(supplier.getAsBoolean()).isFalse();
}

@Test
void testSystemPropertySupplierInstrumentClassTrue() {
System.clearProperty("instrument");
System.setProperty("instrument." + CompositeInvocationEventHandler.class.getName(), "true");
systemProperties.remove("instrument");
systemProperties.set("instrument." + CompositeInvocationEventHandler.class.getName(), "true");
BooleanSupplier supplier =
AbstractInvocationEventHandler.getSystemPropertySupplier(CompositeInvocationEventHandler.class);
assertThat(supplier.getAsBoolean()).isTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,22 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.properties.SystemProperties;

@ExtendWith(SystemStubsExtension.class)
final class InstrumentationPropertiesTest {
@SystemStub
private SystemProperties systemProperties;

private ListeningExecutorService executorService;

@BeforeEach
void before() {
executorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
System.clearProperty("instrument");
systemProperties.remove("instrument");
System.getProperties()
.entrySet()
.removeIf(entry -> entry.getKey().toString().startsWith("instrument"));
Expand All @@ -64,29 +71,29 @@ void testSystemPropertySupplierEnabledByDefault() {

@Test
void testSystemPropertySupplierInstrumentFalse() {
System.setProperty("instrument", "false");
systemProperties.set("instrument", "false");
BooleanSupplier supplier = InstrumentationProperties.getSystemPropertySupplier("test");
assertThat(supplier.asBoolean()).isFalse();
}

@Test
void testSystemPropertySupplierInstrumentTrue() {
System.setProperty("instrument", "true");
systemProperties.set("instrument", "true");
BooleanSupplier supplier = InstrumentationProperties.getSystemPropertySupplier("test");
assertThat(supplier.asBoolean()).isTrue();
}

@Test
void testSystemPropertySupplierInstrumentClassFalse() {
System.setProperty("instrument.test", "false");
systemProperties.set("instrument.test", "false");
BooleanSupplier supplier = InstrumentationProperties.getSystemPropertySupplier("test");
assertThat(supplier.asBoolean()).isFalse();
}

@Test
void testSystemPropertySupplierInstrumentClassTrue() {
System.clearProperty("instrument");
System.setProperty("instrument.test", "true");
systemProperties.remove("instrument");
systemProperties.set("instrument.test", "true");
BooleanSupplier supplier = InstrumentationProperties.getSystemPropertySupplier("test");
assertThat(supplier.asBoolean()).isTrue();
}
Expand Down Expand Up @@ -118,12 +125,12 @@ void racingSystemProperties() throws Exception {
},
() -> {
barrier.await();
return "setProperty: " + System.setProperty("instrument.test", "true");
return "setProperty: " + systemProperties.set("instrument.test", "true");
},
() -> {
barrier.await();
for (int i = 0; i < 1000; i++) {
System.setProperty("test" + i, "value" + i);
systemProperties.set("test" + i, "value" + i);
}
return "setProperties: " + System.getProperties();
},
Expand Down Expand Up @@ -194,64 +201,64 @@ void testIsSpecificEnabled_notSet_defaultFalse() {

@Test
void testIsSpecificEnabled_setGarbage() {
System.setProperty("instrument.garbage", "garbage");
systemProperties.set("instrument.garbage", "garbage");
InstrumentationProperties.reload();
assertThat(InstrumentationProperties.isSpecificEnabled("garbage")).isFalse();
}

@Test
void testIsSpecificEnabled_setGarbage_defaultTrue() {
System.setProperty("instrument.garbage", "garbage");
systemProperties.set("instrument.garbage", "garbage");
InstrumentationProperties.reload();
assertThat(InstrumentationProperties.isSpecificEnabled("garbage", true)).isFalse();
}

@Test
void testIsSpecificEnabled_setGarbage_defaultFalse() {
System.setProperty("instrument.garbage", "garbage");
systemProperties.set("instrument.garbage", "garbage");
InstrumentationProperties.reload();
assertThat(InstrumentationProperties.isSpecificEnabled("garbage", false))
.isFalse();
}

@Test
void testIsSpecificEnabled_setTrue() {
System.setProperty("instrument.true", "true");
systemProperties.set("instrument.true", "true");
InstrumentationProperties.reload();
assertThat(InstrumentationProperties.isSpecificEnabled("true")).isTrue();
}

@Test
void testIsSpecificEnabled_setTrue_defaultTrue() {
System.setProperty("instrument.true", "true");
systemProperties.set("instrument.true", "true");
InstrumentationProperties.reload();
assertThat(InstrumentationProperties.isSpecificEnabled("true", true)).isTrue();
}

@Test
void testIsSpecificEnabled_setTrue_defaultFalse() {
System.setProperty("instrument.true", "true");
systemProperties.set("instrument.true", "true");
InstrumentationProperties.reload();
assertThat(InstrumentationProperties.isSpecificEnabled("true", false)).isTrue();
}

@Test
void testIsSpecificEnabled_setFalse() {
System.setProperty("instrument.false", "false");
systemProperties.set("instrument.false", "false");
InstrumentationProperties.reload();
assertThat(InstrumentationProperties.isSpecificEnabled("false")).isFalse();
}

@Test
void testIsSpecificEnabled_setFalse_defaultTrue() {
System.setProperty("instrument.false", "false");
systemProperties.set("instrument.false", "false");
InstrumentationProperties.reload();
assertThat(InstrumentationProperties.isSpecificEnabled("false", true)).isFalse();
}

@Test
void testIsSpecificEnabled_setFalse_defaultFalse() {
System.setProperty("instrument.false", "false");
systemProperties.set("instrument.false", "false");
InstrumentationProperties.reload();
assertThat(InstrumentationProperties.isSpecificEnabled("false", false)).isFalse();
}
Expand Down
2 changes: 2 additions & 0 deletions tritium-lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ dependencies {
testImplementation 'org.mockito:mockito-core'
testImplementation 'org.mockito:mockito-junit-jupiter'
testImplementation 'org.slf4j:slf4j-simple'
testImplementation 'uk.org.webcompere:system-stubs-core'
testImplementation 'uk.org.webcompere:system-stubs-jupiter'
testImplementation project(':tritium-test')
}
26 changes: 14 additions & 12 deletions tritium-lib/src/test/java/com/palantir/tritium/TritiumTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,26 @@
import com.palantir.tritium.test.TestInterface;
import java.util.Map;
import java.util.SortedMap;
import javax.annotation.Nullable;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.impl.SimpleLogger;
import org.slf4j.impl.TestLogs;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.properties.SystemProperties;

@ExtendWith(SystemStubsExtension.class)
@SuppressWarnings("NullAway")
public class TritiumTest {
static {
System.setProperty("org.slf4j.simpleLogger.log.performance", LoggingLevel.TRACE.name());
@SystemStub
private static SystemProperties systemProperties;

@BeforeAll
static void beforeAll() {
systemProperties.set("org.slf4j.simpleLogger.log.performance", LoggingLevel.TRACE.name());
TestLogs.setLevel("performance", LoggingLevel.TRACE.name());
TestLogs.logTo("/dev/null");
}
Expand All @@ -63,21 +72,14 @@ public class TritiumTest {
.putSafeTags("endpoint", "test")
.build();

@Nullable
private String previousLogLevel = null;

@BeforeEach
public void before() {
previousLogLevel = System.setProperty(LOG_KEY, LoggingLevel.TRACE.name());
systemProperties.set(LOG_KEY, LoggingLevel.TRACE.name());
}

@AfterEach
public void after() {
if (previousLogLevel == null) {
System.clearProperty(LOG_KEY);
} else {
System.setProperty(LOG_KEY, previousLogLevel);
}
systemProperties.remove(LOG_KEY);

try (ConsoleReporter reporter =
ConsoleReporter.forRegistry(metricRegistry).build()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,32 @@
import com.palantir.tritium.test.TestImplementation;
import com.palantir.tritium.test.TestInterface;
import java.util.Collections;
import javax.annotation.Nullable;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.impl.SimpleLogger;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.properties.SystemProperties;

@ExtendWith(SystemStubsExtension.class)
public class LoggingInstrumentationTest {
@SystemStub
private SystemProperties systemProperties;

private static final String LOG_KEY = SimpleLogger.LOG_KEY_PREFIX + "com.palantir";

@Nullable
private String previousLogLevel = null;

@BeforeEach
public void before() {
previousLogLevel = System.setProperty(LOG_KEY, LoggingLevel.TRACE.name());
systemProperties.set(LOG_KEY, LoggingLevel.TRACE.name());
}

@AfterEach
public void after() {
if (previousLogLevel == null) {
System.clearProperty(LOG_KEY);
} else {
System.setProperty(LOG_KEY, previousLogLevel);
}
systemProperties.remove(LOG_KEY);
}

@Test
Expand Down Expand Up @@ -93,7 +92,7 @@ public String test() {
assertThat(delegate.invocationCount()).isOne();
}

private static void testLoggingAtLevel(LoggingLevel level) {
private void testLoggingAtLevel(LoggingLevel level) {
TestImplementation delegate = new TestImplementation();
assertThat(level)
.isIn(LoggingLevel.ERROR, LoggingLevel.WARN, LoggingLevel.INFO, LoggingLevel.DEBUG, LoggingLevel.TRACE);
Expand Down Expand Up @@ -135,7 +134,7 @@ private static Logger getLogger() {
return LoggerFactory.getLogger(LoggingInstrumentationTest.class);
}

private static void enableLoggingForLevel(LoggingLevel level) {
System.setProperty(SimpleLogger.LOG_KEY_PREFIX + LoggingInstrumentationTest.class.getName(), level.name());
private void enableLoggingForLevel(LoggingLevel level) {
systemProperties.set(SimpleLogger.LOG_KEY_PREFIX + LoggingInstrumentationTest.class.getName(), level.name());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,15 @@
import org.mockito.stubbing.Answer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.properties.SystemProperties;

@ExtendWith(MockitoExtension.class)
@ExtendWith({MockitoExtension.class, SystemStubsExtension.class})
@SuppressWarnings({"NullAway", "SystemOut", "WeakerAccess"}) // mock injection, dumping metrics to standard out
public abstract class InstrumentationTest {
@SystemStub
private SystemProperties systemProperties;

private static final String EXPECTED_METRIC_NAME = TestInterface.class.getName() + ".test";

Expand All @@ -91,7 +96,7 @@ public abstract class InstrumentationTest {

@BeforeEach
void before() {
System.setProperty("instrument.dynamic-proxy", Boolean.toString(!useByteBuddy()));
systemProperties.set("instrument.dynamic-proxy", Boolean.toString(!useByteBuddy()));
InstrumentationProperties.reload();
}

Expand All @@ -104,7 +109,7 @@ void after() {
}
Tagged.report(reporter, taggedMetricRegistry);
}
System.clearProperty("instrument.dynamic-proxy");
systemProperties.remove("instrument.dynamic-proxy");
InstrumentationProperties.reload();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,19 @@
import com.palantir.tritium.proxy.Instrumentation;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.properties.SystemProperties;

@ExtendWith(SystemStubsExtension.class)
class ByteBuddyInstrumentationPackageAccessTest {
@SystemStub
private SystemProperties systemProperties;

@BeforeEach
void before() {
System.setProperty("instrument.dynamic-proxy", "false");
systemProperties.set("instrument.dynamic-proxy", "false");
InstrumentationProperties.reload();
}

Expand Down
Loading

0 comments on commit 9a2bc77

Please sign in to comment.