Best Cerberus-source code snippet using org.cerberus.service.xmlunit.Difference.equals
Source:XmlUnitServiceTest.java
1/**2 * Cerberus Copyright (C) 2013 - 2017 cerberustesting3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4 *5 * This file is part of Cerberus.6 *7 * Cerberus is free software: you can redistribute it and/or modify8 * it under the terms of the GNU General Public License as published by9 * the Free Software Foundation, either version 3 of the License, or10 * (at your option) any later version.11 *12 * Cerberus is distributed in the hope that it will be useful,13 * but WITHOUT ANY WARRANTY; without even the implied warranty of14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15 * GNU General Public License for more details.16 *17 * You should have received a copy of the GNU General Public License18 * along with Cerberus. If not, see <http://www.gnu.org/licenses/>.19 */20package org.cerberus.service.xmlunit;21import org.cerberus.service.xmlunit.impl.XmlUnitService;22import java.io.IOException;23import java.lang.reflect.InvocationTargetException;24import java.lang.reflect.Method;25import java.net.URL;26import javax.xml.soap.SOAPException;27import org.cerberus.util.XmlUtilException;28import org.custommonkey.xmlunit.DetailedDiff;29import org.custommonkey.xmlunit.XMLUnit;30import org.junit.Assert;31import org.junit.Before;32import org.junit.BeforeClass;33import org.junit.Test;34import org.junit.runner.RunWith;35import org.mockito.InjectMocks;36import org.mockito.runners.MockitoJUnitRunner;37import org.powermock.core.classloader.annotations.PrepareForTest;38import org.springframework.test.context.ContextConfiguration;39import org.xml.sax.SAXException;40/**41 * {@link XmlUnitService} unit tests42 *43 * @author abourdon44 */45@RunWith(MockitoJUnitRunner.class)46@PrepareForTest({XmlUnitService.class})47@ContextConfiguration(locations = {"/applicationContextTest.xml"})48public class XmlUnitServiceTest {49 @InjectMocks50 private XmlUnitService xmlUnitService;51 private Differences differences;52 @BeforeClass53 public static void beforeClass() {54 XMLUnit.setIgnoreComments(true);55 XMLUnit.setIgnoreWhitespace(true);56 XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);57 XMLUnit.setCompareUnmatched(false);58 }59 public XmlUnitServiceTest() {60 }61 @Before62 public void before() throws XmlUtilException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {63 differences = new Differences();64 Method init = xmlUnitService.getClass().getDeclaredMethod("init");65 init.setAccessible(true);66 init.invoke(xmlUnitService);67 }68 @Test69 public void testIsElementPresentWithElementPresent() throws SOAPException {70 String xmlResponse = "<root><a>1</a><a>2</a></root>";71 Assert.assertTrue(xmlUnitService.isElementPresent(xmlResponse, "/root/a"));72 }73 @Test74 public void testIsElementPresentWithElementPresentWithNamespace() {75 String xmlResponse = "<root xmlns:prefix=\"http://prefix\"><prefix:a>1</prefix:a><prefix:a>2</prefix:a></root>";76 Assert.assertTrue(xmlUnitService.isElementPresent(xmlResponse, "/root/prefix:a"));77 }78 @Test79 public void testIsElementPresentWithElementAbsent() {80 String xmlResponse = "<root><a>1</a><a>2</a></root>";81 Assert.assertFalse(xmlUnitService.isElementPresent(xmlResponse, "/root/b"));82 }83 @Test84 public void testIsSimilarTreeWithExistingElementAndSimilarTree() {85 String xmlResponse = "<root><a>1</a><a>2</a></root>";86 Assert.assertTrue(xmlUnitService.isSimilarTree(xmlResponse, "/root", "<root><a>foo</a><a>bar</a></root>"));87 }88 @Test89 public void testIsSimilarTreeWithExistingElementAndIdenticalTree() {90 String xmlResponse = "<root><a>1</a><a>2</a></root>";91 Assert.assertTrue(xmlUnitService.isSimilarTree(xmlResponse, "/root", "<root><a>1</a><a>2</a></root>"));92 }93 @Test94 public void testIsSimilarTreeWithExistingElementAndSimilarTreeWithNamespace() {95 String xmlResponse = "<root xmlns:prefix=\"http://prefix\"><prefix:a>1</prefix:a><prefix:a>2</prefix:a></root>";96 Assert.assertTrue(xmlUnitService.isSimilarTree(xmlResponse, "/root/prefix:a", "<prefix:a xmlns:prefix=\"http://prefix\">1</prefix:a>"));97 }98 @Test99 public void testIsSimilarTreeWithExistingElementAndNotSimilarTree() {100 String xmlResponse = "<root><a>1</a><a>2</a></root>";101 Assert.assertFalse(xmlUnitService.isSimilarTree(xmlResponse, "/root", "<root><wrong>foo</wrong><a>bar</a></root>"));102 }103 @Test104 public void testIsSimilarTreeWithNotExistingElementAndSimilarTree() {105 String xmlResponse = "<root><a>1</a><a>2</a></root>";106 Assert.assertFalse(xmlUnitService.isSimilarTree(xmlResponse, "/plop", "<root><a>foo</a><a>bar</a></root>"));107 }108 @Test109 public void testIsSimilarTreeWithNotExistingElementAndNotSimilarTree() {110 String xmlResponse = "<root><a>1</a><a>2</a></root>";111 Assert.assertFalse(xmlUnitService.isSimilarTree(xmlResponse, "/plop", "<root><wrong>foo</wrong><a>bar</a></root>"));112 }113 @Test114 public void testGetFromXmlWithValidURLAndExistingElement() {115 Assert.assertEquals("2", xmlUnitService.getFromXml(getClass().getResource("/org/cerberus/serviceEngine/impl/data.xml").toString(), "/root/a[2]/text()"));116 }117 @Test118 public void testGetFromXmlWithValidURLAndExistingElementJustTheFirstOne() {119 Assert.assertEquals("1", xmlUnitService.getFromXml(getClass().getResource("/org/cerberus/serviceEngine/impl/data.xml").toString(), "/root/a/text()"));120 }121 @Test122 public void testGetFromXmlWithValidURLAndExistingElementWithNamespace() {123 Assert.assertEquals("2", xmlUnitService.getFromXml(getClass().getResource("/org/cerberus/serviceEngine/impl/data-namespaces.xml").toString(), "/:root/prefix:a[2]/text()"));124 }125 @Test126 public void testGetFromXmlWithValidURLAndNotExistingElement() {127 Assert.assertEquals(XmlUnitService.DEFAULT_GET_FROM_XML_VALUE, xmlUnitService.getFromXml(getClass().getResource("/org/cerberus/serviceEngine/impl/data.xml").toString(), "/root/b"));128 }129 130 @Test131 public void testGetSubstringFromXmlWithValidURLAndExistingElement() {132 Assert.assertEquals("1234", xmlUnitService.getFromXml(getClass().getResource("/org/cerberus/serviceEngine/impl/data.xml").toString(), "substring(/root/a[3], 1, 4)"));133 }134 135 @Test136 public void testGetFromXmlWithValidURLAndExistingMultipleElement() {137 Assert.assertEquals("1", xmlUnitService.getFromXml(getClass().getResource("/org/cerberus/serviceEngine/impl/data.xml").toString(), "/root/c"));138 }139// @Test140// public void testGetFromXmlWithNonValidURL() {141// Assert.assertEquals(XmlUnitService.DEFAULT_GET_FROM_XML_VALUE, xmlUnitService.getFromXml("file:/non-valid-url", "/foo/bar"));142// }143 @Test144 public void testGetFromXmlWithValidXMLTextAndExistingElement() {145 String xmlResponse = "<root><a>1</a><a>2</a></root>";146 Assert.assertEquals("2", xmlUnitService.getFromXml(xmlResponse, "/root/a[2]/text()"));147 }148 @Test149 public void testGetFromXmlWithValidXMLTextAndExistingElementJustTheFirstOne() {150 String xmlResponse = "<root><a>1</a><a>2</a></root>";151 Assert.assertEquals("1", xmlUnitService.getFromXml(xmlResponse, "/root/a/text()"));152 }153 @Test154 public void testGetFromXmlWithValidXMLTextAndExistingElementWithNamespace() {155 String xmlResponse = "<root xmlns:prefix=\"http://prefix\"><prefix:a>1</prefix:a><prefix:a>2</prefix:a></root>";156 Assert.assertEquals("2", xmlUnitService.getFromXml(xmlResponse, "/root/prefix:a[2]/text()"));157 }158 @Test159 public void testGetFromXmlWithValidXMLTextAndNotExistingElement() {160 String xmlResponse = "<root><a>1</a><a>2</a></root>";161 Assert.assertEquals(XmlUnitService.DEFAULT_GET_FROM_XML_VALUE, xmlUnitService.getFromXml(xmlResponse, "/root/b"));162 }163 @Test164 public void testGetDifferencesFromXmlWithNoDifference() throws XmlUtilException {165 String expected = differences.mkString();166 String actual = xmlUnitService.getDifferencesFromXml("<root><a>1</a></root>", "<root><a>1</a></root>");167 Assert.assertEquals(expected, actual);168 }169 @Test170 public void testGetDifferencesFromXmlWithValueDifference() throws XmlUtilException {171 differences.addDifference(new Difference("/root[1]/a[1]/text()[1]"));172 String expected = differences.mkString();173 String actual = xmlUnitService.getDifferencesFromXml("<root><a>1</a></root>", "<root><a>2</a></root>");174 Assert.assertEquals(expected, actual);175 }176 @Test177 public void testGetDifferencesFromXmlWithStructureDifference() throws XmlUtilException {178 differences.addDifference(new Difference("/root[1]/a[1]"));179 differences.addDifference(new Difference("/root[1]/b[1]"));180 String expected = differences.mkString();181 String diff = xmlUnitService.getDifferencesFromXml("<root><a>1</a></root>", "<root><b>1</b></root>");182 Assert.assertEquals(expected, diff);183 }184 @Test185 public void testGetDifferencesFromXmlByUsingURL() throws XmlUtilException {186 differences.addDifference(new Difference("/root[1]/a[1]"));187 differences.addDifference(new Difference("/root[1]/b[1]"));188 String expected = differences.mkString();189 URL left = getClass().getResource("/org/cerberus/serviceEngine/impl/left.xml");190 URL right = getClass().getResource("/org/cerberus/serviceEngine/impl/right.xml");191 String actual = xmlUnitService.getDifferencesFromXml("url=" + left, "url=" + right);192 Assert.assertEquals(expected, actual);193 }194 @Test195 public void testRemoveDifferenceWhenDifferenceMatch() throws XmlUtilException, SAXException, IOException {196 differences.addDifference(new Difference("/root[1]/a[1]"));197 differences.addDifference(new Difference("/root[1]/a[2]"));198 differences.addDifference(new Difference("/root[1]/b[1]"));199 String actual = xmlUnitService.removeDifference("/root\\[1\\]/a\\[[1-2]\\]", differences.mkString());200 String expected = "<differences><difference>/root[1]/b[1]</difference></differences>";201 DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(expected, actual));202 Assert.assertTrue(diff.toString(), diff.similar());203 }204 @Test205 public void testRemoveDifferenceWhenDifferenceMatchAll() throws XmlUtilException, SAXException, IOException {206 differences.addDifference(new Difference("/root[1]/a[1]"));207 differences.addDifference(new Difference("/root[1]/a[2]"));208 differences.addDifference(new Difference("/root[1]/b[1]"));209 String actual = xmlUnitService.removeDifference(".*root.*", differences.mkString());210 Assert.assertEquals(Differences.EMPTY_DIFFERENCES_STRING, actual);211 }212 @Test213 public void testRemoveDifferenceWhenNoDifferenceMatch() throws XmlUtilException, SAXException, IOException {214 differences.addDifference(new Difference("/root[1]/a[1]"));215 differences.addDifference(new Difference("/root[1]/b[1]"));216 String actual = xmlUnitService.removeDifference("toto", differences.mkString());217 String expected = "<differences><difference>/root[1]/a[1]</difference><difference>/root[1]/b[1]</difference></differences>";218 DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(expected, actual));219 Assert.assertTrue(diff.toString(), diff.similar());220 }221 @Test222 public void testRemoveDifferenceFromEmptyDifferences() throws XmlUtilException {223 String expected = Differences.EMPTY_DIFFERENCES_STRING;224 String actual = xmlUnitService.removeDifference("foo", Differences.EMPTY_DIFFERENCES_STRING);225 Assert.assertEquals(expected, actual);226 }227 @Test228 public void testIsElementEqualsWithExistingElement() {229 String xmlResponse = "<root><a>1</a><a>2</a></root>";230 Assert.assertTrue(xmlUnitService.isElementEquals(xmlResponse, "/root/a", "<a>2</a>"));231 }232 @Test233 public void testIsElementEqualsWithNotFormatedExistingElement() {234 String xmlResponse = "<root><a>1</a><a>2</a></root>";235 Assert.assertTrue(xmlUnitService.isElementEquals(xmlResponse, "/root/a", " <a>2</a> "));236 }237 @Test238 public void testIsElementEqualsWithExistingElementWithNamespace() {239 String xmlResponse = "<root xmlns:prefix=\"http://prefix\"><prefix:a>1</prefix:a><prefix:a>2</prefix:a></root>";240 Assert.assertTrue(xmlUnitService.isElementEquals(xmlResponse, "/root/prefix:a", "<prefix:a xmlns:prefix=\"http://prefix\">2</prefix:a>"));241 }242 @Test243 public void testIsElementEqualsWithNotExistingElement() {244 String xmlResponse = "<root><a>1</a><a>2</a></root>";245 Assert.assertFalse(xmlUnitService.isElementEquals(xmlResponse, "/root/a", "<a>3</a>"));246 }247// @Test248// public void testIsElementEqualsWithNullTCE() {249// Assert.assertFalse(xmlUnitService.isElementEquals(null, "/foo", "<bar/>"));250// }251//252// @Test253// public void testIsElementEqualsWithNullXPath() {254// Assert.assertFalse(xmlUnitService.isElementEquals("<test></test>", null, "<bar/>"));255// }256//257// @Test258// public void testIsElementEqualsWithNullElement() {259// Assert.assertFalse(xmlUnitService.isElementEquals("<test></test>", "/foo", null));260// }261}...
Source:XmlUnitService.java
1/**2 * Cerberus Copyright (C) 2013 - 2017 cerberustesting3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4 *5 * This file is part of Cerberus.6 *7 * Cerberus is free software: you can redistribute it and/or modify8 * it under the terms of the GNU General Public License as published by9 * the Free Software Foundation, either version 3 of the License, or10 * (at your option) any later version.11 *12 * Cerberus is distributed in the hope that it will be useful,13 * but WITHOUT ANY WARRANTY; without even the implied warranty of14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15 * GNU General Public License for more details.16 *17 * You should have received a copy of the GNU General Public License18 * along with Cerberus. If not, see <http://www.gnu.org/licenses/>.19 */20package org.cerberus.service.xmlunit.impl;2122import java.net.MalformedURLException;23import java.net.URL;24import java.util.logging.Level;25import java.util.regex.Pattern;2627import javax.annotation.PostConstruct;2829import org.apache.logging.log4j.Logger;30import org.apache.logging.log4j.LogManager;31import org.cerberus.service.xmlunit.IXmlUnitService;32import org.cerberus.service.xmlunit.AInputTranslator;33import org.cerberus.service.xmlunit.Differences;34import org.cerberus.service.xmlunit.DifferencesException;35import org.cerberus.service.xmlunit.InputTranslator;36import org.cerberus.service.xmlunit.InputTranslatorException;37import org.cerberus.service.xmlunit.InputTranslatorManager;38import org.cerberus.service.xmlunit.InputTranslatorUtil;39import org.cerberus.util.StringUtil;40import org.cerberus.util.XmlUtil;41import org.cerberus.util.XmlUtilException;42import org.custommonkey.xmlunit.DetailedDiff;43import org.custommonkey.xmlunit.Difference;44import org.custommonkey.xmlunit.XMLUnit;45import org.springframework.stereotype.Service;46import org.w3c.dom.Document;47import org.w3c.dom.Node;48import org.w3c.dom.NodeList;4950/**51 *52 * @author bcivel53 */54@Service55public class XmlUnitService implements IXmlUnitService {5657 /**58 * The associated {@link Logger} to this class59 */60 private static final Logger LOG = LogManager.getLogger(XmlUnitService.class);6162 /**63 * Difference value for null XPath64 */65 public static final String NULL_XPATH = "null";6667 /**68 * The default value for the getFromXML action69 */70 public static final String DEFAULT_GET_FROM_XML_VALUE = null;7172 /**73 * Prefixed input handling74 */75 private InputTranslatorManager<Document> inputTranslator;7677 @PostConstruct78 private void init() {79 initInputTranslator();80 initXMLUnitProperties();81 }8283 /**84 * Initializes {@link #inputTranslator} by two {@link InputTranslator}85 * <ul>86 * <li>One for handle the <code>url</code> prefix</li>87 * <li>One for handle without prefix</li>88 * </ul>89 */90 private void initInputTranslator() {91 inputTranslator = new InputTranslatorManager<Document>();92 // Add handling on the "url" prefix, to get URL input93 inputTranslator.addTranslator(new AInputTranslator<Document>("url") {94 @Override95 public Document translate(String input) throws InputTranslatorException {96 try {97 URL urlInput = new URL(InputTranslatorUtil.getValue(input));98 return XmlUtil.fromURL(urlInput);99 } catch (MalformedURLException e) {100 throw new InputTranslatorException(e);101 } catch (XmlUtilException e) {102 throw new InputTranslatorException(e);103 }104 }105 });106 // Add handling for raw XML input107 inputTranslator.addTranslator(new AInputTranslator<Document>(null) {108 @Override109 public Document translate(String input) throws InputTranslatorException {110 try {111 return XmlUtil.fromString(input);112 } catch (XmlUtilException e) {113 throw new InputTranslatorException(e);114 }115 }116 });117 }118119 /**120 * Initializes {@link XMLUnit} properties121 */122 private void initXMLUnitProperties() {123 XMLUnit.setIgnoreComments(true);124 XMLUnit.setIgnoreWhitespace(true);125 XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);126 XMLUnit.setCompareUnmatched(false);127 }128129 @Override130 public boolean isElementPresent(String lastSOAPResponse, String xpath) {131 if (xpath == null) {132 LOG.warn("Null argument");133 return false;134 }135136 try {137 return XmlUtil.evaluate(lastSOAPResponse, xpath).getLength() != 0;138 } catch (XmlUtilException e) {139 LOG.warn("Unable to check if element is present", e);140 }141142 return false;143 }144145 @Override146 public boolean isSimilarTree(String lastSOAPResponse, String xpath, String tree) {147 if (xpath == null || tree == null) {148 LOG.warn("Null argument");149 return false;150 }151152 try {153 NodeList candidates = XmlUtil.evaluate(lastSOAPResponse, xpath);154 for (Node candidate : new XmlUtil.IterableNodeList(candidates)) {155 boolean found = true;156 for (org.cerberus.service.xmlunit.Difference difference : Differences.fromString(getDifferencesFromXml(XmlUtil.toString(candidate), tree))) {157 if (!difference.getDiff().endsWith("/text()[1]")) {158 found = false;159 }160 }161162 if (found) {163 return true;164 }165 }166 } catch (XmlUtilException e) {167 LOG.warn("Unable to check similar tree", e);168 } catch (DifferencesException e) {169 LOG.warn("Unable to check similar tree", e);170 }171172 return false;173 }174175 @Override176 public String getFromXml(final String xmlToParse, final String xpath) {177 if (xpath == null) {178 LOG.warn("Null argument");179 return DEFAULT_GET_FROM_XML_VALUE;180 }181182 try {183 final Document document = StringUtil.isURL(xmlToParse) ? XmlUtil.fromURL(new URL(xmlToParse)) : XmlUtil.fromString(xmlToParse);184 final String result = XmlUtil.evaluateString(document, xpath);185 // Not that in case of multiple values then send the first one186 return result != null && result.length() > 0 ? result : DEFAULT_GET_FROM_XML_VALUE;187 } catch (XmlUtilException e) {188 LOG.warn("Unable to get from xml", e);189 } catch (MalformedURLException e) {190 LOG.warn("Unable to get from xml", e);191 }192193 return DEFAULT_GET_FROM_XML_VALUE;194 }195196 @Override197 public String getDifferencesFromXml(String left, String right) {198 try {199 // Gets the detailed diff between left and right argument200 Document leftDocument = inputTranslator.translate(left);201 Document rightDocument = inputTranslator.translate(right);202 DetailedDiff diffs = new DetailedDiff(XMLUnit.compareXML(leftDocument, rightDocument));203204 // Creates the result structure which will contain difference list205 Differences resultDiff = new Differences();206207 // Add each difference to our result structure208 for (Object diff : diffs.getAllDifferences()) {209 if (!(diff instanceof Difference)) {210 LOG.warn("Unable to handle no XMLUnit Difference " + diff);211 continue;212 }213 Difference wellTypedDiff = (Difference) diff;214 String xPathLocation = wellTypedDiff.getControlNodeDetail().getXpathLocation();215 // Null XPath location means additional data from the right216 // structure.217 // Then we retrieve XPath from the right structure.218 if (xPathLocation == null) {219 xPathLocation = wellTypedDiff.getTestNodeDetail().getXpathLocation();220 }221 // If location is still null, then both of left and right222 // differences have been marked as null223 // This case should never happen224 if (xPathLocation == null) {225 LOG.warn("Null left and right differences found");226 xPathLocation = NULL_XPATH;227 }228 resultDiff.addDifference(new org.cerberus.service.xmlunit.Difference(xPathLocation));229 }230231 // Finally returns the String representation of our result structure232 return resultDiff.mkString();233 } catch (InputTranslatorException e) {234 LOG.warn("Unable to get differences from XML", e);235 }236237 return null;238 }239240 @Override241 public String removeDifference(String pattern, String differences) {242 if (pattern == null || differences == null) {243 LOG.warn("Null argument");244 return null;245 }246247 try {248 // Gets the difference list from the differences249 Differences current = Differences.fromString(differences);250 Differences returned = new Differences();251252 // Compiles the given pattern253 Pattern compiledPattern = Pattern.compile(pattern);254 for (org.cerberus.service.xmlunit.Difference currentDiff : current.getDifferences()) {255 if (compiledPattern.matcher(currentDiff.getDiff()).matches()) {256 continue;257 }258 returned.addDifference(currentDiff);259 }260261 // Returns the empty String if there is no difference left, or the262 // String XML representation263 return returned.mkString();264 } catch (DifferencesException e) {265 LOG.warn("Unable to remove differences", e);266 }267268 return null;269 }270271 @Override272 public boolean isElementEquals(String lastSOAPResponse, String xpath, String expectedElement) {273 if (lastSOAPResponse == null || xpath == null || expectedElement == null) {274 LOG.warn("Null argument");275 return false;276 }277278 try {279 NodeList candidates = XmlUtil.evaluate(lastSOAPResponse, xpath);280 LOG.debug(candidates.toString());281 for (Document candidate : XmlUtil.fromNodeList(candidates)) {282 if (Differences.fromString(getDifferencesFromXml(XmlUtil.toString(candidate), expectedElement)).isEmpty()) {283 return true;284 }285 }286 } catch (XmlUtilException xue) {287 LOG.warn("Unable to check if element equality", xue);288 } catch (DifferencesException de) {289 LOG.warn("Unable to check if element equality", de);290 }291292 return false;293 }294295 @Override296 public Document getXmlDocument(String lastSOAPResponse) {297 Document document = null;298 try {299 document = XmlUtil.fromString(lastSOAPResponse);300 return document;301 } catch (XmlUtilException ex) {302 LOG.warn(ex);303 }304 return document;305 }306}
...
Source:DifferencesTest.java
1/**2 * Cerberus Copyright (C) 2013 - 2017 cerberustesting3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4 *5 * This file is part of Cerberus.6 *7 * Cerberus is free software: you can redistribute it and/or modify8 * it under the terms of the GNU General Public License as published by9 * the Free Software Foundation, either version 3 of the License, or10 * (at your option) any later version.11 *12 * Cerberus is distributed in the hope that it will be useful,13 * but WITHOUT ANY WARRANTY; without even the implied warranty of14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15 * GNU General Public License for more details.16 *17 * You should have received a copy of the GNU General Public License18 * along with Cerberus. If not, see <http://www.gnu.org/licenses/>.19 */20package org.cerberus.service.xmlunit;21import org.cerberus.service.xmlunit.Difference;22import org.cerberus.service.xmlunit.DifferencesException;23import org.cerberus.service.xmlunit.Differences;24import java.io.IOException;25import org.cerberus.util.XmlUtil;26import org.cerberus.util.XmlUtilException;27import org.custommonkey.xmlunit.DetailedDiff;28import org.custommonkey.xmlunit.XMLUnit;29import org.junit.Assert;30import org.junit.Before;31import org.junit.BeforeClass;32import org.junit.Test;33import org.w3c.dom.Document;34import org.w3c.dom.Element;35import org.xml.sax.SAXException;36/**37 * {@link Differences} unit tests38 * 39 * @author abourdon40 */41public class DifferencesTest {42 private Differences differences;43 @BeforeClass44 public static void beforeClass() {45 XMLUnit.setIgnoreComments(true);46 XMLUnit.setIgnoreWhitespace(true);47 XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);48 XMLUnit.setCompareUnmatched(false);49 }50 public DifferencesTest() {51 }52 @Before53 public void before() {54 differences = new Differences();55 }56 @Test57 public void testAddDifference() {58 Difference expected = new Difference("diff");59 differences.addDifference(expected);60 Assert.assertEquals("Add a difference increase the difference list to 1", 1, differences.getDifferences().size());61 Assert.assertEquals("Add a difference correctly add the given difference", expected, differences.getDifferences().get(0));62 }63 @Test64 public void testRemoveExistingDifference() {65 Difference diff = new Difference("diff");66 differences.addDifference(diff);67 differences.removeDifference(diff);68 Assert.assertTrue("Remove an existing difference cause remove it from the differences list", differences.getDifferences().isEmpty());69 }70 @Test71 public void testRemoveNotExistingDifference() {72 Difference diff1 = new Difference("diff1");73 differences.addDifference(diff1);74 Difference diff2 = new Difference("diff2");75 differences.removeDifference(diff2);76 Assert.assertEquals("Remove a not existing difference cause make the differences list unchanged", 1, differences.getDifferences().size());77 Assert.assertEquals("Remove a not existing difference cause make the differences list unchanged", diff1, differences.getDifferences().get(0));78 }79 @Test80 public void testMkStringWhenExistingDifference() throws XmlUtilException, SAXException, IOException {81 differences.addDifference(new Difference("diff1"));82 differences.addDifference(new Difference("diff2"));83 String actual = differences.mkString();84 Document doc = XmlUtil.newDocument();85 Element root = doc.createElement(Differences.DIFFERENCES_NODE);86 doc.appendChild(root);87 Element diff1 = doc.createElement(Differences.DIFFERENCE_NODE);88 diff1.appendChild(doc.createTextNode("diff1"));89 root.appendChild(diff1);90 Element diff2 = doc.createElement(Differences.DIFFERENCE_NODE);91 diff2.appendChild(doc.createTextNode("diff2"));92 root.appendChild(diff2);93 String expected = XmlUtil.toString(doc);94 DetailedDiff result = new DetailedDiff(XMLUnit.compareXML(expected, actual));95 Assert.assertTrue("Differences can be correctly transforms as String", result.similar());96 }97 @Test98 public void testMkStringWhenNotExistingDifference() throws XmlUtilException, SAXException, IOException {99 String actual = differences.mkString();100 String expected = Differences.EMPTY_DIFFERENCES_STRING;101 Assert.assertEquals("Differences can be correctly transforms as Document when there is no differences", expected, actual);102 }103 @Test104 public void testToDocumentWhenExistingDifference() throws DifferencesException, XmlUtilException {105 Document actual = differences.toDocument();106 Document expected = XmlUtil.newDocument();107 Element root = expected.createElement(Differences.DIFFERENCES_NODE);108 expected.appendChild(root);109 DetailedDiff result = new DetailedDiff(XMLUnit.compareXML(expected, actual));110 Assert.assertTrue("Differences can be correctly transforms as Document when there is no differences", result.similar());111 }112}...
equals
Using AI Code Generation
1package org.cerberus.service.xmlunit;2import java.io.IOException;3import java.util.Iterator;4import java.util.List;5import javax.xml.parsers.ParserConfigurationException;6import javax.xml.transform.TransformerException;7import org.custommonkey.xmlunit.Difference;8import org.custommonkey.xmlunit.DifferenceEngine;9import org.custommonkey.xmlunit.DifferenceListener;10import org.custommonkey.xmlunit.DifferenceResult;11import org.custommonkey.xmlunit.DifferenceResultException;12import org.custommonkey.xmlunit.DifferenceResultListener;13import org.custommonkey.xmlunit.DifferenceResultListenerFactory;14import org.custommonkey.xmlunit.DifferenceResultListenerFactoryImpl;15import org.custommonkey.xmlunit.DifferenceResultListenerImpl;16import org.custommonkey.xmlunit.DifferenceResultListenerImpl2;17import org.custommonkey.xmlunit.DifferenceResultListenerImpl3;18import org.custommonkey.xmlunit.DifferenceResultListenerImpl4;19import org.custommonkey.xmlunit.DifferenceResultListenerImpl5;20import org.custommonkey.xmlunit.DifferenceResultListenerImpl6;21import org.custommonkey.xmlunit.DifferenceResultListenerImpl7;22import org.custommonkey.xmlunit.DifferenceResultListenerImpl8;23import org.custommonkey.xmlunit.DifferenceResultListenerImpl9;24import org.custommonkey.xmlunit.DifferenceResultListenerImpl10;25import org.custommonkey.xmlunit.DifferenceResultListenerImpl11;26import org.custommonkey.xmlunit.DifferenceResultListenerImpl12;27import org.custommonkey.xmlunit.DifferenceResultListenerImpl13;28import org.custommonkey.xmlunit.DifferenceResultListenerImpl14;29import org.custommonkey.xmlunit.DifferenceResultListenerImpl15;30import org.custommonkey.xmlunit.DifferenceResultListenerImpl16;31import org.custommonkey.xmlunit.DifferenceResultListenerImpl17;32import org.custommonkey.xmlunit.DifferenceResultListenerImpl18;33import org.custommonkey.xmlunit.DifferenceResultListenerImpl19;34import org.custommonkey.xmlunit.DifferenceResultListenerImpl20;35import org.custommonkey.xmlunit.DifferenceResultListenerImpl21;36import org.custommonkey.xmlunit.DifferenceResultListenerImpl22;37import org.custommonkey.xmlunit.DifferenceResultListenerImpl23;38import org.custommonkey.xmlunit.DifferenceResultListenerImpl24;39import org.custommonkey.xmlunit.DifferenceResultListenerImpl25;40import org.custommonkey.xmlunit.DifferenceResultListenerImpl26;41import org.custommonkey.xmlunit.DifferenceResultListenerImpl27;42import org.custommonkey.xmlunit.DifferenceResultListenerImpl28;43import org.custommonkey.xmlunit.DifferenceResult
equals
Using AI Code Generation
1import java.io.File;2import java.io.IOException;3import java.io.StringReader;4import java.io.StringWriter;5import java.util.Iterator;6import java.util.List;7import javax.xml.parsers.DocumentBuilder;8import javax.xml.parsers.DocumentBuilderFactory;9import javax.xml.parsers.ParserConfigurationException;10import javax.xml.transform.TransformerException;11import javax.xml.transform.TransformerFactoryConfigurationError;12import javax.xml.transform.dom.DOMSource;13import javax.xml.transform.stream.StreamResult;14import javax.xml.transform.stream.StreamSource;15import org.custommonkey.xmlunit.Diff;16import org.custommonkey.xmlunit.Difference;17import org.custommonkey.xmlunit.DifferenceConstants;18import org.custommonkey.xmlunit.DifferenceListener;19import org.custommonkey.xmlunit.XMLUnit;20import org.w3c.dom.Document;21import org.xml.sax.SAXException;22public class xmlunit {23 private static DocumentBuilder builder;24 public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerFactoryConfigurationError, TransformerException {25 XMLUnit.setIgnoreWhitespace(true);26 XMLUnit.setIgnoreAttributeOrder(true);27 XMLUnit.setIgnoreComments(true);28 XMLUnit.setNormalize(true);29 XMLUnit.setNormalizeWhitespace(true);30 XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);31 XMLUnit.setCompareUnmatched(false);32 XMLUnit.setIgnoreComments(true);33 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();34 factory.setNamespaceAware(true);35 builder = factory.newDocumentBuilder();36 Document doc1 = builder.parse(new File("C:\\Users\\Admin\\Desktop\\xmlunit\\src\\main\\java\\file1.xml"));37 Document doc2 = builder.parse(new File("C:\\Users\\Admin\\Desktop\\xmlunit\\src\\main\\java\\file2.xml"));38 DOMSource domSource1 = new DOMSource(doc1);39 DOMSource domSource2 = new DOMSource(doc2);40 StreamSource streamSource1 = new StreamSource(new StringReader(domSourceToString(domSource1)));41 StreamSource streamSource2 = new StreamSource(new StringReader(domSourceToString(domSource2)));42 Diff diff = new Diff(streamSource1, streamSource2);43 DifferenceListener differenceListener = new DifferenceListener() {44 public int differenceFound(Difference
equals
Using AI Code Generation
1import java.io.File;2import java.io.IOException;3import java.util.Iterator;4import java.util.List;5import org.apache.commons.io.FileUtils;6import org.custommonkey.xmlunit.Difference;7import org.custommonkey.xmlunit.DifferenceListener;8import org.custommonkey.xmlunit.DifferenceEvaluators;9import org.custommonkey.xmlunit.XMLUnit;10import org.custommonkey.xmlunit.XMLTestCase;11import org.xml.sax.SAXException;12public class XMLUtil extends XMLTestCase {13 public static void main(String[] args) throws Exception {14 XMLUtil xmlUtil = new XMLUtil();15 xmlUtil.testXMLUnit();16 }17 public void testXMLUnit() throws Exception {18 String actual = FileUtils.readFileToString(new File("actual.xml"));19 String expected = FileUtils.readFileToString(new File("expected.xml"));20 try {21 XMLUnit.setIgnoreWhitespace(true);22 XMLUnit.setIgnoreAttributeOrder(true);23 XMLUnit.setIgnoreComments(true);24 XMLUnit.setNormalizeWhitespace(true);25 XMLUnit.setNormalize(true);26 XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);27 XMLUnit.setCompareUnmatched(false);28 XMLUnit.setIgnoreComments(true);29 XMLUnit.setIgnoreAttributeOrder(true);30 XMLUnit.setIgnoreWhitespace(true);31 XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);32 XMLUnit.setCompareUnmatched(false);33 XMLUnit.setNormalize(true);34 XMLUnit.setNormalizeWhitespace(true);35 XMLUnit.setIgnoreComments(true);36 XMLUnit.setIgnoreAttributeOrder(true);37 XMLUnit.setIgnoreWhitespace(true);38 XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);39 XMLUnit.setCompareUnmatched(false);40 XMLUnit.setNormalize(true);41 XMLUnit.setNormalizeWhitespace(true);42 XMLUnit.setIgnoreComments(true);43 XMLUnit.setIgnoreAttributeOrder(true);44 XMLUnit.setIgnoreWhitespace(true);45 XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);46 XMLUnit.setCompareUnmatched(false);47 XMLUnit.setNormalize(true);48 XMLUnit.setNormalizeWhitespace(true);49 XMLUnit.setIgnoreComments(true);50 XMLUnit.setIgnoreAttributeOrder(true);51 XMLUnit.setIgnoreWhitespace(true);52 XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);53 XMLUnit.setCompareUnmatched(false);54 XMLUnit.setNormalize(true);55 XMLUnit.setNormalizeWhitespace(true);56 XMLUnit.setIgnoreComments(true);
equals
Using AI Code Generation
1package org.cerberus.service.xmlunit;2import java.io.File;3import java.io.IOException;4import java.net.MalformedURLException;5import java.net.URL;6import java.util.Iterator;7import java.util.List;8import javax.xml.parsers.DocumentBuilder;9import javax.xml.parsers.DocumentBuilderFactory;10import javax.xml.parsers.ParserConfigurationException;11import javax.xml.transform.TransformerException;12import javax.xml.transform.TransformerFactoryConfigurationError;13import javax.xml.transform.dom.DOMSource;14import javax.xml.transform.stream.StreamResult;15import javax.xml.transform.stream.StreamSource;16import javax.xml.validation.Schema;17import javax.xml.validation.SchemaFactory;18import javax.xml.validation.Validator;19import org.cerberus.service.xmlunit.Difference;20import org.cerberus.service.xmlunit.DifferenceEngine;21import org.cerberus.service.xmlunit.DifferenceListener;22import org.cerberus.service.xmlunit.DifferenceResult;23import org.cerberus.service.xmlunit.DifferenceResultException;24import org.cerberus.service.xmlunit.DifferenceResultListener;25import org.cerberus.service.xmlunit.DifferenceResultType;26import org.cerberus.service.xmlunit.DifferenceType;27import org.cerberus.service.xmlunit.DifferenceValidator;28import org.cerberus.service.xmlunit.DifferenceValidatorFactory;29import org.cerberus.service.xmlunit.DifferenceValidatorFactoryImpl;30import org.cerberus.service.xmlunit.ValidatorException;31import org.cerberus.service.xmlunit.XMLUnitException;32import org.cerberus.service.xmlunit.XMLUnitRuntimeException;33import org.cerberus.service.xmlunit.XMLUnitValidationException;34import org.cerberus.service.xmlunit.XMLUnitXMLException;35import org.cerberus.service.xmlunit.XMLUnitXSLException;36import org.cerberus.service.xmlunit.XMLUnitXSLTException;37import org.cerberus.service.xmlunit.XMLUnitXSLTResultException;38import org.cerberus.service.xmlunit.XMLUnitXSLTValidationException;39import org.cerberus.service.xmlunit.XMLUnitXSLTValidationResultException;40import org.cerberus.service.xmlunit.XMLUnitXSLValidationException;41import org.cerberus.service.xmlunit.XMLUnitXSLValidationResultException;42import org.cerberus.service.xmlunit.XMLUnitXSLValidationWarningException;43import org.cerberus.service.xmlunit.XMLUnitXSLValidationWarning
equals
Using AI Code Generation
1import org.cerberus.service.xmlunit.Difference;2import org.cerberus.service.xmlunit.Differences;3import java.io.File;4import java.io.IOException;5public class 3 {6 public static void main(String[] args) throws IOException {7 File file1 = new File("C:\\Users\\sachin\\Desktop\\1.xml");8 File file2 = new File("C:\\Users\\sachin\\Desktop\\2.xml");9 Differences differences = new Differences(file1, file2);10 for (Difference difference : differences) {11 System.out.println(difference);12 }13 }14}
equals
Using AI Code Generation
1import java.io.File;2import java.util.Iterator;3import java.util.List;4import org.cerberus.service.xmlunit.Difference;5import org.cerberus.service.xmlunit.DifferenceEngine;6import org.cerberus.service.xmlunit.DifferenceListener;7import org.cerberus.service.xmlunit.DifferenceResult;8import org.cerberus.service.xmlunit.DifferenceResultImpl;9import org.cerberus.service.xmlunit.DifferenceResultListener;10import org.cerberus.service.xmlunit.DifferenceResultListenerImpl;11import org.cerberus.service.xmlunit.DifferenceResultSummary;12import org.cerberus.service.xmlunit.DifferenceResultSummaryImpl;13import org.cerberus.service.xmlunit.DifferenceResultSummaryListener;14import org.cerberus.service.xmlunit.DifferenceResultSummaryListenerImpl;15import org.cerberus.service.xmlunit.DifferenceResultSummaryWithTest;16import org.cerberus.service.xmlunit.DifferenceResultSummaryWithTestImpl;17import org.cerberus.service.xmlunit.DifferenceResultSummaryWithTestListener;18import org.cerberus.service.xmlunit.DifferenceResultSummaryWithTestListenerImpl;19import org.cerberus.service.xmlunit.DifferenceResultWithTest;20import org.cerberus.service.xmlunit.DifferenceResultWithTestImpl;21import org.cerberus.service.xmlunit.DifferenceResultWithTestListener;22import org.cerberus.service.xmlunit.DifferenceResultWithTestListenerImpl;23import org.cerberus.service.xmlunit.DifferenceType;24import org.cerberus.service.xmlunit.DifferenceTypeImpl;25import org.cerberus.service.xmlunit.DifferenceTypeListener;26import org.cerberus.service.xmlunit.DifferenceTypeListenerImpl;27import org.cerberus.service.xmlunit.DifferenceTypeSummary;28import org.cerberus.service.xmlunit.DifferenceTypeSummaryImpl;29import org.cerberus.service.xmlunit.DifferenceTypeSummaryListener;30import org.cerberus.service.xmlunit.DifferenceTypeSummaryListenerImpl;31import org.cerberus.service.xmlunit.DifferenceTypeSummaryWithTest;32import org.cerberus.service.xmlunit.DifferenceTypeSummaryWithTestImpl;33import org.cerberus.service.xmlunit.DifferenceTypeSummary
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!