-
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '2.18' of github.com:FasterXML/jackson-dataformat-xml in…
…to 2.18
- Loading branch information
Showing
2 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
...17/java/com/fasterxml/jackson/dataformat/xml/records/failing/XmlWrapperRecord517Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.fasterxml.jackson.dataformat.xml.records.failing; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
|
||
import com.fasterxml.jackson.dataformat.xml.XmlTestBase; | ||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; | ||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; | ||
|
||
// [databind#517] XML wrapper doesn't work with java records | ||
// Equivalent to on in jdk17/.../deser/XmlWrapperRecord517Test.java | ||
public class XmlWrapperRecord517Test | ||
extends XmlTestBase | ||
{ | ||
|
||
public record Request( | ||
@JacksonXmlElementWrapper(localName = "messages") | ||
@JacksonXmlProperty(localName = "message") | ||
List<Message> messages | ||
) { | ||
public Request {} | ||
|
||
private Request() {this(null);} | ||
} | ||
|
||
public record Message(String text) { | ||
public Message { | ||
} | ||
|
||
private Message() { | ||
this(null); | ||
} | ||
} | ||
|
||
private final String expectedXML = | ||
"<Request>" + | ||
"<messages>" + | ||
"<message>" + | ||
"<text>Hello, World!</text>" + | ||
"</message>" + | ||
"</messages>" + | ||
"</Request>"; | ||
|
||
@Test | ||
public void testWrapper() throws Exception { | ||
XmlWrapperRecord517Test.Request request = new Request(List.of(new Message("Hello, World!"))); | ||
|
||
// test serialization | ||
String xml = newMapper().writeValueAsString(request); | ||
assertEquals(expectedXML, xml); | ||
|
||
// test deserialization | ||
Request result = newMapper().readValue(xml, Request.class); | ||
|
||
assertEquals(request.messages().size(), result.messages().size()); | ||
assertEquals(request.messages().get(0).text(), result.messages().get(0).text()); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
src/test/java/com/fasterxml/jackson/dataformat/xml/deser/XmlWrapperClass517Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.fasterxml.jackson.dataformat.xml.deser; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.junit.Test; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.dataformat.xml.XmlTestBase; | ||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; | ||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; | ||
|
||
// [databind#517] XML wrapper doesn't work with java records | ||
// Equivalent to on in jdk17/.../records/XmlWrapperRecord517Test.java | ||
public class XmlWrapperClass517Test | ||
extends XmlTestBase | ||
{ | ||
public static final class Request { | ||
@JacksonXmlElementWrapper(localName = "messages") | ||
@JacksonXmlProperty(localName = "message") | ||
private final List<Message> messages; | ||
|
||
private Request() { this.messages = null; } | ||
public Request(List<Message> messages) { this.messages = messages; } | ||
|
||
public List<Message> getMessages() { return messages; } | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof Request)) return false; | ||
return Objects.equals(messages, ((Request)o).messages); | ||
} | ||
|
||
@Override | ||
public int hashCode() { return Objects.hash(messages); } | ||
|
||
@Override | ||
public String toString() { return "Request{messages=" + messages + '}'; } | ||
} | ||
|
||
public static final class Message { | ||
|
||
private final String text; | ||
|
||
private Message() { this.text = null; } | ||
public Message(String text) { this.text = text; } | ||
|
||
public String getText() { return text; } | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof Message)) return false; | ||
Message message = (Message) o; | ||
return Objects.equals(text, message.text); | ||
} | ||
|
||
@Override | ||
public int hashCode() { return Objects.hash(text); } | ||
|
||
@Override | ||
public String toString() { return "Message{text='" + text + "\'}"; } | ||
} | ||
|
||
private final ObjectMapper mapper = newMapper(); | ||
|
||
private final String expectedXML = | ||
"<Request>" + | ||
"<messages>" + | ||
"<message>" + | ||
"<text>given text</text>" + | ||
"</message>" + | ||
"</messages>" + | ||
"</Request>"; | ||
|
||
@Test | ||
public void testShouldSerialize() throws Exception { | ||
Request givenRequest = _createRequest("given text"); | ||
|
||
String actualXml = mapper.writeValueAsString(givenRequest); | ||
|
||
assertEquals(expectedXML, actualXml); | ||
} | ||
|
||
@Test | ||
public void testShouldDeserialize() throws Exception { | ||
Request expected = _createRequest("given text"); | ||
|
||
Request actualRequest = mapper.readValue(expectedXML, Request.class); | ||
|
||
assertEquals(expected, actualRequest); | ||
} | ||
|
||
private Request _createRequest(String givenText) { | ||
List<Message> messages = new ArrayList<>(); | ||
messages.add(new Message(givenText)); | ||
return new Request(messages); | ||
} | ||
|
||
} |