Skip to content

Commit 646e156

Browse files
committed
post-review changes
1 parent 857ac4d commit 646e156

File tree

4 files changed

+52
-41
lines changed

4 files changed

+52
-41
lines changed

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

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.contrib.jmxscraper;
7+
8+
/**
9+
* Exception indicating something is wrong with the provided arguments or reading the configuration
10+
* from them
11+
*/
12+
public class InvalidArgumentException extends Exception {
13+
14+
private static final long serialVersionUID = 0L;
15+
16+
public InvalidArgumentException(String msg) {
17+
super(msg);
18+
}
19+
20+
public InvalidArgumentException(String msg, Throwable cause) {
21+
super(msg, cause);
22+
}
23+
}

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static void main(String[] args) {
8383
} catch (ConfigurationException e) {
8484
logger.log(Level.SEVERE, "invalid configuration ", e);
8585
System.exit(1);
86-
} catch (ArgumentsParsingException e) {
86+
} catch (InvalidArgumentException e) {
8787
logger.log(Level.SEVERE, "invalid configuration provided through arguments", e);
8888
logger.info(
8989
"Usage: java -jar <path_to_jmxscraper.jar> "
@@ -116,18 +116,18 @@ static void propagateToSystemProperties(Properties properties) {
116116
*
117117
* @param args application commandline arguments
118118
*/
119-
static Properties parseArgs(List<String> args) throws ArgumentsParsingException {
119+
static Properties parseArgs(List<String> args) throws InvalidArgumentException {
120120

121121
if (args.isEmpty()) {
122122
// empty properties from stdin or external file
123123
// config could still be provided through JVM system properties
124124
return new Properties();
125125
}
126126
if (args.size() != 2) {
127-
throw new ArgumentsParsingException("Exactly two arguments expected, got " + args.size());
127+
throw new InvalidArgumentException("Exactly two arguments expected, got " + args.size());
128128
}
129129
if (!args.get(0).equalsIgnoreCase(CONFIG_ARG)) {
130-
throw new ArgumentsParsingException("Unexpected first argument must be '" + CONFIG_ARG + "'");
130+
throw new InvalidArgumentException("Unexpected first argument must be '" + CONFIG_ARG + "'");
131131
}
132132

133133
String path = args.get(1);
@@ -138,28 +138,30 @@ static Properties parseArgs(List<String> args) throws ArgumentsParsingException
138138
}
139139
}
140140

141-
private static Properties loadPropertiesFromStdin() throws ArgumentsParsingException {
141+
private static Properties loadPropertiesFromStdin() throws InvalidArgumentException {
142142
Properties properties = new Properties();
143143
try (InputStream is = new DataInputStream(System.in)) {
144144
properties.load(is);
145145
return properties;
146146
} catch (IOException e) {
147-
throw new ArgumentsParsingException("Failed to read config properties from stdin", e);
147+
// an IO error is very unlikely here
148+
throw new InvalidArgumentException("Failed to read config properties from stdin", e);
148149
}
149150
}
150151

151-
private static Properties loadPropertiesFromPath(String path) throws ArgumentsParsingException {
152+
private static Properties loadPropertiesFromPath(String path) throws InvalidArgumentException {
152153
Properties properties = new Properties();
153154
try (InputStream is = Files.newInputStream(Paths.get(path))) {
154155
properties.load(is);
155156
return properties;
156157
} catch (IOException e) {
157-
throw new ArgumentsParsingException(
158+
throw new InvalidArgumentException(
158159
"Failed to read config properties file: '" + path + "'", e);
159160
}
160161
}
161162

162-
JmxScraper(JmxConnectorBuilder client, JmxMetricInsight service, JmxScraperConfig config) {
163+
private JmxScraper(
164+
JmxConnectorBuilder client, JmxMetricInsight service, JmxScraperConfig config) {
163165
this.client = client;
164166
this.service = service;
165167
this.config = config;

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,33 @@
2020
import org.junitpioneer.jupiter.ClearSystemProperty;
2121

2222
class JmxScraperTest {
23+
2324
@Test
2425
void shouldThrowExceptionWhenInvalidCommandLineArgsProvided() {
25-
// Given
26-
List<String> emptyArgs = Collections.singletonList("-nonExistentOption");
26+
testInvalidArguments("-nonExistentOption");
27+
testInvalidArguments("-potato", "-config");
28+
testInvalidArguments("-config", "path", "-nonExistentOption");
29+
}
2730

28-
// When and Then
29-
assertThatThrownBy(() -> JmxScraper.parseArgs(emptyArgs))
30-
.isInstanceOf(ArgumentsParsingException.class);
31+
@Test
32+
void emptyArgumentsAllowed() throws InvalidArgumentException {
33+
assertThat(JmxScraper.parseArgs(Collections.emptyList()))
34+
.describedAs("empty arguments allowed to use JVM properties")
35+
.isEmpty();
3136
}
3237

3338
@Test
34-
void shouldThrowExceptionWhenTooManyCommandLineArgsProvided() {
35-
// Given
36-
List<String> args = Arrays.asList("-config", "path", "-nonExistentOption");
39+
void shouldThrowExceptionWhenMissingProperties() {
40+
testInvalidArguments("-config", "missing.properties");
41+
}
3742

38-
// When and Then
39-
assertThatThrownBy(() -> JmxScraper.parseArgs(args))
40-
.isInstanceOf(ArgumentsParsingException.class);
43+
private static void testInvalidArguments(String... args) {
44+
assertThatThrownBy(() -> JmxScraper.parseArgs(Arrays.asList(args)))
45+
.isInstanceOf(InvalidArgumentException.class);
4146
}
4247

4348
@Test
44-
void shouldCreateConfig_propertiesLoadedFromFile() throws ArgumentsParsingException {
49+
void shouldCreateConfig_propertiesLoadedFromFile() throws InvalidArgumentException {
4550
// Given
4651
String filePath =
4752
ClassLoader.getSystemClassLoader().getResource("validConfig.properties").getPath();
@@ -58,8 +63,7 @@ void shouldCreateConfig_propertiesLoadedFromFile() throws ArgumentsParsingExcept
5863
}
5964

6065
@Test
61-
void shouldCreateConfig_propertiesLoadedFromStdIn()
62-
throws ArgumentsParsingException, IOException {
66+
void shouldCreateConfig_propertiesLoadedFromStdIn() throws InvalidArgumentException, IOException {
6367
InputStream originalIn = System.in;
6468
try (InputStream stream =
6569
ClassLoader.getSystemClassLoader().getResourceAsStream("validConfig.properties")) {

0 commit comments

Comments
 (0)