INT-3503 equals & hashCode for ToStringFriendlyJN

JIRA: https://jira.spring.io/browse/INT-3503

**Cherry-pick to 4.0.x**

PR Comments and test
This commit is contained in:
Artem Bilan
2014-09-02 14:10:35 +03:00
committed by Gary Russell
parent be4fd62cea
commit 65e2430079
2 changed files with 33 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,14 +35,16 @@ import org.springframework.util.Assert;
* A SpEL {@link PropertyAccessor} that knows how to read on Jackson JSON objects.
*
* @author Eric Bottard
* @author Artem Bilan
* @since 3.0
*/
public class JsonPropertyAccessor implements PropertyAccessor {
/**
* The kind of types this can work with.
*/
private static final Class<?>[] SUPPORTED_CLASSES = new Class<?>[] { String.class, ToStringFriendlyJsonNode.class,
ObjectNode.class, ArrayNode.class };
private static final Class<?>[] SUPPORTED_CLASSES = new Class<?>[] {String.class, ToStringFriendlyJsonNode.class,
ObjectNode.class, ArrayNode.class};
// Note: ObjectMapper is thread-safe
private ObjectMapper objectMapper = new ObjectMapper();
@@ -144,6 +146,7 @@ public class JsonPropertyAccessor implements PropertyAccessor {
}
public static class ToStringFriendlyJsonNode {
private final JsonNode node;
public ToStringFriendlyJsonNode(JsonNode node) {
@@ -159,8 +162,20 @@ public class JsonPropertyAccessor implements PropertyAccessor {
else {
return node.toString();
}
}
@Override
public boolean equals(Object o) {
return this == o
|| (!(o == null || getClass() != o.getClass())
&& this.node.equals(((ToStringFriendlyJsonNode) o).node));
}
@Override
public int hashCode() {
return this.node.toString().hashCode();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,7 +17,10 @@
package org.springframework.integration.json;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
@@ -31,6 +34,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* Tests for {@link JsonPropertyAccessor}.
*
* @author Eric Bottard
* @author Artem Bilan
* @since 3.0
*/
public class JsonPropertyAccessorTests {
@@ -48,8 +53,14 @@ public class JsonPropertyAccessorTests {
@Test
public void testSimpleLookup() throws Exception {
Object json = mapper.readTree("{\"foo\": \"bar\"}");
Object actual = evaluate(json, "foo", Object.class);
assertEquals("bar", actual.toString());
Object value = evaluate(json, "foo", Object.class);
assertThat(value, Matchers.instanceOf(JsonPropertyAccessor.ToStringFriendlyJsonNode.class));
assertEquals("bar", value.toString());
Object json2 = mapper.readTree("{\"foo\": \"bar\"}");
Object value2 = evaluate(json2, "foo", Object.class);
assertThat(value2, Matchers.instanceOf(JsonPropertyAccessor.ToStringFriendlyJsonNode.class));
assertTrue(value.equals(value2));
assertEquals(value.hashCode(), value2.hashCode());
}
@Test(expected = SpelEvaluationException.class)