From 7d6362da73a0b972298a358e1fe48cdef2f938e4 Mon Sep 17 00:00:00 2001 From: huiyang Date: Sat, 18 Nov 2023 20:34:32 -0600 Subject: [PATCH] Improve test reliability by resolving nondeterministic order of Json --- activemq-stomp/pom.xml | 5 ++ .../activemq/transport/stomp/SamplePojo.java | 12 +++++ .../activemq/transport/stomp/StompTest.java | 47 ++++++++++++++++--- pom.xml | 5 ++ 4 files changed, 63 insertions(+), 6 deletions(-) diff --git a/activemq-stomp/pom.xml b/activemq-stomp/pom.xml index ed582985da..4bad4500cf 100644 --- a/activemq-stomp/pom.xml +++ b/activemq-stomp/pom.xml @@ -53,6 +53,11 @@ jettison true + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + true + diff --git a/activemq-stomp/src/test/java/org/apache/activemq/transport/stomp/SamplePojo.java b/activemq-stomp/src/test/java/org/apache/activemq/transport/stomp/SamplePojo.java index 9ed4ef4fd9..701c33daf2 100644 --- a/activemq-stomp/src/test/java/org/apache/activemq/transport/stomp/SamplePojo.java +++ b/activemq-stomp/src/test/java/org/apache/activemq/transport/stomp/SamplePojo.java @@ -52,4 +52,16 @@ public class SamplePojo implements Serializable { public void setName(String name) { this.name = name; } + + //implement equals + + @Override + public boolean equals(Object obj) { + if (obj instanceof SamplePojo) { + SamplePojo other = (SamplePojo) obj; + return name.equals(other.name) && city.equals(other.city); + } + return false; + } + } diff --git a/activemq-stomp/src/test/java/org/apache/activemq/transport/stomp/StompTest.java b/activemq-stomp/src/test/java/org/apache/activemq/transport/stomp/StompTest.java index 9b54d17efa..9f0d65e909 100644 --- a/activemq-stomp/src/test/java/org/apache/activemq/transport/stomp/StompTest.java +++ b/activemq-stomp/src/test/java/org/apache/activemq/transport/stomp/StompTest.java @@ -35,6 +35,9 @@ import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; import jakarta.jms.BytesMessage; import jakarta.jms.Connection; import jakarta.jms.JMSException; @@ -1090,7 +1093,7 @@ public class StompTest extends StompTestSupport { frame = stompConnection.receiveFrame(); - assertTrue(frame.trim().endsWith(xmlObject)); + compareFrameXML(frame, xmlObject); } @Test(timeout = 60000) @@ -1110,7 +1113,7 @@ public class StompTest extends StompTestSupport { frame = stompConnection.receiveFrame(); - assertTrue(frame.trim().endsWith(jsonObject)); + compareFrameJson(frame, jsonObject); } @Test(timeout = 60000) @@ -1131,7 +1134,7 @@ public class StompTest extends StompTestSupport { frame = stompConnection.receiveFrame(); - assertTrue(frame.trim().endsWith(xmlObject)); + compareFrameXML(frame, xmlObject); } @Test(timeout = 60000) @@ -1152,7 +1155,26 @@ public class StompTest extends StompTestSupport { frame = stompConnection.receiveFrame(); - assertTrue(frame.trim().endsWith(xmlObject)); + + + compareFrameXML(frame, xmlObject); + } + + private void compareFrameXML(String frame, String xmlObject) { + + String xmlReceived = frame.trim().substring(frame.indexOf("")); + + try { + //use jackson xml to compare + XmlMapper xmlMapper = new XmlMapper(); + SamplePojo pojoReceived = xmlMapper.readValue(xmlReceived, SamplePojo.class); + SamplePojo pojoObject = xmlMapper.readValue(xmlObject, SamplePojo.class); + + assertEquals(pojoReceived, pojoObject); + } catch (Exception e) { + fail("Exception while comparing XML: " + e.getMessage()); + } + } @Test(timeout = 60000) @@ -1177,7 +1199,7 @@ public class StompTest extends StompTestSupport { frame = stompConnection.receiveFrame(); - assertTrue(frame.trim().endsWith(xmlObject)); + compareFrameXML(frame, xmlObject); StompFrame xmlFrame = stompConnection.receive(); @@ -1336,7 +1358,20 @@ public class StompTest extends StompTestSupport { frame = stompConnection.receiveFrame(); - assertTrue(frame.trim().endsWith(jsonObject)); + compareFrameJson(frame, jsonObject); + } + + private void compareFrameJson(String frame, String jsonObject) { + String receivedJson = frame.trim().substring(frame.indexOf("{\"pojo\":{")); + try { + ObjectMapper mapper = new ObjectMapper(); + JsonNode pojoReceived = mapper.readTree(receivedJson); + JsonNode pojoObject = mapper.readTree(jsonObject); + + assertEquals(pojoReceived, pojoObject); + } catch (Exception e) { + fail("Exception while comparing JSON: " + e.getMessage()); + } } @Test(timeout = 60000) diff --git a/pom.xml b/pom.xml index d5c1b22626..261df6ee66 100644 --- a/pom.xml +++ b/pom.xml @@ -612,6 +612,11 @@ jackson-databind ${jackson-version} + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + ${jackson-version} +