Skip to content

Commit 2c16a92

Browse files
committed
fix tests
1 parent 30094f5 commit 2c16a92

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

jmx-scraper/src/main/java/io/opentelemetry/contrib/jmxscraper/config/JmxScraperConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ public static JmxScraperConfig fromProperties(
169169
public void propagateSystemProperties() {
170170
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
171171

172-
String key = (String) entry.getKey();
173-
String value = (String) entry.getValue();
172+
String key = entry.getKey().toString();
173+
String value = entry.getValue().toString();
174174
if (key.startsWith("otel.")
175175
|| key.startsWith("javax.net.ssl.keyStore")
176176
|| key.startsWith("javax.net.ssl.trustStore")) {

jmx-scraper/src/test/java/io/opentelemetry/contrib/jmxscraper/config/JmxScraperConfigTest.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1010

1111
import java.util.Properties;
12+
import org.junit.jupiter.api.AfterEach;
1213
import org.junit.jupiter.api.BeforeAll;
1314
import org.junit.jupiter.api.Test;
1415
import org.junitpioneer.jupiter.ClearSystemProperty;
15-
import org.junitpioneer.jupiter.SetSystemProperty;
1616

1717
class JmxScraperConfigTest {
1818
private static Properties validProperties;
@@ -34,6 +34,18 @@ static void setUp() {
3434
validProperties.setProperty(JmxScraperConfig.JMX_REALM, "some-realm");
3535
}
3636

37+
@AfterEach
38+
void afterEach() {
39+
// make sure that no test leaked in global system properties
40+
Properties systemProperties = System.getProperties();
41+
for (Object k : systemProperties.keySet()) {
42+
String key = k.toString();
43+
if (key.startsWith("otel.") || key.startsWith("javax.net.ssl.")) {
44+
System.clearProperty(key);
45+
}
46+
}
47+
}
48+
3749
@Test
3850
void shouldCreateMinimalValidConfiguration() throws ConfigurationException {
3951
// Given
@@ -77,6 +89,10 @@ void shouldUseValuesFromProperties() throws ConfigurationException {
7789
properties.setProperty("javax.net.ssl.trustStorePassword", "def456");
7890
properties.setProperty("javax.net.ssl.trustStoreType", "JKS");
7991

92+
assertThat(System.getProperty("javax.net.ssl.keyStore"))
93+
.describedAs("keystore config should not be set")
94+
.isNull();
95+
8096
// When
8197
JmxScraperConfig config = JmxScraperConfig.fromProperties(properties, new Properties());
8298
config.propagateSystemProperties();
@@ -105,16 +121,24 @@ void shouldUseValuesFromProperties() throws ConfigurationException {
105121
}
106122

107123
@Test
108-
@SetSystemProperty(key = "otel.jmx.service.url", value = "originalServiceUrl")
109-
@SetSystemProperty(key = "javax.net.ssl.keyStorePassword", value = "originalPassword")
124+
@ClearSystemProperty(key = "otel.jmx.service.url")
125+
@ClearSystemProperty(key = "javax.net.ssl.keyStorePassword")
110126
void shouldRetainPredefinedSystemProperties() throws ConfigurationException {
111127
// Given
112-
// Properties to be propagated to system, properties
128+
// user properties to be propagated to system properties
113129
Properties properties = (Properties) validProperties.clone();
114130
properties.setProperty("javax.net.ssl.keyStorePassword", "abc123");
115131

132+
// system properties
133+
Properties systemProperties = new Properties();
134+
systemProperties.put("otel.jmx.service.url", "originalServiceUrl");
135+
systemProperties.put("javax.net.ssl.keyStorePassword", "originalPassword");
136+
116137
// When
117-
JmxScraperConfig.fromProperties(properties, new Properties());
138+
JmxScraperConfig config = JmxScraperConfig.fromProperties(properties, systemProperties);
139+
// even when effective configuration is propagated to system properties original values are kept
140+
// due to priority of system properties over user-provided ones.
141+
config.propagateSystemProperties();
118142

119143
// Then
120144
assertThat(System.getProperty("otel.jmx.service.url")).isEqualTo("originalServiceUrl");

0 commit comments

Comments
 (0)