Skip to content

Commit

Permalink
add test for caught exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbempel committed Sep 2, 2024
1 parent b49c3bb commit 2fed31d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,37 @@ public void methodProbeLocalVarsDeepScopes() throws IOException, URISyntaxExcept
assertCaptureLocals(snapshot.getCaptures().getReturn(), "localVarL4", "int", "4");
}

@Test
public void methodProbeExceptionLocalVars() throws IOException, URISyntaxException {
final String CLASS_NAME = "com.datadog.debugger.CapturedSnapshot31";
LogProbe probe = createProbeAtExit(PROBE_ID, CLASS_NAME, "caughtException", "(String)");
TestSnapshotListener listener = installProbes(CLASS_NAME, probe);
Class<?> testClass = compileAndLoadClass(CLASS_NAME);
int result = Reflect.onClass(testClass).call("main", "illegalState").get();
assertEquals(0, result);
Snapshot snapshot = assertOneSnapshot(listener);
assertEquals(2, snapshot.getCaptures().getReturn().getLocals().size());
Map<String, String> expectedFields = new HashMap<>();
expectedFields.put("detailMessage", "state");
assertCaptureLocals(
snapshot.getCaptures().getReturn(),
"ex",
IllegalStateException.class.getTypeName(),
expectedFields);
listener.snapshots.clear();
result = Reflect.onClass(testClass).call("main", "illegalArgument").get();
assertEquals(0, result);
snapshot = assertOneSnapshot(listener);
assertEquals(2, snapshot.getCaptures().getReturn().getLocals().size());
expectedFields = new HashMap<>();
expectedFields.put("detailMessage", "argument");
assertCaptureLocals(
snapshot.getCaptures().getReturn(),
"ex",
IllegalArgumentException.class.getTypeName(),
expectedFields);
}

@Test
public void enumConstructorArgs() throws IOException, URISyntaxException {
final String CLASS_NAME = "com.datadog.debugger.CapturedSnapshot23";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public static int main(String arg) throws Exception {
if ("deepScopes".equals(arg)) {
return new CapturedSnapshot31().deepScopes(arg);
}
if (arg.startsWith("illegal")) {
return new CapturedSnapshot31().caughtException(arg);
}
return 0;
}

Expand Down Expand Up @@ -60,4 +63,22 @@ private int deepScopes(String arg) {
}
return localVarL0;
}

private int caughtException(String arg) {
try {
if ("illegalState".equals(arg)) {
throw new IllegalStateException("state");
}
if ("illegalArgument".equals(arg)) {
throw new IllegalArgumentException("argument");
}
return -1;
} catch (IllegalStateException ex) {
ex.printStackTrace();
return 0;
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
return 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ void testFullMethod() throws Exception {
"java.lang.String",
"42, foobar, 3.42, {key1=val1, key2=val2, key3=val3}, var1,var2,var3");
assertNotNull(snapshot.getCaptures().getReturn().getLocals());
assertEquals(1, snapshot.getCaptures().getReturn().getLocals().size()); // @return
// ex & @return are the only locals
assertEquals(2, snapshot.getCaptures().getReturn().getLocals().size());
assertNull(snapshot.getCaptures().getReturn().getCapturedThrowable());
snapshotReceived.set(true);
});
Expand Down

0 comments on commit 2fed31d

Please sign in to comment.