Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR 6524 with amendments #6569

Merged
merged 15 commits into from
Jul 13, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,38 @@
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

class CaseInsensitiveMap extends HashMap<String, String> {

private static final long serialVersionUID = -4202518750189126871L;

CaseInsensitiveMap() {}

CaseInsensitiveMap(Map<String, String> carrier) {
super(carrier);
if (carrier != null) {
this.putAll(carrier);
}
}

@Override
public String put(String key, String value) {
return super.put(key.toLowerCase(Locale.ROOT), value);
return super.put(getKeyLowerCase(key), value);
}

@Override
public void putAll(Map<? extends String, ? extends String> m) {
m.forEach(this::put);
}

private static String getKeyLowerCase(@Nonnull String key) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we normally don't include NonNull annotations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm. I don't remember either way. Happy to remove if you think we should skip it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return key.toLowerCase(Locale.ROOT);
}

@Override
@Nullable
public String get(Object key) {
return super.get(((String) key).toLowerCase(Locale.ROOT));
return super.get(getKeyLowerCase((String) key));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.api.incubator.propagation;

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;

import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;

class CaseInsensitiveMapTest {

@Test
void createByConstructor() {
Map<String, String> map = new HashMap<>();
map.put("Key1", "test");
map.put("Key2", "test2");

CaseInsensitiveMap caseInsensitiveMap = new CaseInsensitiveMap(map);

Map<String, String> standardMap = new HashMap<>();
standardMap.put("key1", "test");
standardMap.put("key2", "test2");

assertThat(caseInsensitiveMap).isEqualTo(standardMap);
}

@Test
void putAll() {
CaseInsensitiveMap caseInsensitiveMap = new CaseInsensitiveMap();
Map<String, String> standardMap = new HashMap<>();
standardMap.put("key1", "test");
standardMap.put("key2", "test2");
caseInsensitiveMap.putAll(standardMap);
assertThat(caseInsensitiveMap).isEqualTo(standardMap);
}

@Test
void putIfAbsent() {
CaseInsensitiveMap caseInsensitiveMap = new CaseInsensitiveMap();
caseInsensitiveMap.putIfAbsent("key1", "test");
assertThat(caseInsensitiveMap.get("KEY1")).isEqualTo("test");
caseInsensitiveMap.putIfAbsent("key1", "nope");
assertThat(caseInsensitiveMap.get("KEY1")).isEqualTo("test");
}

@Test
void createByConstructorWithNullMap() {
CaseInsensitiveMap caseInsensitiveMap = new CaseInsensitiveMap(null);
assertThat(caseInsensitiveMap).isEmpty();
}

@Test
void caseInsensitivity() {
CaseInsensitiveMap caseInsensitiveMap = new CaseInsensitiveMap(null);

assertThat(caseInsensitiveMap).isEmpty();

caseInsensitiveMap.put("KEY1", "test1");
caseInsensitiveMap.put("KEY2", "test2");
assertThat(caseInsensitiveMap.get("key1")).isEqualTo("test1");
assertThat(caseInsensitiveMap.get("key2")).isEqualTo("test2");
assertThat(caseInsensitiveMap.get("kEy2")).isEqualTo("test2");
assertThat(caseInsensitiveMap.get("KEY2")).isEqualTo("test2");
}
}
Loading