66import static com .datadog .debugger .util .MoshiSnapshotHelper .NOT_CAPTURED_REASON ;
77import static com .datadog .debugger .util .MoshiSnapshotHelper .REDACTED_IDENT_REASON ;
88import static com .datadog .debugger .util .MoshiSnapshotHelper .REDACTED_TYPE_REASON ;
9+ import static com .datadog .debugger .util .MoshiSnapshotTestHelper .VALUE_ADAPTER ;
910import static com .datadog .debugger .util .TestHelper .setFieldInConfig ;
1011import static datadog .trace .bootstrap .debugger .util .Redaction .REDACTED_VALUE ;
1112import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
@@ -105,8 +106,6 @@ public class CapturedSnapshotTest {
105106 private static final ProbeId PROBE_ID2 = new ProbeId ("beae1807-f3b0-4ea8-a74f-826790c5e6f7" , 0 );
106107 private static final ProbeId PROBE_ID3 = new ProbeId ("beae1807-f3b0-4ea8-a74f-826790c5e6f8" , 0 );
107108 private static final String SERVICE_NAME = "service-name" ;
108- private static final JsonAdapter <CapturedContext .CapturedValue > VALUE_ADAPTER =
109- new MoshiSnapshotTestHelper .CapturedValueAdapter ();
110109 private static final JsonAdapter <Map <String , Object >> GENERIC_ADAPTER =
111110 MoshiHelper .createGenericAdapter ();
112111
@@ -1074,9 +1073,10 @@ public void staticFieldCondition() throws IOException, URISyntaxException {
10741073 Map <String , CapturedContext .CapturedValue > staticFields =
10751074 snapshot .getCaptures ().getReturn ().getStaticFields ();
10761075 assertEquals (4 , staticFields .size ());
1077- assertEquals ("foo" , getValue (staticFields .get ("strField" )));
1078- assertEquals ("1001" , getValue (staticFields .get ("intField" )));
1079- assertEquals (String .valueOf (Math .PI ), getValue (staticFields .get ("doubleField" )));
1076+ assertEquals ("foo" , MoshiSnapshotTestHelper .getValue (staticFields .get ("strField" )));
1077+ assertEquals ("1001" , MoshiSnapshotTestHelper .getValue (staticFields .get ("intField" )));
1078+ assertEquals (
1079+ String .valueOf (Math .PI ), MoshiSnapshotTestHelper .getValue (staticFields .get ("doubleField" )));
10801080 assertTrue (staticFields .containsKey ("intArrayField" ));
10811081 }
10821082
@@ -1407,11 +1407,11 @@ public void staticInheritedFields() throws IOException, URISyntaxException {
14071407 Map <String , CapturedContext .CapturedValue > staticFields =
14081408 snapshot .getCaptures ().getReturn ().getStaticFields ();
14091409 assertEquals (7 , staticFields .size ());
1410- assertEquals ("barfoo" , getValue (staticFields .get ("strValue" )));
1411- assertEquals ("48" , getValue (staticFields .get ("intValue" )));
1412- assertEquals ("6.28" , getValue (staticFields .get ("doubleValue" )));
1413- assertEquals ("[1, 2, 3, 4]" , getValue (staticFields .get ("longValues" )));
1414- assertEquals ("[foo, bar]" , getValue (staticFields .get ("strValues" )));
1410+ assertEquals ("barfoo" , MoshiSnapshotTestHelper . getValue (staticFields .get ("strValue" )));
1411+ assertEquals ("48" , MoshiSnapshotTestHelper . getValue (staticFields .get ("intValue" )));
1412+ assertEquals ("6.28" , MoshiSnapshotTestHelper . getValue (staticFields .get ("doubleValue" )));
1413+ assertEquals ("[1, 2, 3, 4]" , MoshiSnapshotTestHelper . getValue (staticFields .get ("longValues" )));
1414+ assertEquals ("[foo, bar]" , MoshiSnapshotTestHelper . getValue (staticFields .get ("strValues" )));
14151415 }
14161416
14171417 @ Test
@@ -2305,14 +2305,14 @@ private void assertCaptureArgs(
23052305 CapturedContext context , String name , String typeName , String value ) {
23062306 CapturedContext .CapturedValue capturedValue = context .getArguments ().get (name );
23072307 assertEquals (typeName , capturedValue .getType ());
2308- assertEquals (value , getValue (capturedValue ));
2308+ assertEquals (value , MoshiSnapshotTestHelper . getValue (capturedValue ));
23092309 }
23102310
23112311 private void assertCaptureLocals (
23122312 CapturedContext context , String name , String typeName , String value ) {
23132313 CapturedContext .CapturedValue localVar = context .getLocals ().get (name );
23142314 assertEquals (typeName , localVar .getType ());
2315- assertEquals (value , getValue (localVar ));
2315+ assertEquals (value , MoshiSnapshotTestHelper . getValue (localVar ));
23162316 }
23172317
23182318 private void assertCaptureLocals (
@@ -2335,7 +2335,7 @@ private void assertCaptureFields(
23352335 CapturedContext context , String name , String typeName , String value ) {
23362336 CapturedContext .CapturedValue field = context .getFields ().get (name );
23372337 assertEquals (typeName , field .getType ());
2338- assertEquals (value , getValue (field ));
2338+ assertEquals (value , MoshiSnapshotTestHelper . getValue (field ));
23392339 }
23402340
23412341 private void assertCaptureFields (
@@ -2385,7 +2385,7 @@ private void assertCaptureFieldCount(CapturedContext context, int expectedFieldC
23852385 private void assertCaptureReturnValue (CapturedContext context , String typeName , String value ) {
23862386 CapturedContext .CapturedValue returnValue = context .getLocals ().get ("@return" );
23872387 assertEquals (typeName , returnValue .getType ());
2388- assertEquals (value , getValue (returnValue ));
2388+ assertEquals (value , MoshiSnapshotTestHelper . getValue (returnValue ));
23892389 }
23902390
23912391 private void assertCaptureReturnValue (
@@ -2425,56 +2425,6 @@ private void assertCaptureThrowable(
24252425 assertEquals (lineNumber , throwable .getStacktrace ().get (0 ).getLineNumber ());
24262426 }
24272427
2428- private static String getValue (CapturedContext .CapturedValue capturedValue ) {
2429- CapturedContext .CapturedValue valued = null ;
2430- try {
2431- valued = VALUE_ADAPTER .fromJson (capturedValue .getStrValue ());
2432- if (valued .getNotCapturedReason () != null ) {
2433- Assertions .fail ("NotCapturedReason: " + valued .getNotCapturedReason ());
2434- }
2435- Object obj = valued .getValue ();
2436- if (obj != null && obj .getClass ().isArray ()) {
2437- if (obj .getClass ().getComponentType ().isPrimitive ()) {
2438- return primitiveArrayToString (obj );
2439- }
2440- return Arrays .toString ((Object []) obj );
2441- }
2442- return obj != null ? String .valueOf (obj ) : null ;
2443- } catch (IOException e ) {
2444- e .printStackTrace ();
2445- return null ;
2446- }
2447- }
2448-
2449- private static String primitiveArrayToString (Object obj ) {
2450- Class <?> componentType = obj .getClass ().getComponentType ();
2451- if (componentType == long .class ) {
2452- return Arrays .toString ((long []) obj );
2453- }
2454- if (componentType == int .class ) {
2455- return Arrays .toString ((int []) obj );
2456- }
2457- if (componentType == short .class ) {
2458- return Arrays .toString ((short []) obj );
2459- }
2460- if (componentType == char .class ) {
2461- return Arrays .toString ((char []) obj );
2462- }
2463- if (componentType == byte .class ) {
2464- return Arrays .toString ((byte []) obj );
2465- }
2466- if (componentType == boolean .class ) {
2467- return Arrays .toString ((boolean []) obj );
2468- }
2469- if (componentType == float .class ) {
2470- return Arrays .toString ((float []) obj );
2471- }
2472- if (componentType == double .class ) {
2473- return Arrays .toString ((double []) obj );
2474- }
2475- return null ;
2476- }
2477-
24782428 public static Map <String , CapturedContext .CapturedValue > getFields (
24792429 CapturedContext .CapturedValue capturedValue ) {
24802430 try {
0 commit comments