Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ testing {
implementation("org.slf4j:log4j-over-slf4j")
implementation("org.slf4j:jcl-over-slf4j")
implementation("org.slf4j:jul-to-slf4j")
implementation("com.github.stefanbirkner:system-rules")
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions dependencyManagement/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ val DEPENDENCIES = listOf(

"io.r2dbc:r2dbc-proxy:1.1.6.RELEASE",
"ch.qos.logback:logback-classic:1.3.15", // 1.4+ requires Java 11+
"com.github.stefanbirkner:system-lambda:1.2.1",
"com.github.stefanbirkner:system-rules:1.19.0",
"uk.org.webcompere:system-stubs-jupiter:2.0.3",
"com.uber.nullaway:nullaway:0.12.10",
"commons-beanutils:commons-beanutils:1.11.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ dependencies {
api("org.mockito:mockito-junit-jupiter")

implementation("io.opentelemetry:opentelemetry-api")
implementation("com.github.stefanbirkner:system-lambda")
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ dependencies {
api("org.mockito:mockito-junit-jupiter")

implementation("io.opentelemetry:opentelemetry-api")
implementation("com.github.stefanbirkner:system-lambda")
}
8 changes: 8 additions & 0 deletions javaagent-tooling/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ testing {
compileOnly("com.google.code.findbugs:annotations")
}
}

val testConfigFile by registering(JvmTestSuite::class) {
dependencies {
implementation(project(":javaagent-tooling"))
// requires mockito-inline
implementation("uk.org.webcompere:system-stubs-jupiter")
}
}
}
}

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.tooling.config;

import static java.util.Collections.emptyMap;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class MethodsConfigurationParserTest {

@ParameterizedTest
@MethodSource("configurationTestData")
void testConfiguration(String value, Map<String, Set<String>> expected) {
Map<String, Set<String>> result = MethodsConfigurationParser.parse(value);

assertThat(result).isEqualTo(expected);
}

static Stream<Arguments> configurationTestData() {
return Stream.of(
Arguments.of(null, emptyMap()),
Arguments.of(" ", emptyMap()),
Arguments.of(
"some.package.ClassName",
Collections.<String, Set<String>>singletonMap(
"some.package.ClassName", Collections.emptySet())),
Arguments.of("some.package.ClassName[ , ]", emptyMap()),
Arguments.of("some.package.ClassName[ , method]", emptyMap()),
Arguments.of(
"some.package.Class$Name[ method , ]",
Collections.singletonMap("some.package.Class$Name", createSet("method"))),
Arguments.of(
"ClassName[ method1,]", Collections.singletonMap("ClassName", createSet("method1"))),
Arguments.of(
"ClassName[method1 , method2]",
Collections.singletonMap("ClassName", createSet("method1", "method2"))),
Arguments.of(
"Class$1[method1 ] ; Class$2[ method2];",
createTwoEntryMap("Class$1", createSet("method1"), "Class$2", createSet("method2"))),
Arguments.of(
"Duplicate[method1] ; Duplicate[method2] ;Duplicate[method3];",
Collections.singletonMap("Duplicate", createSet("method3"))));
}

private static Map<String, Set<String>> createTwoEntryMap(
String key1, Set<String> value1, String key2, Set<String> value2) {
Map<String, Set<String>> map = new HashMap<>();
map.put(key1, value1);
map.put(key2, value2);
return map;
}

private static Set<String> createSet(String... elements) {
Set<String> set = new HashSet<>();
for (String element : elements) {
set.add(element);
}
return set;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.tooling.config;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.properties.SystemProperties;

@ExtendWith(SystemStubsExtension.class)
class ConfigurationFileTest {

@TempDir File tmpDir;

@SystemStub private EnvironmentVariables environmentVariables;

@SystemStub private SystemProperties systemProperties;

@Test
void shouldUseEnvVar() throws IOException {
String path = createFile("config", "property1=val-env");
environmentVariables.set("OTEL_JAVAAGENT_CONFIGURATION_FILE", path);

Map<String, String> properties = ConfigurationFile.loadConfigFile();

assertThat(properties).containsEntry("property1", "val-env");
}

@Test
void shouldUseSystemProperty() throws IOException {
String path = createFile("config", "property1=val-sys");
systemProperties.set("otel.javaagent.configuration-file", path);

Map<String, String> properties = ConfigurationFile.loadConfigFile();

assertThat(properties).containsEntry("property1", "val-sys");
}

@Test
void shouldUseSystemPropertyOverEnvVar() throws IOException {
String pathEnv = createFile("configEnv", "property1=val-env");
String path = createFile("config", "property1=val-sys");
systemProperties.set("otel.javaagent.configuration-file", path);
environmentVariables.set("OTEL_JAVAAGENT_CONFIGURATION_FILE", pathEnv);

Map<String, String> properties = ConfigurationFile.loadConfigFile();

assertThat(properties).containsEntry("property1", "val-sys");
}

@Test
void shouldReturnEmptyPropertiesIfFileDoesNotExist() {
systemProperties.set("otel.javaagent.configuration-file", "somePath");

Map<String, String> properties = ConfigurationFile.loadConfigFile();

assertThat(properties).isEmpty();
}

@Test
void shouldReturnEmptyPropertiesIfPropertyIsNotSet() {
Map<String, String> properties = ConfigurationFile.loadConfigFile();

assertThat(properties).isEmpty();
}

private String createFile(String name, String contents) throws IOException {
File file = new File(tmpDir, name);
try (Writer writer =
new OutputStreamWriter(Files.newOutputStream(file.toPath()), StandardCharsets.UTF_8)) {
writer.write(contents);
}
return file.getAbsolutePath();
}
}
Loading