99import static org .assertj .core .api .Assertions .assertThatThrownBy ;
1010
1111import java .util .Properties ;
12+ import org .junit .jupiter .api .AfterEach ;
1213import org .junit .jupiter .api .BeforeAll ;
1314import org .junit .jupiter .api .Test ;
1415import org .junitpioneer .jupiter .ClearSystemProperty ;
15- import org .junitpioneer .jupiter .SetSystemProperty ;
1616
1717class 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