Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.*;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.ResourceLock;
import org.junit.jupiter.api.parallel.Resources;

import com.fasterxml.jackson.annotation.*;

Expand Down Expand Up @@ -91,6 +93,10 @@ private LowerCaseEnum() { }
public String toString() { return name().toLowerCase(Locale.ROOT); }
}

protected static enum LocaleSensitiveEnum {
IS_ADMIN
}

static class MapBean {
public Map<TestEnum,Integer> map = new HashMap<>();

Expand Down Expand Up @@ -392,6 +398,26 @@ public void testEnumFeature_WRITE_ENUMS_TO_LOWERCASE() throws Exception {
assertEquals(q("b"), m.writeValueAsString(TestEnum.B));
// NOTE: cannot be dynamically changed
}

// [databind#5993]: Follow-up to #5994; enum lower-case serialization
// should not use the default Locale
@ResourceLock(Resources.LOCALE)
@Test
public void testEnumFeature_WRITE_ENUMS_TO_LOWERCASEUsesRootLocale() throws Exception {
Locale old = Locale.getDefault();
try {
Locale.setDefault(Locale.forLanguageTag("tr-TR"));
assertEquals("\u0131", "I".toLowerCase(),
"Test requires a default locale where \"I\".toLowerCase() yields U+0131");

ObjectMapper m = jsonMapperBuilder()
.configure(EnumFeature.WRITE_ENUMS_TO_LOWERCASE, true)
.build();
assertEquals(q("is_admin"), m.writeValueAsString(LocaleSensitiveEnum.IS_ADMIN));
} finally {
Locale.setDefault(old);
}
}
}

// [JACKSON-757], non-inner enum
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/tools/jackson/databind/util/EnumValuesTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package tools.jackson.databind.util;

import java.util.List;
import java.util.Locale;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.ResourceLock;
import org.junit.jupiter.api.parallel.Resources;

import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.SerializationConfig;
Expand Down Expand Up @@ -30,6 +33,10 @@ enum ABC {
public String toString() { return desc; }
}

enum LocaleSensitiveABC {
IS_ADMIN
}

private final ObjectMapper MAPPER = newJsonMapper();

@Test
Expand Down Expand Up @@ -91,6 +98,28 @@ public void testConstructFromNameLowerCased() {
assertEquals(3, values.internalMap().size());
}

// [databind#5993]: Follow-up to #5994; deprecated EnumValues lower-case path
// should not use the default Locale
@ResourceLock(Resources.LOCALE)
@Test
public void testConstructFromNameLowerCasedWithRootLocale() {
Locale old = Locale.getDefault();
try {
Locale.setDefault(Locale.forLanguageTag("tr-TR"));
assertEquals("\u0131", "I".toLowerCase(),
"Test requires a default locale where \"I\".toLowerCase() yields U+0131");

SerializationConfig cfg = MAPPER.serializationConfig()
.with(EnumFeature.WRITE_ENUMS_TO_LOWERCASE);
AnnotatedClass enumClass = resolve(MAPPER, LocaleSensitiveABC.class);

EnumValues values = EnumValues.constructFromName(cfg, enumClass);
assertEquals("is_admin", values.serializedValueFor(LocaleSensitiveABC.IS_ADMIN).toString());
} finally {
Locale.setDefault(old);
}
}

private AnnotatedClass resolve(ObjectMapper mapper, Class<?> enumClass) {
return AnnotatedClassResolver.resolve(mapper.serializationConfig(),
mapper.constructType(enumClass), null);
Expand Down
Loading