Mapstoragemanager-commits
Threads by month
- ----- 2026 -----
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
March 2010
- 6 participants
- 152 discussions
r152 - in trunk/msm-fromtoXML/src: main/java/org/nuiton/mapstoragemanager/plugins/exporter main/java/org/nuiton/mapstoragemanager/plugins/importer test/java/org/nuiton/mapstoragemanager/plugins/importer test/resources
by dlanglais@users.nuiton.org 01 Mar '10
by dlanglais@users.nuiton.org 01 Mar '10
01 Mar '10
Author: dlanglais
Date: 2010-03-01 20:52:08 +0100 (Mon, 01 Mar 2010)
New Revision: 152
Added:
trunk/msm-fromtoXML/src/test/resources/fiveColumns.xml
trunk/msm-fromtoXML/src/test/resources/sixCells.xml
Removed:
trunk/msm-fromtoXML/src/test/resources/twoTablesFiveColumns.xml
Modified:
trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXML.java
trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/importer/FromXML.java
trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/importer/FromXMLTest.java
Log:
Avancement test de FromXML.java.
Import cellules OK.
Pas de prise en compte de version.
Modified: trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXML.java
===================================================================
--- trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXML.java 2010-03-01 18:25:21 UTC (rev 151)
+++ trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXML.java 2010-03-01 19:52:08 UTC (rev 152)
@@ -52,7 +52,7 @@
}
/**
- * For eath table of the database, the document is constructed.
+ * For each table of the database, the document is constructed.
* @param bigTable the bigTable.
* @param database the database Element.
*/
@@ -76,7 +76,7 @@
}
/**
- * For eath column of the table, the document is constructed.
+ * For each column of the table, the document is constructed.
* @param bigTable the bigTable.
* @param table the table Element.
*/
@@ -103,7 +103,7 @@
}
/**
- * For eath cell of the column, the document is constructed.
+ * For each cell of the column, the document is constructed.
* @param bigTable the bigTable.
* @param column the column Element.
*/
Modified: trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/importer/FromXML.java
===================================================================
--- trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/importer/FromXML.java 2010-03-01 18:25:21 UTC (rev 151)
+++ trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/importer/FromXML.java 2010-03-01 19:52:08 UTC (rev 152)
@@ -57,15 +57,15 @@
}
/**
- * For eath table of the database, the document is constructed.
+ * For each table of the database, the document is constructed.
* @param bigTable the bigTable.
* @param database the database Element.
*/
private void forEachTable(NewBigTable bigTable, final Element database) {
- // the tables name.
- List<Element> tableElements = database.getChildren("table");
+ // the table elements.
+ List<Element> tables = database.getChildren("table");
- for (Element table : tableElements) {
+ for (Element table : tables) {
// get table name and add to the bigTable.
String tableName = table.getAttributeValue("tableName");
bigTable.createTable(tableName);
@@ -76,7 +76,7 @@
}
/**
- * For eath column of the table, the document is constructed.
+ * For each column of the table, the document is constructed.
* @param bigTable the bigTable.
* @param table the table Element.
*/
@@ -84,10 +84,10 @@
// the table name.
String tableName = table.getAttributeValue("tableName");
- // the columns name.
- List<Element> columnElements = table.getChildren("column");
+ // the column elements.
+ List<Element> columns = table.getChildren("column");
- for (Element column : columnElements) {
+ for (Element column : columns) {
// get column name and add to the table.
String columnName = column.getAttributeValue("columnName");
bigTable.createColumn(tableName, columnName);
@@ -98,7 +98,7 @@
}
/**
- * For eath cell of the column, the document is constructed.
+ * For each cell of the column, the document is constructed.
* @param bigTable the bigTable.
* @param column the column Element.
*/
@@ -109,36 +109,16 @@
// the column name.
String columnName = column.getAttributeValue("columnName");
- // the table keys.
- Set<String> keys = bigTable.getKeys(tableName);
+ // the cell elements.
+ List<Element> cells = column.getChildren("cell");
- String content = "";
- for (String key : keys) {
- try{
- content = bigTable.get(tableName, columnName, key);
- if ( ! "".equals(content)) {
+ for (Element cell : cells) {
+ // get cell key and content.
+ String cellKey = cell.getAttributeValue("key");
+ String cellContent = cell.getChild("value").getText();
- // new cell Element.
- Element cell = new Element("cell");
- // new attribute which contain the cell key.
- Attribute cellKeyAttr = new Attribute("key", key);
- cell.setAttribute(cellKeyAttr);
-
- // we add the cell to the column.
- column.addContent(cell);
-
- // new value Element.
- Element value = new Element("value");
- value.setText(content);
-
- // we add the value to the cell.
- cell.addContent(value);
- }
- }
- catch (NoSuchElementException e) {
- LOG.trace(e, e);
- }
-
+ // add content to the bigtable.
+ bigTable.put(tableName, columnName, cellKey, cellContent);
}
}
Modified: trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/importer/FromXMLTest.java
===================================================================
--- trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/importer/FromXMLTest.java 2010-03-01 18:25:21 UTC (rev 151)
+++ trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/importer/FromXMLTest.java 2010-03-01 19:52:08 UTC (rev 152)
@@ -8,8 +8,12 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
+import java.util.NoSuchElementException;
import java.util.Set;
+import junit.framework.Assert;
import junit.framework.TestCase;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.nuiton.mapstoragemanager.plugins.Importer;
import org.nuiton.mapstoragemanager.plugins.NewBigTable;
import org.nuiton.mapstoragemanager.plugins.bighashmapv2.BigHashMap;
@@ -21,12 +25,18 @@
*/
public class FromXMLTest extends TestCase {
+ /**
+ * Logger.
+ */
+ private static final Log LOG = LogFactory.getLog(FromXMLTest.class);
+
// public void testImportFrom() {
// Assert.fail();
// }
/**
- * Import the database from the file fiveTables and verify the content.
+ * Import the database from the file fiveTables.
+ * Verify the content.
*/
public void testImportFromFiveTables() {
@@ -39,6 +49,9 @@
importer.importFrom(bigTable, fiveTables);
+ /************************
+ * Test Tables *
+ ************************/
Set<String> tableNamesExpected;
{
/**
@@ -62,10 +75,14 @@
}
}
- public void testImportFromTwoTablesFiveColumns() {
+ /**
+ * Import the database from the file fiveColumns.
+ * Verify the content.
+ */
+ public void testImportFromFiveColumns() {
File twoTablesFiveColumns =
- getFile.getTestFile("/src/test/resources/twoTablesFiveColumns.xml");
+ getFile.getTestFile("/src/test/resources/fiveColumns.xml");
// import fiveTables.
Importer importer = new FromXML();
@@ -73,6 +90,9 @@
importer.importFrom(bigTable, twoTablesFiveColumns);
+ /************************
+ * Test Tables *
+ ************************/
Set<String> tableNamesExpected;
{
/**
@@ -85,6 +105,9 @@
Set<String> tableNamesActual = bigTable.getTablesNames();
assertEquals(tableNamesExpected, tableNamesActual);
+ /************************
+ * Test Columns *
+ ************************/
Map<String, Set<String>> columnNamesExpected;
{
/**
@@ -101,11 +124,143 @@
columnNamesExpected.put("table1", table1ColumnNames);
columnNamesExpected.put("table2", table2ColumnNames);
}
-
+
Set<String> columnNamesActual;
for (String tableName : tableNamesActual) {
columnNamesActual = bigTable.getColumnsNames(tableName);
assertEquals(columnNamesExpected.get(tableName), columnNamesActual);
}
}
+
+ /**
+ * Import the database from the file tenCells.
+ * Verify the content.
+ */
+ public void testImportFromThreeTenCells() {
+
+ File twoTablesFiveColumns =
+ getFile.getTestFile("/src/test/resources/sixCells.xml");
+
+ // import fiveTables.
+ Importer importer = new FromXML();
+ NewBigTable bigTable = new BigHashMap();
+
+ importer.importFrom(bigTable, twoTablesFiveColumns);
+
+ /************************
+ * Test Tables *
+ ************************/
+ Set<String> tableNamesExpected;
+ {
+ /**
+ * Init tableNames.
+ */
+ tableNamesExpected = new HashSet<String>();
+ tableNamesExpected.add("table1");
+ tableNamesExpected.add("table2");
+ }
+ Set<String> tableNamesActual = bigTable.getTablesNames();
+ assertEquals(tableNamesExpected, tableNamesActual);
+
+ /************************
+ * Test Columns *
+ ************************/
+ Map<String, Set<String>> columnNamesExpected;
+ {
+ /**
+ * Init columnNames.
+ */
+ columnNamesExpected = new HashMap<String, Set<String>>();
+ Set<String> table1ColumnNames = new HashSet<String>();
+ table1ColumnNames.add("column1");
+ Set<String> table2ColumnNames = new HashSet<String>();
+ table2ColumnNames.add("column2");
+ table2ColumnNames.add("column3");
+ columnNamesExpected.put("table1", table1ColumnNames);
+ columnNamesExpected.put("table2", table2ColumnNames);
+ }
+
+ Set<String> columnNamesActual;
+ for (String tableName : tableNamesActual) {
+ columnNamesActual = bigTable.getColumnsNames(tableName);
+ assertEquals(columnNamesExpected.get(tableName), columnNamesActual);
+ }
+
+ /************************
+ * Test Cells *
+ ************************/
+ Map<String, Map<String, Map<String, String>>> CellContentExpected;
+ {
+ /**
+ * Init cellContent.
+ */
+ CellContentExpected = new HashMap<String,
+ Map<String, Map<String, String>>>();
+ // table 1.
+ Map<String, Map<String, String>> table1 =
+ new HashMap<String, Map<String, String>>();
+ // column 1.
+ Map<String,String> column1 = new HashMap<String, String>();
+ column1.put("111", "111");
+ table1.put("column1", column1);
+ // table 2.
+ Map<String, Map<String, String>> table2 =
+ new HashMap<String, Map<String, String>>();
+ // column 2.
+ Map<String,String> column2 = new HashMap<String, String>();
+ column2.put("221", "221");
+ column2.put("222", "222");
+ table2.put("column2", column2);
+ // column 3.
+ Map<String,String> column3 = new HashMap<String, String>();
+ column3.put("231", "231");
+ column3.put("232", "232");
+ column3.put("233", "233");
+ table2.put("column3", column3);
+
+ CellContentExpected.put("table1", table1);
+ CellContentExpected.put("table2", table2);
+ LOG.info(CellContentExpected);
+ }
+
+ Set<String> tableKeys;
+ String cellContentActual;
+ String cellContentExpected = null;
+ for (String tableName : tableNamesActual) {
+ columnNamesActual = bigTable.getColumnsNames(tableName);
+ tableKeys = bigTable.getKeys(tableName);
+ for (String columnName : columnNamesActual) {
+ for (String cellKey : tableKeys) {
+
+ try {
+ try{
+ cellContentExpected =
+ CellContentExpected
+ .get(tableName)
+ .get(columnName)
+ .get(cellKey);
+ } catch (NullPointerException ex) {
+ LOG.trace(ex);
+ }
+
+ LOG.info(tableName + ' '
+ + columnName + ' '
+ + cellKey + ' '
+ + cellContentExpected);
+
+ cellContentActual =
+ bigTable.get(tableName, columnName, cellKey);
+ assertEquals(cellContentExpected, cellContentActual);
+ } catch (NoSuchElementException ex) {
+ if(cellContentExpected != null) {
+ LOG.error(ex, ex);
+ Assert.fail();
+ } else {
+ LOG.trace(ex);
+ }
+ }
+ }
+ }
+ }
+ }
}
Added: trunk/msm-fromtoXML/src/test/resources/fiveColumns.xml
===================================================================
--- trunk/msm-fromtoXML/src/test/resources/fiveColumns.xml (rev 0)
+++ trunk/msm-fromtoXML/src/test/resources/fiveColumns.xml 2010-03-01 19:52:08 UTC (rev 152)
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<database>
+ <table tableName="table1">
+ <column columnName="column1" />
+ <column columnName="column2" />
+ </table>
+ <table tableName="table2">
+ <column columnName="column3" />
+ <column columnName="column4" />
+ <column columnName="column5" />
+ </table>
+</database>
+
Added: trunk/msm-fromtoXML/src/test/resources/sixCells.xml
===================================================================
--- trunk/msm-fromtoXML/src/test/resources/sixCells.xml (rev 0)
+++ trunk/msm-fromtoXML/src/test/resources/sixCells.xml 2010-03-01 19:52:08 UTC (rev 152)
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<database>
+ <table tableName="table1">
+ <column columnName="column1">
+ <cell key="111">
+ <value>111</value>
+ </cell>
+ </column>
+ </table>
+ <table tableName="table2">
+ <column columnName="column2">
+ <cell key="221">
+ <value>221</value>
+ </cell>
+ <cell key="222">
+ <value>222</value>
+ </cell>
+ </column>
+ <column columnName="column3">
+ <cell key="231">
+ <value>231</value>
+ </cell>
+ <cell key="232">
+ <value>232</value>
+ </cell>
+ <cell key="233">
+ <value>233</value>
+ </cell>
+ </column>
+ </table>
+</database>
+
Deleted: trunk/msm-fromtoXML/src/test/resources/twoTablesFiveColumns.xml
===================================================================
--- trunk/msm-fromtoXML/src/test/resources/twoTablesFiveColumns.xml 2010-03-01 18:25:21 UTC (rev 151)
+++ trunk/msm-fromtoXML/src/test/resources/twoTablesFiveColumns.xml 2010-03-01 19:52:08 UTC (rev 152)
@@ -1,13 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<database>
- <table tableName="table1">
- <column columnName="column1" />
- <column columnName="column2" />
- </table>
- <table tableName="table2">
- <column columnName="column3" />
- <column columnName="column4" />
- <column columnName="column5" />
- </table>
-</database>
-
1
0
r151 - in trunk/msm-fromtoXML/src: main/java/org/nuiton/mapstoragemanager/plugins/exporter main/java/org/nuiton/mapstoragemanager/plugins/importer test test/java/org/nuiton/mapstoragemanager/plugins test/java/org/nuiton/mapstoragemanager/plugins/exporter test/java/org/nuiton/mapstoragemanager/plugins/importer test/resources
by dlanglais@users.nuiton.org 01 Mar '10
by dlanglais@users.nuiton.org 01 Mar '10
01 Mar '10
Author: dlanglais
Date: 2010-03-01 19:25:21 +0100 (Mon, 01 Mar 2010)
New Revision: 151
Added:
trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/importer/FromXML.java
trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/getFile.java
trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/importer/FromXMLTest.java
trunk/msm-fromtoXML/src/test/resources/
trunk/msm-fromtoXML/src/test/resources/fiveTables.xml
trunk/msm-fromtoXML/src/test/resources/twoTablesFiveColumns.xml
Modified:
trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXML.java
trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLTest.java
Log:
Commencement FromXML.java, et classe de test.
Pour le moment import Tables et Colonnes : OK.
Toujours pas de gestion des versions.
Modified: trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXML.java
===================================================================
--- trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXML.java 2010-02-28 17:58:02 UTC (rev 150)
+++ trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXML.java 2010-03-01 18:25:21 UTC (rev 151)
@@ -6,6 +6,7 @@
import java.io.File;
import java.io.FileOutputStream;
+import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.Set;
import org.apache.commons.logging.Log;
@@ -28,27 +29,26 @@
* Logger.
*/
private static final Log LOG = LogFactory.getLog(ToXML.class);
+ /**
+ * the jdom document.
+ */
+ private static Document document;
- // the racine which is the database.
- private static Element racine;
-
- // the jdom document.
- private static Document document;
-
+ /**
+ * {@inheritDoc}
+ */
@Override
public void exportTo(NewBigTable bigTable, File file) {
- racine = new Element("database");
- document = new Document(racine);
+ Element database = new Element("database");
+ document = new Document(database);
// Construction of the document.
- forEachTable(bigTable, racine);
+ forEachTable(bigTable, database);
- // the methods to print and to save the database in XML file.
- //toSreen();
- if (file != null) {
- save(file);
- }
+ // print the jdom document.
+// toSreen();
+ save(file);
}
/**
@@ -56,7 +56,7 @@
* @param bigTable the bigTable.
* @param database the database Element.
*/
- private void forEachTable(NewBigTable bigTable, Element database) {
+ private void forEachTable(final NewBigTable bigTable, Element database) {
// the tables name.
Set<String> tablesNames = bigTable.getTablesNames();
@@ -80,25 +80,25 @@
* @param bigTable the bigTable.
* @param table the table Element.
*/
- private void forEachColumn(NewBigTable bigTable, Element table) {
+ private void forEachColumn(final NewBigTable bigTable, Element table) {
// the table name.
String tableName = table.getAttributeValue("tableName");
// the columns name.
Set<String> columnNames = bigTable.getColumnsNames(tableName);
- for (String column : columnNames) {
+ for (String columnName : columnNames) {
// new column Element.
- Element columnElement = new Element("column");
+ Element column = new Element("column");
// new attribute which contain the column name.
- Attribute columnNameAttribute = new Attribute("columnName", column);
- columnElement.setAttribute(columnNameAttribute);
+ Attribute columnNameAttr = new Attribute("columnName", columnName);
+ column.setAttribute(columnNameAttr);
// we add the column to the table.
- table.addContent(columnElement);
+ table.addContent(column);
- // we search the columns of the table.
- forEachCell(bigTable, columnElement);
+ // we search the cells of the table.
+ forEachCell(bigTable, column);
}
}
@@ -107,7 +107,7 @@
* @param bigTable the bigTable.
* @param column the column Element.
*/
- private void forEachCell(NewBigTable bigTable, Element column) {
+ private void forEachCell(final NewBigTable bigTable, Element column) {
// the table name.
String tableName =
column.getParentElement().getAttributeValue("tableName");
@@ -124,27 +124,24 @@
if ( ! "".equals(content)) {
// new cell Element.
- Element cellElement = new Element("cell");
+ Element cell = new Element("cell");
// new attribute which contain the cell key.
- Attribute cellKeyAttribute = new Attribute("key", key);
- cellElement.setAttribute(cellKeyAttribute);
+ Attribute cellKeyAttr = new Attribute("key", key);
+ cell.setAttribute(cellKeyAttr);
// we add the cell to the column.
- column.addContent(cellElement);
+ column.addContent(cell);
// new value Element.
- Element valueElement = new Element("value");
- // // new attribute which contain the value version.
- // Attribute valueVersionAttribute = new Attribute("version", v);
- // valueElement.setAttribute(valueVersionAttribute);
- valueElement.setText(content);
+ Element value = new Element("value");
+ value.setText(content);
// we add the value to the cell.
- cellElement.addContent(valueElement);
+ cell.addContent(value);
}
}
catch (NoSuchElementException e) {
-// LOG.warn(e, e);
+ LOG.trace(e, e);
}
}
@@ -170,9 +167,11 @@
private static void save(File file) {
try {
XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
- output.output(document, new FileOutputStream(file));
- } catch (java.io.IOException e) {
- LOG.error(e, e);
+ FileOutputStream fos = new FileOutputStream(file);
+ output.output(document, fos);
+ fos.close();
+ } catch (IOException ex) {
+ LOG.error(ex, ex);
}
}
}
Added: trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/importer/FromXML.java
===================================================================
--- trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/importer/FromXML.java (rev 0)
+++ trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/importer/FromXML.java 2010-03-01 18:25:21 UTC (rev 151)
@@ -0,0 +1,172 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.nuiton.mapstoragemanager.plugins.importer;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Set;
+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.JDOMException;
+import org.jdom.input.SAXBuilder;
+import org.jdom.output.Format;
+import org.jdom.output.XMLOutputter;
+import org.nuiton.mapstoragemanager.plugins.Importer;
+import org.nuiton.mapstoragemanager.plugins.NewBigTable;
+
+/**
+ *
+ * @author Dorian Langlais
+ */
+public class FromXML implements Importer {
+
+ /**
+ * Logger.
+ */
+ private static final Log LOG = LogFactory.getLog(FromXML.class);
+ /**
+ * the jdom document.
+ */
+ private static Document document;
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void importFrom(NewBigTable bigTable, File file) {
+ try {
+ open(file);
+
+ Element racine = document.getRootElement();
+
+ // Construction of the bigtable database.
+ forEachTable(bigTable, racine);
+
+ // print the jdomDocument.
+// toSreen();
+ } catch (IOException ex) {
+ LOG.error(ex, ex);
+ }
+ }
+
+ /**
+ * For eath table of the database, the document is constructed.
+ * @param bigTable the bigTable.
+ * @param database the database Element.
+ */
+ private void forEachTable(NewBigTable bigTable, final Element database) {
+ // the tables name.
+ List<Element> tableElements = database.getChildren("table");
+
+ for (Element table : tableElements) {
+ // get table name and add to the bigTable.
+ String tableName = table.getAttributeValue("tableName");
+ bigTable.createTable(tableName);
+
+ // we search the columns of the table.
+ forEachColumn(bigTable, table);
+ }
+ }
+
+ /**
+ * For eath column of the table, the document is constructed.
+ * @param bigTable the bigTable.
+ * @param table the table Element.
+ */
+ private void forEachColumn(NewBigTable bigTable, final Element table) {
+ // the table name.
+ String tableName = table.getAttributeValue("tableName");
+
+ // the columns name.
+ List<Element> columnElements = table.getChildren("column");
+
+ for (Element column : columnElements) {
+ // get column name and add to the table.
+ String columnName = column.getAttributeValue("columnName");
+ bigTable.createColumn(tableName, columnName);
+
+ // we search the cells of the table.
+ forEachCell(bigTable, column);
+ }
+ }
+
+ /**
+ * For eath cell of the column, the document is constructed.
+ * @param bigTable the bigTable.
+ * @param column the column Element.
+ */
+ private void forEachCell(NewBigTable bigTable, final Element column) {
+ // the table name.
+ String tableName =
+ column.getParentElement().getAttributeValue("tableName");
+ // the column name.
+ String columnName = column.getAttributeValue("columnName");
+
+ // the table keys.
+ Set<String> keys = bigTable.getKeys(tableName);
+
+ String content = "";
+ for (String key : keys) {
+ try{
+ content = bigTable.get(tableName, columnName, key);
+ if ( ! "".equals(content)) {
+
+ // new cell Element.
+ Element cell = new Element("cell");
+ // new attribute which contain the cell key.
+ Attribute cellKeyAttr = new Attribute("key", key);
+ cell.setAttribute(cellKeyAttr);
+
+ // we add the cell to the column.
+ column.addContent(cell);
+
+ // new value Element.
+ Element value = new Element("value");
+ value.setText(content);
+
+ // we add the value to the cell.
+ cell.addContent(value);
+ }
+ }
+ catch (NoSuchElementException e) {
+ LOG.trace(e, e);
+ }
+
+ }
+ }
+
+ /**
+ * toSreen().
+ * show the XML content on System.out
+ */
+ static void toSreen() {
+ try {
+ XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
+ output.output(document, System.out);
+ } catch (java.io.IOException e) {
+ LOG.error(e, e);
+ }
+ }
+
+ /**
+ * Method to load the jdom document of the file.
+ * @param file the file from which the database is saved.
+ */
+ private static void open(File file) throws IOException {
+ //On crée une instance de SAXBuilder
+ SAXBuilder saxBuilder = new SAXBuilder();
+ try {
+ // we construct the jdom document from the file.
+ document = saxBuilder.build(file);
+ } catch (JDOMException ex) {
+ LOG.error(ex, ex);
+ }
+ }
+}
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 17:58:02 UTC (rev 150)
+++ trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLTest.java 2010-03-01 18:25:21 UTC (rev 151)
@@ -6,8 +6,8 @@
package org.nuiton.mapstoragemanager.plugins.exporter;
//import java.io.IOException;
+import java.io.File;
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;
@@ -20,6 +20,7 @@
import org.nuiton.mapstoragemanager.plugins.NewBigTable;
import org.nuiton.mapstoragemanager.plugins.AssertJdomElement;
import org.nuiton.mapstoragemanager.plugins.bighashmapv2.BigHashMap;
+import org.nuiton.mapstoragemanager.plugins.getFile;
/**
*
@@ -40,14 +41,13 @@
/**
* Test to export a bigTable database in a xml format.
*/
- public void testToXML() {
- // init actual et expected.
+ public void testExportTo() {
createXMLDocument();
+ // init actual et expected.
Element actualRoot = actual.getRootElement();
Element expectedRoot = expected.getRootElement();
-// assertTrue(testEquals(expectedRoot, actualRoot));
AssertJdomElement.assertEquivalent(expectedRoot, actualRoot);
}
@@ -208,7 +208,10 @@
cell10.addContent(value10);
Exporter exporter = new ToXML();
- exporter.exportTo(nbt, null);
+ File exportTest =
+ getFile.getTestFile("/src/test/resources/exportTest.xml");
+ exporter.exportTo(nbt,exportTest);
+ exportTest.delete();
// expected.toString();
@@ -245,116 +248,9 @@
}
/**
- * Test if to Element are equal.
- * @param expected expected element.
- * @param actual actual element.
- * @return true if expected and actual are equivalents.
+ * A main to test quickly.
+ * @param args args.
*/
- /*public static boolean testEquals(Element expected, Element actual) {
-
- boolean trace = true;
-// String expectedContent = expected.getContent();
-// String actualContent = actual.getContent();
-
- if(trace) {
- 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)) {
- return false;
- }
-
- 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()) {
- 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()) {
- if(trace) {
- System.out.println(expectedValue);
- System.out.println(actualValue);
- }
- return expected.getValue().equals(actual.getValue());
- }
-// 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;
- 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(trace) {
- System.out.println("RETURN FALSE");
- }
- return false;
- }
- }
-
- return true;
- }*/
-
public static void main(String[] args) {
NewBigTable nbt = new BigHashMap();
nbt.createTable("table1");
@@ -380,6 +276,9 @@
nbt.put("table2", "column5", "10", "content10");
Exporter exporter = new ToXML();
- exporter.exportTo(nbt, null);
+ File exportTest =
+ getFile.getTestFile("/src/test/resources/exportTest.xml");
+ exporter.exportTo(nbt,exportTest);
+ exportTest.delete();
}
}
Added: trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/getFile.java
===================================================================
--- trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/getFile.java (rev 0)
+++ trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/getFile.java 2010-03-01 18:25:21 UTC (rev 151)
@@ -0,0 +1,52 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.nuiton.mapstoragemanager.plugins;
+
+import java.io.File;
+
+/**
+ *
+ * @author Dorian Langlais
+ */
+public class getFile {
+
+ private static String basedir;
+
+ public static File getTestFile(String path) {
+ return new File(getBasedir(), path);
+ }
+
+ public static File getTestFile(String basedir, String path) {
+ File basedirFile = new File(basedir);
+
+ if (!basedirFile.isAbsolute()) {
+ basedirFile = getTestFile(basedir);
+ }
+
+ return new File(basedirFile, path);
+ }
+
+ public static String getTestPath(String path) {
+ return getTestFile(path).getAbsolutePath();
+ }
+
+ public static String getTestPath(String basedir, String path) {
+ return getTestFile(basedir, path).getAbsolutePath();
+ }
+
+ public static String getBasedir() {
+ if (basedir != null) {
+ return basedir;
+ }
+
+ basedir = System.getProperty("basedir");
+
+ if (basedir == null) {
+ basedir = new File("").getAbsolutePath();
+ }
+
+ return basedir;
+ }
+}
Added: trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/importer/FromXMLTest.java
===================================================================
--- trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/importer/FromXMLTest.java (rev 0)
+++ trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/importer/FromXMLTest.java 2010-03-01 18:25:21 UTC (rev 151)
@@ -0,0 +1,111 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.nuiton.mapstoragemanager.plugins.importer;
+
+import java.io.File;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import junit.framework.TestCase;
+import org.nuiton.mapstoragemanager.plugins.Importer;
+import org.nuiton.mapstoragemanager.plugins.NewBigTable;
+import org.nuiton.mapstoragemanager.plugins.bighashmapv2.BigHashMap;
+import org.nuiton.mapstoragemanager.plugins.getFile;
+
+/**
+ *
+ * @author Dorian Langlais
+ */
+public class FromXMLTest extends TestCase {
+
+// public void testImportFrom() {
+// Assert.fail();
+// }
+
+ /**
+ * Import the database from the file fiveTables and verify the content.
+ */
+ public void testImportFromFiveTables() {
+
+ File fiveTables =
+ getFile.getTestFile("/src/test/resources/fiveTables.xml");
+
+ // import fiveTables.
+ Importer importer = new FromXML();
+ NewBigTable bigTable = new BigHashMap();
+
+ importer.importFrom(bigTable, fiveTables);
+
+ Set<String> tableNamesExpected;
+ {
+ /**
+ * Init tableNames.
+ */
+ tableNamesExpected = new HashSet<String>();
+ tableNamesExpected.add("table1");
+ tableNamesExpected.add("table2");
+ tableNamesExpected.add("table3");
+ tableNamesExpected.add("table4");
+ tableNamesExpected.add("table5");
+ }
+ Set<String> tableNamesActual = bigTable.getTablesNames();
+ assertEquals(tableNamesExpected, tableNamesActual);
+
+
+ for (String tableName : tableNamesActual) {
+ Set<String> columnNamesExpected = new HashSet<String>();
+ assertEquals(columnNamesExpected,
+ bigTable.getColumnsNames(tableName));
+ }
+ }
+
+ public void testImportFromTwoTablesFiveColumns() {
+
+ File twoTablesFiveColumns =
+ getFile.getTestFile("/src/test/resources/twoTablesFiveColumns.xml");
+
+ // import fiveTables.
+ Importer importer = new FromXML();
+ NewBigTable bigTable = new BigHashMap();
+
+ importer.importFrom(bigTable, twoTablesFiveColumns);
+
+ Set<String> tableNamesExpected;
+ {
+ /**
+ * Init tableNames.
+ */
+ tableNamesExpected = new HashSet<String>();
+ tableNamesExpected.add("table1");
+ tableNamesExpected.add("table2");
+ }
+ Set<String> tableNamesActual = bigTable.getTablesNames();
+ assertEquals(tableNamesExpected, tableNamesActual);
+
+ Map<String, Set<String>> columnNamesExpected;
+ {
+ /**
+ * Init columnNames.
+ */
+ columnNamesExpected = new HashMap<String, Set<String>>();
+ Set<String> table1ColumnNames = new HashSet<String>();
+ table1ColumnNames.add("column1");
+ table1ColumnNames.add("column2");
+ Set<String> table2ColumnNames = new HashSet<String>();
+ table2ColumnNames.add("column3");
+ table2ColumnNames.add("column4");
+ table2ColumnNames.add("column5");
+ columnNamesExpected.put("table1", table1ColumnNames);
+ columnNamesExpected.put("table2", table2ColumnNames);
+ }
+
+ Set<String> columnNamesActual;
+ for (String tableName : tableNamesActual) {
+ columnNamesActual = bigTable.getColumnsNames(tableName);
+ assertEquals(columnNamesExpected.get(tableName), columnNamesActual);
+ }
+ }
+}
Added: trunk/msm-fromtoXML/src/test/resources/fiveTables.xml
===================================================================
--- trunk/msm-fromtoXML/src/test/resources/fiveTables.xml (rev 0)
+++ trunk/msm-fromtoXML/src/test/resources/fiveTables.xml 2010-03-01 18:25:21 UTC (rev 151)
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<database>
+ <table tableName="table1" />
+ <table tableName="table2" />
+ <table tableName="table3" />
+ <table tableName="table4" />
+ <table tableName="table5" />
+</database>
+
Added: trunk/msm-fromtoXML/src/test/resources/twoTablesFiveColumns.xml
===================================================================
--- trunk/msm-fromtoXML/src/test/resources/twoTablesFiveColumns.xml (rev 0)
+++ trunk/msm-fromtoXML/src/test/resources/twoTablesFiveColumns.xml 2010-03-01 18:25:21 UTC (rev 151)
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<database>
+ <table tableName="table1">
+ <column columnName="column1" />
+ <column columnName="column2" />
+ </table>
+ <table tableName="table2">
+ <column columnName="column3" />
+ <column columnName="column4" />
+ <column columnName="column5" />
+ </table>
+</database>
+
1
0