Author: dlanglais Date: 2010-02-28 14:30:46 +0100 (Sun, 28 Feb 2010) New Revision: 144 Added: trunk/msm-fromtoXML/src/main/resources/ trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/AssertJdomElement.java trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/AssertJdomElementTest.java Modified: trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLTest.java Log: Transformation de la m?\195?\169thode de test d'?\195?\169quivalent d'Element jdom en assertEquivalent. D?\195?\169but de test de cette m?\195?\169thode assertEquivalent. Added: trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/AssertJdomElement.java =================================================================== --- trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/AssertJdomElement.java (rev 0) +++ trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/AssertJdomElement.java 2010-02-28 13:30:46 UTC (rev 144) @@ -0,0 +1,170 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.nuiton.mapstoragemanager.plugins; + +import java.util.List; +import junit.framework.AssertionFailedError; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.jdom.Attribute; +import org.jdom.Element; + +/** + * + * @author Dorian Langlais + */ +public class AssertJdomElement { + + /** + * Logger. + */ + private static final Log LOG = LogFactory.getLog(AssertJdomElement.class); + + private static boolean test = true; + + /** + * Test if to Element are equivalent. + * @param expected expected element. + * @param actual actual element. + * @return true if expected and actual are equivalents. + */ + 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(); + + if(!expectedElementName.equals(actualElementName)) { + throw new AssertionFailedError(); + } + + List<Attribute> expectedAttributes = expected.getAttributes(); + List<Attribute> actualAttributes = actual.getAttributes(); + + List<Element> expectedChildren = expected.getChildren(); + List<Element> actualChildren = actual.getChildren(); + + String expectedValue = expected.getValue(); + String actualValue = actual.getValue(); + + + // we compare attributes. + if (expectedAttributes.size() != actualAttributes.size()) { + throw new AssertionFailedError(); +// return; + } else if (expectedAttributes.size() == 1) { + // case with only one attribute. + String expectedAttribute = expectedAttributes.toString(); + String actualAttribute = actualAttributes.toString(); + if (! expectedAttribute.equals(actualAttribute)) { + throw new AssertionFailedError(); +// return; + } + } else { + // case with only many attribute. + // TODO -> we have only one attribute for instance. + } + + 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); + } + + if (expectedChildren.size() != actualChildren.size()) { + throw new AssertionFailedError(); +// return; + } else if (expectedChildren.isEmpty()) { + if(test) { + LOG.info(expectedValue + '\n' + actualValue); + } + if (!expected.getValue().equals(actual.getValue())) { + throw new AssertionFailedError(); + + } + return; + } +// System.out.println(expectedChildren); +// System.out.println(actualChildren); + + boolean TF; + // same elements. + for (Element expectedChild : expectedChildren) { + TF = false; + for (Element actualChild : actualChildren) { + if(actualChild.getChildren().size() == + expectedChild.getChildren().size()) { + try { + assertEquivalent(expectedChild, actualChild); + TF = true; + } + catch (AssertionFailedError e) { + LOG.error(e); + } + /*if (testEquals(expectedChild,actualChild)) { +// actualChildren.remove(actualChild); + TF = true; + if(trace) { + System.out.println("EQUIVALENT"); + System.out.println(expectedChild.getValue()); + System.out.println(actualChild.getValue()); + } + } else { + if(trace) { + System.out.println("NOT EQUIVALENT"); + System.out.println(expectedChild.getValue()); + System.out.println(actualChild.getValue()); + } + }*/ + } + } + if (TF == false) { + if(test) { + LOG.info("RETURN FALSE"); + } + throw new AssertionFailedError(); + } + } + + return; + } + + public static void assertNotEquivalent(Element expected, Element actual) + throws AssertionFailedError { + try{ + assertEquivalent(expected, actual); + } + catch (AssertionFailedError e) { + if(test) { + LOG.info(expected.toString() + + ' ' + actual.toString() + " : not equals"); + } + return; + } + if(test) { + LOG.info(expected.toString() + + ' ' + actual.toString() + " : equals"); + } + throw new AssertionFailedError(); + } +} Added: trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/AssertJdomElementTest.java =================================================================== --- trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/AssertJdomElementTest.java (rev 0) +++ trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/AssertJdomElementTest.java 2010-02-28 13:30:46 UTC (rev 144) @@ -0,0 +1,91 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.nuiton.mapstoragemanager.plugins; + +import junit.framework.TestCase; +import org.jdom.Attribute; +import org.jdom.Element; +import static + org.nuiton.mapstoragemanager.plugins.AssertJdomElement.assertEquivalent; +import static + org.nuiton.mapstoragemanager.plugins.AssertJdomElement.assertNotEquivalent; + +/** + * + * @author Dorian Langlais + */ +public class AssertJdomElementTest extends TestCase { + + /** + * test if two simple elements with same name are equivalents. + * test if two simple elements with different name are equivalents. + */ + public void testSimpleElement() { + Element a = new Element("test"); + Element b = new Element("test"); + Element c = new Element("test2"); + + assertEquivalent(a, b); + assertNotEquivalent(a, c); + assertNotEquivalent(b, c); + } + + /** + * test if two simple elements with attributes are equivalents or not. + */ + public void testSimpleElementWithAttributes() { + Element a = new Element("test"); + Element b = new Element("test"); + Element c = new Element("test2"); + Element d = new Element("test2"); + + 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.setAttribute(new Attribute("attrib1", "attrib1")); + assertNotEquivalent(a, b); + + b.setAttribute(new Attribute("attrib1", "attrib1")); + assertEquivalent(a, b); + + c.setAttribute(new Attribute("attrib2", "attrib2")); + assertNotEquivalent(c, d); + + d.setAttribute(new Attribute("attrib2", "attrib2")); + assertEquivalent(c, d); + + /** + * we put same attributes in different order. + */ + 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("attrib4", "attrib4")); + assertNotEquivalent(c, d); + c.setAttribute(new Attribute("attrib5", "attrib5")); + assertNotEquivalent(c, d); + + d.setAttribute(new Attribute("attrib5", "attrib5")); + assertNotEquivalent(c, d); + d.setAttribute(new Attribute("attrib4", "attrib4")); + assertEquivalent(c, d); + } +} Modified: trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLTest.java =================================================================== --- trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLTest.java 2010-02-28 11:06:53 UTC (rev 143) +++ trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLTest.java 2010-02-28 13:30:46 UTC (rev 144) @@ -5,7 +5,7 @@ package org.nuiton.mapstoragemanager.plugins.exporter; -import java.io.IOException; +//import java.io.IOException; import java.lang.reflect.Field; import java.util.List; import junit.framework.TestCase; @@ -14,10 +14,11 @@ import org.jdom.Attribute; import org.jdom.Document; import org.jdom.Element; -import org.jdom.output.Format; -import org.jdom.output.XMLOutputter; +//import org.jdom.output.Format; +//import org.jdom.output.XMLOutputter; import org.nuiton.mapstoragemanager.plugins.Exporter; import org.nuiton.mapstoragemanager.plugins.NewBigTable; +import org.nuiton.mapstoragemanager.plugins.AssertJdomElement; import org.nuiton.mapstoragemanager.plugins.bighashmapv2.BigHashMap; /** @@ -46,7 +47,8 @@ Element actualRoot = actual.getRootElement(); Element expectedRoot = expected.getRootElement(); - assertTrue(testEquals(expectedRoot, actualRoot)); +// assertTrue(testEquals(expectedRoot, actualRoot)); + AssertJdomElement.assertEquivalent(expectedRoot, actualRoot); } /** @@ -248,7 +250,7 @@ * @param actual actual element. * @return true if expected and actual are equivalents. */ - public static boolean testEquals(Element expected, Element actual) { + /*public static boolean testEquals(Element expected, Element actual) { boolean trace = true; // String expectedContent = expected.getContent(); @@ -260,6 +262,13 @@ // System.out.println(expected.getContent()); // System.out.println(actual.getContent()); + String expectedElementName = expected.getName(); + String actualElementName = actual.getName(); + + if(!expectedElementName.equals(actualElementName)) { + return false; + } + List<Attribute> expectedAttributes = expected.getAttributes(); List<Attribute> actualAttributes = actual.getAttributes(); @@ -344,7 +353,7 @@ } return true; - } + }*/ public static void main(String[] args) { NewBigTable nbt = new BigHashMap();