From b55b1862074774cde8680e398597988dc74353aa Mon Sep 17 00:00:00 2001 From: brunobat Date: Fri, 23 Jun 2023 11:08:29 +0100 Subject: [PATCH] Env vars support on build analytics. Rename config files Local config -> disabled:false --- docs/src/main/asciidoc/build-analytics.adoc | 8 +- .../tools/analytics-common/pom.xml | 6 + .../quarkus/analytics/AnalyticsService.java | 3 +- .../io/quarkus/analytics/ConfigService.java | 13 +- .../analytics/config/FileLocationsImpl.java | 6 +- .../dto/config/AnalyticsLocalConfig.java | 2 +- .../analytics/dto/config/LocalConfig.java | 14 +-- .../io/quarkus/analytics/rest/RestClient.java | 5 +- .../quarkus/analytics/util/PropertyUtils.java | 71 +++++++++++ .../analytics/AnalyticsServicePromptTest.java | 4 +- .../analytics/ConfigServiceManualTest.java | 113 ------------------ .../quarkus/analytics/ConfigServiceTest.java | 6 +- .../config/TestFileLocationsImpl.java | 8 +- .../analytics/util/PropertyUtilsTest.java | 87 ++++++++++++++ .../tools/devtools-common/pom.xml | 2 +- independent-projects/tools/pom.xml | 1 + 16 files changed, 203 insertions(+), 146 deletions(-) create mode 100644 independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/util/PropertyUtils.java delete mode 100644 independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/ConfigServiceManualTest.java create mode 100644 independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/util/PropertyUtilsTest.java diff --git a/docs/src/main/asciidoc/build-analytics.adoc b/docs/src/main/asciidoc/build-analytics.adoc index 6f1c211cde0b6..46b3a92a125fb 100644 --- a/docs/src/main/asciidoc/build-analytics.adoc +++ b/docs/src/main/asciidoc/build-analytics.adoc @@ -130,9 +130,9 @@ Analytics configurations are stored at the `.redhat` folder in the user's home d `anonymousId` is the locally generated anonymous user id. -`com.redhat.devtools.quarkus.localconfig` holds if the user has agreed to send analytics data or not. +`io.quarkus.analytics.localconfig` holds if the user has agreed to send analytics data or not. -`com.redhat.devtools.quarkus.remoteconfig` cache of the remote configuration that allows to disable analytics globally. +`io.quarkus.analytics.remoteconfig` cache of the remote configuration that allows to disable analytics globally. == Show me the code! @@ -146,10 +146,10 @@ The uploaded events are stored under the project's `/target` folder with the nam If you have opted in and would like to disable build time analytics, you can do so in two ways: -* Globally, by editing the `com.redhat.devtools.quarkus.localconfig` file in the '.redhat' folder of your user’s home directory. Update the file as follows: +* Globally, by editing the `io.quarkus.analytics.localconfig` file in the '.redhat' folder of your user’s home directory. Update the file as follows: [source,json] ---- -{"active":false} +{"disabled":true} ---- * Per project, by using the system property `quarkus.analytics.disabled=true` when building the project. diff --git a/independent-projects/tools/analytics-common/pom.xml b/independent-projects/tools/analytics-common/pom.xml index 95284b02278c4..578c18425be74 100644 --- a/independent-projects/tools/analytics-common/pom.xml +++ b/independent-projects/tools/analytics-common/pom.xml @@ -61,6 +61,12 @@ mockito-core test + + uk.org.webcompere + system-stubs-jupiter + ${system-stubs-jupiter.version} + test + \ No newline at end of file diff --git a/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/AnalyticsService.java b/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/AnalyticsService.java index a79effea6b077..ced6f2827e41b 100644 --- a/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/AnalyticsService.java +++ b/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/AnalyticsService.java @@ -60,6 +60,7 @@ import io.quarkus.analytics.dto.segment.TrackProperties; import io.quarkus.analytics.rest.RestClient; import io.quarkus.analytics.util.FileUtils; +import io.quarkus.analytics.util.PropertyUtils; import io.quarkus.bootstrap.model.ApplicationModel; import io.quarkus.devtools.messagewriter.MessageWriter; import io.quarkus.maven.dependency.ArtifactCoords; @@ -122,7 +123,7 @@ public void close() { try { // complete all. Normally, the queue should have only 1 element. CompletableFuture.allOf(postFutures.toArray(new CompletableFuture[0])).get( - Integer.getInteger("quarkus.analytics.timeout", DEFAULT_TIMEOUT), + PropertyUtils.getProperty("quarkus.analytics.timeout", DEFAULT_TIMEOUT), TimeUnit.MILLISECONDS); if (log.isDebugEnabled() && !postFutures.isEmpty()) { log.debug("[Quarkus build analytics] Build analytics sent successfully. Sent event can be seen at .../target/" + diff --git a/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/ConfigService.java b/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/ConfigService.java index 6f8034bc28139..38aad6455ceb0 100644 --- a/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/ConfigService.java +++ b/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/ConfigService.java @@ -1,5 +1,7 @@ package io.quarkus.analytics; +import static io.quarkus.analytics.util.PropertyUtils.getProperty; + import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -74,8 +76,8 @@ public ConfigService(final ConfigClient client, final AnonymousUserId userId, fi } public void userAcceptance(Function analyticsEnabledSupplier) { - final int timeout = Integer.getInteger(QUARKUS_ANALYTICS_PROMPT_TIMEOUT, 10); - if (Files.exists(localConfigFile) || Boolean.getBoolean(QUARKUS_ANALYTICS_DISABLED_LOCAL_PROP)) { + final int timeout = getProperty(QUARKUS_ANALYTICS_PROMPT_TIMEOUT, 10); + if (Files.exists(localConfigFile) || getProperty(QUARKUS_ANALYTICS_DISABLED_LOCAL_PROP, false)) { return; // ask nothing } else { try { @@ -89,7 +91,8 @@ public void userAcceptance(Function analyticsEnabledSupplier) { } final boolean isActive = userInput.equals("y") || userInput.equals("yes") || userInput.startsWith("yy"); FileUtils.createFileAndParent(localConfigFile); - FileUtils.write(new LocalConfig(isActive), localConfigFile); + final boolean isDisabled = !isActive;// just to make it explicit + FileUtils.write(new LocalConfig(isDisabled), localConfigFile); log.info("[Quarkus build analytics] Quarkus Build Analytics " + (isActive ? "enabled" : "disabled") + " by the user." + NEW_LINE); } catch (TimeoutException e) { @@ -122,12 +125,12 @@ public boolean isActive() { if (!Files.exists(localConfigFile)) { return false; // disabled because user has not decided yet } else if (!loadConfig(LocalConfig.class, localConfigFile) - .map(LocalConfig::isActive) + .map(localConfig -> !localConfig.isDisabled()) .orElse(true)) { return false; // disabled by the user and recorded on the local config } - if (Boolean.getBoolean(QUARKUS_ANALYTICS_DISABLED_LOCAL_PROP)) { + if (getProperty(QUARKUS_ANALYTICS_DISABLED_LOCAL_PROP, false)) { return false; // disabled by local property } AnalyticsRemoteConfig analyticsRemoteConfig = getRemoteConfig(); diff --git a/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/config/FileLocationsImpl.java b/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/config/FileLocationsImpl.java index 7f99f36dbac97..b9526b9447698 100644 --- a/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/config/FileLocationsImpl.java +++ b/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/config/FileLocationsImpl.java @@ -15,10 +15,10 @@ public class FileLocationsImpl implements FileLocations { ".redhat"); private static final Path UUID_FILE = RED_HAT.resolve("anonymousId"); - private static final Path REMOTE_CONFIG_FILE = RED_HAT.resolve("com.redhat.devtools.quarkus.remoteconfig"); + private static final Path REMOTE_CONFIG_FILE = RED_HAT.resolve("io.quarkus.analytics.remoteconfig"); private static final Path LAST_REMOTE_CONFIG_TRY_FILE = RED_HAT.resolve( - "com.redhat.devtools.quarkus.analytics.lasttry"); - private static final Path LOCAL_CONFIG_FILE = RED_HAT.resolve("com.redhat.devtools.quarkus.localconfig"); + "io.quarkus.analytics.lasttry"); + private static final Path LOCAL_CONFIG_FILE = RED_HAT.resolve("io.quarkus.analytics.localconfig"); private static final String BUILD_ANALYTICS_EVENT_FILE_NAME = "build-analytics-event.json"; // singleton diff --git a/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/dto/config/AnalyticsLocalConfig.java b/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/dto/config/AnalyticsLocalConfig.java index 339ba6a002839..f36c6d2052c37 100644 --- a/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/dto/config/AnalyticsLocalConfig.java +++ b/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/dto/config/AnalyticsLocalConfig.java @@ -5,5 +5,5 @@ public interface AnalyticsLocalConfig { * @return true if the analytics is enabled * @return */ - boolean isActive(); + boolean isDisabled(); } diff --git a/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/dto/config/LocalConfig.java b/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/dto/config/LocalConfig.java index 914762a574e2c..b308289474c59 100644 --- a/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/dto/config/LocalConfig.java +++ b/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/dto/config/LocalConfig.java @@ -3,21 +3,21 @@ import java.io.Serializable; public class LocalConfig implements AnalyticsLocalConfig, Serializable { - private boolean active; + private boolean disabled; - public LocalConfig(boolean active) { - this.active = active; + public LocalConfig(boolean disabled) { + this.disabled = disabled; } public LocalConfig() { } @Override - public boolean isActive() { - return active; + public boolean isDisabled() { + return disabled; } - public void setActive(boolean active) { - this.active = active; + public void setDisabled(boolean disabled) { + this.disabled = disabled; } } diff --git a/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/rest/RestClient.java b/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/rest/RestClient.java index 22e187c4192a0..d1c166476e7cd 100644 --- a/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/rest/RestClient.java +++ b/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/rest/RestClient.java @@ -1,5 +1,6 @@ package io.quarkus.analytics.rest; +import static io.quarkus.analytics.util.PropertyUtils.getProperty; import static io.quarkus.analytics.util.StringUtils.getObjectMapper; import static java.util.concurrent.TimeUnit.MILLISECONDS; @@ -64,11 +65,11 @@ static String getAuthHeader(final String key) { private final URI segmentTraceUri; - private final int timeoutMs = Integer.getInteger("quarkus.analytics.timeout", DEFAULT_TIMEOUT); + private final int timeoutMs = getProperty("quarkus.analytics.timeout", DEFAULT_TIMEOUT); public RestClient(MessageWriter log) { this.log = log; - final String segmentBaseUri = System.getProperty("quarkus.analytics.uri.base", "https://api.segment.io/"); + final String segmentBaseUri = getProperty("quarkus.analytics.uri.base", "https://api.segment.io/"); this.segmentIdentityUri = getUri(segmentBaseUri + IDENTITY_ENDPOINT); this.segmentTraceUri = getUri(segmentBaseUri + TRACK_ENDPOINT); } diff --git a/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/util/PropertyUtils.java b/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/util/PropertyUtils.java new file mode 100644 index 0000000000000..dc698be0644bf --- /dev/null +++ b/independent-projects/tools/analytics-common/src/main/java/io/quarkus/analytics/util/PropertyUtils.java @@ -0,0 +1,71 @@ +package io.quarkus.analytics.util; + +public class PropertyUtils { + + public static Integer getProperty(String propertyName, int defaultValue) { + if (propertyName == null) { + throw new IllegalArgumentException("Property name cannot be null"); + } + Integer result = Integer.getInteger(propertyName); + try { + if (result == null) { + String stringValue = System.getenv(transformToEnvVarName(propertyName)); + if (stringValue != null) { + result = Integer.parseInt(stringValue); + } else { + result = defaultValue; + } + } + } catch (NumberFormatException e) { + result = defaultValue; + } + return result; + } + + public static String getProperty(String propertyName, String defaultValue) { + if (propertyName == null) { + throw new IllegalArgumentException("Property name cannot be null"); + } + String result = System.getProperty(propertyName); + try { + if (result == null) { + String stringValue = System.getenv(transformToEnvVarName(propertyName)); + if (stringValue != null) { + result = stringValue; + } else { + result = defaultValue; + } + } + } catch (NumberFormatException e) { + result = defaultValue; + } + return result; + } + + public static boolean getProperty(String propertyName, boolean defaultValue) { + if (propertyName == null) { + throw new IllegalArgumentException("Property name cannot be null"); + } + boolean result; + String systemValue = System.getProperty(propertyName); + try { + if (systemValue == null) { + String envValue = System.getenv(transformToEnvVarName(propertyName)); + if (envValue != null) { + result = Boolean.parseBoolean(envValue); + } else { + result = defaultValue; + } + } else { + result = Boolean.parseBoolean(systemValue); + } + } catch (NumberFormatException e) { + result = defaultValue; + } + return result; + } + + private static String transformToEnvVarName(String propertyName) { + return propertyName.toUpperCase().replace('.', '_'); + } +} diff --git a/independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/AnalyticsServicePromptTest.java b/independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/AnalyticsServicePromptTest.java index 6499c898fe865..07d3df954c868 100644 --- a/independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/AnalyticsServicePromptTest.java +++ b/independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/AnalyticsServicePromptTest.java @@ -47,7 +47,7 @@ void testConsoleQuestion_yes() throws IOException { Optional localConfig = FileUtils.read(LocalConfig.class, fileLocations.getLocalConfigFile(), MessageWriter.info()); assertTrue(localConfig.isPresent()); - assertTrue(localConfig.get().isActive()); + assertFalse(localConfig.get().isDisabled()); } @Test @@ -61,7 +61,7 @@ void testConsoleQuestion_no() throws IOException { Optional localConfig = FileUtils.read(LocalConfig.class, fileLocations.getLocalConfigFile(), MessageWriter.info()); assertTrue(localConfig.isPresent()); - assertFalse(localConfig.get().isActive()); + assertTrue(localConfig.get().isDisabled()); } @Test diff --git a/independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/ConfigServiceManualTest.java b/independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/ConfigServiceManualTest.java deleted file mode 100644 index 183b659c79728..0000000000000 --- a/independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/ConfigServiceManualTest.java +++ /dev/null @@ -1,113 +0,0 @@ -package io.quarkus.analytics; - -import static io.quarkus.analytics.common.TestFilesUtils.backupExisting; -import static io.quarkus.analytics.common.TestFilesUtils.restore; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.time.Duration; -import java.util.Collections; - -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; - -import io.quarkus.analytics.common.TestRestClient; -import io.quarkus.analytics.config.FileLocations; -import io.quarkus.analytics.config.FileLocationsImpl; -import io.quarkus.analytics.dto.config.NoopRemoteConfig; -import io.quarkus.analytics.dto.config.RemoteConfig; -import io.quarkus.analytics.util.FileUtils; -import io.quarkus.devtools.messagewriter.MessageWriter; - -@Disabled("For manual testing only") -class ConfigServiceManualTest { - - private final FileLocations fileLocations = FileLocationsImpl.INSTANCE; - - @Test - void activeWithConfig() throws IOException { - backupExisting(getBackupConfigFile(), - fileLocations.getRemoteConfigFile()); - - RemoteConfig remoteConfig = RemoteConfig.builder() - .active(true) - .denyQuarkusVersions(Collections.emptyList()) - .denyUserIds(Collections.emptyList()) - .refreshInterval(Duration.ofHours(12)).build(); - - FileUtils.write(remoteConfig, fileLocations.getRemoteConfigFile()); - long lastModified = fileLocations.getRemoteConfigFile().toFile().lastModified(); - - ConfigService configService = new ConfigService(new TestRestClient(remoteConfig), - AnonymousUserId.getInstance(fileLocations, MessageWriter.info()), - fileLocations, - MessageWriter.info()); - assertNotNull(configService); - assertTrue(configService.isActive()); - assertEquals(lastModified, fileLocations.getRemoteConfigFile().toFile().lastModified(), "File must not change"); - - restore(getBackupConfigFile(), - fileLocations.getRemoteConfigFile()); - } - - @Test - void activeWithoutConfig() throws IOException { - backupExisting(getBackupConfigFile(), - fileLocations.getRemoteConfigFile()); - - RemoteConfig remoteConfig = RemoteConfig.builder() - .active(true) - .denyQuarkusVersions(Collections.emptyList()) - .denyUserIds(Collections.emptyList()) - .refreshInterval(Duration.ZERO).build(); - - assertFalse(Files.exists(fileLocations.getRemoteConfigFile())); - - ConfigService configService = new ConfigService(new TestRestClient(remoteConfig), - AnonymousUserId.getInstance(fileLocations, MessageWriter.info()), - fileLocations, - MessageWriter.info()); - assertNotNull(configService); - assertTrue(configService.isActive()); - - assertTrue(Files.exists(fileLocations.getRemoteConfigFile())); - - restore(getBackupConfigFile(), - fileLocations.getRemoteConfigFile()); - } - - @Test - void remoteConfigOff() throws IOException { - backupExisting(getBackupConfigFile(), - fileLocations.getRemoteConfigFile()); - - ConfigService configService = new ConfigService(new TestRestClient(NoopRemoteConfig.INSTANCE), - AnonymousUserId.getInstance(fileLocations, MessageWriter.info()), - fileLocations, - MessageWriter.info()); - assertNotNull(configService); - assertFalse(configService.isActive()); - - restore(getBackupConfigFile(), - fileLocations.getRemoteConfigFile()); - } - - @Test - void isArtifactActive() { - - } - - @Test - void isArtifactInactive() { - - } - - private Path getBackupConfigFile() { - return fileLocations.getFolder().resolve("com.redhat.devtools.quarkus.analytics.back"); - } -} diff --git a/independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/ConfigServiceTest.java b/independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/ConfigServiceTest.java index 8bf72b469bdf4..5eadff2a19057 100644 --- a/independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/ConfigServiceTest.java +++ b/independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/ConfigServiceTest.java @@ -40,7 +40,7 @@ void tearDown() throws IOException { } @Test - void activeWithConfig() throws IOException { + void activeWithRemoteConfig() throws IOException { ConfigService configService = createConfigService(); long lastModified = fileLocations.getRemoteConfigFile().toFile().lastModified(); @@ -63,7 +63,7 @@ void inactiveNoQuestionAsked() throws IOException { @Test void inactiveUserAnsweredNo() throws IOException { deleteLocalConfigFile(); - FileUtils.append("{\"active\":false}", fileLocations.getLocalConfigFile()); + FileUtils.append("{\"disabled\":true}", fileLocations.getLocalConfigFile()); ConfigService configService = createConfigService(); @@ -72,7 +72,7 @@ void inactiveUserAnsweredNo() throws IOException { } @Test - void activeWithoutConfig() throws IOException { + void activeConfig() throws IOException { RemoteConfig remoteConfig = RemoteConfig.builder() .active(true) .denyQuarkusVersions(Collections.emptyList()) diff --git a/independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/config/TestFileLocationsImpl.java b/independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/config/TestFileLocationsImpl.java index 8a7d794535552..713316823adfe 100644 --- a/independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/config/TestFileLocationsImpl.java +++ b/independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/config/TestFileLocationsImpl.java @@ -20,12 +20,12 @@ public TestFileLocationsImpl() throws IOException { public TestFileLocationsImpl(final boolean skipLocal) throws IOException { tempDir = Files.createTempDirectory("temp_test_" + UUID.randomUUID().toString()); uuidFile = tempDir.resolve("anonymousId"); - remoteConfigFile = tempDir.resolve("com.redhat.devtools.quarkus.remoteconfig"); - lastTryFile = tempDir.resolve("com.redhat.devtools.quarkus.analytics.lasttry"); - localConfigFile = tempDir.resolve("com.redhat.devtools.quarkus.localconfig"); + remoteConfigFile = tempDir.resolve("io.quarkus.analytics.remoteconfig"); + lastTryFile = tempDir.resolve("io.quarkus.analytics.lasttry"); + localConfigFile = tempDir.resolve("io.quarkus.analytics.localconfig"); if (!skipLocal) { Files.createFile(localConfigFile); - Files.write(localConfigFile, "{\"active\":true}".getBytes()); + Files.write(localConfigFile, "{\"disabled\":false}".getBytes()); } } diff --git a/independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/util/PropertyUtilsTest.java b/independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/util/PropertyUtilsTest.java new file mode 100644 index 0000000000000..44f37b8613ed9 --- /dev/null +++ b/independent-projects/tools/analytics-common/src/test/java/io/quarkus/analytics/util/PropertyUtilsTest.java @@ -0,0 +1,87 @@ +package io.quarkus.analytics.util; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.params.provider.Arguments.arguments; + +import java.util.stream.Stream; + +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import uk.org.webcompere.systemstubs.environment.EnvironmentVariables; +import uk.org.webcompere.systemstubs.jupiter.SystemStub; +import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; + +@ExtendWith(SystemStubsExtension.class) +class PropertyUtilsTest { + + @SystemStub + private EnvironmentVariables environmentVariables; + + private static Stream getIntTest() { + return Stream.of( + arguments("3", null, 1, 3), + arguments(null, "4", 1, 4), + arguments("5", "6", 1, 6), + arguments(null, null, 1, 1)); + } + + private static Stream getStringProperty() { + return Stream.of( + arguments("3", null, "1", "3"), + arguments(null, "4", "1", "4"), + arguments("5", "6", "1", "6"), + arguments(null, null, "1", "1")); + } + + private static Stream getBooleanProperty() { + return Stream.of( + arguments("true", null, "false", "true"), + arguments(null, "true", "false", "true"), + arguments("true", "false", "false", "false"), + arguments("false", "true", "false", "true"), + arguments(null, null, "true", "true")); + } + + @ParameterizedTest + @MethodSource + void getIntTest(String env, String sys, int defaultValue, int expected) { + if (env != null) { + environmentVariables.set("TEST_ENV", env); + } + if (sys != null) { + System.setProperty("test.env", sys); + } + assertEquals(expected, PropertyUtils.getProperty("test.env", defaultValue)); + System.clearProperty("test.env"); + } + + @ParameterizedTest + @MethodSource + void getStringProperty(String env, String sys, String defaultValue, String expected) { + if (env != null) { + environmentVariables.set("TEST_ENV", env); + } + if (sys != null) { + System.setProperty("test.env", sys); + } + assertEquals(expected, PropertyUtils.getProperty("test.env", defaultValue)); + System.clearProperty("test.env"); + } + + @ParameterizedTest + @MethodSource + void getBooleanProperty(String env, String sys, boolean defaultValue, boolean expected) { + if (env != null) { + environmentVariables.set("TEST_ENV", env); + } + if (sys != null) { + System.setProperty("test.env", sys); + } + assertEquals(expected, PropertyUtils.getProperty("test.env", defaultValue)); + System.clearProperty("test.env"); + } + +} diff --git a/independent-projects/tools/devtools-common/pom.xml b/independent-projects/tools/devtools-common/pom.xml index 8f52c0ee0821e..a8dfd837041f7 100644 --- a/independent-projects/tools/devtools-common/pom.xml +++ b/independent-projects/tools/devtools-common/pom.xml @@ -120,7 +120,7 @@ uk.org.webcompere system-stubs-jupiter - 2.0.1 + ${system-stubs-jupiter.version} test diff --git a/independent-projects/tools/pom.xml b/independent-projects/tools/pom.xml index b6410681d7239..daf73b7eb1149 100644 --- a/independent-projects/tools/pom.xml +++ b/independent-projects/tools/pom.xml @@ -66,6 +66,7 @@ ${project.version} 21 3.1.2 + 2.0.2 registry-client