Skip to content

Commit

Permalink
add some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkolson committed Aug 4, 2024
1 parent 416e938 commit 4248b13
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.hamburgchimps.jackson;

public class NoAnnotationExamplePojo {
private String example;
private String demo;

public NoAnnotationExamplePojo() {}

public NoAnnotationExamplePojo(String example, String demo) {
this.example = example;
this.demo = demo;
}

public String getExample() {
return example;
}

public void setExample(String example) {
this.example = example;
}

public String getDemo() {
return demo;
}

public void setDemo(String demo) {
this.demo = demo;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package de.hamburgchimps.jackson;

public record NoAnnotationExampleRecord(String example, String demo) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package de.hamburgchimps.jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.junit.jupiter.api.Test;

public class TestPojoDeserialization {
private static final XmlMapper xmlMapper = new XmlMapper();

private static final String NO_ANNOTATION_EXAMPLE_POJO_XML;
private static final String JSON_PROPERTY_NO_ARGS_EXAMPLE_POJO_XML;
private static final String XML_ANNOTATION_NO_ARGS_EXAMPLE_POJO_XML;
private static final String JSON_PROPERTY_WITH_ARG_EXAMPLE_POJO_XML;
private static final String XML_ANNOTATION_WITH_ARG_EXAMPLE_POJO_XML;

static {
try {
NO_ANNOTATION_EXAMPLE_POJO_XML = xmlMapper.writeValueAsString(new NoAnnotationExamplePojo("hooray", "oh no"));
JSON_PROPERTY_NO_ARGS_EXAMPLE_POJO_XML = xmlMapper.writeValueAsString(new JsonPropertyNoArgsExamplePojo("hooray", "oh no"));
XML_ANNOTATION_NO_ARGS_EXAMPLE_POJO_XML = xmlMapper.writeValueAsString(new XmlAnnotationNoArgsExamplePojo("hooray", "oh no"));
JSON_PROPERTY_WITH_ARG_EXAMPLE_POJO_XML = xmlMapper.writeValueAsString(new JsonPropertyWithArgExamplePojo("hooray", "oh no"));
XML_ANNOTATION_WITH_ARG_EXAMPLE_POJO_XML = xmlMapper.writeValueAsString(new XmlAnnotationWithArgExamplePojo("hooray", "oh no"));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

@Test
void NoAnnotation_pojo() throws JsonProcessingException {
xmlMapper.readValue(NO_ANNOTATION_EXAMPLE_POJO_XML, NoAnnotationExamplePojo.class);
}

@Test
void JsonProperty_pojo_no_args() throws JsonProcessingException {
xmlMapper.readValue(JSON_PROPERTY_NO_ARGS_EXAMPLE_POJO_XML, JsonPropertyNoArgsExamplePojo.class);
}

@Test
void JacksonXmlProperty_pojo_no_args() throws JsonProcessingException {
xmlMapper.readValue(XML_ANNOTATION_NO_ARGS_EXAMPLE_POJO_XML, XmlAnnotationNoArgsExamplePojo.class);
}

@Test
void JsonProperty_pojo_with_arg() throws JsonProcessingException {
xmlMapper.readValue(JSON_PROPERTY_WITH_ARG_EXAMPLE_POJO_XML, JsonPropertyWithArgExamplePojo.class);
}

@Test
void JacksonXmlProperty_pojo_with_arg() throws JsonProcessingException {
xmlMapper.readValue(XML_ANNOTATION_WITH_ARG_EXAMPLE_POJO_XML, XmlAnnotationWithArgExamplePojo.class);
}
}
46 changes: 46 additions & 0 deletions src/test/java/de/hamburgchimps/jackson/TestPojoSerialization.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package de.hamburgchimps.jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class TestPojoSerialization {
static final XmlMapper xmlMapper = new XmlMapper();

@BeforeAll
static void setUpMapper() {
xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
}

@Test
void NoAnnotation_pojo() throws JsonProcessingException {
var exampleFromNoAnnotationPojo = new NoAnnotationExamplePojo("hooray", "oh no");
System.out.println(xmlMapper.writeValueAsString(exampleFromNoAnnotationPojo));
}

@Test
void JsonProperty_pojo_no_args() throws JsonProcessingException {
var exampleFromJsonPropertyNoArgsPojo = new JsonPropertyNoArgsExamplePojo("hooray", "oh no");
System.out.println(xmlMapper.writeValueAsString(exampleFromJsonPropertyNoArgsPojo));
}

@Test
void JacksonXmlProperty_pojo_no_args() throws JsonProcessingException {
var exampleFromXmlAnnotationNoArgsExamplePojo = new XmlAnnotationNoArgsExamplePojo("hooray", "oh no");
System.out.println(xmlMapper.writeValueAsString(exampleFromXmlAnnotationNoArgsExamplePojo));
}

@Test
void JsonProperty_pojo_with_arg() throws JsonProcessingException {
var exampleFromJsonPropertyWithArgPojo = new JsonPropertyWithArgExamplePojo("hooray", "oh no");
System.out.println(xmlMapper.writeValueAsString(exampleFromJsonPropertyWithArgPojo));
}

@Test
void JacksonXmlProperty_pojo_with_arg() throws JsonProcessingException {
var exampleFromXmlAnnotationWithArgPojo = new XmlAnnotationWithArgExamplePojo("hooray", "oh no");
System.out.println(xmlMapper.writeValueAsString(exampleFromXmlAnnotationWithArgPojo));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,47 @@
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.junit.jupiter.api.Test;

public class TestDeserialization {
public class TestRecordDeserialization {
private static final XmlMapper xmlMapper = new XmlMapper();

private static final String JSON_PROPERTY_NO_ARGS_EXAMPLE_POJO_XML;
private static final String NO_ANNOTATION_EXAMPLE_RECORD_XML;
private static final String JSON_PROPERTY_NO_ARGS_EXAMPLE_RECORD_XML;
private static final String XML_ANNOTATION_NO_ARGS_EXAMPLE_POJO_XML;
private static final String XML_ANNOTATION_NO_ARGS_EXAMPLE_RECORD_XML;
private static final String JSON_PROPERTY_WITH_ARG_EXAMPLE_POJO_XML;
private static final String JSON_PROPERTY_WITH_ARG_EXAMPLE_RECORD_XML;
private static final String XML_ANNOTATION_WITH_ARG_EXAMPLE_POJO_XML;
private static final String XML_ANNOTATION_WITH_ARG_EXAMPLE_RECORD_XML;

static {
try {
JSON_PROPERTY_NO_ARGS_EXAMPLE_POJO_XML = xmlMapper.writeValueAsString(new JsonPropertyNoArgsExamplePojo("hooray", "oh no"));
NO_ANNOTATION_EXAMPLE_RECORD_XML = xmlMapper.writeValueAsString(new NoAnnotationExampleRecord("hooray", "oh no"));
JSON_PROPERTY_NO_ARGS_EXAMPLE_RECORD_XML = xmlMapper.writeValueAsString(new JsonPropertyNoArgsExampleRecord("hooray", "oh no"));
XML_ANNOTATION_NO_ARGS_EXAMPLE_POJO_XML = xmlMapper.writeValueAsString(new XmlAnnotationNoArgsExamplePojo("hooray", "oh no"));
XML_ANNOTATION_NO_ARGS_EXAMPLE_RECORD_XML = xmlMapper.writeValueAsString(new XmlAnnotationNoArgsExampleRecord("hooray", "oh no"));
JSON_PROPERTY_WITH_ARG_EXAMPLE_POJO_XML = xmlMapper.writeValueAsString(new JsonPropertyWithArgExamplePojo("hooray", "oh no"));
JSON_PROPERTY_WITH_ARG_EXAMPLE_RECORD_XML = xmlMapper.writeValueAsString(new JsonPropertyWithArgExampleRecord("hooray", "oh no"));
XML_ANNOTATION_WITH_ARG_EXAMPLE_POJO_XML = xmlMapper.writeValueAsString(new XmlAnnotationWithArgExamplePojo("hooray", "oh no"));
XML_ANNOTATION_WITH_ARG_EXAMPLE_RECORD_XML = xmlMapper.writeValueAsString(new XmlAnnotationNoArgsExampleRecord("hooray", "oh no"));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

@Test
void JsonProperty_pojo_no_args() throws JsonProcessingException {
xmlMapper.readValue(JSON_PROPERTY_NO_ARGS_EXAMPLE_POJO_XML, JsonPropertyNoArgsExamplePojo.class);
void NoAnnotation_record() throws JsonProcessingException {
xmlMapper.readValue(NO_ANNOTATION_EXAMPLE_RECORD_XML, NoAnnotationExampleRecord.class);
}

@Test
void JsonProperty_record_no_args() throws JsonProcessingException {
xmlMapper.readValue(JSON_PROPERTY_NO_ARGS_EXAMPLE_RECORD_XML, JsonPropertyNoArgsExampleRecord.class);
}

@Test
void JacksonXmlProperty_pojo_no_args() throws JsonProcessingException {
xmlMapper.readValue(XML_ANNOTATION_NO_ARGS_EXAMPLE_POJO_XML, XmlAnnotationNoArgsExamplePojo.class);
}

@Test
void JacksonXmlProperty_record_no_args() throws JsonProcessingException {
xmlMapper.readValue(XML_ANNOTATION_NO_ARGS_EXAMPLE_RECORD_XML, XmlAnnotationNoArgsExampleRecord.class);
}

@Test
void JsonProperty_pojo_with_arg() throws JsonProcessingException {
xmlMapper.readValue(JSON_PROPERTY_WITH_ARG_EXAMPLE_POJO_XML, JsonPropertyWithArgExamplePojo.class);
}

@Test
void JsonProperty_record_with_arg() throws JsonProcessingException {
xmlMapper.readValue(JSON_PROPERTY_WITH_ARG_EXAMPLE_RECORD_XML, JsonPropertyWithArgExampleRecord.class);
}

@Test
void JacksonXmlProperty_pojo_with_arg() throws JsonProcessingException {
xmlMapper.readValue(XML_ANNOTATION_WITH_ARG_EXAMPLE_POJO_XML, XmlAnnotationWithArgExamplePojo.class);
}

@Test
void JacksonXmlProperty_record_with_arg() throws JsonProcessingException {
xmlMapper.readValue(XML_ANNOTATION_WITH_ARG_EXAMPLE_RECORD_XML, XmlAnnotationWithArgExampleRecord.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package de.hamburgchimps.jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class TestRecordSerialization {
static final XmlMapper xmlMapper = new XmlMapper();

@BeforeAll
static void setUpMapper() {
xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
}

@Test
void NoAnnotation_record() throws JsonProcessingException {
var exampleFromNoAnnotationRecord = new NoAnnotationExampleRecord("hooray", "oh no");
System.out.println(xmlMapper.writeValueAsString(exampleFromNoAnnotationRecord));
}

@Test
void JsonProperty_record_no_args() throws JsonProcessingException {
var exampleFromJsonPropertyNoArgsRecord = new JsonPropertyNoArgsExampleRecord("hooray", "oh no");
System.out.println(xmlMapper.writeValueAsString(exampleFromJsonPropertyNoArgsRecord));
}

@Test
void JacksonXmlProperty_record_no_args() throws JsonProcessingException {
var exampleFromXmlAnnotationNoArgsExampleRecord = new XmlAnnotationNoArgsExampleRecord("hooray", "oh no");
System.out.println(xmlMapper.writeValueAsString(exampleFromXmlAnnotationNoArgsExampleRecord));
}

@Test
void JsonProperty_record_with_arg() throws JsonProcessingException {
var exampleFromJsonPropertyWithArgRecord = new JsonPropertyWithArgExampleRecord("hooray", "oh no");
System.out.println(xmlMapper.writeValueAsString(exampleFromJsonPropertyWithArgRecord));
}

@Test
void JacksonXmlProperty_record_with_arg() throws JsonProcessingException {
var exampleFromXmlAnnotationWithArgRecord = new XmlAnnotationWithArgExampleRecord("hooray", "oh no");
System.out.println(xmlMapper.writeValueAsString(exampleFromXmlAnnotationWithArgRecord));
}
}
57 changes: 0 additions & 57 deletions src/test/java/de/hamburgchimps/jackson/TestSerialization.java

This file was deleted.

0 comments on commit 4248b13

Please sign in to comment.