Skip to content

Commit 9272bfe

Browse files
committed
minor test refactor
1 parent d23de4f commit 9272bfe

File tree

1 file changed

+17
-28
lines changed
  • instrumentation/jmx-metrics/library/src/test/java/io/opentelemetry/instrumentation/jmx/engine

1 file changed

+17
-28
lines changed

instrumentation/jmx-metrics/library/src/test/java/io/opentelemetry/instrumentation/jmx/engine/RuleParserTest.java

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class RuleParserTest {
3030
@BeforeAll
3131
static void setup() {
3232
parser = RuleParser.get();
33-
assertThat(parser == null).isFalse();
33+
assertThat(parser).isNotNull();
3434
}
3535

3636
/*
@@ -68,9 +68,7 @@ static void setup() {
6868

6969
@Test
7070
void testConf2() {
71-
InputStream is = new ByteArrayInputStream(CONF2.getBytes(StandardCharsets.UTF_8));
72-
JmxConfig config = parser.loadConfig(is);
73-
assertThat(config).isNotNull();
71+
JmxConfig config = parseConf(CONF2);
7472

7573
List<JmxRule> defs = config.getRules();
7674
assertThat(defs).hasSize(2);
@@ -103,9 +101,7 @@ void testConf2() {
103101

104102
@Test
105103
void testConf3() {
106-
InputStream is = new ByteArrayInputStream(CONF3.getBytes(StandardCharsets.UTF_8));
107-
JmxConfig config = parser.loadConfig(is);
108-
assertThat(config).isNotNull();
104+
JmxConfig config = parseConf(CONF3);
109105

110106
List<JmxRule> defs = config.getRules();
111107
assertThat(defs).hasSize(1);
@@ -149,9 +145,7 @@ void testConf3() {
149145

150146
@Test
151147
void testConf4() throws Exception {
152-
InputStream is = new ByteArrayInputStream(CONF4.getBytes(StandardCharsets.UTF_8));
153-
JmxConfig config = parser.loadConfig(is);
154-
assertThat(config).isNotNull();
148+
JmxConfig config = parseConf(CONF4);
155149

156150
List<JmxRule> defs = config.getRules();
157151
assertThat(defs).hasSize(1);
@@ -193,9 +187,7 @@ void testConf4() throws Exception {
193187

194188
@Test
195189
void testConf5() throws Exception {
196-
InputStream is = new ByteArrayInputStream(CONF5.getBytes(StandardCharsets.UTF_8));
197-
JmxConfig config = parser.loadConfig(is);
198-
assertThat(config).isNotNull();
190+
JmxConfig config = parseConf(CONF5);
199191

200192
List<JmxRule> defs = config.getRules();
201193
assertThat(defs).hasSize(1);
@@ -227,9 +219,7 @@ void testConf5() throws Exception {
227219

228220
@Test
229221
void testConf6() throws Exception {
230-
InputStream is = new ByteArrayInputStream(CONF6.getBytes(StandardCharsets.UTF_8));
231-
JmxConfig config = parser.loadConfig(is);
232-
assertThat(config).isNotNull();
222+
JmxConfig config = parseConf(CONF6);
233223

234224
List<JmxRule> defs = config.getRules();
235225
assertThat(defs).hasSize(1);
@@ -261,9 +251,7 @@ void testConf6() throws Exception {
261251

262252
@Test
263253
void testConf7() throws Exception {
264-
InputStream is = new ByteArrayInputStream(CONF7.getBytes(StandardCharsets.UTF_8));
265-
JmxConfig config = parser.loadConfig(is);
266-
assertThat(config).isNotNull();
254+
JmxConfig config = parseConf(CONF7);
267255

268256
List<JmxRule> defs = config.getRules();
269257
assertThat(defs).hasSize(1);
@@ -290,9 +278,7 @@ void testConf7() throws Exception {
290278

291279
@Test
292280
void testConf8() throws Exception {
293-
InputStream is = new ByteArrayInputStream(CONF8.getBytes(StandardCharsets.UTF_8));
294-
JmxConfig config = parser.loadConfig(is);
295-
assertThat(config).isNotNull();
281+
JmxConfig config = parseConf(CONF8);
296282

297283
List<JmxRule> defs = config.getRules();
298284
assertThat(defs).hasSize(1);
@@ -314,23 +300,26 @@ void testConf8() throws Exception {
314300

315301
@Test
316302
void testEmptyConf() {
317-
InputStream is = new ByteArrayInputStream(EMPTY_CONF.getBytes(StandardCharsets.UTF_8));
318-
JmxConfig config = parser.loadConfig(is);
303+
JmxConfig config = parseConf(EMPTY_CONF);
319304
assertThat(config.getRules()).isEmpty();
320305
}
321306

307+
private static JmxConfig parseConf(String s) {
308+
InputStream is = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));
309+
JmxConfig jmxConfig = parser.loadConfig(is);
310+
assertThat(jmxConfig).isNotNull();
311+
return jmxConfig;
312+
}
313+
322314
/*
323315
* Negative tests
324316
*/
325317

326318
private static void runNegativeTest(String yaml) {
327-
InputStream is = new ByteArrayInputStream(yaml.getBytes(StandardCharsets.UTF_8));
328-
329319
Assertions.assertThrows(
330320
Exception.class,
331321
() -> {
332-
JmxConfig config = parser.loadConfig(is);
333-
assertThat(config).isNotNull();
322+
JmxConfig config = parseConf(yaml);
334323

335324
List<JmxRule> defs = config.getRules();
336325
assertThat(defs).hasSize(1);

0 commit comments

Comments
 (0)