Java源码示例:org.custommonkey.xmlunit.DetailedDiff
示例1
@Override
public MatchResult match(String value) {
DetailedDiff diff = HtmlUtils.compare(expectedValue, value);
return new MatchResult() {
@Override
public boolean isExactMatch() {
return diff.similar();
}
@Override
public double getDistance() {
return diff.getAllDifferences().size();
}
};
}
示例2
private <T> void unmarshalAndMarshal(final Class<T> type, final java.lang.String xmlFileName, final java.lang.String expectedFile) throws Exception {
final InputStream in = getInputStream(xmlFileName);
assertNotNull(in);
final Object object = JaxbOpenejbJar2.unmarshal(type, in);
final String actual = JaxbOpenejbJar2.marshal(type, object);
final String expected;
if (xmlFileName.equals(expectedFile)) {
expected = readContent(getInputStream(xmlFileName));
} else {
expected = readContent(getInputStream(expectedFile));
}
XMLUnit.setIgnoreWhitespace(true);
try {
final Diff myDiff = new DetailedDiff(new Diff(expected, actual));
assertTrue("Files are not similar " + myDiff, myDiff.similar());
} catch (final AssertionFailedError e) {
e.printStackTrace();
assertEquals(expected, actual);
throw e;
}
}
示例3
public void marshallAndUnmarshall(final String xmlFile) throws Exception {
final InputStream in = this.getClass().getClassLoader().getResourceAsStream(xmlFile);
final String expected = readContent(in);
final Object object = JaxbWls.unmarshal(WeblogicEjbJar.class, new ByteArrayInputStream(expected.getBytes()));
final JAXBElement element = (JAXBElement) object;
assertTrue(element.getValue() instanceof WeblogicEjbJar);
final String actual = JaxbWls.marshal(WeblogicEjbJar.class, element);
XMLUnit.setIgnoreWhitespace(true);
try {
final Diff myDiff = new DetailedDiff(new Diff(expected, actual));
assertTrue("Files are not similar " + myDiff, myDiff.similar());
} catch (final AssertionFailedError e) {
assertEquals(expected, actual);
throw e;
}
}
示例4
@Test
public void writeDeliverResponse() throws Exception {
DeliverResponse deliverResponse = new DeliverResponse();
deliverResponse.setErrorCode(5);
deliverResponse.setErrorMessage("Success");
StringWriter sw = new StringWriter();
SxmpWriter.write(sw, deliverResponse);
logger.debug(sw.toString());
StringBuilder expectedXML = new StringBuilder(200)
.append("<?xml version=\"1.0\"?>\n")
.append("<operation type=\"deliver\">\n")
.append(" <deliverResponse>\n")
.append(" <error code=\"5\" message=\"Success\"/>\n")
.append(" </deliverResponse>\n")
.append("</operation>\n")
.append("");
// compare to actual correct submit response
XMLUnit.setIgnoreWhitespace(true);
Diff myDiff = new Diff(expectedXML.toString(), sw.toString());
DetailedDiff myDetailedDiff = new DetailedDiff(myDiff);
Assert.assertTrue("XML are similar " + myDetailedDiff, myDetailedDiff.similar());
}
示例5
@Test
public void writeDeliveryReportResponse() throws Exception {
DeliveryReportResponse deliveryResponse = new DeliveryReportResponse();
deliveryResponse.setErrorCode(5);
deliveryResponse.setErrorMessage("Success");
StringWriter sw = new StringWriter();
SxmpWriter.write(sw, deliveryResponse);
logger.debug(sw.toString());
StringBuilder expectedXML = new StringBuilder(200)
.append("<?xml version=\"1.0\"?>\n")
.append("<operation type=\"deliveryReport\">\n")
.append(" <deliveryReportResponse>\n")
.append(" <error code=\"5\" message=\"Success\"/>\n")
.append(" </deliveryReportResponse>\n")
.append("</operation>\n")
.append("");
// compare to actual correct submit response
XMLUnit.setIgnoreWhitespace(true);
Diff myDiff = new Diff(expectedXML.toString(), sw.toString());
DetailedDiff myDetailedDiff = new DetailedDiff(myDiff);
Assert.assertTrue("XML are similar " + myDetailedDiff, myDetailedDiff.similar());
}
示例6
public static XmlAsserter.AssertResult assertXml(String expected, String actual) {
XmlAsserter.AssertResult result = new XmlAsserter.AssertResult();
try {
LOG.debug("expected : " + linearize(expected));
LOG.debug("actual : " + linearize(actual));
Diff diff = new Diff(expected, actual);
diff.overrideDifferenceListener(listener);
diff.overrideElementQualifier(qualifier);
result.setSimilar(diff.similar());
LOG.debug("Similar : " + result.isSimilar());
DetailedDiff detDiff = new DetailedDiff(diff);
List<Difference> differences = detDiff.getAllDifferences();
Iterator var7 = differences.iterator();
while(var7.hasNext()) {
Difference difference = (Difference)var7.next();
if (!difference.isRecoverable()) {
LOG.debug(difference.toString() + " expected :" + difference.getControlNodeDetail() + " but was :" + difference.getTestNodeDetail() + " " + difference.getDescription());
result.getDifferences().add(difference);
}
}
} catch (Exception var8) {
LOG.error(var8.getMessage(), var8);
Assert.fail(var8.getMessage());
}
return result;
}
示例7
/**
* BETA version of APOGEN-DOM-differencing mechanism
*
* @param doc1
* @param doc2
* @return list of Differences
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
*/
@SuppressWarnings("unchecked")
public static List<Difference> customisedDomDiff(String string, String string2)
throws ParserConfigurationException, SAXException, IOException {
org.w3c.dom.Document doc1 = asDocument(string, true);
org.w3c.dom.Document doc2 = asDocument(string2, true);
XMLUnit.setControlParser("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
XMLUnit.setTestParser("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
XMLUnit.setSAXParserFactory("org.apache.xerces.jaxp.SAXParserFactoryImpl");
XMLUnit.setNormalizeWhitespace(true);
XMLUnit.setIgnoreAttributeOrder(true);
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreComments(true);
XMLUnit.setNormalize(true);
XMLUnit.setIgnoreDiffBetweenTextAndCDATA(false);
Diff d = new Diff(doc1, doc2);
DetailedDiff dd = new DetailedDiff(d);
dd.overrideDifferenceListener(new DomDifferenceListener());
dd.overrideElementQualifier(null);
return dd.getAllDifferences();
}
示例8
@SuppressWarnings("unchecked")
private static void logUnrecoverableDifferences(DetailedDiff diff) {
for (Difference difference : (List<Difference>) diff.getAllDifferences()) {
if (!difference.isRecoverable()) {
LOG.error(difference.toString()); // NOSONAR
}
}
}
示例9
/**
* Customization of the XmlUnit assertXMLEqual.
*
* @param xsdFileName XML schema file name
* @param expected expected result
* @param result actual result
*/
protected void compare(
final String xsdFileName,
final Document expected,
final Document result) {
boolean oldIgnore = XMLUnit.getIgnoreWhitespace();
XMLUnit.setIgnoreWhitespace(true);
try {
DetailedDiff d = new DetailedDiff(new Diff(expected, result));
assertTrue(xsdFileName + ": expected pieces to be identical, "
+ d.toString(), d.identical());
} finally {
XMLUnit.setIgnoreWhitespace(oldIgnore);
}
}
示例10
private void assertXMLEquals(String expectedXML, String actualXML) throws SAXException, IOException {
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreAttributeOrder(true);
XMLUnit.setNormalize(true);
DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(expectedXML, actualXML));
List<?> allDifferences = diff.getAllDifferences();
Assert.assertEquals("XML differences found: " + diff.toString(), 0, allDifferences.size());
}
示例11
public static void printDetailedDiff(Diff diff, PrintStream ps) {
DetailedDiff detDiff = new DetailedDiff(diff);
for (Object object : detDiff.getAllDifferences()) {
Difference difference = (Difference) object;
ps.println(difference);
}
}
示例12
public void testAll() throws Exception {
final JAXBContext ctx = JAXBContextFactory.newInstance(OpenejbJar.class);
final Unmarshaller unmarshaller = ctx.createUnmarshaller();
final InputStream in = this.getClass().getClassLoader().getResourceAsStream("openejb-jar.xml");
final String expected = readContent(in);
unmarshaller.setEventHandler(new TestValidationEventHandler());
final Object object = unmarshaller.unmarshal(new ByteArrayInputStream(expected.getBytes()));
assertTrue(object instanceof OpenejbJar);
final Marshaller marshaller = ctx.createMarshaller();
marshaller.setProperty("jaxb.formatted.output", true);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
marshaller.marshal(object, baos);
final String actual = new String(baos.toByteArray());
XMLUnit.setIgnoreWhitespace(true);
try {
final Diff myDiff = new DetailedDiff(new Diff(expected, actual));
assertTrue("Files are not similar " + myDiff, myDiff.similar());
} catch (final AssertionFailedError e) {
e.printStackTrace();
assertEquals(expected, actual);
throw e;
}
}
示例13
@Test
public void writeSubmitResponse() throws Exception {
SubmitResponse submitResponse = new SubmitResponse();
submitResponse.setErrorCode(5);
submitResponse.setErrorMessage("Success");
submitResponse.setTicketId("THISISTICKET");
StringWriter sw = new StringWriter();
SxmpWriter.write(sw, submitResponse);
logger.debug(sw.toString());
StringBuilder expectedXML = new StringBuilder(200)
.append("<?xml version=\"1.0\"?>\n")
.append("<operation type=\"submit\">\n")
.append(" <submitResponse>\n")
.append(" <error code=\"5\" message=\"Success\"/>\n")
.append(" <ticketId>THISISTICKET</ticketId>\n")
.append(" </submitResponse>\n")
.append("</operation>\n")
.append("");
// compare to actual correct submit response
XMLUnit.setIgnoreWhitespace(true);
Diff myDiff = new Diff(expectedXML.toString(), sw.toString());
DetailedDiff myDetailedDiff = new DetailedDiff(myDiff);
Assert.assertTrue("XML are similar " + myDetailedDiff, myDetailedDiff.similar());
}
示例14
@Test
public void writeSubmitRequest0() throws Exception {
SubmitRequest request = new SubmitRequest();
request.setAccount(new Account("customer1", "test1"));
request.setOperatorId(20);
request.setReferenceId("TESTREF");
request.setSourceAddress(new MobileAddress(MobileAddress.Type.NETWORK, "40404"));
request.setDestinationAddress(new MobileAddress(MobileAddress.Type.INTERNATIONAL, "+13135551212"));
request.setText("Hello World");
StringWriter sw = new StringWriter();
SxmpWriter.write(sw, request);
logger.debug(sw.toString());
StringBuilder expectedXML = new StringBuilder(200)
.append("<?xml version=\"1.0\"?>\n")
.append("<operation type=\"submit\">\n")
.append(" <account username=\"customer1\" password=\"test1\"/>\n")
.append(" <submitRequest referenceId=\"TESTREF\">\n")
.append(" <operatorId>20</operatorId>\n")
.append(" <priority>0</priority>\n")
.append(" <deliveryReport>false</deliveryReport>\n")
.append(" <sourceAddress type=\"network\">40404</sourceAddress>\n")
.append(" <destinationAddress type=\"international\">+13135551212</destinationAddress>\n")
.append(" <text encoding=\"UTF-8\">48656C6C6F20576F726C64</text>\n")
.append(" </submitRequest>\n")
.append("</operation>\n")
.append("");
// compare to actual correct submit response
XMLUnit.setIgnoreWhitespace(true);
Diff myDiff = new Diff(expectedXML.toString(), sw.toString());
DetailedDiff myDetailedDiff = new DetailedDiff(myDiff);
Assert.assertTrue("XML are similar " + myDetailedDiff, myDetailedDiff.similar());
}
示例15
@Test
public void writeSubmitRequestWithPriority() throws Exception {
SubmitRequest request = new SubmitRequest();
request.setAccount(new Account("customer1", "test1"));
request.setOperatorId(20);
request.setReferenceId("TESTREF");
request.setSourceAddress(new MobileAddress(MobileAddress.Type.NETWORK, "40404"));
request.setDestinationAddress(new MobileAddress(MobileAddress.Type.INTERNATIONAL, "+13135551212"));
request.setText("Hello World");
request.setPriority(Priority.URGENT);
StringWriter sw = new StringWriter();
SxmpWriter.write(sw, request);
logger.debug(sw.toString());
StringBuilder expectedXML = new StringBuilder(200)
.append("<?xml version=\"1.0\"?>\n")
.append("<operation type=\"submit\">\n")
.append(" <account username=\"customer1\" password=\"test1\"/>\n")
.append(" <submitRequest referenceId=\"TESTREF\">\n")
.append(" <operatorId>20</operatorId>\n")
.append(" <priority>2</priority>\n")
.append(" <deliveryReport>false</deliveryReport>\n")
.append(" <sourceAddress type=\"network\">40404</sourceAddress>\n")
.append(" <destinationAddress type=\"international\">+13135551212</destinationAddress>\n")
.append(" <text encoding=\"UTF-8\">48656C6C6F20576F726C64</text>\n")
.append(" </submitRequest>\n")
.append("</operation>\n")
.append("");
// compare to actual correct submit response
XMLUnit.setIgnoreWhitespace(true);
Diff myDiff = new Diff(expectedXML.toString(), sw.toString());
DetailedDiff myDetailedDiff = new DetailedDiff(myDiff);
Assert.assertTrue("XML are similar " + myDetailedDiff, myDetailedDiff.similar());
}
示例16
@Test
public void writeSubmitRequestWithLatin1() throws Exception {
SubmitRequest request = new SubmitRequest();
request.setAccount(new Account("customer1", "test1"));
request.setOperatorId(20);
request.setReferenceId("TESTREF");
request.setSourceAddress(new MobileAddress(MobileAddress.Type.NETWORK, "40404"));
request.setDestinationAddress(new MobileAddress(MobileAddress.Type.INTERNATIONAL, "+13135551212"));
request.setText("Hello World", TextEncoding.ISO_8859_1);
StringWriter sw = new StringWriter();
SxmpWriter.write(sw, request);
logger.debug(sw.toString());
StringBuilder expectedXML = new StringBuilder(200)
.append("<?xml version=\"1.0\"?>\n")
.append("<operation type=\"submit\">\n")
.append(" <account username=\"customer1\" password=\"test1\"/>\n")
.append(" <submitRequest referenceId=\"TESTREF\">\n")
.append(" <operatorId>20</operatorId>\n")
.append(" <priority>0</priority>\n")
.append(" <deliveryReport>false</deliveryReport>\n")
.append(" <sourceAddress type=\"network\">40404</sourceAddress>\n")
.append(" <destinationAddress type=\"international\">+13135551212</destinationAddress>\n")
.append(" <text encoding=\"ISO-8859-1\">48656C6C6F20576F726C64</text>\n")
.append(" </submitRequest>\n")
.append("</operation>\n")
.append("");
// compare to actual correct submit response
XMLUnit.setIgnoreWhitespace(true);
Diff myDiff = new Diff(expectedXML.toString(), sw.toString());
DetailedDiff myDetailedDiff = new DetailedDiff(myDiff);
Assert.assertTrue("XML are similar " + myDetailedDiff, myDetailedDiff.similar());
}
示例17
@Test
public void writeSubmitRequestWithUnicodeCharsInUTF8() throws Exception {
SubmitRequest request = new SubmitRequest();
request.setAccount(new Account("customer1", "test1"));
request.setOperatorId(20);
request.setReferenceId("TESTREF");
request.setSourceAddress(new MobileAddress(MobileAddress.Type.NETWORK, "40404"));
request.setDestinationAddress(new MobileAddress(MobileAddress.Type.INTERNATIONAL, "+13135551212"));
request.setTextEncoding(TextEncoding.UTF_8);
String text = "\u20AC\u0623\u0647\u0644\u0627\u0020\u0647\u0630\u0647\u0020\u0627\u0644\u062a\u062c\u0631\u0628\u0629\u0020\u0627\u0644\u0623\u0648\u0644\u0649";
request.setText(text);
StringWriter sw = new StringWriter();
SxmpWriter.write(sw, request);
logger.debug(sw.toString());
StringBuilder expectedXML = new StringBuilder(200)
.append("<?xml version=\"1.0\"?>\n")
.append("<operation type=\"submit\">\n")
.append(" <account username=\"customer1\" password=\"test1\"/>\n")
.append(" <submitRequest referenceId=\"TESTREF\">\n")
.append(" <operatorId>20</operatorId>\n")
.append(" <priority>0</priority>\n")
.append(" <deliveryReport>false</deliveryReport>\n")
.append(" <sourceAddress type=\"network\">40404</sourceAddress>\n")
.append(" <destinationAddress type=\"international\">+13135551212</destinationAddress>\n")
.append(" <text encoding=\"UTF-8\">E282ACD8A3D987D984D8A720D987D8B0D98720D8A7D984D8AAD8ACD8B1D8A8D8A920D8A7D984D8A3D988D984D989</text>\n")
.append(" </submitRequest>\n")
.append("</operation>\n")
.append("");
// compare to actual correct submit response
XMLUnit.setIgnoreWhitespace(true);
Diff myDiff = new Diff(expectedXML.toString(), sw.toString());
DetailedDiff myDetailedDiff = new DetailedDiff(myDiff);
Assert.assertTrue("XML are similar " + myDetailedDiff, myDetailedDiff.similar());
}
示例18
@Test
public void writeDeliverRequest0() throws Exception {
DeliverRequest request = new DeliverRequest();
request.setAccount(new Account("customer1", "test1"));
request.setOperatorId(20);
request.setReferenceId("TESTREF");
request.setSourceAddress(new MobileAddress(MobileAddress.Type.NETWORK, "40404"));
request.setDestinationAddress(new MobileAddress(MobileAddress.Type.INTERNATIONAL, "+13135551212"));
request.setText("Hello World");
StringWriter sw = new StringWriter();
SxmpWriter.write(sw, request);
logger.debug(sw.toString());
StringBuilder expectedXML = new StringBuilder(200)
.append("<?xml version=\"1.0\"?>\n")
.append("<operation type=\"deliver\">\n")
.append(" <account username=\"customer1\" password=\"test1\"/>\n")
.append(" <deliverRequest referenceId=\"TESTREF\">\n")
.append(" <operatorId>20</operatorId>\n")
.append(" <priority>0</priority>\n")
.append(" <sourceAddress type=\"network\">40404</sourceAddress>\n")
.append(" <destinationAddress type=\"international\">+13135551212</destinationAddress>\n")
.append(" <text encoding=\"UTF-8\">48656C6C6F20576F726C64</text>\n")
.append(" </deliverRequest>\n")
.append("</operation>\n")
.append("");
// compare to actual correct submit response
XMLUnit.setIgnoreWhitespace(true);
Diff myDiff = new Diff(expectedXML.toString(), sw.toString());
DetailedDiff myDetailedDiff = new DetailedDiff(myDiff);
Assert.assertTrue("XML are similar " + myDetailedDiff, myDetailedDiff.similar());
}
示例19
@Test
public void writeDeliverRequestWithTicketId0() throws Exception {
DeliverRequest request = new DeliverRequest();
request.setAccount(new Account("customer1", "test1"));
request.setOperatorId(20);
request.setTicketId("THISISATESTTICKETID");
request.setReferenceId("TESTREF");
request.setSourceAddress(new MobileAddress(MobileAddress.Type.NETWORK, "40404"));
request.setDestinationAddress(new MobileAddress(MobileAddress.Type.INTERNATIONAL, "+13135551212"));
request.setText("Hello World");
StringWriter sw = new StringWriter();
SxmpWriter.write(sw, request);
logger.debug(sw.toString());
StringBuilder expectedXML = new StringBuilder(200)
.append("<?xml version=\"1.0\"?>\n")
.append("<operation type=\"deliver\">\n")
.append(" <account username=\"customer1\" password=\"test1\"/>\n")
.append(" <deliverRequest referenceId=\"TESTREF\">\n")
.append(" <ticketId>THISISATESTTICKETID</ticketId>")
.append(" <operatorId>20</operatorId>\n")
.append(" <priority>0</priority>\n")
.append(" <sourceAddress type=\"network\">40404</sourceAddress>\n")
.append(" <destinationAddress type=\"international\">+13135551212</destinationAddress>\n")
.append(" <text encoding=\"UTF-8\">48656C6C6F20576F726C64</text>\n")
.append(" </deliverRequest>\n")
.append("</operation>\n")
.append("");
// compare to actual correct submit response
XMLUnit.setIgnoreWhitespace(true);
Diff myDiff = new Diff(expectedXML.toString(), sw.toString());
DetailedDiff myDetailedDiff = new DetailedDiff(myDiff);
Assert.assertTrue("XML are similar " + myDetailedDiff, myDetailedDiff.similar());
}
示例20
@Test
public void writeDeliveryReportRequest0() throws Exception {
DeliveryReportRequest request = new DeliveryReportRequest();
request.setAccount(new Account("customer1", "test1"));
//request.setOperatorId(20);
request.setReferenceId("TESTREF");
request.setStatus(new DeliveryStatus(5, "Expired"));
request.setTicketId("000:20090118002220948:000");
//request.setSourceAddress(new MobileAddress(MobileAddress.Type.NETWORK, "40404"));
//request.setDestinationAddress(new MobileAddress(MobileAddress.Type.INTERNATIONAL, "+13135551212"));
//request.setText("Hello World");
StringWriter sw = new StringWriter();
SxmpWriter.write(sw, request);
logger.debug(sw.toString());
StringBuilder expectedXML = new StringBuilder(200)
.append("<?xml version=\"1.0\"?>\n")
.append("<operation type=\"deliveryReport\">\n")
.append(" <account username=\"customer1\" password=\"test1\"/>\n")
.append(" <deliveryReportRequest referenceId=\"TESTREF\">\n")
.append(" <status code=\"5\" message=\"Expired\"/>\n")
.append(" <ticketId>000:20090118002220948:000</ticketId>\n")
.append(" </deliveryReportRequest>\n")
.append("</operation>\n")
.append("");
// compare to actual correct submit response
XMLUnit.setIgnoreWhitespace(true);
Diff myDiff = new Diff(expectedXML.toString(), sw.toString());
DetailedDiff myDetailedDiff = new DetailedDiff(myDiff);
Assert.assertTrue("XML are similar " + myDetailedDiff, myDetailedDiff.similar());
}
示例21
@Test
public void writeDeliveryReportRequestWithCreateFinalMessageErrorCode() throws Exception {
DeliveryReportRequest request = new DeliveryReportRequest();
request.setAccount(new Account("customer1", "test1"));
request.setReferenceId("TESTREF");
request.setStatus(new DeliveryStatus(5, "EXPIRED"));
request.setTicketId("000:20090118002220948:000");
// new properties in version 1.2
request.setCreateDate(new DateTime(2010,5,30,9,30,10,0, DateTimeZone.UTC));
request.setFinalDate(new DateTime(2010,5,30,9,30,15,314, DateTimeZone.UTC));
request.setMessageErrorCode(new Integer(102));
StringWriter sw = new StringWriter();
SxmpWriter.write(sw, request);
logger.debug(sw.toString());
StringBuilder expectedXML = new StringBuilder(200)
.append("<?xml version=\"1.0\"?>\n")
.append("<operation type=\"deliveryReport\">\n")
.append(" <account username=\"customer1\" password=\"test1\"/>\n")
.append(" <deliveryReportRequest referenceId=\"TESTREF\">\n")
.append(" <status code=\"5\" message=\"EXPIRED\"/>\n")
.append(" <ticketId>000:20090118002220948:000</ticketId>\n")
.append(" <messageError code=\"102\"/>\n")
.append(" <createDate>2010-05-30T09:30:10.000Z</createDate>\n")
.append(" <finalDate>2010-05-30T09:30:15.314Z</finalDate>\n")
.append(" </deliveryReportRequest>\n")
.append("</operation>\n")
.append("");
// compare to actual correct submit response
XMLUnit.setIgnoreWhitespace(true);
Diff myDiff = new Diff(expectedXML.toString(), sw.toString());
DetailedDiff myDetailedDiff = new DetailedDiff(myDiff);
Assert.assertTrue("XML are similar " + myDetailedDiff, myDetailedDiff.similar());
}
示例22
@Test
public void writeSubmitRequestWithAlphanumeric() throws Exception {
SubmitRequest request = new SubmitRequest();
request.setAccount(new Account("customer1", "test1"));
request.setOperatorId(20);
request.setSourceAddress(new MobileAddress(MobileAddress.Type.ALPHANUMERIC, "TestAlpha"));
request.setDestinationAddress(new MobileAddress(MobileAddress.Type.INTERNATIONAL, "+13135551212"));
request.setText("Hello World");
StringWriter sw = new StringWriter();
SxmpWriter.write(sw, request);
logger.debug(sw.toString());
StringBuilder expectedXML = new StringBuilder(200)
.append("<?xml version=\"1.0\"?>\n")
.append("<operation type=\"submit\">\n")
.append(" <account username=\"customer1\" password=\"test1\"/>\n")
.append(" <submitRequest>\n")
.append(" <operatorId>20</operatorId>\n")
.append(" <priority>0</priority>\n")
.append(" <deliveryReport>false</deliveryReport>\n")
.append(" <sourceAddress type=\"alphanumeric\">TestAlpha</sourceAddress>\n")
.append(" <destinationAddress type=\"international\">+13135551212</destinationAddress>\n")
.append(" <text encoding=\"UTF-8\">48656C6C6F20576F726C64</text>\n")
.append(" </submitRequest>\n")
.append("</operation>\n")
.append("");
// compare to actual correct submit response
XMLUnit.setIgnoreWhitespace(true);
Diff myDiff = new Diff(expectedXML.toString(), sw.toString());
DetailedDiff myDetailedDiff = new DetailedDiff(myDiff);
Assert.assertTrue("XML are similar " + myDetailedDiff, myDetailedDiff.similar());
}
示例23
@Test
public void writeSubmitRequestWithLongPushDestination() throws Exception {
SubmitRequest request = new SubmitRequest(SxmpParser.VERSION_1_1);
request.setAccount(new Account("customer1", "test1"));
request.setOperatorId(20);
request.setDestinationAddress(new MobileAddress(MobileAddress.Type.PUSH_DESTINATION, "abcd1234fghi 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"));
request.setText("Hello World");
StringWriter sw = new StringWriter();
SxmpWriter.write(sw, request);
logger.debug(sw.toString());
StringBuilder expectedXML = new StringBuilder(200)
.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
.append("<operation type=\"submit\">\n")
.append(" <account username=\"customer1\" password=\"test1\"/>\n")
.append(" <submitRequest>\n")
.append(" <operatorId>20</operatorId>\n")
.append(" <priority>0</priority>\n")
.append(" <deliveryReport>false</deliveryReport>\n")
.append(" <destinationAddress type=\"push_destination\">abcd1234fghi 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789</destinationAddress>\n")
.append(" <text encoding=\"UTF-8\">48656C6C6F20576F726C64</text>\n")
.append(" </submitRequest>\n")
.append("</operation>\n")
.append("");
// compare to actual correct submit response
XMLUnit.setIgnoreWhitespace(true);
Diff myDiff = new Diff(expectedXML.toString(), sw.toString());
DetailedDiff myDetailedDiff = new DetailedDiff(myDiff);
Assert.assertTrue("XML are similar " + myDetailedDiff, myDetailedDiff.similar());
// guarantee there's no optionalParams entry
Assert.assertTrue(!expectedXML.toString().contains("optionalParams"));
}
示例24
@Test
public void writeSubmitRequestWithUTF8PushDestination() throws Exception {
SubmitRequest request = new SubmitRequest(SxmpParser.VERSION_1_1);
request.setAccount(new Account("customer1", "test1"));
request.setOperatorId(20);
request.setDestinationAddress(new MobileAddress(MobileAddress.Type.PUSH_DESTINATION, "abcd\n1234\rfghi-€£æ_\u20AC\u0623\u0647\u0644"));
request.setText("Hello World");
StringWriter sw = new StringWriter();
SxmpWriter.write(sw, request);
logger.debug("UTF8 PUSH DEST: "+sw.toString());
StringBuilder expectedXML = new StringBuilder(200)
.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
.append("<operation type=\"submit\">\n")
.append(" <account username=\"customer1\" password=\"test1\"/>\n")
.append(" <submitRequest>\n")
.append(" <operatorId>20</operatorId>\n")
.append(" <priority>0</priority>\n")
.append(" <deliveryReport>false</deliveryReport>\n")
.append(" <destinationAddress type=\"push_destination\">abcd\n1234\rfghi-€£æ_\u20AC\u0623\u0647\u0644</destinationAddress>\n")
.append(" <text encoding=\"UTF-8\">48656C6C6F20576F726C64</text>\n")
.append(" </submitRequest>\n")
.append("</operation>\n")
.append("");
// compare to actual correct submit response
XMLUnit.setIgnoreWhitespace(true);
Diff myDiff = new Diff(expectedXML.toString(), sw.toString());
DetailedDiff myDetailedDiff = new DetailedDiff(myDiff);
Assert.assertTrue("XML are similar " + myDetailedDiff, myDetailedDiff.similar());
// verify there's no optionalParams entry
Assert.assertTrue(!expectedXML.toString().contains("optionalParams"));
}
示例25
@Test
public void writeDeliverRequestWithAlphanumeric() throws Exception {
DeliverRequest request = new DeliverRequest();
request.setAccount(new Account("customer1", "test1"));
request.setOperatorId(20);
request.setDestinationAddress(new MobileAddress(MobileAddress.Type.ALPHANUMERIC, "TestAlpha"));
request.setSourceAddress(new MobileAddress(MobileAddress.Type.INTERNATIONAL, "+13135551212"));
request.setText("Hello World");
StringWriter sw = new StringWriter();
SxmpWriter.write(sw, request);
logger.debug(sw.toString());
StringBuilder expectedXML = new StringBuilder(200)
.append("<?xml version=\"1.0\"?>\n")
.append("<operation type=\"deliver\">\n")
.append(" <account username=\"customer1\" password=\"test1\"/>\n")
.append(" <deliverRequest>\n")
.append(" <operatorId>20</operatorId>\n")
.append(" <priority>0</priority>\n")
.append(" <sourceAddress type=\"international\">+13135551212</sourceAddress>\n")
.append(" <destinationAddress type=\"alphanumeric\">TestAlpha</destinationAddress>\n")
.append(" <text encoding=\"UTF-8\">48656C6C6F20576F726C64</text>\n")
.append(" </deliverRequest>\n")
.append("</operation>\n")
.append("");
// compare to actual correct submit response
XMLUnit.setIgnoreWhitespace(true);
Diff myDiff = new Diff(expectedXML.toString(), sw.toString());
DetailedDiff myDetailedDiff = new DetailedDiff(myDiff);
Assert.assertTrue("XML are similar " + myDetailedDiff, myDetailedDiff.similar());
}
示例26
@Test
public void writeSubmitRequestWithNationalAndApplication() throws Exception {
SubmitRequest request = new SubmitRequest();
request.setAccount(new Account("customer1", "test1"));
request.setApplication(new Application("TestApp"));
request.setOperatorId(20);
request.setSourceAddress(new MobileAddress(MobileAddress.Type.NATIONAL, "0123456789"));
request.setDestinationAddress(new MobileAddress(MobileAddress.Type.INTERNATIONAL, "+13135551212"));
request.setText("Hello World");
StringWriter sw = new StringWriter();
SxmpWriter.write(sw, request);
logger.debug(sw.toString());
StringBuilder expectedXML = new StringBuilder(200)
.append("<?xml version=\"1.0\"?>\n")
.append("<operation type=\"submit\">\n")
.append(" <account username=\"customer1\" password=\"test1\"/>\n")
.append(" <application>TestApp</application>\n")
.append(" <submitRequest>\n")
.append(" <operatorId>20</operatorId>\n")
.append(" <priority>0</priority>\n")
.append(" <deliveryReport>false</deliveryReport>\n")
.append(" <sourceAddress type=\"national\">0123456789</sourceAddress>\n")
.append(" <destinationAddress type=\"international\">+13135551212</destinationAddress>\n")
.append(" <text encoding=\"UTF-8\">48656C6C6F20576F726C64</text>\n")
.append(" </submitRequest>\n")
.append("</operation>\n")
.append("");
// compare to actual correct submit response
XMLUnit.setIgnoreWhitespace(true);
Diff myDiff = new Diff(expectedXML.toString(), sw.toString());
DetailedDiff myDetailedDiff = new DetailedDiff(myDiff);
Assert.assertTrue("XML are similar " + myDetailedDiff, myDetailedDiff.similar());
}
示例27
@Test
public void writeDeliverRequestWithNationalAndApplication() throws Exception {
DeliverRequest request = new DeliverRequest();
request.setAccount(new Account("customer1", "test1"));
request.setApplication(new Application("TestApp"));
request.setOperatorId(20);
request.setDestinationAddress(new MobileAddress(MobileAddress.Type.ALPHANUMERIC, "TestAlpha"));
request.setSourceAddress(new MobileAddress(MobileAddress.Type.NATIONAL, "0123456789"));
request.setText("Hello World");
StringWriter sw = new StringWriter();
SxmpWriter.write(sw, request);
logger.debug(sw.toString());
StringBuilder expectedXML = new StringBuilder(200)
.append("<?xml version=\"1.0\"?>\n")
.append("<operation type=\"deliver\">\n")
.append(" <account username=\"customer1\" password=\"test1\"/>\n")
.append(" <application>TestApp</application>\n")
.append(" <deliverRequest>\n")
.append(" <operatorId>20</operatorId>\n")
.append(" <priority>0</priority>\n")
.append(" <sourceAddress type=\"national\">0123456789</sourceAddress>\n")
.append(" <destinationAddress type=\"alphanumeric\">TestAlpha</destinationAddress>\n")
.append(" <text encoding=\"UTF-8\">48656C6C6F20576F726C64</text>\n")
.append(" </deliverRequest>\n")
.append("</operation>\n")
.append("");
// compare to actual correct submit response
XMLUnit.setIgnoreWhitespace(true);
Diff myDiff = new Diff(expectedXML.toString(), sw.toString());
DetailedDiff myDetailedDiff = new DetailedDiff(myDiff);
Assert.assertTrue("XML are similar " + myDetailedDiff, myDetailedDiff.similar());
}
示例28
@SuppressWarnings("unchecked")
private static void logDifferences(DetailedDiff diff) {
for (Difference difference : (List<Difference>) diff.getAllDifferences()) {
LOG.error(difference.toString()); // NOSONAR
}
}
示例29
public void compare ()
{
// Comparing
try {
output.println("Comparing " + controlFile + " to " + testFile);
final Document controlDoc;
final Document testDoc;
try (InputStream cis = new FileInputStream(controlFile)) {
controlDoc = new PositionalXMLReader().readXML(cis);
}
try (InputStream tis = new FileInputStream(testFile)) {
testDoc = new PositionalXMLReader().readXML(tis);
}
// Tuning
XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);
XMLUnit.setNormalizeWhitespace(true);
XMLUnit.setIgnoreWhitespace(true);
///XMLUnit.setIgnoreComments(true); NO!!!!!!!!
// The setIgnoreComments triggers the use of XSLT transform
// which 1/ ruins userdata and 2/ fails on xml:space and xml:lang.
// Moreover, comments are actually ignored by Diff
///XMLUnit.setCompareUnmatched(false); NO need
// Customization
Filter filter = (filterFile != null)
? new BasicFilter(new FileInputStream(filterFile))
: null;
Diff diff = new Diff(controlDoc, testDoc, null);
diff.overrideElementQualifier(
new ElementNameAndAttributeQualifier("number"));
diff.overrideDifferenceListener(
new MusicDifferenceListener(filter, output));
output.println("Similar: " + diff.similar());
output.println("Identical: " + diff.identical());
DetailedDiff detDiff = new DetailedDiff(diff);
List differences = detDiff.getAllDifferences();
output.println();
output.println("Physical differences: " + differences.size());
int diffId = 0;
for (Object object : differences) {
Difference difference = (Difference) object;
if (!difference.isRecoverable()
&& ((filter == null) || !filter.canIgnore(difference))) {
diffId++;
output.dump(diffId, difference);
}
}
output.println("Logical differences: " + diffId);
logger.info("Logical differences: {}", diffId);
} catch (ConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
}
示例30
/**
* TODO Doesn't seem there are any asserts here
*
* @throws Exception
*/
public void testEjbJar() throws Exception {
final String fileName = "ejb-jar-example1.xml";
final Event test = Event.start("Test");
final URL resource = this.getClass().getClassLoader().getResource(fileName);
final String expected = IO.slurp(resource);
final Event ejbJarJAXBCreate = Event.start("EjbJarJAXBCreate");
ejbJarJAXBCreate.stop();
final Event unmarshalEvent = Event.start("unmarshal");
final Object value;
final EjbJar$JAXB jaxbType = new EjbJar$JAXB();
value = Sxc.unmarshalJavaee(resource, jaxbType);
unmarshalEvent.stop();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final Event marshall = Event.start("marshall");
Sxc.marshall(jaxbType, value, baos);
marshall.stop();
final String result = new String(baos.toByteArray(), "UTF-8");
XMLUnit.setIgnoreComments(Boolean.TRUE);
XMLUnit.setIgnoreWhitespace(Boolean.TRUE);
XMLUnit.setIgnoreAttributeOrder(Boolean.TRUE);
XMLUnit.setIgnoreDiffBetweenTextAndCDATA(Boolean.TRUE);
final Diff diff = new Diff(expected.trim(), result.trim());
final Diff myDiff = new DetailedDiff(diff);
final AtomicInteger differenceNumber = new AtomicInteger(0); // just to get an int wrapper for the test
myDiff.overrideDifferenceListener(new IgnoreTextAndAttributeValuesDifferenceListener() {
@Override
public int differenceFound(final Difference difference) {
if (!difference.isRecoverable()) {
differenceNumber.incrementAndGet();
System.err.println(">>> " + difference.toString());
}
return 0;
}
});
assertTrue("Files are not identical", myDiff.identical());
test.stop();
}