From eed331dbe277948effa49239cef608fd9e9b9494 Mon Sep 17 00:00:00 2001 From: Eric Bottard Date: Fri, 12 Jul 2013 15:22:46 +0200 Subject: [PATCH] #93 - Fix VndErrors un-marshalling when using Jackson. Added equals() and hashCode() methods on VndErrors and VndError. Original pull request: #94. --- .../springframework/hateoas/VndErrors.java | 95 +++++++++++++++++-- .../hateoas/VndErrorsMarshallingTest.java | 32 ++++++- src/test/resources/vnderror.xml | 2 +- 3 files changed, 117 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/springframework/hateoas/VndErrors.java b/src/main/java/org/springframework/hateoas/VndErrors.java index 4e957813..b7c3feed 100644 --- a/src/main/java/org/springframework/hateoas/VndErrors.java +++ b/src/main/java/org/springframework/hateoas/VndErrors.java @@ -27,8 +27,6 @@ import javax.xml.bind.annotation.XmlType; import org.springframework.util.Assert; -import com.fasterxml.jackson.annotation.JsonValue; - /** * A representation model class to be rendered as specified for the media type {@code application/vnd.error}. * @@ -38,7 +36,7 @@ import com.fasterxml.jackson.annotation.JsonValue; @XmlRootElement(name = "errors") public class VndErrors implements Iterable { - @XmlElement(name = "error") + @XmlElement(name = "error")// private final List vndErrors; /** @@ -68,6 +66,20 @@ public class VndErrors implements Iterable { this.vndErrors.addAll(Arrays.asList(errors)); } + /** + * Creates a new {@link VndErrors} wrapper for the given {@link VndErrors}. + * + * @param errors must not be {@literal null} or empty. + */ + @com.fasterxml.jackson.annotation.JsonCreator + @org.codehaus.jackson.annotate.JsonCreator + public VndErrors(List errors) { + + Assert.notNull(errors, "Errors must not be null!"); + Assert.isTrue(!errors.isEmpty(), "Errors must not be empty!"); + this.vndErrors = errors; + } + /** * Protected default constructor to allow JAXB marshalling. */ @@ -96,7 +108,7 @@ public class VndErrors implements Iterable { return vndErrors; } - /* + /* * (non-Javadoc) * @see java.lang.Iterable#iterator() */ @@ -105,6 +117,34 @@ public class VndErrors implements Iterable { return this.vndErrors.iterator(); } + /* + * (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + return vndErrors.hashCode(); + } + + /* + * (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + + if (this == obj) { + return true; + } + + if (!(obj instanceof VndErrors)) { + return false; + } + + VndErrors that = (VndErrors) obj; + return this.vndErrors.equals(that.vndErrors); + } + /** * A single {@link VndError}. * @@ -113,14 +153,14 @@ public class VndErrors implements Iterable { @XmlType public static class VndError extends ResourceSupport { - @com.fasterxml.jackson.annotation.JsonProperty - @org.codehaus.jackson.annotate.JsonProperty - @XmlAttribute + @com.fasterxml.jackson.annotation.JsonProperty// + @org.codehaus.jackson.annotate.JsonProperty// + @XmlAttribute// private final String logref; - @com.fasterxml.jackson.annotation.JsonProperty - @org.codehaus.jackson.annotate.JsonProperty - @XmlElement + @com.fasterxml.jackson.annotation.JsonProperty// + @org.codehaus.jackson.annotate.JsonProperty// + @XmlElement// private final String message; /** @@ -166,5 +206,40 @@ public class VndErrors implements Iterable { public String getMessage() { return message; } + + /* + * (non-Javadoc) + * @see org.springframework.hateoas.ResourceSupport#hashCode() + */ + @Override + public int hashCode() { + + int result = 17; + + result += 31 * logref.hashCode(); + result += 31 * message.hashCode(); + + return result; + } + + /* + * (non-Javadoc) + * @see org.springframework.hateoas.ResourceSupport#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + + if (obj == this) { + return true; + } + + if (!(obj instanceof VndError)) { + return false; + } + + VndError that = (VndError) obj; + + return this.logref.equals(that.logref) && this.message.equals(that.message); + } } } diff --git a/src/test/java/org/springframework/hateoas/VndErrorsMarshallingTest.java b/src/test/java/org/springframework/hateoas/VndErrorsMarshallingTest.java index 38bc68c9..ae6c6af2 100644 --- a/src/test/java/org/springframework/hateoas/VndErrorsMarshallingTest.java +++ b/src/test/java/org/springframework/hateoas/VndErrorsMarshallingTest.java @@ -20,6 +20,7 @@ import static org.junit.Assert.*; import java.io.FileInputStream; import java.io.IOException; +import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; import java.nio.MappedByteBuffer; @@ -28,6 +29,7 @@ import java.nio.charset.Charset; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.SerializationConfig.Feature; @@ -50,6 +52,7 @@ public class VndErrorsMarshallingTest { ObjectMapper jackson1Mapper; com.fasterxml.jackson.databind.ObjectMapper jackson2Mapper; Marshaller marshaller; + Unmarshaller unmarshaller; VndErrors errors; String jsonReference; @@ -74,9 +77,10 @@ public class VndErrorsMarshallingTest { JAXBContext context = JAXBContext.newInstance(VndErrors.class); marshaller = context.createMarshaller(); + unmarshaller = context.createUnmarshaller(); VndError error = new VndError("42", "Validation failed!", // - new Link("http://...", "help"), new Link("http://...", "describes")); + new Link("http://...", "describes"), new Link("http://...", "help")); errors = new VndErrors(error, error, error); } @@ -98,6 +102,32 @@ public class VndErrorsMarshallingTest { assertThat(writer.toString(), is(xmlReference)); } + /** + * @see #93, #94 + */ + @Test + public void jackson1UnMarshalling() throws Exception { + VndErrors actual = jackson1Mapper.readValue(jsonReference, VndErrors.class); + assertThat(actual, is(errors)); + } + + /** + * @see #93, #94 + */ + @Test + public void jackson2UnMarshalling() throws Exception { + assertThat(jackson2Mapper.readValue(jsonReference, VndErrors.class), is(errors)); + } + + /** + * @see #93, #94 + */ + @Test + public void jaxbUnMarshalling() throws Exception { + VndErrors actual = (VndErrors) unmarshaller.unmarshal(new StringReader(xmlReference)); + assertThat(actual, is(errors)); + } + private static String readFile(org.springframework.core.io.Resource resource) throws IOException { FileInputStream stream = new FileInputStream(resource.getFile()); diff --git a/src/test/resources/vnderror.xml b/src/test/resources/vnderror.xml index 4e3ba221..72b07134 100644 --- a/src/test/resources/vnderror.xml +++ b/src/test/resources/vnderror.xml @@ -1 +1 @@ -Validation failed!Validation failed!Validation failed! \ No newline at end of file +Validation failed!Validation failed!Validation failed! \ No newline at end of file