Skip to content

Commit 07c61ef

Browse files
committed
test coverage
1 parent 593162e commit 07c61ef

File tree

2 files changed

+102
-41
lines changed

2 files changed

+102
-41
lines changed

sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/IncubatingUtil.java

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package io.opentelemetry.sdk.autoconfigure;
77

8+
import static java.util.Objects.requireNonNull;
9+
810
import io.opentelemetry.api.incubator.config.ConfigProvider;
911
import io.opentelemetry.api.incubator.config.DeclarativeConfigException;
1012
import io.opentelemetry.api.incubator.config.GlobalConfigProvider;
@@ -18,7 +20,6 @@
1820
import java.io.InputStream;
1921
import java.lang.reflect.InvocationTargetException;
2022
import java.lang.reflect.Method;
21-
import java.util.Objects;
2223
import java.util.logging.Logger;
2324
import javax.annotation.Nullable;
2425

@@ -33,28 +34,32 @@ final class IncubatingUtil {
3334

3435
private IncubatingUtil() {}
3536

37+
// Visible for testing
38+
interface Factory {
39+
@Nullable
40+
AutoConfiguredOpenTelemetrySdk create()
41+
throws ClassNotFoundException,
42+
NoSuchMethodException,
43+
IllegalAccessException,
44+
InvocationTargetException;
45+
}
46+
3647
static AutoConfiguredOpenTelemetrySdk configureFromFile(
3748
Logger logger, String configurationFile, ComponentLoader componentLoader) {
3849
logger.fine("Autoconfiguring from configuration file: " + configurationFile);
3950
try (FileInputStream fis = new FileInputStream(configurationFile)) {
40-
Object model =
41-
Class.forName(
42-
"io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfiguration")
43-
.getMethod("parse", InputStream.class)
44-
.invoke(null, fis);
45-
return getOpenTelemetrySdk(model, componentLoader);
51+
return requireNonNull(
52+
createWithFactory(
53+
"file",
54+
() ->
55+
getOpenTelemetrySdk(
56+
Class.forName(
57+
"io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfiguration")
58+
.getMethod("parse", InputStream.class)
59+
.invoke(null, fis),
60+
componentLoader)));
4661
} catch (FileNotFoundException e) {
4762
throw new ConfigurationException("Configuration file not found", e);
48-
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e) {
49-
throw new ConfigurationException(
50-
"Error configuring from file. Is opentelemetry-sdk-extension-incubator on the classpath?",
51-
e);
52-
} catch (InvocationTargetException e) {
53-
Throwable cause = e.getCause();
54-
if (cause instanceof DeclarativeConfigException) {
55-
throw toConfigurationException((DeclarativeConfigException) cause);
56-
}
57-
throw new ConfigurationException("Unexpected error configuring from file", e);
5863
} catch (IOException e) {
5964
// IOException (other than FileNotFoundException which is caught above) is only thrown
6065
// above by FileInputStream.close()
@@ -64,30 +69,22 @@ static AutoConfiguredOpenTelemetrySdk configureFromFile(
6469

6570
@Nullable
6671
public static AutoConfiguredOpenTelemetrySdk configureFromSpi(ComponentLoader componentLoader) {
67-
try {
68-
Class<?> providerClass =
69-
Class.forName(
70-
"io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfigurationProvider");
71-
Method getConfigurationModel = providerClass.getMethod("getConfigurationModel");
72+
return createWithFactory(
73+
"SPI",
74+
() -> {
75+
Class<?> providerClass =
76+
Class.forName(
77+
"io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfigurationProvider");
78+
Method getConfigurationModel = providerClass.getMethod("getConfigurationModel");
7279

73-
for (Object configProvider : componentLoader.load(providerClass)) {
74-
Object model = getConfigurationModel.invoke(configProvider);
75-
if (model != null) {
76-
return getOpenTelemetrySdk(model, componentLoader);
77-
}
78-
}
79-
return null;
80-
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e) {
81-
throw new ConfigurationException(
82-
"Error configuring from SPI. Is opentelemetry-sdk-extension-incubator on the classpath?",
83-
e);
84-
} catch (InvocationTargetException e) {
85-
Throwable cause = e.getCause();
86-
if (cause instanceof DeclarativeConfigException) {
87-
throw toConfigurationException((DeclarativeConfigException) cause);
88-
}
89-
throw new ConfigurationException("Unexpected error configuring from SPI", e);
90-
}
80+
for (Object configProvider : componentLoader.load(providerClass)) {
81+
Object model = getConfigurationModel.invoke(configProvider);
82+
if (model != null) {
83+
return getOpenTelemetrySdk(model, componentLoader);
84+
}
85+
}
86+
return null;
87+
});
9188
}
9289

9390
private static AutoConfiguredOpenTelemetrySdk getOpenTelemetrySdk(
@@ -119,9 +116,29 @@ private static AutoConfiguredOpenTelemetrySdk getOpenTelemetrySdk(
119116
return AutoConfiguredOpenTelemetrySdk.create(sdk, Resource.getDefault(), null, configProvider);
120117
}
121118

119+
// Visible for testing
120+
@Nullable
121+
static AutoConfiguredOpenTelemetrySdk createWithFactory(String name, Factory factory) {
122+
try {
123+
return factory.create();
124+
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e) {
125+
throw new ConfigurationException(
126+
String.format(
127+
"Error configuring from %s. Is opentelemetry-sdk-extension-incubator on the classpath?",
128+
name),
129+
e);
130+
} catch (InvocationTargetException e) {
131+
Throwable cause = e.getCause();
132+
if (cause instanceof DeclarativeConfigException) {
133+
throw toConfigurationException((DeclarativeConfigException) cause);
134+
}
135+
throw new ConfigurationException("Unexpected error configuring from " + name, e);
136+
}
137+
}
138+
122139
private static ConfigurationException toConfigurationException(
123140
DeclarativeConfigException exception) {
124-
String message = Objects.requireNonNull(exception.getMessage());
141+
String message = requireNonNull(exception.getMessage());
125142
return new ConfigurationException(message, exception);
126143
}
127144

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.sdk.autoconfigure;
7+
8+
import static org.assertj.core.api.Assertions.assertThatCode;
9+
10+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
11+
import java.lang.reflect.InvocationTargetException;
12+
import org.junit.jupiter.api.Test;
13+
14+
class IncubatingUtilTest {
15+
16+
@Test
17+
void classNotFoundException() {
18+
assertThatCode(
19+
() ->
20+
IncubatingUtil.createWithFactory(
21+
"test",
22+
() -> {
23+
Class.forName("foo");
24+
return null;
25+
}))
26+
.isInstanceOf(ConfigurationException.class)
27+
.hasMessage(
28+
"Error configuring from test. Is opentelemetry-sdk-extension-incubator on the classpath?");
29+
}
30+
31+
@Test
32+
void invocationTargetException() {
33+
assertThatCode(
34+
() ->
35+
IncubatingUtil.createWithFactory(
36+
"test",
37+
() -> {
38+
throw new InvocationTargetException(new RuntimeException("test exception"));
39+
}))
40+
.isInstanceOf(ConfigurationException.class)
41+
.hasMessage("Unexpected error configuring from test")
42+
.hasRootCauseMessage("test exception");
43+
}
44+
}

0 commit comments

Comments
 (0)