Skip to content

Commit a2fcb3a

Browse files
committed
Match testOnlyClasspath using URI instead of String.
We already get the "needle" in the form of a URI, so this skips a step, and has the benefit of also working on Windows.
1 parent 4c9acbb commit a2fcb3a

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

test/framework/src/main/java/org/elasticsearch/entitlement/bootstrap/TestEntitlementBootstrap.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@
2929

3030
import java.io.IOException;
3131
import java.io.InputStream;
32+
import java.net.URI;
33+
import java.net.URISyntaxException;
3234
import java.net.URL;
3335
import java.nio.file.Path;
36+
import java.nio.file.Paths;
3437
import java.util.ArrayList;
3538
import java.util.Arrays;
3639
import java.util.Collection;
@@ -128,11 +131,11 @@ private static TestPolicyManager createPolicyManager(PathLookup pathLookup) thro
128131
FilesEntitlementsValidation.validate(pluginPolicies, pathLookup);
129132

130133
String testOnlyPathString = System.getenv("es.entitlement.testOnlyPath");
131-
Set<String> testOnlyClassPath;
134+
Set<URI> testOnlyClassPath;
132135
if (testOnlyPathString == null) {
133136
testOnlyClassPath = Set.of();
134137
} else {
135-
testOnlyClassPath = Arrays.stream(testOnlyPathString.split(separator)).collect(toCollection(TreeSet::new));
138+
testOnlyClassPath = Arrays.stream(testOnlyPathString.split(separator)).map(Paths::get).map(Path::toUri).collect(toCollection(TreeSet::new));
136139
}
137140

138141
return new TestPolicyManager(

test/framework/src/main/java/org/elasticsearch/entitlement/runtime/policy/TestPolicyManager.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.entitlement.runtime.policy.entitlements.Entitlement;
1313
import org.elasticsearch.test.ESTestCase;
1414

15+
import java.net.URI;
1516
import java.net.URISyntaxException;
1617
import java.nio.file.Path;
1718
import java.security.CodeSource;
@@ -35,7 +36,7 @@ public class TestPolicyManager extends PolicyManager {
3536
*/
3637
final Map<Class<?>, ModuleEntitlements> classEntitlementsMap = new ConcurrentHashMap<>();
3738

38-
final Collection<String> testOnlyClasspath;
39+
final Collection<URI> testOnlyClasspath;
3940

4041
public TestPolicyManager(
4142
Policy serverPolicy,
@@ -44,7 +45,7 @@ public TestPolicyManager(
4445
Function<Class<?>, PolicyScope> scopeResolver,
4546
Map<String, Collection<Path>> pluginSourcePaths,
4647
PathLookup pathLookup,
47-
Collection<String> testOnlyClasspath
48+
Collection<URI> testOnlyClasspath
4849
) {
4950
super(serverPolicy, apmAgentEntitlements, pluginPolicies, scopeResolver, pluginSourcePaths, pathLookup);
5051
this.testOnlyClasspath = testOnlyClasspath;
@@ -135,15 +136,12 @@ private boolean isTestCode(Class<?> requestingClass) {
135136
// This can happen for JDK classes
136137
return false;
137138
}
138-
String needle;
139+
URI needle;
139140
try {
140-
needle = codeSource.getLocation().toURI().getPath();
141+
needle = codeSource.getLocation().toURI();
141142
} catch (URISyntaxException e) {
142143
throw new IllegalStateException(e);
143144
}
144-
if (needle.endsWith("/")) {
145-
needle = needle.substring(0, needle.length() - 1);
146-
}
147145
boolean result = testOnlyClasspath.contains(needle);
148146
return result;
149147
}

0 commit comments

Comments
 (0)