Author: dlanglais Date: 2010-02-27 22:15:05 +0100 (Sat, 27 Feb 2010) New Revision: 141 Added: trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLTest.java Removed: trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ExporterTest.java Log: Commencement d'une fonction de comparaison de document jdom... Deleted: trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ExporterTest.java =================================================================== --- trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ExporterTest.java 2010-02-27 16:31:53 UTC (rev 140) +++ trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ExporterTest.java 2010-02-27 21:15:05 UTC (rev 141) @@ -1,251 +0,0 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ - -package org.nuiton.mapstoragemanager.plugins.exporter; - -import java.io.IOException; -import java.lang.reflect.Field; -import junit.framework.TestCase; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.jdom.Attribute; -import org.jdom.Document; -import org.jdom.Element; -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.bighashmapv2.BigHashMap; - -/** - * - * @author Dorian Langlais - */ -public class ExporterTest extends TestCase { - - /** - * Logger. - */ - private static final Log LOG = LogFactory.getLog(ExporterTest.class); -// private static final Log LOGXML = LogFactory.getLog(ToXML.class); - public void testToXML() { - - Document actual = null; - Document expected = new Document(); - - /******************************** - * Creation of the database. * - ********************************/ - NewBigTable nbt = new BigHashMap(); - Element database = new Element("database"); - expected.addContent(database); - - /******************************** - * Creation of the tables. * - ********************************/ - nbt.createTable("table1"); - Element table1 = new Element("table"); - Attribute table1name = new Attribute("tableName", "table1"); - table1.setAttribute(table1name); - database.addContent(table1); - nbt.createTable("table2"); - Element table2 = new Element("table"); - Attribute table2name = new Attribute("tableName", "table2"); - table2.setAttribute(table2name); - database.addContent(table2); - - /******************************** - * Creation of the columns. * - ********************************/ - nbt.createColumn("table1", "column1"); - Element column1 = new Element("column"); - Attribute column1name = new Attribute("columnName", "column1"); - column1.setAttribute(column1name); - table1.addContent(column1); - - nbt.createColumn("table1", "column2"); - Element column2 = new Element("column"); - Attribute column2name = new Attribute("columnName", "column2"); - column2.setAttribute(column2name); - table1.addContent(column2); - - nbt.createColumn("table2", "column3"); - Element column3 = new Element("column"); - Attribute column3name = new Attribute("columnName", "column3"); - column3.setAttribute(column3name); - table2.addContent(column3); - - nbt.createColumn("table2", "column4"); - Element column4 = new Element("column"); - Attribute column4name = new Attribute("columnName", "column4"); - column4.setAttribute(column4name); - table2.addContent(column4); - - nbt.createColumn("table2", "column5"); - Element column5 = new Element("column"); - Attribute column5name = new Attribute("columnName", "column5"); - column5.setAttribute(column5name); - table2.addContent(column5); - - - /******************************** - * Put elements in the columns. * - ********************************/ - nbt.put("table1", "column1", "1", "content1"); - Element cell1 = new Element("cell"); - Attribute cell1key = new Attribute("key", "1"); - cell1.setAttribute(cell1key); - column1.addContent(cell1); - Element value1 = new Element("value"); - value1.setText("content1"); - cell1.addContent(value1); - - nbt.put("table1", "column1", "2", "content2"); - Element cell2 = new Element("cell"); - Attribute cell2key = new Attribute("key", "2"); - cell2.setAttribute(cell2key); - column1.addContent(cell2); - Element value2 = new Element("value"); - value2.setText("content2"); - cell2.addContent(value2); - - nbt.put("table1", "column2", "3", "content3"); - Element cell3 = new Element("cell"); - Attribute cell3key = new Attribute("key", "3"); - cell3.setAttribute(cell3key); - column2.addContent(cell3); - Element value3 = new Element("value"); - value3.setText("content3"); - cell3.addContent(value3); - - nbt.put("table1", "column2", "4", "content4"); - Element cell4 = new Element("cell"); - Attribute cell4key = new Attribute("key", "4"); - cell4.setAttribute(cell4key); - column2.addContent(cell4); - Element value4 = new Element("value"); - value4.setText("content4"); - cell4.addContent(value4); - - nbt.put("table2", "column3", "5", "content5"); - Element cell5 = new Element("cell"); - Attribute cell5key = new Attribute("key", "5"); - cell5.setAttribute(cell5key); - column3.addContent(cell5); - Element value5 = new Element("value"); - 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); - - Exporter exporter = new ToXML(); - exporter.exportTo(nbt, null); - - -// expected.toString(); - - Field fdocument; - try { - fdocument = exporter.getClass().getDeclaredField("document"); - fdocument.setAccessible(true); - actual = (Document) fdocument.get(exporter); - } catch (IllegalArgumentException ex) { - LOG.error(ex, ex); - } catch (IllegalAccessException ex) { - LOG.error(ex, ex); - } catch (NoSuchFieldException ex) { - LOG.error(ex, ex); - } catch (SecurityException 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); - } - -// System.out.println(expected); -// System.out.println(actual); -// assertEquals(expected.toString(),actual.toString()); -// assertEquals(expected, actual); - } - - public static void main(String[] args) { - NewBigTable nbt = new BigHashMap(); - nbt.createTable("table1"); - nbt.createTable("table2"); - - nbt.createColumn("table1", "column1"); - nbt.createColumn("table1", "column2"); - - nbt.createColumn("table2", "column3"); - nbt.createColumn("table2", "column4"); - nbt.createColumn("table2", "column5"); - - nbt.put("table1", "column1", "1", "content1"); - nbt.put("table1", "column1", "2", "content2"); - nbt.put("table1", "column2", "3", "content3"); - nbt.put("table1", "column2", "4", "content4"); - - nbt.put("table2", "column3", "5", "content5"); - nbt.put("table2", "column3", "6", "content6"); - nbt.put("table2", "column4", "7", "content7"); - nbt.put("table2", "column4", "8", "content8"); - nbt.put("table2", "column5", "9", "content9"); - nbt.put("table2", "column5", "10", "content10"); - - Exporter exporter = new ToXML(); - exporter.exportTo(nbt, null); - - Document a = new Document(); - Document b = new Document(); - } -} Added: 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 (rev 0) +++ trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLTest.java 2010-02-27 21:15:05 UTC (rev 141) @@ -0,0 +1,319 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.nuiton.mapstoragemanager.plugins.exporter; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.List; +import junit.framework.TestCase; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.jdom.Attribute; +import org.jdom.Document; +import org.jdom.Element; +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.bighashmapv2.BigHashMap; + +/** + * + * @author Dorian Langlais + */ +public class ToXMLTest extends TestCase { + + /** + * Logger. + */ + private static final Log LOG = LogFactory.getLog(ToXMLTest.class); + + /** + * Test to export a bigTable database in a xml format. + */ + public void testToXML() { + + Document actual = null; + Document expected = new Document(); + + /******************************** + * Creation of the database. * + ********************************/ + NewBigTable nbt = new BigHashMap(); + Element database = new Element("database"); + expected.addContent(database); + + /******************************** + * Creation of the tables. * + ********************************/ + nbt.createTable("table1"); + Element table1 = new Element("table"); + Attribute table1name = new Attribute("tableName", "table1"); + table1.setAttribute(table1name); + database.addContent(table1); + nbt.createTable("table2"); + Element table2 = new Element("table"); + Attribute table2name = new Attribute("tableName", "table2"); + table2.setAttribute(table2name); + database.addContent(table2); + + /******************************** + * Creation of the columns. * + ********************************/ + nbt.createColumn("table1", "column1"); + Element column1 = new Element("column"); + Attribute column1name = new Attribute("columnName", "column1"); + column1.setAttribute(column1name); + table1.addContent(column1); + + nbt.createColumn("table1", "column2"); + Element column2 = new Element("column"); + Attribute column2name = new Attribute("columnName", "column2"); + column2.setAttribute(column2name); + table1.addContent(column2); + + nbt.createColumn("table2", "column3"); + Element column3 = new Element("column"); + Attribute column3name = new Attribute("columnName", "column3"); + column3.setAttribute(column3name); + table2.addContent(column3); + + nbt.createColumn("table2", "column4"); + Element column4 = new Element("column"); + Attribute column4name = new Attribute("columnName", "column4"); + column4.setAttribute(column4name); + table2.addContent(column4); + + nbt.createColumn("table2", "column5"); + Element column5 = new Element("column"); + Attribute column5name = new Attribute("columnName", "column5"); + column5.setAttribute(column5name); + table2.addContent(column5); + + + /******************************** + * Put elements in the columns. * + ********************************/ + nbt.put("table1", "column1", "1", "content1"); + Element cell1 = new Element("cell"); + Attribute cell1key = new Attribute("key", "1"); + cell1.setAttribute(cell1key); + column1.addContent(cell1); + Element value1 = new Element("value"); + value1.setText("content1"); + cell1.addContent(value1); + + nbt.put("table1", "column1", "2", "content2"); + Element cell2 = new Element("cell"); + Attribute cell2key = new Attribute("key", "2"); + cell2.setAttribute(cell2key); + column1.addContent(cell2); + Element value2 = new Element("value"); + value2.setText("content2"); + cell2.addContent(value2); + + nbt.put("table1", "column2", "3", "content3"); + Element cell3 = new Element("cell"); + Attribute cell3key = new Attribute("key", "3"); + cell3.setAttribute(cell3key); + column2.addContent(cell3); + Element value3 = new Element("value"); + value3.setText("content3"); + cell3.addContent(value3); + + nbt.put("table1", "column2", "4", "content4"); + Element cell4 = new Element("cell"); + Attribute cell4key = new Attribute("key", "4"); + cell4.setAttribute(cell4key); + column2.addContent(cell4); + Element value4 = new Element("value"); + value4.setText("content4"); + cell4.addContent(value4); + + nbt.put("table2", "column3", "5", "content5"); + Element cell5 = new Element("cell"); + Attribute cell5key = new Attribute("key", "5"); + cell5.setAttribute(cell5key); + column3.addContent(cell5); + Element value5 = new Element("value"); + 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); + + Exporter exporter = new ToXML(); + exporter.exportTo(nbt, null); + + +// expected.toString(); + + Field fdocument; + try { + fdocument = exporter.getClass().getDeclaredField("document"); + fdocument.setAccessible(true); + actual = (Document) fdocument.get(exporter); + } catch (IllegalArgumentException ex) { + LOG.error(ex, ex); + } catch (IllegalAccessException ex) { + LOG.error(ex, ex); + } catch (NoSuchFieldException ex) { + LOG.error(ex, ex); + } catch (SecurityException 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()); +// System.out.println(expected); +// System.out.println(actual); +// assertEquals(expected.toString(),actual.toString()); +// assertEquals(expected, actual); + } + + public static boolean testEquals(Element expected, Element actual) { + + System.out.println(expected.getContent()); + System.out.println(actual.getContent()); + + // same attribute list. +// if (!expected.getAttributes().equals(actual.getAttributes())) { +// return false; +// } + +// if (!expected.getAttributes().equals(actual.getAttributes())) { +// return false; +// } + +// 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 = (List<Element>) expected.getChildren(); + List<Element> actualChildren = (List<Element>) actual.getChildren(); + + if (expectedChildren.size() != actualChildren.size()) { + return false; + } else if (expectedChildren.isEmpty()) { + return expected.getContent().equals(actual.getContent()); + } +// 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()) { + if (testEquals(expectedChild,actualChild)) { +// actualChildren.remove(actualChild); + TF = true; + } else { + // System.out.println(expectedChild.getChildren()); + // System.out.println(actualChild.getChildren()); + } + } + } +// if (TF == false) { +// return false; +// } + } + + return true; + } + + public static void main(String[] args) { + NewBigTable nbt = new BigHashMap(); + nbt.createTable("table1"); + nbt.createTable("table2"); + + nbt.createColumn("table1", "column1"); + nbt.createColumn("table1", "column2"); + + nbt.createColumn("table2", "column3"); + nbt.createColumn("table2", "column4"); + nbt.createColumn("table2", "column5"); + + nbt.put("table1", "column1", "1", "content1"); + nbt.put("table1", "column1", "2", "content2"); + nbt.put("table1", "column2", "3", "content3"); + nbt.put("table1", "column2", "4", "content4"); + + nbt.put("table2", "column3", "5", "content5"); + nbt.put("table2", "column3", "6", "content6"); + nbt.put("table2", "column4", "7", "content7"); + nbt.put("table2", "column4", "8", "content8"); + nbt.put("table2", "column5", "9", "content9"); + nbt.put("table2", "column5", "10", "content10"); + + Exporter exporter = new ToXML(); + exporter.exportTo(nbt, null); + + Document a = new Document(); + Document b = new Document(); + } +}