Skip to content

Commit 9078a8c

Browse files
authored
Refactor tests of StringBuffer substring (#8000)
1 parent bdfcc9b commit 9078a8c

File tree

3 files changed

+48
-14
lines changed

3 files changed

+48
-14
lines changed

dd-java-agent/instrumentation/java-lang/src/test/groovy/datadog/trace/instrumentation/java/lang/StringBuilderCallSiteTest.groovy

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,23 @@ class StringBuilderCallSiteTest extends AgentTestRunner {
190190
where:
191191
param | beginIndex | expected
192192
sb('012345') | 1 | '12345'
193+
}
194+
195+
def 'test string buffer substring call site'() {
196+
setup:
197+
final iastModule = Mock(StringModule)
198+
InstrumentationBridge.registerIastModule(iastModule)
199+
200+
when:
201+
final result = TestStringBufferSuite.substring(param, beginIndex)
202+
203+
then:
204+
result == expected
205+
1 * iastModule.onStringSubSequence(param, beginIndex, param.length(), expected)
206+
0 * _
207+
208+
where:
209+
param | beginIndex | expected
193210
sbf('012345') | 1 | '12345'
194211
}
195212
@@ -209,6 +226,23 @@ class StringBuilderCallSiteTest extends AgentTestRunner {
209226
where:
210227
param | beginIndex | endIndex | expected
211228
sb('012345') | 1 | 5 | '1234'
229+
}
230+
231+
def 'test string buffer substring with endIndex call site'() {
232+
setup:
233+
final iastModule = Mock(StringModule)
234+
InstrumentationBridge.registerIastModule(iastModule)
235+
236+
when:
237+
final result = TestStringBufferSuite.substring(param, beginIndex, endIndex)
238+
239+
then:
240+
result == expected
241+
1 * iastModule.onStringSubSequence(param, beginIndex, endIndex, expected)
242+
0 * _
243+
244+
where:
245+
param | beginIndex | endIndex | expected
212246
sbf('012345') | 1 | 5 | '1234'
213247
}
214248

dd-java-agent/instrumentation/java-lang/src/test/java/foo/bar/TestStringBufferSuite.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,18 @@ public String toString(final StringBuffer buffer) {
5151
LOGGER.debug("After string buffer toString {}", result);
5252
return result;
5353
}
54+
55+
public static String substring(StringBuffer self, int beginIndex, int endIndex) {
56+
LOGGER.debug("Before string buffer substring {} from {} to {}", self, beginIndex, endIndex);
57+
final String result = self.substring(beginIndex, endIndex);
58+
LOGGER.debug("After string buffer substring {}", result);
59+
return result;
60+
}
61+
62+
public static String substring(StringBuffer self, int beginIndex) {
63+
LOGGER.debug("Before string buffer substring {} from {}", self, beginIndex);
64+
final String result = self.substring(beginIndex);
65+
LOGGER.debug("After string buffer substring {}", result);
66+
return result;
67+
}
5468
}

dd-java-agent/instrumentation/java-lang/src/test/java/foo/bar/TestStringBuilderSuite.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,4 @@ public static String substring(StringBuilder self, int beginIndex) {
9090
LOGGER.debug("After string builder substring {}", result);
9191
return result;
9292
}
93-
94-
public static String substring(StringBuffer self, int beginIndex, int endIndex) {
95-
LOGGER.debug("Before string buffer substring {} from {} to {}", self, beginIndex, endIndex);
96-
final String result = self.substring(beginIndex, endIndex);
97-
LOGGER.debug("After string buffer substring {}", result);
98-
return result;
99-
}
100-
101-
public static String substring(StringBuffer self, int beginIndex) {
102-
LOGGER.debug("Before string buffer substring {} from {}", self, beginIndex);
103-
final String result = self.substring(beginIndex);
104-
LOGGER.debug("After string buffer substring {}", result);
105-
return result;
106-
}
10793
}

0 commit comments

Comments
 (0)