|
16 | 16 | import static io.opentelemetry.api.common.AttributeKey.valueKey; |
17 | 17 | import static java.util.Collections.singletonList; |
18 | 18 | import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
19 | 20 |
|
20 | 21 | import com.google.common.testing.EqualsTester; |
21 | 22 | import io.opentelemetry.api.common.AttributeKey; |
@@ -187,6 +188,26 @@ void create_NullEmptyValue() { |
187 | 188 | assertThat(resource.getAttributes().size()).isEqualTo(1); |
188 | 189 | } |
189 | 190 |
|
| 191 | + @Test |
| 192 | + void create_invalidAttributeKey() { |
| 193 | + assertThatThrownBy(() -> Resource.create(Attributes.of(stringKey("\u0002ab"), "value"))) |
| 194 | + .isInstanceOf(IllegalArgumentException.class) |
| 195 | + .hasMessageContaining("Attribute key should be a ASCII string"); |
| 196 | + |
| 197 | + assertThatThrownBy(() -> Resource.create(Attributes.of(stringKey("key\u007f"), "value"))) |
| 198 | + .isInstanceOf(IllegalArgumentException.class) |
| 199 | + .hasMessageContaining("Attribute key should be a ASCII string"); |
| 200 | + |
| 201 | + String tooLongKey = new String(new char[256]).replace('\0', 'a'); |
| 202 | + assertThatThrownBy(() -> Resource.create(Attributes.of(stringKey(tooLongKey), "value"))) |
| 203 | + .isInstanceOf(IllegalArgumentException.class) |
| 204 | + .hasMessageContaining("Attribute key should be a ASCII string"); |
| 205 | + |
| 206 | + String maxValidKey = new String(new char[255]).replace('\0', 'a'); |
| 207 | + Resource resource = Resource.create(Attributes.of(stringKey(maxValidKey), "value")); |
| 208 | + assertThat(resource.getAttribute(stringKey(maxValidKey))).isEqualTo("value"); |
| 209 | + } |
| 210 | + |
190 | 211 | @Test |
191 | 212 | void testResourceEquals() { |
192 | 213 | Attributes attribute1 = Attributes.of(stringKey("a"), "1", stringKey("b"), "2"); |
|
0 commit comments