From e2356000ba8b2816a52bb2b6096d25182410e192 Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Thu, 8 Dec 2022 16:28:49 +0100 Subject: [PATCH] add missing test --- instrumentation-api/build.gradle.kts | 7 ++ .../internal/ConfigPropertiesUtilTest.java | 75 +++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/internal/ConfigPropertiesUtilTest.java diff --git a/instrumentation-api/build.gradle.kts b/instrumentation-api/build.gradle.kts index aca99db42278..1f846f7ef4db 100644 --- a/instrumentation-api/build.gradle.kts +++ b/instrumentation-api/build.gradle.kts @@ -20,6 +20,7 @@ dependencies { testImplementation(project(":testing-common")) testImplementation("io.opentelemetry:opentelemetry-sdk-metrics") testImplementation("io.opentelemetry:opentelemetry-sdk-testing") + testImplementation("org.junit-pioneer:junit-pioneer") jmhImplementation(project(":instrumentation-api-semconv")) } @@ -35,4 +36,10 @@ tasks { isEnabled.set(false) } } + + withType().configureEach { + // required on jdk17 + jvmArgs("--add-opens=java.base/java.util=ALL-UNNAMED") + jvmArgs("-XX:+IgnoreUnrecognizedVMOptions") + } } diff --git a/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/internal/ConfigPropertiesUtilTest.java b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/internal/ConfigPropertiesUtilTest.java new file mode 100644 index 000000000000..8a496322b6e0 --- /dev/null +++ b/instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/internal/ConfigPropertiesUtilTest.java @@ -0,0 +1,75 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.api.internal; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.junitpioneer.jupiter.SetEnvironmentVariable; +import org.junitpioneer.jupiter.SetSystemProperty; + +class ConfigPropertiesUtilTest { + + @SetEnvironmentVariable(key = "TEST_PROPERTY_STRING", value = "env") + @SetSystemProperty(key = "test.property.string", value = "sys") + @Test + void getString_systemProperty() { + assertThat(ConfigPropertiesUtil.getString("test.property.string")).isEqualTo("sys"); + } + + @SetEnvironmentVariable(key = "TEST_PROPERTY_STRING", value = "env") + @Test + void getString_environmentVariable() { + assertThat(ConfigPropertiesUtil.getString("test.property.string")).isEqualTo("env"); + } + + @Test + void getString_none() { + assertThat(ConfigPropertiesUtil.getString("test.property.string")).isNull(); + } + + @SetEnvironmentVariable(key = "TEST_PROPERTY_INT", value = "12") + @SetSystemProperty(key = "test.property.int", value = "42") + @Test + void getInt_systemProperty() { + assertThat(ConfigPropertiesUtil.getInt("test.property.int", -1)).isEqualTo(42); + } + + @SetEnvironmentVariable(key = "TEST_PROPERTY_INT", value = "12") + @Test + void getInt_environmentVariable() { + assertThat(ConfigPropertiesUtil.getInt("test.property.int", -1)).isEqualTo(12); + } + + @Test + void getInt_none() { + assertThat(ConfigPropertiesUtil.getInt("test.property.int", -1)).isEqualTo(-1); + } + + @SetSystemProperty(key = "test.property.int", value = "not a number") + @Test + void getInt_invalidNumber() { + assertThat(ConfigPropertiesUtil.getInt("test.property.int", -1)).isEqualTo(-1); + } + + @SetEnvironmentVariable(key = "TEST_PROPERTY_BOOLEAN", value = "false") + @SetSystemProperty(key = "test.property.boolean", value = "true") + @Test + void getBoolean_systemProperty() { + assertThat(ConfigPropertiesUtil.getBoolean("test.property.boolean", false)).isTrue(); + } + + @SetEnvironmentVariable(key = "TEST_PROPERTY_BOOLEAN", value = "true") + @Test + void getBoolean_environmentVariable() { + assertThat(ConfigPropertiesUtil.getBoolean("test.property.boolean", false)).isTrue(); + } + + @Test + void getBoolean_none() { + assertThat(ConfigPropertiesUtil.getBoolean("test.property.boolean", false)).isFalse(); + } +}