Skip to content

Commit

Permalink
Minor test refactoring (combine single-case class to bigger one)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 13, 2024
1 parent 26d1fa3 commit 66e3409
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 60 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

import tools.jackson.core.type.TypeReference;

import tools.jackson.databind.*;
import tools.jackson.databind.deser.Deserializers;
import tools.jackson.databind.deser.jdk.EnumDeserializer;
import tools.jackson.databind.exc.ValueInstantiationException;
import tools.jackson.databind.introspect.AnnotatedMethod;
import tools.jackson.databind.module.SimpleModule;
import tools.jackson.databind.testutil.DatabindTestUtil;

import static org.junit.jupiter.api.Assertions.*;

import static tools.jackson.databind.testutil.DatabindTestUtil.*;

public class EnumCreatorTest
public class EnumCreatorTest extends DatabindTestUtil
{
protected enum EnumWithCreator {
A, B;
Expand Down Expand Up @@ -227,6 +228,35 @@ public static Enum3280 getByValue(@JsonProperty("b") String value) {
}
}

static class DataClass4544 {
public DataEnum4544 data;
}

public enum DataEnum4544
{
TEST(0);

private final int data;

DataEnum4544(int data) {
this.data = data;
}

// Important! Without ignoring accessor will find logical property
// that matches Creator parameter... and assume properties-based
@JsonIgnore
public int getData() {
return data;
}

@JsonCreator
public static DataEnum4544 of(@ImplicitName("data") int data) {
return Arrays.stream(values())
.filter(it -> it.getData() == data)
.findAny().get();
}
}

/*
/**********************************************************
/* Test methods
Expand Down Expand Up @@ -403,4 +433,17 @@ public void testEnumsFromStringUnwrapped() throws Exception
assertEquals(TestEnumFromString.class, ob.getClass());
assertSame(TestEnumFromString.ENUM_A, ob);
}

// for [databind#4544]
@Test
void testEnumsWithImplicitNames4544() throws Exception {
final ObjectMapper mapper = jsonMapperBuilder()
.annotationIntrospector(new ImplicitNameIntrospector())
.build();

String json = a2q("{'data': 0}");
DataClass4544 data = mapper.readValue(json, DataClass4544.class);

assertEquals(DataEnum4544.TEST, data.data);
}
}

0 comments on commit 66e3409

Please sign in to comment.