Skip to content

Commit a268ebc

Browse files
committed
updates
1 parent 7dddef5 commit a268ebc

File tree

4 files changed

+566
-57
lines changed

4 files changed

+566
-57
lines changed

api/all/src/main/java/io/opentelemetry/api/internal/ImmutableKeyValuePairs.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ public boolean equals(@Nullable Object o) {
243243
return false;
244244
}
245245
ImmutableKeyValuePairs<?, ?> that = (ImmutableKeyValuePairs<?, ?>) o;
246+
// deepEquals is needed to support byte[] attribute values
246247
return Arrays.deepEquals(this.data, that.data);
247248
}
248249

@@ -252,6 +253,7 @@ public int hashCode() {
252253
if (result == 0) {
253254
result = 1;
254255
result *= 1000003;
256+
// deepHashCode is needed to support byte[] attribute values
255257
result ^= Arrays.deepHashCode(data);
256258
hashcode = result;
257259
}

api/incubator/src/test/java/io/opentelemetry/api/incubator/common/ExtendedAttributesTest.java

Lines changed: 23 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,6 @@ void get_ExtendedAttributeKey(
3737
actualValue = mapValue;
3838
}
3939

40-
if (extendedAttributeKey.getType() == ExtendedAttributeType.BYTE_ARRAY) {
41-
assertThat((byte[]) actualValue)
42-
.describedAs(key + "(" + extendedAttributeKey.getType() + ")")
43-
.containsExactly((byte[]) value);
44-
return;
45-
}
46-
if (extendedAttributeKey.getType() == ExtendedAttributeType.VALUE_ARRAY) {
47-
@SuppressWarnings("unchecked")
48-
List<Value<?>> actualList = (List<Value<?>>) actualValue;
49-
@SuppressWarnings("unchecked")
50-
List<Value<?>> expectedList = (List<Value<?>>) value;
51-
assertThat(actualList)
52-
.describedAs(key + "(" + extendedAttributeKey.getType() + ")")
53-
.isEqualTo(expectedList);
54-
return;
55-
}
56-
5740
assertThat(actualValue)
5841
.describedAs(key + "(" + extendedAttributeKey.getType() + ")")
5942
.isEqualTo(value);
@@ -90,6 +73,20 @@ void forEach(ExtendedAttributes extendedAttributes, Map<String, Object> expected
9073
assertMapEquals(seenEntries, expectedMap);
9174
}
9275

76+
@SuppressWarnings("unchecked")
77+
private static void assertMapEquals(Map<String, Object> actual, Map<String, Object> expected) {
78+
assertThat(actual.keySet()).isEqualTo(expected.keySet());
79+
actual.forEach(
80+
(key, value) -> {
81+
Object expectedValue = expected.get(key);
82+
if (value instanceof Map && expectedValue instanceof Map) {
83+
assertMapEquals((Map<String, Object>) value, (Map<String, Object>) expectedValue);
84+
return;
85+
}
86+
assertThat(value).isEqualTo(expectedValue);
87+
});
88+
}
89+
9390
@ParameterizedTest
9491
@MethodSource("attributesArgs")
9592
void size(ExtendedAttributes extendedAttributes, Map<String, Object> expectedMap) {
@@ -114,55 +111,16 @@ private static void assertEquals(
114111
assertThat(actual.size()).isEqualTo(expected.size());
115112
actual.forEach(
116113
(key, value) -> {
117-
if (key.getType() == ExtendedAttributeType.BYTE_ARRAY) {
118-
assertThat((byte[]) value).containsExactly((byte[]) expected.get(key.getKey()));
119-
return;
120-
}
121-
if (key.getType() == ExtendedAttributeType.VALUE_ARRAY) {
122-
assertThat((List<Value<?>>) value)
123-
.isEqualTo((List<Value<?>>) expected.get(key.getKey()));
124-
return;
125-
}
126114
if (key.getType() == ExtendedAttributeType.MAP) {
127115
assertEquals(
128116
((ExtendedAttributes) value).asMap(),
129117
(Map<String, Object>) expected.get(key.getKey()));
130118
return;
131119
}
132-
assertThat(expected.get(key.getKey())).isEqualTo(value);
120+
assertThat(value).isEqualTo(expected.get(key.getKey()));
133121
});
134122
}
135123

136-
@SuppressWarnings("unchecked")
137-
private static void assertMapEquals(Map<String, Object> actual, Map<String, Object> expected) {
138-
assertThat(actual.keySet()).isEqualTo(expected.keySet());
139-
actual.forEach(
140-
(key, value) -> {
141-
Object expectedValue = expected.get(key);
142-
if (value instanceof byte[] && expectedValue instanceof byte[]) {
143-
assertThat((byte[]) value).containsExactly((byte[]) expectedValue);
144-
return;
145-
}
146-
if (isValueArray(value) && isValueArray(expectedValue)) {
147-
assertThat((List<Value<?>>) value).isEqualTo((List<Value<?>>) expectedValue);
148-
return;
149-
}
150-
if (value instanceof Map && expectedValue instanceof Map) {
151-
assertMapEquals((Map<String, Object>) value, (Map<String, Object>) expectedValue);
152-
return;
153-
}
154-
assertThat(expectedValue).isEqualTo(value);
155-
});
156-
}
157-
158-
private static boolean isValueArray(Object value) {
159-
if (!(value instanceof List)) {
160-
return false;
161-
}
162-
List<?> list = (List<?>) value;
163-
return !list.isEmpty() && list.get(0) instanceof Value;
164-
}
165-
166124
@ParameterizedTest
167125
@MethodSource("attributesArgs")
168126
void asAttributes(ExtendedAttributes extendedAttributes, Map<String, Object> expectedMap) {
@@ -182,6 +140,14 @@ void asAttributes(ExtendedAttributes extendedAttributes, Map<String, Object> exp
182140
assertThat(attributes.size()).isEqualTo(expectedSize);
183141
}
184142

143+
private static boolean isValueArray(Object value) {
144+
if (!(value instanceof List)) {
145+
return false;
146+
}
147+
List<?> list = (List<?>) value;
148+
return !list.isEmpty() && list.get(0) instanceof Value;
149+
}
150+
185151
@ParameterizedTest
186152
@MethodSource("attributesArgs")
187153
void toBuilder(ExtendedAttributes extendedAttributes, Map<String, Object> expectedMap) {

0 commit comments

Comments
 (0)