Skip to content

Commit 4747f93

Browse files
committed
address comments
1 parent c7521ee commit 4747f93

File tree

2 files changed

+32
-40
lines changed

2 files changed

+32
-40
lines changed

buildSrc/call-site-instrumentation-plugin/src/test/java/datadog/trace/plugin/csi/impl/assertion/AssertBuilder.java

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.FileNotFoundException;
1919
import java.lang.reflect.Field;
2020
import java.lang.reflect.Method;
21+
import java.util.Collections;
2122
import java.util.List;
2223
import java.util.Set;
2324
import java.util.stream.Collectors;
@@ -39,10 +40,8 @@ public CallSiteAssert build() {
3940
Method enabled = null;
4041
Set<String> enabledArgs = null;
4142
Object[] enabledDeclaration = getEnabledDeclaration(targetType, interfaces);
42-
if (enabledDeclaration != null) {
43-
enabled = (Method) enabledDeclaration[0];
44-
enabledArgs = (Set<String>) enabledDeclaration[1];
45-
}
43+
enabled = (Method) enabledDeclaration[0];
44+
enabledArgs = (Set<String>) enabledDeclaration[1];
4645
return new CallSiteAssert(
4746
interfaces,
4847
getSpi(targetType),
@@ -53,8 +52,8 @@ public CallSiteAssert build() {
5352
}
5453

5554
protected Set<Class<?>> getSpi(ClassOrInterfaceDeclaration type) {
56-
return type.getAnnotationByName("AutoService").stream()
57-
.flatMap(
55+
return type.getAnnotationByName("AutoService")
56+
.<Set<Class<?>>>map(
5857
annotation ->
5958
annotation.asNormalAnnotationExpr().getPairs().stream()
6059
.filter(pair -> pair.getNameAsString().equals("value"))
@@ -70,9 +69,10 @@ protected Set<Class<?>> getSpi(ClassOrInterfaceDeclaration type) {
7069
.asReferenceType()
7170
.getTypeDeclaration()
7271
.get()
73-
.getQualifiedName())))
74-
.map(AssertBuilder::loadClass)
75-
.collect(Collectors.toSet());
72+
.getQualifiedName()))
73+
.map(AssertBuilder::loadClass)
74+
.collect(Collectors.toSet()))
75+
.orElse(Collections.emptySet());
7676
}
7777

7878
protected Set<Class<?>> getInterfaces(ClassOrInterfaceDeclaration type) {
@@ -93,31 +93,26 @@ protected Set<Class<?>> getInterfaces(ClassOrInterfaceDeclaration type) {
9393
}
9494

9595
private static Class<?> loadClass(String qualifiedName) {
96-
// Try the name as-is first
97-
try {
98-
return Class.forName(qualifiedName);
99-
} catch (ClassNotFoundException e) {
100-
// Try progressively replacing dots with $ from right to left for inner classes
101-
// We need to try all possible combinations
102-
String current = qualifiedName;
103-
int lastDot = current.lastIndexOf('.');
104-
while (lastDot > 0) {
105-
current = current.substring(0, lastDot) + "$" + current.substring(lastDot + 1);
106-
try {
107-
return Class.forName(current);
108-
} catch (ClassNotFoundException ex) {
109-
// Continue trying with the next dot
110-
lastDot = current.lastIndexOf('.', lastDot - 1);
96+
// Try progressively replacing dots with $ from right to left for inner classes
97+
String current = qualifiedName;
98+
int lastDot = current.lastIndexOf('.');
99+
do {
100+
try {
101+
return Class.forName(current);
102+
} catch (ClassNotFoundException e) {
103+
if (lastDot <= 0) {
104+
throw new RuntimeException(new ClassNotFoundException(qualifiedName));
111105
}
106+
current = current.substring(0, lastDot) + "$" + current.substring(lastDot + 1);
107+
lastDot = current.lastIndexOf('.', lastDot - 1);
112108
}
113-
throw new RuntimeException(new ClassNotFoundException(qualifiedName));
114-
}
109+
} while (true);
115110
}
116111

117112
protected Object[] getEnabledDeclaration(
118113
ClassOrInterfaceDeclaration type, Set<Class<?>> interfaces) {
119114
if (!interfaces.contains(CallSites.HasEnabledProperty.class)) {
120-
return null;
115+
return new Object[] {null, null};
121116
}
122117
MethodDeclaration isEnabled = type.getMethodsByName("isEnabled").get(0);
123118
MethodCallExpr enabledMethodCall =

buildSrc/call-site-instrumentation-plugin/src/test/java/datadog/trace/plugin/csi/impl/ext/IastExtensionTest.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static datadog.trace.plugin.csi.util.CallSiteUtils.classNameToType;
55
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
66
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
78
import static org.mockito.Mockito.mock;
89
import static org.mockito.Mockito.when;
910

@@ -30,13 +31,11 @@
3031
import java.util.Set;
3132
import java.util.function.Consumer;
3233
import java.util.stream.Collectors;
33-
import java.util.stream.Stream;
3434
import org.junit.jupiter.api.BeforeEach;
3535
import org.junit.jupiter.api.Test;
3636
import org.junit.jupiter.api.io.TempDir;
3737
import org.junit.jupiter.params.ParameterizedTest;
38-
import org.junit.jupiter.params.provider.Arguments;
39-
import org.junit.jupiter.params.provider.MethodSource;
38+
import org.junit.jupiter.params.provider.CsvSource;
4039
import org.objectweb.asm.Type;
4140

4241
class IastExtensionTest extends BaseCsiPluginTest {
@@ -56,14 +55,14 @@ void setup() throws Exception {
5655
Files.createDirectories(srcFolder);
5756
}
5857

59-
static Stream<Arguments> testThatExtensionOnlyAppliesToIastAdvicesProvider() {
60-
return Stream.of(
61-
Arguments.of(CallSites.class.getName(), false),
62-
Arguments.of(IastExtension.IAST_CALL_SITES_FQCN, true));
63-
}
64-
6558
@ParameterizedTest
66-
@MethodSource("testThatExtensionOnlyAppliesToIastAdvicesProvider")
59+
@CsvSource(
60+
delimiter = '|',
61+
nullValues = "null",
62+
value = {
63+
"datadog.trace.agent.tooling.csi.CallSites | false",
64+
"datadog.trace.agent.tooling.iast.IastCallSites | true"
65+
})
6766
void testThatExtensionOnlyAppliesToIastAdvices(String typeName, boolean expected) {
6867
Type type = classNameToType(typeName);
6968
Type[] types = new Type[] {type};
@@ -85,9 +84,7 @@ void testThatExtensionGeneratesACallSiteWithTelemetry() throws Exception {
8584
CallSiteSpecification spec = buildClassSpecification(IastExtensionCallSite.class);
8685
AdviceGenerator generator = buildAdviceGenerator(buildDir);
8786
CallSiteResult result = generator.generate(spec);
88-
if (!result.isSuccess()) {
89-
throw new IllegalArgumentException("Error with call site " + IastExtensionCallSite.class);
90-
}
87+
assertTrue(result.isSuccess());
9188
IastExtension extension = new IastExtension();
9289

9390
extension.apply(config, result);

0 commit comments

Comments
 (0)