Author: dlanglais Date: 2010-02-28 16:19:21 +0100 (Sun, 28 Feb 2010) New Revision: 145 Modified: trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/AssertJdomElement.java trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/AssertJdomElementTest.java Log: Prise en compte des attributs dans la comparaison de deux elements. Modified: trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/AssertJdomElement.java =================================================================== --- trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/AssertJdomElement.java 2010-02-28 13:30:46 UTC (rev 144) +++ trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/AssertJdomElement.java 2010-02-28 15:19:21 UTC (rev 145) @@ -23,7 +23,7 @@ */ private static final Log LOG = LogFactory.getLog(AssertJdomElement.class); - private static boolean test = true; + private static boolean test = false; /** * Test if to Element are equivalent. @@ -34,14 +34,9 @@ public static void assertEquivalent(Element expected, Element actual) throws AssertionFailedError { -// String expectedContent = expected.getContent(); -// String actualContent = actual.getContent(); - if(test) { System.out.println("---------------------------------------------"); } -// System.out.println(expected.getContent()); -// System.out.println(actual.getContent()); String expectedElementName = expected.getName(); String actualElementName = actual.getName(); @@ -74,21 +69,28 @@ } } else { // case with only many attribute. - // TODO -> we have only one attribute for instance. + for(Attribute expectedAttribute : expectedAttributes) { + String attributeName = expectedAttribute.getName(); + String expectedAttributeValue = + expected.getAttributeValue(attributeName); + String actualAttributeValue = + actual.getAttributeValue(attributeName); + if (!expectedAttributeValue.equals(actualAttributeValue)) { + throw new AssertionFailedError(); + } + } } if(test) { LOG.info("Attribues" + '\n' + expectedAttributes - + '\n' + actualAttributes); - -// System.out.println("Children"); -// System.out.println(expectedChildren); -// System.out.println(actualChildren); - -// System.out.println("Value"); -// System.out.println(expectedValue); -// System.out.println(actualValue); + + '\n' + actualAttributes + + '\n' + "Children" + + '\n' + expectedChildren + + '\n' + actualChildren + + '\n' + "Value" + + '\n' + expectedValue + + '\n' + actualValue); } if (expectedChildren.size() != actualChildren.size()) { Modified: trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/AssertJdomElementTest.java =================================================================== --- trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/AssertJdomElementTest.java 2010-02-28 13:30:46 UTC (rev 144) +++ trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/AssertJdomElementTest.java 2010-02-28 15:19:21 UTC (rev 145) @@ -37,6 +37,100 @@ * test if two simple elements with attributes are equivalents or not. */ public void testSimpleElementWithAttributes() { + Element a; + Element b; + Element c; + Element d; + +// assertEquivalent(a, b); +// assertEquivalent(c, d); +// assertNotEquivalent(a, c); +// assertNotEquivalent(b, c); +// assertNotEquivalent(a, d); +// assertNotEquivalent(b, d); + + /** + * We put attribute and verify they are not same. + */ + { + a = new Element("test"); + b = new Element("test"); + c = new Element("test2"); + d = new Element("test2"); + + a.setAttribute(new Attribute("attrib1", "attrib1")); + assertNotEquivalent(a, b); + + b.setAttribute(new Attribute("attrib1", "attrib1")); + assertEquivalent(a, b); + + c.setAttribute(new Attribute("attrib1", "attrib1")); + assertNotEquivalent(c, d); + + d.setAttribute(new Attribute("attrib1", "attrib1")); + assertEquivalent(c, d); + } + + /** + * we put same attributes in different order. + */ + { + a = new Element("test"); + b = new Element("test"); + c = new Element("test2"); + d = new Element("test2"); + + a.setAttribute(new Attribute("attrib2", "attrib2")); + assertNotEquivalent(a, b); + a.setAttribute(new Attribute("attrib3", "attrib3")); + assertNotEquivalent(a, b); + + b.setAttribute(new Attribute("attrib3", "attrib3")); + assertNotEquivalent(a, b); + b.setAttribute(new Attribute("attrib2", "attrib2")); + assertEquivalent(a, b); + + c.setAttribute(new Attribute("attrib2", "attrib2")); + assertNotEquivalent(c, d); + c.setAttribute(new Attribute("attrib3", "attrib3")); + assertNotEquivalent(c, d); + + d.setAttribute(new Attribute("attrib3", "attrib3")); + assertNotEquivalent(c, d); + d.setAttribute(new Attribute("attrib2", "attrib2")); + assertEquivalent(c, d); + } + + /** + * we put different attributes. + */ + { + a = new Element("test"); + b = new Element("test"); + + a.setAttribute(new Attribute("attrib4", "attrib4")); + a.setAttribute(new Attribute("attrib5", "attrib5")); + assertNotEquivalent(a, b); + + b.setAttribute(new Attribute("attrib6", "attrib6")); + b.setAttribute(new Attribute("attrib7", "attrib7")); + assertNotEquivalent(a, b); + + + a.setAttribute(new Attribute("attrib7", "attrib7")); + a.setAttribute(new Attribute("attrib6", "attrib6")); + assertNotEquivalent(a, b); + + b.setAttribute(new Attribute("attrib5", "attrib5")); + b.setAttribute(new Attribute("attrib4", "attrib4")); + assertEquivalent(a, b); + } + } + + /** + * test if two simple elements with values are equivalent or not. + */ + public void testSimpleElementWithValue() { Element a = new Element("test"); Element b = new Element("test"); Element c = new Element("test2"); @@ -49,43 +143,84 @@ assertNotEquivalent(a, d); assertNotEquivalent(b, d); - /** - * We put attribute and verify they are not same. - */ - - a.setAttribute(new Attribute("attrib1", "attrib1")); + a.setText("test"); assertNotEquivalent(a, b); - - b.setAttribute(new Attribute("attrib1", "attrib1")); + b.setText("test"); assertEquivalent(a, b); - - c.setAttribute(new Attribute("attrib2", "attrib2")); + c.setText("test"); assertNotEquivalent(c, d); - - d.setAttribute(new Attribute("attrib2", "attrib2")); + d.setText("test"); assertEquivalent(c, d); /** - * we put same attributes in different order. + * We test that with same value, they are not equivalents. */ - a.setAttribute(new Attribute("attrib2", "attrib2")); - assertNotEquivalent(a, b); - a.setAttribute(new Attribute("attrib3", "attrib3")); - assertNotEquivalent(a, b); + assertNotEquivalent(a, c); + assertNotEquivalent(b, c); + assertNotEquivalent(a, d); + assertNotEquivalent(b, d); + } - b.setAttribute(new Attribute("attrib3", "attrib3")); - assertNotEquivalent(a, b); - b.setAttribute(new Attribute("attrib2", "attrib2")); - assertEquivalent(a, b); + /** + * test if two simple elements with values are equivalent or not. + */ + public void testSimpleElementWithSimpleChildren() { + final Element childA = new Element("childTest1"); + final Element childB = new Element("childTest2"); + assertNotEquivalent(childA,childB); - c.setAttribute(new Attribute("attrib4", "attrib4")); - assertNotEquivalent(c, d); - c.setAttribute(new Attribute("attrib5", "attrib5")); - assertNotEquivalent(c, d); + { + /** + * test with children in same order. + */ + Element a = new Element("test"); + Element b = new Element("test"); + assertEquivalent(a, b); - d.setAttribute(new Attribute("attrib5", "attrib5")); - assertNotEquivalent(c, d); - d.setAttribute(new Attribute("attrib4", "attrib4")); - assertEquivalent(c, d); + a.addContent((Element) childA.clone()); + assertNotEquivalent(a, b); + b.addContent((Element) childA.clone()); + assertEquivalent(a, b); + + a.addContent((Element) childB.clone()); + assertNotEquivalent(a, b); + b.addContent((Element) childB.clone()); + assertEquivalent(a, b); + } + { + /** + * test with children in different order. + */ + Element a = new Element("test"); + Element b = new Element("test"); + assertEquivalent(a, b); + + a.addContent((Element) childA.clone()); + assertNotEquivalent(a, b); + b.addContent((Element) childB.clone()); + assertNotEquivalent(a, b); + a.addContent((Element) childB.clone()); + assertNotEquivalent(a, b); + b.addContent((Element) childA.clone()); + assertEquivalent(a, b); + } + { + /** + * test with one chidren in one Element + * and with two same children in the second element. + */ + Element a = new Element("test"); + Element b = new Element("test"); + assertEquivalent(a, b); + + a.addContent((Element) childA.clone()); + a.addContent((Element) childA.clone()); + assertNotEquivalent(a, b); + + b.addContent((Element) childA.clone()); + assertNotEquivalent(a, b); + b.addContent((Element) childA.clone()); + assertEquivalent(a, b); + } } }