Skip to content

Commit 0c1db1e

Browse files
authored
Follow-up with dd-trace-api Groovy to Java test file migration (#10811)
Follow-up with dd-trace-api Groovy to Java test file migration Co-authored-by: sarah.chen <sarah.chen@datadoghq.com>
1 parent a5ea541 commit 0c1db1e

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
lines changed

.claude/skills/migrate-groovy-to-java/SKILL.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
---
22
name: migrate-groovy-to-java
3-
description: migrate test groovy files to java
3+
description: >
4+
Converts Spock/Groovy test files in a Gradle module to equivalent JUnit 5 Java tests.
5+
Use when asked to "migrate groovy", "convert groovy to java", "g2j", or when a module
6+
has .groovy test files that need to be replaced with .java equivalents.
47
---
58

69
Migrate test Groovy files to Java using JUnit 5
@@ -18,6 +21,8 @@ When converting Groovy code to Java code, make sure that:
1821
- Ensure parameterized test names are human-readable (i.e. no hashcodes); instead add a description string as the first `Arguments.arguments(...)` value or index the test case
1922
- When converting tuples, create a light dedicated structure instead to keep the typing system
2023
- Instead of checking a state and throwing an exception, use JUnit asserts
24+
- Instead of using `assertTrue(a.equals(b))` or `assertFalse(a.equals(b))`, use `assertEquals(expected, actual)` and `assertNotEquals(unexpected, actual)`
25+
- Import frequently used types rather than using fully-qualified names inline, to improve readability
2126
- Do not wrap checked exceptions and throw a Runtime exception; prefer adding a throws clause at method declaration
2227
- Do not mark local variables `final`
2328
- Ensure variables are human-readable; avoid single-letter names and pre-define variables that are referenced multiple times

dd-trace-api/src/test/java/datadog/trace/api/IdGenerationStrategyTest.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,26 @@
66
import static org.junit.jupiter.api.Assertions.assertNotNull;
77
import static org.junit.jupiter.api.Assertions.assertNull;
88
import static org.junit.jupiter.api.Assertions.assertThrows;
9-
import static org.junit.jupiter.api.Assertions.assertTrue;
109

1110
import java.security.SecureRandom;
1211
import java.util.HashSet;
1312
import java.util.Set;
13+
import org.junit.jupiter.api.Test;
1414
import org.junit.jupiter.params.ParameterizedTest;
1515
import org.junit.jupiter.params.provider.CsvSource;
1616
import org.junit.jupiter.params.provider.ValueSource;
1717

1818
class IdGenerationStrategyTest {
1919

2020
@ParameterizedTest(name = "generate id with {1} and {0} bits")
21-
@CsvSource(
22-
value = {
23-
"false|RANDOM",
24-
"false|SEQUENTIAL",
25-
"false|SECURE_RANDOM",
26-
"true|RANDOM",
27-
"true|SEQUENTIAL",
28-
"true|SECURE_RANDOM"
29-
},
30-
delimiter = '|')
21+
@CsvSource({
22+
"false,RANDOM",
23+
"false,SEQUENTIAL",
24+
"false,SECURE_RANDOM",
25+
"true,RANDOM",
26+
"true,SEQUENTIAL",
27+
"true,SECURE_RANDOM"
28+
})
3129
void generateIdWithStrategyAndBitSize(
3230
boolean traceId128BitGenerationEnabled, String strategyName) {
3331
IdGenerationStrategy strategy =
@@ -37,10 +35,9 @@ void generateIdWithStrategyAndBitSize(
3735
for (int index = 0; index <= 32768; index++) {
3836
DDTraceId traceId = strategy.generateTraceId();
3937
assertNotNull(traceId);
40-
assertFalse(traceId.equals(null));
41-
assertFalse(traceId.equals("foo"));
38+
assertNotEquals(null, traceId);
39+
assertNotEquals("foo", traceId);
4240
assertNotEquals(DDTraceId.ZERO, traceId);
43-
assertTrue(traceId.equals(traceId));
4441

4542
int expectedHash =
4643
(int)
@@ -61,7 +58,7 @@ void returnNullForNonExistingStrategy(String strategyName) {
6158
assertNull(IdGenerationStrategy.fromName(strategyName));
6259
}
6360

64-
@org.junit.jupiter.api.Test
61+
@Test
6562
void exceptionCreatedOnSecureRandomStrategy() {
6663
ExceptionInInitializerError error =
6764
assertThrows(
@@ -77,7 +74,7 @@ void exceptionCreatedOnSecureRandomStrategy() {
7774
assertEquals("SecureRandom init exception", error.getCause().getMessage());
7875
}
7976

80-
@org.junit.jupiter.api.Test
77+
@Test
8178
void secureRandomIdsWillAlwaysBeNonZero() {
8279
ScriptedSecureRandom random = new ScriptedSecureRandom(new long[] {0L, 47L, 0L, 11L});
8380
CallCounter providerCallCounter = new CallCounter();

dd-trace-api/src/test/java/datadog/trace/api/internal/util/HexStringUtilsTest.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,23 @@
88
class HexStringUtilsTest {
99

1010
@ParameterizedTest(name = "test hexadecimal String representations high={0} low={1} size={2}")
11-
@CsvSource(
12-
value = {
13-
"0|0|10",
14-
"0|0|16",
15-
"0|0|20",
16-
"0|0|32",
17-
"0|0|40",
18-
"1|2|10",
19-
"1|2|16",
20-
"1|2|20",
21-
"1|2|32",
22-
"1|2|40",
23-
"6536977903480360123|3270264562721133536|10",
24-
"6536977903480360123|3270264562721133536|16",
25-
"6536977903480360123|3270264562721133536|20",
26-
"6536977903480360123|3270264562721133536|32",
27-
"6536977903480360123|3270264562721133536|40"
28-
},
29-
delimiter = '|')
11+
@CsvSource({
12+
"0,0,10",
13+
"0,0,16",
14+
"0,0,20",
15+
"0,0,32",
16+
"0,0,40",
17+
"1,2,10",
18+
"1,2,16",
19+
"1,2,20",
20+
"1,2,32",
21+
"1,2,40",
22+
"6536977903480360123,3270264562721133536,10",
23+
"6536977903480360123,3270264562721133536,16",
24+
"6536977903480360123,3270264562721133536,20",
25+
"6536977903480360123,3270264562721133536,32",
26+
"6536977903480360123,3270264562721133536,40"
27+
})
3028
void testHexadecimalStringRepresentations(long highOrderBits, long lowOrderBits, int size) {
3129
int highOrderSize = Math.min(16, Math.max(0, size - 16));
3230
int lowOrderSize = Math.min(16, size);

0 commit comments

Comments
 (0)