BAEL-245: Add more tests to align with previous enum example and prevent build fail (#981)
* Add NDC and JBoss Logging to the demo application * NDC for Log4j, Log4j2 and JBoss Logging * Simplify NDC example by making it a single operation instead of two * Make NDC example as RestController, Use JBoss Logging only as a logging bridge * Fix merge conflicts in pull request - log-mdc pom.xml updated * BAEL-445 Update to Spring security SpEL example * BAEL-445: Change tabs to spaces in the updated code * BAEL-245: Add Enum Serialization exmaple * BAEL-245: Remove the folder jackson/src/test/java/com/baeldung/jackson/dtos/withEnum as the example is not used anymore * Add more enum serialization examples to align with previous example and prevent build fail
This commit is contained in:
parent
e27131f3a5
commit
aacc25a752
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.jackson.dtos.withEnum;
|
||||
|
||||
public enum DistanceEnumSimple {
|
||||
KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001);
|
||||
|
||||
private String unit;
|
||||
private final double meters;
|
||||
|
||||
private DistanceEnumSimple(String unit, double meters) {
|
||||
this.unit = unit;
|
||||
this.meters = meters;
|
||||
}
|
||||
|
||||
public double getMeters() {
|
||||
return meters;
|
||||
}
|
||||
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public void setUnit(String unit) {
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.jackson.dtos.withEnum;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
|
||||
public enum DistanceEnumWithJsonFormat {
|
||||
KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001);
|
||||
|
||||
private String unit;
|
||||
private final double meters;
|
||||
|
||||
private DistanceEnumWithJsonFormat(String unit, double meters) {
|
||||
this.unit = unit;
|
||||
this.meters = meters;
|
||||
}
|
||||
|
||||
public double getMeters() {
|
||||
return meters;
|
||||
}
|
||||
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public void setUnit(String unit) {
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.jackson.dtos.withEnum;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
public enum DistanceEnumWithValue {
|
||||
KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001);
|
||||
|
||||
private String unit;
|
||||
private final double meters;
|
||||
|
||||
private DistanceEnumWithValue(String unit, double meters) {
|
||||
this.unit = unit;
|
||||
this.meters = meters;
|
||||
}
|
||||
|
||||
@JsonValue
|
||||
public double getMeters() {
|
||||
return meters;
|
||||
}
|
||||
|
||||
public String getUnit() {
|
||||
return unit;
|
||||
}
|
||||
|
||||
public void setUnit(String unit) {
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package com.baeldung.jackson.dtos.withEnum;
|
||||
|
||||
import com.baeldung.jackson.enums.Distance;
|
||||
|
||||
public class MyDtoWithEnumCustom {
|
||||
|
||||
private String stringValue;
|
||||
private int intValue;
|
||||
private boolean booleanValue;
|
||||
private Distance type;
|
||||
|
||||
public MyDtoWithEnumCustom() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MyDtoWithEnumCustom(final String stringValue, final int intValue, final boolean booleanValue, final Distance type) {
|
||||
super();
|
||||
|
||||
this.stringValue = stringValue;
|
||||
this.intValue = intValue;
|
||||
this.booleanValue = booleanValue;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public String getStringValue() {
|
||||
return stringValue;
|
||||
}
|
||||
|
||||
public void setStringValue(final String stringValue) {
|
||||
this.stringValue = stringValue;
|
||||
}
|
||||
|
||||
public int getIntValue() {
|
||||
return intValue;
|
||||
}
|
||||
|
||||
public void setIntValue(final int intValue) {
|
||||
this.intValue = intValue;
|
||||
}
|
||||
|
||||
public boolean isBooleanValue() {
|
||||
return booleanValue;
|
||||
}
|
||||
|
||||
public void setBooleanValue(final boolean booleanValue) {
|
||||
this.booleanValue = booleanValue;
|
||||
}
|
||||
|
||||
public Distance getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(final Distance type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.jackson.dtos.withEnum;
|
||||
|
||||
public class MyDtoWithEnumJsonFormat {
|
||||
|
||||
private String stringValue;
|
||||
private int intValue;
|
||||
private boolean booleanValue;
|
||||
private DistanceEnumWithJsonFormat distanceType;
|
||||
|
||||
public MyDtoWithEnumJsonFormat() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MyDtoWithEnumJsonFormat(final String stringValue, final int intValue, final boolean booleanValue, final DistanceEnumWithJsonFormat type) {
|
||||
super();
|
||||
|
||||
this.stringValue = stringValue;
|
||||
this.intValue = intValue;
|
||||
this.booleanValue = booleanValue;
|
||||
this.distanceType = type;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public String getStringValue() {
|
||||
return stringValue;
|
||||
}
|
||||
|
||||
public void setStringValue(final String stringValue) {
|
||||
this.stringValue = stringValue;
|
||||
}
|
||||
|
||||
public int getIntValue() {
|
||||
return intValue;
|
||||
}
|
||||
|
||||
public void setIntValue(final int intValue) {
|
||||
this.intValue = intValue;
|
||||
}
|
||||
|
||||
public boolean isBooleanValue() {
|
||||
return booleanValue;
|
||||
}
|
||||
|
||||
public void setBooleanValue(final boolean booleanValue) {
|
||||
this.booleanValue = booleanValue;
|
||||
}
|
||||
|
||||
public DistanceEnumWithJsonFormat getDistanceType() {
|
||||
return distanceType;
|
||||
}
|
||||
|
||||
public void setDistanceType(final DistanceEnumWithJsonFormat type) {
|
||||
this.distanceType = type;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,32 @@
|
|||
package com.baeldung.jackson.test;
|
||||
|
||||
import com.baeldung.jackson.annotation.*;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jackson.annotation.BeanWithCreator;
|
||||
import com.baeldung.jackson.annotation.BeanWithCustomAnnotation;
|
||||
import com.baeldung.jackson.annotation.BeanWithFilter;
|
||||
import com.baeldung.jackson.annotation.BeanWithGetter;
|
||||
import com.baeldung.jackson.annotation.BeanWithIgnore;
|
||||
import com.baeldung.jackson.annotation.BeanWithInject;
|
||||
import com.baeldung.jackson.annotation.ExtendableBean;
|
||||
import com.baeldung.jackson.annotation.MyBean;
|
||||
import com.baeldung.jackson.annotation.PrivateBean;
|
||||
import com.baeldung.jackson.annotation.RawBean;
|
||||
import com.baeldung.jackson.annotation.UnwrappedUser;
|
||||
import com.baeldung.jackson.annotation.UserWithIgnoreType;
|
||||
import com.baeldung.jackson.annotation.Zoo;
|
||||
import com.baeldung.jackson.bidirection.ItemWithIdentity;
|
||||
import com.baeldung.jackson.bidirection.ItemWithRef;
|
||||
import com.baeldung.jackson.bidirection.UserWithIdentity;
|
||||
|
@ -8,7 +34,7 @@ import com.baeldung.jackson.bidirection.UserWithRef;
|
|||
import com.baeldung.jackson.date.EventWithFormat;
|
||||
import com.baeldung.jackson.date.EventWithSerializer;
|
||||
import com.baeldung.jackson.dtos.MyMixInForIgnoreType;
|
||||
import com.baeldung.jackson.dtos.withEnum.TypeEnumWithValue;
|
||||
import com.baeldung.jackson.dtos.withEnum.DistanceEnumWithValue;
|
||||
import com.baeldung.jackson.exception.UserWithRoot;
|
||||
import com.baeldung.jackson.jsonview.Item;
|
||||
import com.baeldung.jackson.jsonview.Views;
|
||||
|
@ -20,17 +46,6 @@ import com.fasterxml.jackson.databind.SerializationFeature;
|
|||
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class JacksonAnnotationTest {
|
||||
|
||||
|
@ -85,10 +100,10 @@ public class JacksonAnnotationTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void whenSerializingUsingJsonValue_thenCorrect() throws JsonProcessingException {
|
||||
final String enumAsString = new ObjectMapper().writeValueAsString(TypeEnumWithValue.TYPE1);
|
||||
public void whenSerializingUsingJsonValue_thenCorrect() throws IOException {
|
||||
final String enumAsString = new ObjectMapper().writeValueAsString(DistanceEnumWithValue.MILE);
|
||||
|
||||
assertThat(enumAsString, is("\"Type A\""));
|
||||
assertThat(enumAsString, is("1609.34"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -6,14 +6,14 @@ import static org.junit.Assert.assertThat;
|
|||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.dtos.withEnum.MyDtoWithEnum;
|
||||
import com.baeldung.jackson.dtos.withEnum.TypeEnum;
|
||||
import com.baeldung.jackson.dtos.withEnum.TypeEnumSimple;
|
||||
import com.baeldung.jackson.dtos.withEnum.TypeEnumWithValue;
|
||||
import com.baeldung.jackson.dtos.withEnum.MyDtoWithEnumCustom;
|
||||
import com.baeldung.jackson.dtos.withEnum.TypeEnumWithCustomSerializer;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jackson.dtos.withEnum.DistanceEnumSimple;
|
||||
import com.baeldung.jackson.dtos.withEnum.DistanceEnumWithJsonFormat;
|
||||
import com.baeldung.jackson.dtos.withEnum.DistanceEnumWithValue;
|
||||
import com.baeldung.jackson.dtos.withEnum.MyDtoWithEnumCustom;
|
||||
import com.baeldung.jackson.dtos.withEnum.MyDtoWithEnumJsonFormat;
|
||||
import com.baeldung.jackson.enums.Distance;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
|
@ -24,10 +24,9 @@ public class JacksonSerializationEnumsUnitTest {
|
|||
@Test
|
||||
public final void whenSerializingASimpleEnum_thenCorrect() throws JsonParseException, IOException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final String enumAsString = mapper.writeValueAsString(TypeEnumSimple.TYPE1);
|
||||
System.out.println(enumAsString);
|
||||
final String enumAsString = mapper.writeValueAsString(DistanceEnumSimple.MILE);
|
||||
|
||||
assertThat(enumAsString, containsString("TYPE1"));
|
||||
assertThat(enumAsString, containsString("MILE"));
|
||||
}
|
||||
|
||||
// tests - enum with main value
|
||||
|
@ -35,10 +34,9 @@ public class JacksonSerializationEnumsUnitTest {
|
|||
@Test
|
||||
public final void whenSerializingAEnumWithValue_thenCorrect() throws JsonParseException, IOException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final String enumAsString = mapper.writeValueAsString(TypeEnumWithValue.TYPE1);
|
||||
System.out.println(enumAsString);
|
||||
final String enumAsString = mapper.writeValueAsString(DistanceEnumWithValue.MILE);
|
||||
|
||||
assertThat(enumAsString, is("\"Type A\""));
|
||||
assertThat(enumAsString, is("1609.34"));
|
||||
}
|
||||
|
||||
// tests - enum
|
||||
|
@ -46,28 +44,25 @@ public class JacksonSerializationEnumsUnitTest {
|
|||
@Test
|
||||
public final void whenSerializingAnEnum_thenCorrect() throws JsonParseException, IOException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final String enumAsString = mapper.writeValueAsString(TypeEnum.TYPE1);
|
||||
final String enumAsString = mapper.writeValueAsString(DistanceEnumWithJsonFormat.MILE);
|
||||
|
||||
System.out.println(enumAsString);
|
||||
assertThat(enumAsString, containsString("\"name\":\"Type A\""));
|
||||
assertThat(enumAsString, containsString("\"meters\":1609.34"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenSerializingEntityWithEnum_thenCorrect() throws JsonParseException, IOException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final String enumAsString = mapper.writeValueAsString(new MyDtoWithEnum("a", 1, true, TypeEnum.TYPE1));
|
||||
final String enumAsString = mapper.writeValueAsString(new MyDtoWithEnumJsonFormat("a", 1, true, DistanceEnumWithJsonFormat.MILE));
|
||||
|
||||
System.out.println(enumAsString);
|
||||
assertThat(enumAsString, containsString("\"name\":\"Type A\""));
|
||||
assertThat(enumAsString, containsString("\"meters\":1609.34"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenSerializingArrayOfEnums_thenCorrect() throws JsonParseException, IOException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final String json = mapper.writeValueAsString(new TypeEnum[] { TypeEnum.TYPE1, TypeEnum.TYPE2 });
|
||||
final String json = mapper.writeValueAsString(new DistanceEnumWithJsonFormat[] { DistanceEnumWithJsonFormat.MILE, DistanceEnumWithJsonFormat.KILOMETER });
|
||||
|
||||
System.out.println(json);
|
||||
assertThat(json, containsString("\"name\":\"Type A\""));
|
||||
assertThat(json, containsString("\"meters\":1609.34"));
|
||||
}
|
||||
|
||||
// tests - enum with custom serializer
|
||||
|
@ -75,10 +70,9 @@ public class JacksonSerializationEnumsUnitTest {
|
|||
@Test
|
||||
public final void givenCustomSerializer_whenSerializingEntityWithEnum_thenCorrect() throws JsonParseException, IOException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final String enumAsString = mapper.writeValueAsString(new MyDtoWithEnumCustom("a", 1, true, TypeEnumWithCustomSerializer.TYPE1));
|
||||
final String enumAsString = mapper.writeValueAsString(new MyDtoWithEnumCustom("a", 1, true, Distance.MILE));
|
||||
|
||||
System.out.println(enumAsString);
|
||||
assertThat(enumAsString, containsString("\"name\":\"Type A\""));
|
||||
assertThat(enumAsString, containsString("\"meters\":1609.34"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue