-
Notifications
You must be signed in to change notification settings - Fork 870
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow specifying a comma separated list of extensions (#6137)
* Allow specifying a comma separated list of extensions * update doc * typo * Update examples/extension/README.md Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com> Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
- Loading branch information
Showing
3 changed files
with
101 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...nt-tooling/src/test/java/io/opentelemetry/javaagent/tooling/ExtensionClassLoaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.tooling; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.io.File; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.jar.Attributes; | ||
import java.util.jar.JarOutputStream; | ||
import java.util.jar.Manifest; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
public class ExtensionClassLoaderTest { | ||
private static final File AGENT_FILE = new File("/agent.jar"); | ||
|
||
@Test | ||
void testParseLocation(@TempDir Path outputDir) throws Exception { | ||
String jarPath1 = createJar("test-1.jar", outputDir); | ||
String jarPath2 = createJar("test-2.jar", outputDir); | ||
// test that non-jar file is skipped | ||
Files.createFile(outputDir.resolve("test.txt")); | ||
{ | ||
List<URL> result = ExtensionClassLoader.parseLocation(jarPath1, AGENT_FILE); | ||
assertEquals(1, result.size()); | ||
} | ||
{ | ||
// empty paths are ignored | ||
List<URL> result = ExtensionClassLoader.parseLocation("," + jarPath1 + ",,,", AGENT_FILE); | ||
assertEquals(1, result.size()); | ||
} | ||
{ | ||
List<URL> result = ExtensionClassLoader.parseLocation(jarPath1 + "," + jarPath2, AGENT_FILE); | ||
assertEquals(2, result.size()); | ||
} | ||
{ | ||
List<URL> result = ExtensionClassLoader.parseLocation(outputDir.toString(), AGENT_FILE); | ||
assertEquals(2, result.size()); | ||
} | ||
{ | ||
List<URL> result = | ||
ExtensionClassLoader.parseLocation( | ||
outputDir + "," + jarPath1 + "," + jarPath2, AGENT_FILE); | ||
assertEquals(4, result.size()); | ||
} | ||
{ | ||
List<URL> result = ExtensionClassLoader.parseLocation("/anydir", AGENT_FILE); | ||
assertEquals(0, result.size()); | ||
} | ||
{ | ||
List<URL> result = ExtensionClassLoader.parseLocation("/anyfile.jar", AGENT_FILE); | ||
assertEquals(0, result.size()); | ||
} | ||
{ | ||
List<URL> result = ExtensionClassLoader.parseLocation(jarPath1 + ",/anyfile.jar", AGENT_FILE); | ||
assertEquals(1, result.size()); | ||
} | ||
} | ||
|
||
private static String createJar(String name, Path directory) throws Exception { | ||
Path jarPath = directory.resolve(name); | ||
createJar(jarPath); | ||
return jarPath.toAbsolutePath().toString(); | ||
} | ||
|
||
private static void createJar(Path path) throws Exception { | ||
Manifest manifest = new Manifest(); | ||
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); | ||
try (JarOutputStream jar = new JarOutputStream(Files.newOutputStream(path), manifest)) { | ||
// empty jar | ||
} | ||
} | ||
} |