From 9078a8c473715936b0d550d497d5b56ba6005571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Vidal=20Dom=C3=ADnguez?= <60353145+Mariovido@users.noreply.github.com> Date: Fri, 22 Nov 2024 13:55:30 +0100 Subject: [PATCH] Refactor tests of StringBuffer substring (#8000) --- .../lang/StringBuilderCallSiteTest.groovy | 34 +++++++++++++++++++ .../java/foo/bar/TestStringBufferSuite.java | 14 ++++++++ .../java/foo/bar/TestStringBuilderSuite.java | 14 -------- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/dd-java-agent/instrumentation/java-lang/src/test/groovy/datadog/trace/instrumentation/java/lang/StringBuilderCallSiteTest.groovy b/dd-java-agent/instrumentation/java-lang/src/test/groovy/datadog/trace/instrumentation/java/lang/StringBuilderCallSiteTest.groovy index 8080c140d0a..a9680db938f 100644 --- a/dd-java-agent/instrumentation/java-lang/src/test/groovy/datadog/trace/instrumentation/java/lang/StringBuilderCallSiteTest.groovy +++ b/dd-java-agent/instrumentation/java-lang/src/test/groovy/datadog/trace/instrumentation/java/lang/StringBuilderCallSiteTest.groovy @@ -190,6 +190,23 @@ class StringBuilderCallSiteTest extends AgentTestRunner { where: param | beginIndex | expected sb('012345') | 1 | '12345' + } + + def 'test string buffer substring call site'() { + setup: + final iastModule = Mock(StringModule) + InstrumentationBridge.registerIastModule(iastModule) + + when: + final result = TestStringBufferSuite.substring(param, beginIndex) + + then: + result == expected + 1 * iastModule.onStringSubSequence(param, beginIndex, param.length(), expected) + 0 * _ + + where: + param | beginIndex | expected sbf('012345') | 1 | '12345' } @@ -209,6 +226,23 @@ class StringBuilderCallSiteTest extends AgentTestRunner { where: param | beginIndex | endIndex | expected sb('012345') | 1 | 5 | '1234' + } + + def 'test string buffer substring with endIndex call site'() { + setup: + final iastModule = Mock(StringModule) + InstrumentationBridge.registerIastModule(iastModule) + + when: + final result = TestStringBufferSuite.substring(param, beginIndex, endIndex) + + then: + result == expected + 1 * iastModule.onStringSubSequence(param, beginIndex, endIndex, expected) + 0 * _ + + where: + param | beginIndex | endIndex | expected sbf('012345') | 1 | 5 | '1234' } diff --git a/dd-java-agent/instrumentation/java-lang/src/test/java/foo/bar/TestStringBufferSuite.java b/dd-java-agent/instrumentation/java-lang/src/test/java/foo/bar/TestStringBufferSuite.java index 2ebe00bd791..94b7f04e732 100644 --- a/dd-java-agent/instrumentation/java-lang/src/test/java/foo/bar/TestStringBufferSuite.java +++ b/dd-java-agent/instrumentation/java-lang/src/test/java/foo/bar/TestStringBufferSuite.java @@ -51,4 +51,18 @@ public String toString(final StringBuffer buffer) { LOGGER.debug("After string buffer toString {}", result); return result; } + + public static String substring(StringBuffer self, int beginIndex, int endIndex) { + LOGGER.debug("Before string buffer substring {} from {} to {}", self, beginIndex, endIndex); + final String result = self.substring(beginIndex, endIndex); + LOGGER.debug("After string buffer substring {}", result); + return result; + } + + public static String substring(StringBuffer self, int beginIndex) { + LOGGER.debug("Before string buffer substring {} from {}", self, beginIndex); + final String result = self.substring(beginIndex); + LOGGER.debug("After string buffer substring {}", result); + return result; + } } diff --git a/dd-java-agent/instrumentation/java-lang/src/test/java/foo/bar/TestStringBuilderSuite.java b/dd-java-agent/instrumentation/java-lang/src/test/java/foo/bar/TestStringBuilderSuite.java index 59280cb09ae..1edd8b1500b 100644 --- a/dd-java-agent/instrumentation/java-lang/src/test/java/foo/bar/TestStringBuilderSuite.java +++ b/dd-java-agent/instrumentation/java-lang/src/test/java/foo/bar/TestStringBuilderSuite.java @@ -90,18 +90,4 @@ public static String substring(StringBuilder self, int beginIndex) { LOGGER.debug("After string builder substring {}", result); return result; } - - public static String substring(StringBuffer self, int beginIndex, int endIndex) { - LOGGER.debug("Before string buffer substring {} from {} to {}", self, beginIndex, endIndex); - final String result = self.substring(beginIndex, endIndex); - LOGGER.debug("After string buffer substring {}", result); - return result; - } - - public static String substring(StringBuffer self, int beginIndex) { - LOGGER.debug("Before string buffer substring {} from {}", self, beginIndex); - final String result = self.substring(beginIndex); - LOGGER.debug("After string buffer substring {}", result); - return result; - } }