Skip to content

Commit a416c70

Browse files
committed
Test coverage
1 parent 07f3f8b commit a416c70

File tree

5 files changed

+84
-10
lines changed

5 files changed

+84
-10
lines changed

components/context/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ apply(from = "$rootDir/gradle/java.gradle")
77
jmh {
88
version = "1.28"
99
}
10+
11+
val excludedClassesInstructionCoverage by extra {
12+
listOf("datadog.context.ContextProviders") // covered by forked test
13+
}

components/context/src/main/java/datadog/context/ContextKey.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,7 @@ public int hashCode() {
2929
return index;
3030
}
3131

32-
@Override
33-
public boolean equals(Object o) {
34-
if (this == o) {
35-
return true;
36-
} else if (o == null || getClass() != o.getClass()) {
37-
return false;
38-
} else {
39-
return index == ((ContextKey<?>) o).index;
40-
}
41-
}
32+
// we want identity equality, so no need to override equals()
4233

4334
@Override
4435
public String toString() {

components/context/src/test/java/datadog/context/ContextBinderTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ void testAttachAndDetach() {
1717
assertEquals(context, Context.from(carrier));
1818
// Detaching removes all context
1919
assertEquals(context, Context.detachFrom(carrier));
20+
assertEquals(root(), Context.detachFrom(carrier));
2021
assertEquals(root(), Context.from(carrier));
2122
}
2223
}

components/context/src/test/java/datadog/context/ContextKeyTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package datadog.context;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
45
import static org.junit.jupiter.api.Assertions.assertNotEquals;
56
import static org.junit.jupiter.api.Assertions.assertNotNull;
67
import static org.junit.jupiter.api.Assertions.assertNull;
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
79

810
import org.junit.jupiter.api.Test;
911
import org.junit.jupiter.params.ParameterizedTest;
@@ -31,4 +33,26 @@ void testKeyNameCollision() {
3133
assertEquals(value, context.get(key1));
3234
assertNull(context.get(key2));
3335
}
36+
37+
@SuppressWarnings({
38+
"EqualsWithItself",
39+
"SimplifiableAssertion",
40+
"ConstantValue",
41+
"EqualsBetweenInconvertibleTypes"
42+
})
43+
@Test
44+
void testEqualsAndHashCode() {
45+
ContextKey<String> key1 = ContextKey.named("same-name");
46+
ContextKey<String> key2 = ContextKey.named("same-name");
47+
// Test equals on self
48+
assertTrue(key1.equals(key1));
49+
assertEquals(key1.hashCode(), key1.hashCode());
50+
// Test equals on null
51+
assertFalse(key1.equals(null));
52+
// Test equals on different object type
53+
assertFalse(key1.equals("value"));
54+
// Test equals on different keys with the same name
55+
assertFalse(key1.equals(key2));
56+
assertNotEquals(key1.hashCode(), key2.hashCode());
57+
}
3458
}

components/context/src/test/java/datadog/context/ContextTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
56
import static org.junit.jupiter.api.Assertions.assertNull;
67
import static org.junit.jupiter.api.Assertions.assertThrows;
78
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -12,6 +13,8 @@
1213
class ContextTest {
1314
static final ContextKey<String> STRING_KEY = ContextKey.named("string-key");
1415
static final ContextKey<Boolean> BOOLEAN_KEY = ContextKey.named("boolean-key");
16+
static final ContextKey<Float> FLOAT_KEY = ContextKey.named("float-key");
17+
static final ContextKey<Long> LONG_KEY = ContextKey.named("long-key");
1518

1619
// demonstrate how values can hide their context keys
1720
static class ValueWithKey implements ImplicitContextKeyed {
@@ -86,19 +89,33 @@ void testEqualsAndHashCode() {
8689
Context context1 = empty.with(STRING_KEY, "value");
8790
Context context2 = empty.with(STRING_KEY, "value ");
8891
Context context3 = empty.with(STRING_KEY, "value ".trim());
92+
Context context4 = empty.with(STRING_KEY, "value").with(BOOLEAN_KEY, true);
8993
// Test equals on self
9094
assertTrue(empty.equals(empty));
9195
assertTrue(context1.equals(context1));
96+
assertTrue(context4.equals(context4));
9297
// Test equals on null
9398
assertFalse(context1.equals(null));
99+
assertFalse(context4.equals(null));
94100
// Test equals on different object type
95101
assertFalse(context1.equals("value"));
102+
assertFalse(context4.equals("value"));
96103
// Test equals on different contexts with the same values
97104
assertTrue(context1.equals(context3));
98105
assertEquals(context1.hashCode(), context3.hashCode());
99106
// Test equals on different contexts
100107
assertFalse(context1.equals(empty));
108+
assertNotEquals(context1.hashCode(), empty.hashCode());
101109
assertFalse(context1.equals(context2));
110+
assertNotEquals(context1.hashCode(), context2.hashCode());
111+
assertFalse(context1.equals(context4));
112+
assertNotEquals(context1.hashCode(), context4.hashCode());
113+
assertFalse(empty.equals(context1));
114+
assertNotEquals(empty.hashCode(), context1.hashCode());
115+
assertFalse(context2.equals(context1));
116+
assertNotEquals(context2.hashCode(), context1.hashCode());
117+
assertFalse(context4.equals(context1));
118+
assertNotEquals(context4.hashCode(), context1.hashCode());
102119
}
103120

104121
@Test
@@ -110,4 +127,41 @@ void testImplicitKey() {
110127
assertNull(ValueWithKey.from(empty));
111128
assertEquals(valueWithKey, ValueWithKey.from(context));
112129
}
130+
131+
@SuppressWarnings({"SimplifiableAssertion"})
132+
@Test
133+
void testInflation() {
134+
Context empty = Context.root();
135+
136+
Context one = empty.with(STRING_KEY, "unset").with(STRING_KEY, "one");
137+
Context two = one.with(BOOLEAN_KEY, false).with(BOOLEAN_KEY, true);
138+
Context three = two.with(FLOAT_KEY, 0.0f).with(FLOAT_KEY, 3.3f);
139+
140+
assertNull(empty.get(STRING_KEY));
141+
assertNull(empty.get(BOOLEAN_KEY));
142+
assertNull(empty.get(FLOAT_KEY));
143+
assertNull(empty.get(LONG_KEY));
144+
145+
assertEquals("one", one.get(STRING_KEY));
146+
assertNull(one.get(BOOLEAN_KEY));
147+
assertNull(one.get(FLOAT_KEY));
148+
assertNull(one.get(LONG_KEY));
149+
150+
assertEquals("one", two.get(STRING_KEY));
151+
assertEquals(true, two.get(BOOLEAN_KEY));
152+
assertNull(two.get(FLOAT_KEY));
153+
assertNull(two.get(LONG_KEY));
154+
155+
assertEquals("one", three.get(STRING_KEY));
156+
assertEquals(true, three.get(BOOLEAN_KEY));
157+
assertEquals(3.3f, three.get(FLOAT_KEY));
158+
assertNull(three.get(LONG_KEY));
159+
160+
assertFalse(empty.equals(one));
161+
assertFalse(one.equals(two));
162+
assertFalse(two.equals(three));
163+
assertNotEquals(one.hashCode(), empty.hashCode());
164+
assertNotEquals(two.hashCode(), one.hashCode());
165+
assertNotEquals(three.hashCode(), two.hashCode());
166+
}
113167
}

0 commit comments

Comments
 (0)