1- package io .opentelemetry .contrib .jmxscraper ;
1+ /*
2+ * Copyright The OpenTelemetry Authors
3+ * SPDX-License-Identifier: Apache-2.0
4+ */
5+
6+ package io .opentelemetry .contrib .jmxscraper .client ;
27
38import static org .assertj .core .api .Assertions .assertThat ;
49
10+ import io .opentelemetry .contrib .jmxscraper .TestApp ;
511import java .io .Closeable ;
612import java .io .IOException ;
713import java .nio .charset .StandardCharsets ;
@@ -53,12 +59,9 @@ static void afterAll() {
5359 }
5460 }
5561
56-
5762 @ Test
5863 void noAuth () {
59- try (AppContainer app = new AppContainer ()
60- .withJmxPort (9990 )
61- .start ()) {
64+ try (AppContainer app = new AppContainer ().withJmxPort (9990 ).start ()) {
6265 testConnector (() -> JmxRemoteClient .createNew (app .getHost (), app .getPort ()).connect ());
6366 }
6467 }
@@ -67,15 +70,13 @@ void noAuth() {
6770 void loginPwdAuth () {
6871 String login = "user" ;
6972 String pwd = "t0p!Secret" ;
70- try (AppContainer app = new AppContainer ()
71- .withJmxPort (9999 )
72- .withUserAuth (login , pwd )
73- .start ()) {
74- testConnector (() -> JmxRemoteClient .createNew (app .getHost (), app .getPort ())
75- .userCredentials (login , pwd )
76- .connect ());
73+ try (AppContainer app = new AppContainer ().withJmxPort (9999 ).withUserAuth (login , pwd ).start ()) {
74+ testConnector (
75+ () ->
76+ JmxRemoteClient .createNew (app .getHost (), app .getPort ())
77+ .userCredentials (login , pwd )
78+ .connect ());
7779 }
78-
7980 }
8081
8182 @ Test
@@ -95,20 +96,20 @@ private static void testConnector(ConnectorSupplier connectorSupplier) {
9596 try (JMXConnector connector = connectorSupplier .get ()) {
9697 assertThat (connector .getMBeanServerConnection ())
9798 .isNotNull ()
98- .satisfies (connection -> {
99- try {
100- ObjectName name = new ObjectName (TestApp .OBJECT_NAME );
101- Object value = connection .getAttribute (name , "IntValue" );
102- assertThat (value ).isEqualTo (42 );
103- } catch (Exception e ) {
104- throw new RuntimeException (e );
105- }
106- });
99+ .satisfies (
100+ connection -> {
101+ try {
102+ ObjectName name = new ObjectName (TestApp .OBJECT_NAME );
103+ Object value = connection .getAttribute (name , "IntValue" );
104+ assertThat (value ).isEqualTo (42 );
105+ } catch (Exception e ) {
106+ throw new RuntimeException (e );
107+ }
108+ });
107109
108110 } catch (IOException e ) {
109111 throw new RuntimeException (e );
110112 }
111-
112113 }
113114
114115 private interface ConnectorSupplier {
@@ -131,19 +132,18 @@ private AppContainer() {
131132 // SSL registry : com.sun.management.jmxremote.registry.ssl
132133 // client side ssl auth: com.sun.management.jmxremote.ssl.need.client.auth
133134
134-
135135 String appJar = System .getProperty ("app.jar.path" );
136- assertThat (Paths .get (appJar ))
137- . isNotEmptyFile ()
138- . isReadable ();
139-
140- this . appContainer = new GenericContainer <>( "openjdk:8u272-jre-slim " )
141- . withCopyFileToContainer ( MountableFile . forHostPath ( appJar ), "/app.jar" )
142- . withLogConsumer ( new Slf4jLogConsumer ( logger ) )
143- . withNetwork ( network )
144- . waitingFor ( Wait .forLogMessage (TestApp .APP_STARTED_MSG + "\\ n" , 1 )
145- .withStartupTimeout (Duration .ofSeconds (5 )))
146- .withCommand ("java" , "-jar" , "/app.jar" );
136+ assertThat (Paths .get (appJar )). isNotEmptyFile (). isReadable ();
137+
138+ this . appContainer =
139+ new GenericContainer <>( "openjdk:8u272-jre-slim" )
140+ . withCopyFileToContainer ( MountableFile . forHostPath ( appJar ), "/app.jar " )
141+ . withLogConsumer ( new Slf4jLogConsumer ( logger ) )
142+ . withNetwork ( network )
143+ . waitingFor (
144+ Wait .forLogMessage (TestApp .APP_STARTED_MSG + "\\ n" , 1 )
145+ .withStartupTimeout (Duration .ofSeconds (5 )))
146+ .withCommand ("java" , "-jar" , "/app.jar" );
147147 }
148148
149149 @ CanIgnoreReturnValue
@@ -177,20 +177,19 @@ AppContainer start() {
177177 properties .put ("com.sun.management.jmxremote.access.file" , "/jmx.access" );
178178 }
179179
180- String confArgs = properties .entrySet ()
181- .stream ()
182- .map (e -> {
183- String s = "-D" + e .getKey ();
184- if (!e .getValue ().isEmpty ()) {
185- s += "=" + e .getValue ();
186- }
187- return s ;
188- })
189- .collect (Collectors .joining (" " ));
190-
191- appContainer
192- .withEnv ("JAVA_TOOL_OPTIONS" , confArgs )
193- .start ();
180+ String confArgs =
181+ properties .entrySet ().stream ()
182+ .map (
183+ e -> {
184+ String s = "-D" + e .getKey ();
185+ if (!e .getValue ().isEmpty ()) {
186+ s += "=" + e .getValue ();
187+ }
188+ return s ;
189+ })
190+ .collect (Collectors .joining (" " ));
191+
192+ appContainer .withEnv ("JAVA_TOOL_OPTIONS" , confArgs ).start ();
194193
195194 logger .info ("Test application JMX port mapped to {}:{}" , getHost (), getPort ());
196195
@@ -238,5 +237,4 @@ private static void writeLine(Path path, String line) throws IOException {
238237 Files .write (path , line .getBytes (StandardCharsets .UTF_8 ));
239238 }
240239 }
241-
242240}
0 commit comments