Author: dlanglais Date: 2010-02-28 12:06:53 +0100 (Sun, 28 Feb 2010) New Revision: 143 Modified: trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLTest.java Log: Avancement fonction de comparaison d'?\195?\169lement jdom. 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:00:34 UTC (rev 142) +++ trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLTest.java 2010-02-28 11:06:53 UTC (rev 143) @@ -31,14 +31,32 @@ */ private static final Log LOG = LogFactory.getLog(ToXMLTest.class); + + + private static Document actual; + private static Document expected; + /** * Test to export a bigTable database in a xml format. */ public void testToXML() { + // init actual et expected. + createXMLDocument(); - Document actual = null; - Document expected = new Document(); + Element actualRoot = actual.getRootElement(); + Element expectedRoot = expected.getRootElement(); + assertTrue(testEquals(expectedRoot, actualRoot)); + } + + /** + * Create a bigTable database and a XMLDocument representing the database. + */ + public void createXMLDocument() { + + actual = null; + expected = new Document(); + /******************************** * Creation of the database. * ********************************/ @@ -142,51 +160,51 @@ value5.setText("content5"); cell5.addContent(value5); -// nbt.put("table2", "column3", "6", "content6"); -// Element cell6 = new Element("cell"); -// Attribute cell6key = new Attribute("key", "6"); -// cell6.setAttribute(cell6key); -// column3.addContent(cell6); -// Element value6 = new Element("value"); -// value6.setText("content6"); -// cell6.addContent(value6); -// -// nbt.put("table2", "column4", "7", "content7"); -// Element cell7 = new Element("cell"); -// Attribute cell7key = new Attribute("key", "7"); -// cell7.setAttribute(cell7key); -// column4.addContent(cell7); -// Element value7 = new Element("value"); -// value7.setText("content7"); -// cell7.addContent(value7); -// -// nbt.put("table2", "column4", "8", "content8"); -// Element cell8 = new Element("cell"); -// Attribute cell8key = new Attribute("key", "8"); -// cell8.setAttribute(cell8key); -// column4.addContent(cell8); -// Element value8 = new Element("value"); -// value8.setText("content8"); -// cell8.addContent(value8); -// -// nbt.put("table2", "column5", "9", "content9"); -// Element cell9 = new Element("cell"); -// Attribute cell9key = new Attribute("key", "9"); -// cell9.setAttribute(cell9key); -// column5.addContent(cell9); -// Element value9 = new Element("value"); -// value9.setText("content9"); -// cell9.addContent(value9); -// -// nbt.put("table2", "column5", "10", "content10"); -// Element cell10 = new Element("cell"); -// Attribute cell10key = new Attribute("key", "10"); -// cell10.setAttribute(cell10key); -// column5.addContent(cell10); -// Element value10 = new Element("value"); -// value10.setText("content10"); -// cell10.addContent(value10); + nbt.put("table2", "column3", "6", "content6"); + Element cell6 = new Element("cell"); + Attribute cell6key = new Attribute("key", "6"); + cell6.setAttribute(cell6key); + column3.addContent(cell6); + Element value6 = new Element("value"); + value6.setText("content6"); + cell6.addContent(value6); + nbt.put("table2", "column4", "7", "content7"); + Element cell7 = new Element("cell"); + Attribute cell7key = new Attribute("key", "7"); + cell7.setAttribute(cell7key); + column4.addContent(cell7); + Element value7 = new Element("value"); + value7.setText("content7"); + cell7.addContent(value7); + + nbt.put("table2", "column4", "8", "content8"); + Element cell8 = new Element("cell"); + Attribute cell8key = new Attribute("key", "8"); + cell8.setAttribute(cell8key); + column4.addContent(cell8); + Element value8 = new Element("value"); + value8.setText("content8"); + cell8.addContent(value8); + + nbt.put("table2", "column5", "9", "content9"); + Element cell9 = new Element("cell"); + Attribute cell9key = new Attribute("key", "9"); + cell9.setAttribute(cell9key); + column5.addContent(cell9); + Element value9 = new Element("value"); + value9.setText("content9"); + cell9.addContent(value9); + + nbt.put("table2", "column5", "10", "content10"); + Element cell10 = new Element("cell"); + Attribute cell10key = new Attribute("key", "10"); + cell10.setAttribute(cell10key); + column5.addContent(cell10); + Element value10 = new Element("value"); + value10.setText("content10"); + cell10.addContent(value10); + Exporter exporter = new ToXML(); exporter.exportTo(nbt, null); @@ -208,13 +226,13 @@ LOG.error(ex, ex); } - try { - XMLOutputter output = new XMLOutputter(Format.getPrettyFormat()); - output.output(expected, System.out); - output.output(actual, System.out); - } catch (IOException ex) { - LOG.error(ex, ex); - } +// try { +// XMLOutputter output = new XMLOutputter(Format.getPrettyFormat()); +// output.output(expected, System.out); +// output.output(actual, System.out); +// } catch (IOException ex) { +// LOG.error(ex, ex); +// } // assertEquals(expected,actual); // testEquals(expected.getRootElement(),actual.getRootElement()); @@ -224,40 +242,71 @@ // assertEquals(expected, actual); } + /** + * Test if to Element are equal. + * @param expected expected element. + * @param actual actual element. + * @return true if expected and actual are equivalents. + */ public static boolean testEquals(Element expected, Element actual) { - System.out.println(expected.getContent()); - System.out.println(actual.getContent()); + boolean trace = true; +// String expectedContent = expected.getContent(); +// String actualContent = actual.getContent(); - // same attribute list. -// if (!expected.getAttributes().equals(actual.getAttributes())) { -// return false; -// } + if(trace) { + System.out.println("---------------------------------------------"); + } +// System.out.println(expected.getContent()); +// System.out.println(actual.getContent()); -// if (!expected.getAttributes().equals(actual.getAttributes())) { -// return false; -// } + List<Attribute> expectedAttributes = expected.getAttributes(); + List<Attribute> actualAttributes = actual.getAttributes(); -// System.out.println(expected.getAttributes()); -// System.out.println(actual.getAttributes()); - // same attributes values. -// for (Attribute attribute : (List<Attribute>) expected.getAttributes()) { -// String attrName = attribute.getName(); -// String expectedAttrValue = expected.getAttributeValue(attrName); -// String actualAttrValue = actual.getAttributeValue(attrName); -// if (!expectedAttrValue.equals(actualAttrValue)) { -// return false; -// } -// } + List<Element> expectedChildren = expected.getChildren(); + List<Element> actualChildren = actual.getChildren(); + String expectedValue = expected.getValue(); + String actualValue = actual.getValue(); - List<Element> expectedChildren = (List<Element>) expected.getChildren(); - List<Element> actualChildren = (List<Element>) actual.getChildren(); + // we compare attributes. + if (expectedAttributes.size() != actualAttributes.size()) { + return false; + } else if (expectedAttributes.size() == 1) { + // case with only one attribute. + String expectedAttribute = expectedAttributes.toString(); + String actualAttribute = actualAttributes.toString(); + if (! expectedAttribute.equals(actualAttribute)) { + return false; + } + } else { + // case with only many attribute. + // TODO -> we have only one attribute for instance. + } + + if(trace) { + System.out.println("Attribues"); + System.out.println(expectedAttributes); + System.out.println(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()) { return false; } else if (expectedChildren.isEmpty()) { - return expected.getContent().equals(actual.getContent()); + if(trace) { + System.out.println(expectedValue); + System.out.println(actualValue); + } + return expected.getValue().equals(actual.getValue()); } // System.out.println(expectedChildren); // System.out.println(actualChildren); @@ -272,15 +321,26 @@ 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 { - // System.out.println(expectedChild.getChildren()); - // System.out.println(actualChild.getChildren()); + if(trace) { + System.out.println("NOT EQUIVALENT"); + System.out.println(expectedChild.getValue()); + System.out.println(actualChild.getValue()); + } } } } -// if (TF == false) { -// return false; -// } + if (TF == false) { + if(trace) { + System.out.println("RETURN FALSE"); + } + return false; + } } return true; @@ -312,8 +372,5 @@ Exporter exporter = new ToXML(); exporter.exportTo(nbt, null); - - Document a = new Document(); - Document b = new Document(); } }