XmlImporter and XmlImporterUtilities have further unit tests. 1 test (for Issue 61) still broken.
git-svn-id: http://google-refine.googlecode.com/svn/trunk@902 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
parent
c21c472434
commit
ecca18f5ae
@ -72,8 +72,6 @@ public class XmlImportUtilities {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static public String[] detectPathFromTag(InputStream inputStream, String tag) {
|
static public String[] detectPathFromTag(InputStream inputStream, String tag) {
|
||||||
//List<RecordElementCandidate> candidates = new ArrayList<RecordElementCandidate>();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
|
XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
|
||||||
|
|
||||||
@ -99,6 +97,8 @@ public class XmlImportUtilities {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static protected List<String> detectRecordElement(XMLStreamReader parser, String tag) throws XMLStreamException {
|
static protected List<String> detectRecordElement(XMLStreamReader parser, String tag) throws XMLStreamException {
|
||||||
|
if(parser.getEventType() == XMLStreamConstants.START_DOCUMENT)
|
||||||
|
parser.next();
|
||||||
String localName = parser.getLocalName();
|
String localName = parser.getLocalName();
|
||||||
String fullName = composeName(parser.getPrefix(), localName);
|
String fullName = composeName(parser.getPrefix(), localName);
|
||||||
if (tag.equals(parser.getLocalName()) || tag.equals(fullName)) {
|
if (tag.equals(parser.getLocalName()) || tag.equals(fullName)) {
|
||||||
@ -327,6 +327,10 @@ public class XmlImportUtilities {
|
|||||||
int pathIndex,
|
int pathIndex,
|
||||||
ImportColumnGroup rootColumnGroup
|
ImportColumnGroup rootColumnGroup
|
||||||
) throws XMLStreamException {
|
) throws XMLStreamException {
|
||||||
|
if(parser.getEventType() == XMLStreamConstants.START_DOCUMENT){
|
||||||
|
logger.warn("Cannot use findRecord method for START_DOCUMENT event");
|
||||||
|
return;
|
||||||
|
}
|
||||||
String tagName = parser.getLocalName();
|
String tagName = parser.getLocalName();
|
||||||
if (tagName.equals(recordPath[pathIndex])) {
|
if (tagName.equals(recordPath[pathIndex])) {
|
||||||
if (pathIndex < recordPath.length - 1) {
|
if (pathIndex < recordPath.length - 1) {
|
||||||
@ -466,7 +470,7 @@ public class XmlImportUtilities {
|
|||||||
ImportRecord record,
|
ImportRecord record,
|
||||||
String columnLocalName,
|
String columnLocalName,
|
||||||
String text,
|
String text,
|
||||||
int commonStaringRowIndex
|
int commonStartingRowIndex
|
||||||
) {
|
) {
|
||||||
if (text == null || ((String) text).isEmpty()) {
|
if (text == null || ((String) text).isEmpty()) {
|
||||||
return;
|
return;
|
||||||
@ -478,7 +482,7 @@ public class XmlImportUtilities {
|
|||||||
int cellIndex = column.cellIndex;
|
int cellIndex = column.cellIndex;
|
||||||
|
|
||||||
while (cellIndex >= record.columnEmptyRowIndices.size()) {
|
while (cellIndex >= record.columnEmptyRowIndices.size()) {
|
||||||
record.columnEmptyRowIndices.add(commonStaringRowIndex);
|
record.columnEmptyRowIndices.add(commonStartingRowIndex);
|
||||||
}
|
}
|
||||||
int rowIndex = record.columnEmptyRowIndices.get(cellIndex);
|
int rowIndex = record.columnEmptyRowIndices.get(cellIndex);
|
||||||
|
|
||||||
@ -491,7 +495,9 @@ public class XmlImportUtilities {
|
|||||||
row.add(null);
|
row.add(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
row.set(cellIndex, new Cell(value, null));
|
logger.trace("Adding cell with value : " + value + " to row : " + rowIndex + " at cell index : " + (cellIndex-1));
|
||||||
|
|
||||||
|
row.set(cellIndex-1, new Cell(value, null));
|
||||||
|
|
||||||
record.columnEmptyRowIndices.set(cellIndex, rowIndex + 1);
|
record.columnEmptyRowIndices.set(cellIndex, rowIndex + 1);
|
||||||
|
|
||||||
|
@ -57,6 +57,9 @@ public class XmlImporter implements Importer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(recordPath == null)
|
||||||
|
return;
|
||||||
|
|
||||||
ImportColumnGroup rootColumnGroup = new ImportColumnGroup();
|
ImportColumnGroup rootColumnGroup = new ImportColumnGroup();
|
||||||
|
|
||||||
XmlImportUtilities.importXml(pis, project, recordPath, rootColumnGroup);
|
XmlImportUtilities.importXml(pis, project, recordPath, rootColumnGroup);
|
||||||
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.metaweb.gridworks.tests.importers;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.testng.Assert;
|
||||||
|
|
||||||
|
import com.metaweb.gridworks.model.Cell;
|
||||||
|
import com.metaweb.gridworks.model.Column;
|
||||||
|
import com.metaweb.gridworks.model.Project;
|
||||||
|
import com.metaweb.gridworks.model.Row;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper methods for Importer testing
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class TestTools {
|
||||||
|
final static Logger logger = LoggerFactory.getLogger("Common");
|
||||||
|
|
||||||
|
public static void AssertGridCreated(Project project, int numCols, int numRows){
|
||||||
|
Assert.assertNotNull(project);
|
||||||
|
Assert.assertNotNull(project.columnModel);
|
||||||
|
Assert.assertNotNull(project.columnModel.columns);
|
||||||
|
Assert.assertEquals(project.columnModel.columns.size(), numCols);
|
||||||
|
Assert.assertNotNull(project.rows);
|
||||||
|
Assert.assertEquals(project.rows.size(), numRows);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void PrintProject(Project project){
|
||||||
|
//some quick and dirty debugging
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for(Column c : project.columnModel.columns){
|
||||||
|
sb.append(c.getName());
|
||||||
|
sb.append("; ");
|
||||||
|
}
|
||||||
|
logger.info(sb.toString());
|
||||||
|
for(Row r : project.rows){
|
||||||
|
sb = new StringBuilder();
|
||||||
|
for(int i = 0; i < r.cells.size(); i++){
|
||||||
|
Cell c = r.getCell(i);
|
||||||
|
if(c != null){
|
||||||
|
sb.append(c.value);
|
||||||
|
sb.append("; ");
|
||||||
|
}else{
|
||||||
|
sb.append("null; ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.info(sb.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.metaweb.gridworks.tests.importers;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.xml.stream.XMLStreamException;
|
||||||
|
import javax.xml.stream.XMLStreamReader;
|
||||||
|
|
||||||
|
import com.metaweb.gridworks.importers.XmlImportUtilities;
|
||||||
|
import com.metaweb.gridworks.model.Project;
|
||||||
|
|
||||||
|
|
||||||
|
public class XmlImportUtilitiesStub extends XmlImportUtilities{
|
||||||
|
public List<String> detectRecordElementWrapper(XMLStreamReader parser, String tag) throws XMLStreamException{
|
||||||
|
return super.detectRecordElement(parser, tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ProcessSubRecordWrapper(Project project, XMLStreamReader parser, ImportColumnGroup columnGroup, ImportRecord record) throws XMLStreamException{
|
||||||
|
super.processSubRecord(project, parser, columnGroup, record);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void findRecordWrapper(Project project, XMLStreamReader parser, String[] recordPath, int pathIndex, ImportColumnGroup rootColumnGroup) throws XMLStreamException{
|
||||||
|
super.findRecord(project, parser, recordPath, pathIndex, rootColumnGroup);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void processRecordWrapper(Project project, XMLStreamReader parser, ImportColumnGroup rootColumnGroup) throws XMLStreamException{
|
||||||
|
super.processRecord(project, parser, rootColumnGroup);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addCellWrapper(Project project, ImportColumnGroup columnGroup, ImportRecord record, String columnLocalName, String text, int commonStartingRowIndex){
|
||||||
|
super.addCell(project, columnGroup, record, columnLocalName, text, commonStartingRowIndex);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,336 @@
|
|||||||
|
package com.metaweb.gridworks.tests.importers;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.xml.stream.FactoryConfigurationError;
|
||||||
|
import javax.xml.stream.XMLInputFactory;
|
||||||
|
import javax.xml.stream.XMLStreamException;
|
||||||
|
import javax.xml.stream.XMLStreamReader;
|
||||||
|
|
||||||
|
import org.apache.log4j.Level;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.testng.Assert;
|
||||||
|
import org.testng.annotations.AfterMethod;
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import com.metaweb.gridworks.importers.XmlImportUtilities.ImportColumn;
|
||||||
|
import com.metaweb.gridworks.importers.XmlImportUtilities.ImportColumnGroup;
|
||||||
|
import com.metaweb.gridworks.importers.XmlImportUtilities.ImportRecord;
|
||||||
|
import com.metaweb.gridworks.model.Project;
|
||||||
|
import com.metaweb.gridworks.model.Row;
|
||||||
|
|
||||||
|
|
||||||
|
public class XmlImportUtilitiesTests {
|
||||||
|
final static Logger logger = LoggerFactory.getLogger("XmlImporterUtilitiesTests");
|
||||||
|
|
||||||
|
//dependencies
|
||||||
|
Project project;
|
||||||
|
XMLStreamReader parser;
|
||||||
|
ImportColumnGroup columnGroup;
|
||||||
|
ImportRecord record;
|
||||||
|
ByteArrayInputStream inputStream;
|
||||||
|
|
||||||
|
//System Under Test
|
||||||
|
XmlImportUtilitiesStub SUT;
|
||||||
|
|
||||||
|
@BeforeMethod
|
||||||
|
public void SetUp(){
|
||||||
|
org.apache.log4j.Logger.getRootLogger().setLevel(Level.toLevel("trace"));
|
||||||
|
SUT = new XmlImportUtilitiesStub();
|
||||||
|
project = new Project();
|
||||||
|
columnGroup = new ImportColumnGroup();
|
||||||
|
record = new ImportRecord();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterMethod
|
||||||
|
public void TearDown() throws IOException{
|
||||||
|
SUT = null;
|
||||||
|
project = null;
|
||||||
|
parser = null;
|
||||||
|
columnGroup = null;
|
||||||
|
record = null;
|
||||||
|
if(inputStream != null)
|
||||||
|
inputStream.close();
|
||||||
|
inputStream = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void detectPathFromTagTest(){
|
||||||
|
loadXml("<?xml version=\"1.0\"?><library><book id=\"1\"><author>author1</author><genre>genre1</genre></book></library>");
|
||||||
|
String tag = "library";
|
||||||
|
|
||||||
|
String[] response = XmlImportUtilitiesStub.detectPathFromTag(inputStream, tag);
|
||||||
|
Assert.assertNotNull(response);
|
||||||
|
Assert.assertEquals(response.length, 1);
|
||||||
|
Assert.assertEquals(response[0], "library");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void detectPathFromTagWithNestedElement(){
|
||||||
|
loadXml("<?xml version=\"1.0\"?><library><book id=\"1\"><author>author1</author><genre>genre1</genre></book></library>");
|
||||||
|
String tag = "book";
|
||||||
|
String[] response = XmlImportUtilitiesStub.detectPathFromTag(inputStream, tag);
|
||||||
|
Assert.assertNotNull(response);
|
||||||
|
Assert.assertEquals(response.length, 2);
|
||||||
|
Assert.assertEquals(response[0], "library");
|
||||||
|
Assert.assertEquals(response[1], "book");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void detectRecordElementTest(){
|
||||||
|
loadXml("<?xml version=\"1.0\"?><library><book id=\"1\"><author>author1</author><genre>genre1</genre></book></library>");
|
||||||
|
createParser();
|
||||||
|
String tag="library";
|
||||||
|
|
||||||
|
List<String> response = new ArrayList<String>();
|
||||||
|
try {
|
||||||
|
response = SUT.detectRecordElementWrapper(parser, tag);
|
||||||
|
} catch (XMLStreamException e) {
|
||||||
|
Assert.fail();
|
||||||
|
}
|
||||||
|
Assert.assertNotNull(response);
|
||||||
|
Assert.assertEquals(response.size(), 1);
|
||||||
|
Assert.assertEquals(response.get(0), "library");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void detectRecordElementCanHandleWithNestedElements(){
|
||||||
|
loadXml("<?xml version=\"1.0\"?><library><book id=\"1\"><author>author1</author><genre>genre1</genre></book></library>");
|
||||||
|
createParser();
|
||||||
|
String tag="book";
|
||||||
|
|
||||||
|
List<String> response = new ArrayList<String>();
|
||||||
|
try {
|
||||||
|
response = SUT.detectRecordElementWrapper(parser, tag);
|
||||||
|
} catch (XMLStreamException e) {
|
||||||
|
Assert.fail();
|
||||||
|
}
|
||||||
|
Assert.assertNotNull(response);
|
||||||
|
Assert.assertEquals(response.size(), 2);
|
||||||
|
Assert.assertEquals(response.get(0), "library");
|
||||||
|
Assert.assertEquals(response.get(1), "book");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void detectRecordElementIsNullForUnfoundTag(){
|
||||||
|
loadXml("<?xml version=\"1.0\"?><library><book id=\"1\"><author>author1</author><genre>genre1</genre></book></library>");
|
||||||
|
createParser();
|
||||||
|
String tag="";
|
||||||
|
|
||||||
|
List<String> response = new ArrayList<String>();
|
||||||
|
try {
|
||||||
|
response = SUT.detectRecordElementWrapper(parser, tag);
|
||||||
|
} catch (XMLStreamException e) {
|
||||||
|
Assert.fail();
|
||||||
|
}
|
||||||
|
Assert.assertNull(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void detectRecordElementRegressionTest(){
|
||||||
|
loadSampleXml();
|
||||||
|
|
||||||
|
String[] path = XmlImportUtilitiesStub.detectRecordElement(inputStream);
|
||||||
|
Assert.assertNotNull(path);
|
||||||
|
Assert.assertEquals(path.length, 2);
|
||||||
|
Assert.assertEquals(path[0], "library");
|
||||||
|
Assert.assertEquals(path[1], "book");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void importXmlTest(){
|
||||||
|
loadSampleXml();
|
||||||
|
|
||||||
|
String[] recordPath = new String[]{"library","book"};
|
||||||
|
XmlImportUtilitiesStub.importXml(inputStream, project, recordPath, columnGroup );
|
||||||
|
|
||||||
|
TestTools.PrintProject(project);
|
||||||
|
TestTools.AssertGridCreated(project, 0, 6);
|
||||||
|
Assert.assertEquals(project.rows.get(0).cells.size(), 4);
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void createColumnsFromImportTest(){
|
||||||
|
|
||||||
|
ImportColumnGroup columnGroup = new ImportColumnGroup();
|
||||||
|
ImportColumn ic1 = new ImportColumn();
|
||||||
|
ic1.name = "hello";
|
||||||
|
ImportColumn ic2 = new ImportColumn();
|
||||||
|
ic2.name = "world";
|
||||||
|
ImportColumnGroup subGroup = new ImportColumnGroup();
|
||||||
|
ImportColumn ic3 = new ImportColumn();
|
||||||
|
ic3.name = "foo";
|
||||||
|
ImportColumn ic4 = new ImportColumn();
|
||||||
|
ic4.name = "bar";
|
||||||
|
subGroup.columns.put("c", ic3);
|
||||||
|
subGroup.columns.put("d", ic4);
|
||||||
|
columnGroup.columns.put("a", ic1);
|
||||||
|
columnGroup.columns.put("b", ic2);
|
||||||
|
columnGroup.subgroups.put("e", subGroup);
|
||||||
|
XmlImportUtilitiesStub.createColumnsFromImport(project, columnGroup);
|
||||||
|
TestTools.PrintProject(project);
|
||||||
|
TestTools.AssertGridCreated(project, 4, 0);
|
||||||
|
Assert.assertEquals(project.columnModel.columns.get(0).getName(), "world");
|
||||||
|
Assert.assertEquals(project.columnModel.columns.get(1).getName(), "hello");
|
||||||
|
Assert.assertEquals(project.columnModel.columns.get(2).getName(), "bar");
|
||||||
|
Assert.assertEquals(project.columnModel.columns.get(3).getName(), "foo");
|
||||||
|
Assert.assertEquals(project.columnModel.columnGroups.get(0).keyColumnIndex, 2);
|
||||||
|
Assert.assertEquals(project.columnModel.columnGroups.get(0).startColumnIndex, 2);
|
||||||
|
Assert.assertEquals(project.columnModel.columnGroups.get(0).columnSpan, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void findRecordTest(){
|
||||||
|
loadSampleXml();
|
||||||
|
createParser();
|
||||||
|
ParserSkip();
|
||||||
|
|
||||||
|
String[] recordPath = new String[]{"library","book"};
|
||||||
|
int pathIndex = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
SUT.findRecordWrapper(project, parser, recordPath, pathIndex, columnGroup);
|
||||||
|
} catch (XMLStreamException e) {
|
||||||
|
Assert.fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
TestTools.PrintProject(project);
|
||||||
|
TestTools.AssertGridCreated(project, 0, 6);
|
||||||
|
Assert.assertEquals(project.rows.get(0).cells.size(), 4);
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void processRecordTest(){
|
||||||
|
loadXml("<?xml version=\"1.0\"?><library><book id=\"1\"><author>author1</author><genre>genre1</genre></book></library>");
|
||||||
|
createParser();
|
||||||
|
ParserSkip();
|
||||||
|
|
||||||
|
try {
|
||||||
|
SUT.processRecordWrapper(project, parser, columnGroup);
|
||||||
|
} catch (XMLStreamException e) {
|
||||||
|
Assert.fail();
|
||||||
|
}
|
||||||
|
TestTools.PrintProject(project);
|
||||||
|
Assert.assertNotNull(project.rows);
|
||||||
|
Assert.assertEquals(project.rows.size(), 1);
|
||||||
|
Row row = project.rows.get(0);
|
||||||
|
Assert.assertNotNull(row);
|
||||||
|
Assert.assertNotNull(row.getCell(1));
|
||||||
|
Assert.assertEquals(row.getCell(1).value, "author1");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void processRecordTestDuplicateColumns(){
|
||||||
|
loadXml("<?xml version=\"1.0\"?><library><book id=\"1\"><author>author1</author><author>author2</author><genre>genre1</genre></book></library>");
|
||||||
|
createParser();
|
||||||
|
ParserSkip();
|
||||||
|
|
||||||
|
try {
|
||||||
|
SUT.processRecordWrapper(project, parser, columnGroup);
|
||||||
|
} catch (XMLStreamException e) {
|
||||||
|
Assert.fail();
|
||||||
|
}
|
||||||
|
TestTools.PrintProject(project);
|
||||||
|
Assert.assertNotNull(project.rows);
|
||||||
|
Assert.assertEquals(project.rows.size(), 2);
|
||||||
|
Row row = project.rows.get(0);
|
||||||
|
Assert.assertNotNull(row);
|
||||||
|
Assert.assertEquals(row.cells.size(), 3);
|
||||||
|
Assert.assertNotNull(row.getCell(1));
|
||||||
|
Assert.assertEquals(row.getCell(1).value, "author1");
|
||||||
|
row = project.rows.get(1);
|
||||||
|
Assert.assertEquals(row.getCell(1).value, "author2");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void processRecordTestNestedElement(){
|
||||||
|
loadXml("<?xml version=\"1.0\"?><library><book id=\"1\"><author><author-name>author1</author-name><author-dob>a date</author-dob></author><genre>genre1</genre></book></library>");
|
||||||
|
createParser();
|
||||||
|
ParserSkip();
|
||||||
|
|
||||||
|
try {
|
||||||
|
SUT.processRecordWrapper(project, parser, columnGroup);
|
||||||
|
} catch (XMLStreamException e) {
|
||||||
|
Assert.fail();
|
||||||
|
}
|
||||||
|
TestTools.PrintProject(project);
|
||||||
|
Assert.assertNotNull(project.rows);
|
||||||
|
Assert.assertEquals(project.rows.size(), 1);
|
||||||
|
Row row = project.rows.get(0);
|
||||||
|
Assert.assertNotNull(row);
|
||||||
|
Assert.assertEquals(row.cells.size(), 4);
|
||||||
|
Assert.assertNotNull(row.getCell(1));
|
||||||
|
Assert.assertEquals(row.getCell(1).value, "author1");
|
||||||
|
Assert.assertNotNull(row.getCell(2));
|
||||||
|
Assert.assertEquals(row.getCell(2).value, "a date");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test(groups={"broken"})
|
||||||
|
public void processSubRecordTest(){
|
||||||
|
loadXml("<?xml version=\"1.0\"?><library><book id=\"1\"><author>author1</author><genre>genre1</genre></book></library>");
|
||||||
|
createParser();
|
||||||
|
ParserSkip();
|
||||||
|
|
||||||
|
try {
|
||||||
|
SUT.ProcessSubRecordWrapper(project, parser, columnGroup, record);
|
||||||
|
} catch (XMLStreamException e) {
|
||||||
|
Assert.fail();
|
||||||
|
}
|
||||||
|
TestTools.PrintProject(project);
|
||||||
|
Assert.fail();
|
||||||
|
//TODO need to verify 'record' was set correctly which we can't do as ImportRecord is an internal class
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(groups={"broken"})
|
||||||
|
public void addCellTest(){
|
||||||
|
String columnLocalName = "author";
|
||||||
|
String text = "Author1, The";
|
||||||
|
int commonStartingRowIndex = 0;
|
||||||
|
project.rows.add(new Row(0));
|
||||||
|
SUT.addCellWrapper(project, columnGroup, record, columnLocalName, text, commonStartingRowIndex);
|
||||||
|
|
||||||
|
Assert.fail();
|
||||||
|
//TODO need to verify 'record' was set correctly which we can't do as ImportRecord is an internal class
|
||||||
|
}
|
||||||
|
|
||||||
|
//----------------helpers-------------
|
||||||
|
public void loadSampleXml(){
|
||||||
|
loadXml( XmlImporterTests.getSample() );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loadXml(String xml){
|
||||||
|
try {
|
||||||
|
inputStream = new ByteArrayInputStream( xml.getBytes( "UTF-8" ) );
|
||||||
|
} catch (UnsupportedEncodingException e1) {
|
||||||
|
Assert.fail();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ParserSkip(){
|
||||||
|
try {
|
||||||
|
parser.next(); //move parser forward once e.g. skip the START_DOCUMENT parser event
|
||||||
|
} catch (XMLStreamException e1) {
|
||||||
|
Assert.fail();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void createParser(){
|
||||||
|
try {
|
||||||
|
parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
|
||||||
|
} catch (XMLStreamException e1) {
|
||||||
|
Assert.fail();
|
||||||
|
} catch (FactoryConfigurationError e1) {
|
||||||
|
Assert.fail();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,207 +1,225 @@
|
|||||||
package com.metaweb.gridworks.tests.importers;
|
package com.metaweb.gridworks.tests.importers;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.testng.Assert;
|
import org.testng.Assert;
|
||||||
import org.testng.annotations.AfterMethod;
|
import org.testng.annotations.AfterMethod;
|
||||||
import org.testng.annotations.BeforeMethod;
|
import org.testng.annotations.BeforeMethod;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import com.metaweb.gridworks.importers.XmlImporter;
|
import com.metaweb.gridworks.importers.XmlImporter;
|
||||||
import com.metaweb.gridworks.model.Cell;
|
import com.metaweb.gridworks.model.Project;
|
||||||
import com.metaweb.gridworks.model.Column;
|
import com.metaweb.gridworks.model.Row;
|
||||||
import com.metaweb.gridworks.model.Project;
|
|
||||||
import com.metaweb.gridworks.model.Row;
|
|
||||||
|
public class XmlImporterTests {
|
||||||
|
final static Logger logger = LoggerFactory.getLogger("XmlImporterTests");
|
||||||
public class XmlImporterTests {
|
|
||||||
final static Logger logger = LoggerFactory.getLogger("XmlImporterTests");
|
//dependencies
|
||||||
|
Project project = null;
|
||||||
//dependencies
|
Properties options = null;
|
||||||
Project project = null;
|
ByteArrayInputStream inputStream = null;
|
||||||
Properties options = null;
|
|
||||||
ByteArrayInputStream inputStream = null;
|
//System Under Test
|
||||||
|
XmlImporter SUT = null;
|
||||||
//System Under Test
|
|
||||||
XmlImporter SUT = null;
|
|
||||||
|
@BeforeMethod
|
||||||
|
public void SetUp(){
|
||||||
@BeforeMethod
|
SUT = new XmlImporter();
|
||||||
public void SetUp(){
|
project = new Project();
|
||||||
SUT = new XmlImporter();
|
options = mock(Properties.class);
|
||||||
project = new Project();
|
}
|
||||||
options = mock(Properties.class);
|
|
||||||
}
|
@AfterMethod
|
||||||
|
public void TearDown() throws IOException{
|
||||||
@AfterMethod
|
SUT = null;
|
||||||
public void TearDown(){
|
project = null;
|
||||||
SUT = null;
|
options = null;
|
||||||
project = null;
|
inputStream.close();
|
||||||
options = null;
|
inputStream = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canParseSample(){
|
public void canParseSample(){
|
||||||
|
RunTest(getSample());
|
||||||
RunTest(getSample());
|
|
||||||
|
TestTools.AssertGridCreated(project, 4, 6);
|
||||||
AssertGridCreate(project, 4, 6);
|
TestTools.PrintProject(project);
|
||||||
PrintProject(project);
|
|
||||||
|
Row row = project.rows.get(0);
|
||||||
Row row = project.rows.get(0);
|
Assert.assertNotNull(row);
|
||||||
Assert.assertNotNull(row);
|
Assert.assertNotNull(row.getCell(1));
|
||||||
Assert.assertNotNull(row.cells);
|
Assert.assertEquals(row.getCell(1).value, "Author 1, The");
|
||||||
Assert.assertNotNull(row.cells.get(2));
|
}
|
||||||
Assert.assertEquals(row.cells.get(2).value, "Author 1, The");
|
|
||||||
|
@Test
|
||||||
|
public void canParseSampleWithDuplicateNestedElements(){
|
||||||
}
|
RunTest(getSampleWithDuplicateNestedElements());
|
||||||
|
|
||||||
@Test
|
TestTools.PrintProject(project);
|
||||||
public void testCanParseLineBreak(){
|
TestTools.AssertGridCreated(project, 4, 12);
|
||||||
|
|
||||||
RunTest(getSampleWithLineBreak());
|
Row row = project.rows.get(0);
|
||||||
|
Assert.assertNotNull(row);
|
||||||
AssertGridCreate(project, 4, 6);
|
Assert.assertEquals(row.cells.size(), 4);
|
||||||
PrintProject(project);
|
Assert.assertNotNull(row.getCell(2));
|
||||||
|
Assert.assertEquals(row.getCell(1).value, "Author 1, The");
|
||||||
Row row = project.rows.get(3);
|
Assert.assertEquals(project.rows.get(1).getCell(1).value, "Author 1, Another");
|
||||||
Assert.assertNotNull(row);
|
}
|
||||||
Assert.assertNotNull(row.cells);
|
|
||||||
Assert.assertNotNull(row.cells.get(2));
|
@Test
|
||||||
Assert.assertEquals(row.cells.get(2).value, "With line\n break");
|
public void testCanParseLineBreak(){
|
||||||
}
|
|
||||||
|
RunTest(getSampleWithLineBreak());
|
||||||
@Test(groups={"broken"})
|
|
||||||
public void testElementsWithVaryingStructure(){
|
TestTools.AssertGridCreated(project, 4, 6);
|
||||||
|
TestTools.PrintProject(project);
|
||||||
|
|
||||||
RunTest(getSampleWithVaryingStructure());
|
Row row = project.rows.get(3);
|
||||||
|
Assert.assertNotNull(row);
|
||||||
AssertGridCreate(project, 5, 6);
|
Assert.assertEquals(row.cells.size(), 4);
|
||||||
PrintProject(project);
|
Assert.assertNotNull(row.getCell(1));
|
||||||
|
Assert.assertEquals(row.getCell(1).value, "With line\n break");
|
||||||
Row row0 = project.rows.get(0);
|
}
|
||||||
Assert.assertNotNull(row0);
|
|
||||||
Assert.assertNotNull(row0.cells);
|
@Test(groups={"broken"})
|
||||||
Assert.assertEquals(row0.cells.size(),6);
|
public void testElementsWithVaryingStructure(){
|
||||||
|
RunTest(getSampleWithVaryingStructure());
|
||||||
Row row5 = project.rows.get(5);
|
|
||||||
Assert.assertNotNull(row5);
|
TestTools.AssertGridCreated(project, 5, 6);
|
||||||
Assert.assertNotNull(row5.cells);
|
TestTools.PrintProject(project);
|
||||||
Assert.assertEquals(row5.cells.size(),6);
|
|
||||||
|
Row row0 = project.rows.get(0);
|
||||||
|
Assert.assertNotNull(row0);
|
||||||
}
|
Assert.assertEquals(row0.cells.size(),6);
|
||||||
|
|
||||||
//------------helper methods---------------
|
Row row5 = project.rows.get(5);
|
||||||
|
Assert.assertNotNull(row5);
|
||||||
protected String getTypicalElement(int id){
|
Assert.assertEquals(row5.cells.size(),6);
|
||||||
return "<book id=\"" + id + "\">" +
|
}
|
||||||
"<author>Author " + id + ", The</author>" +
|
|
||||||
"<title>Book title " + id + "</title>" +
|
@Test
|
||||||
"<publish_date>2010-05-26</publish_date>" +
|
public void testElementWithNestedTree(){
|
||||||
"</book>";
|
RunTest(getSampleWithTreeStructure());
|
||||||
}
|
TestTools.AssertGridCreated(project, 5, 6);
|
||||||
|
TestTools.PrintProject(project);
|
||||||
protected String getSample(){
|
Assert.assertEquals(project.columnModel.columnGroups.size(),1);
|
||||||
StringBuilder sb = new StringBuilder();
|
Assert.assertEquals(project.columnModel.columnGroups.get(0).keyColumnIndex, 2);
|
||||||
sb.append("<?xml version=\"1.0\"?><library>");
|
Assert.assertEquals(project.columnModel.columnGroups.get(0).startColumnIndex, 2);
|
||||||
for(int i = 1; i < 7; i++){
|
Assert.assertNull(project.columnModel.columnGroups.get(0).parentGroup);
|
||||||
sb.append(getTypicalElement(i));
|
Assert.assertEquals(project.columnModel.columnGroups.get(0).subgroups.size(),0);
|
||||||
}
|
Assert.assertEquals(project.columnModel.columnGroups.get(0).columnSpan,2);
|
||||||
sb.append("</library>");
|
}
|
||||||
return sb.toString();
|
|
||||||
}
|
//------------helper methods---------------
|
||||||
|
|
||||||
protected String getSampleWithLineBreak(){
|
public static String getTypicalElement(int id){
|
||||||
StringBuilder sb = new StringBuilder();
|
return "<book id=\"" + id + "\">" +
|
||||||
sb.append("<?xml version=\"1.0\"?><library>");
|
"<author>Author " + id + ", The</author>" +
|
||||||
for(int i = 1; i < 4; i++){
|
"<title>Book title " + id + "</title>" +
|
||||||
sb.append(getTypicalElement(i));
|
"<publish_date>2010-05-26</publish_date>" +
|
||||||
}
|
"</book>";
|
||||||
sb.append("<book id=\"4\">" +
|
}
|
||||||
"<author>With line\n break</author>" +
|
|
||||||
"<title>Book title 4</title>" +
|
public static String getElementWithDuplicateSubElement(int id){
|
||||||
"<publish_date>2010-05-26</publish_date>" +
|
return "<book id=\"" + id + "\">" +
|
||||||
"</book>");
|
"<author>Author " + id + ", The</author>" +
|
||||||
sb.append(getTypicalElement(5));
|
"<author>Author " + id + ", Another</author>" +
|
||||||
sb.append(getTypicalElement(6));
|
"<title>Book title " + id + "</title>" +
|
||||||
sb.append("</library>");
|
"<publish_date>2010-05-26</publish_date>" +
|
||||||
return sb.toString();
|
"</book>";
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String getSampleWithVaryingStructure(){
|
public static String getSample(){
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("<?xml version=\"1.0\"?><library>");
|
sb.append("<?xml version=\"1.0\"?><library>");
|
||||||
for(int i = 1; i < 6; i++){
|
for(int i = 1; i < 7; i++){
|
||||||
sb.append(getTypicalElement(i));
|
sb.append(getTypicalElement(i));
|
||||||
}
|
}
|
||||||
sb.append("<book id=\"6\">" +
|
sb.append("</library>");
|
||||||
"<author>With line\n break</author>" +
|
return sb.toString();
|
||||||
"<title>Book title 6</title>" +
|
}
|
||||||
"<genre>New element not seen in other records</genre>" +
|
|
||||||
"<publish_date>2010-05-26</publish_date>" +
|
public static String getSampleWithDuplicateNestedElements(){
|
||||||
"</book>");
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("</library>");
|
sb.append("<?xml version=\"1.0\"?><library>");
|
||||||
return sb.toString();
|
for(int i = 1; i < 7; i++){
|
||||||
}
|
sb.append(getElementWithDuplicateSubElement(i));
|
||||||
|
}
|
||||||
private void RunTest(String testString){
|
sb.append("</library>");
|
||||||
try {
|
return sb.toString();
|
||||||
inputStream = new ByteArrayInputStream( testString.getBytes( "UTF-8" ) );
|
|
||||||
} catch (UnsupportedEncodingException e1) {
|
}
|
||||||
Assert.fail();
|
|
||||||
}
|
public static String getSampleWithLineBreak(){
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
try {
|
sb.append("<?xml version=\"1.0\"?><library>");
|
||||||
SUT.read(inputStream, project, options);
|
for(int i = 1; i < 4; i++){
|
||||||
} catch (Exception e) {
|
sb.append(getTypicalElement(i));
|
||||||
Assert.fail();
|
}
|
||||||
}
|
sb.append("<book id=\"4\">" +
|
||||||
|
"<author>With line\n break</author>" +
|
||||||
try {
|
"<title>Book title 4</title>" +
|
||||||
inputStream.close();
|
"<publish_date>2010-05-26</publish_date>" +
|
||||||
} catch (IOException e) {
|
"</book>");
|
||||||
Assert.fail();
|
sb.append(getTypicalElement(5));
|
||||||
}
|
sb.append(getTypicalElement(6));
|
||||||
}
|
sb.append("</library>");
|
||||||
|
return sb.toString();
|
||||||
private void AssertGridCreate(Project project, int numCols, int numRows){
|
}
|
||||||
Assert.assertNotNull(project);
|
|
||||||
Assert.assertNotNull(project.columnModel);
|
public static String getSampleWithVaryingStructure(){
|
||||||
Assert.assertNotNull(project.columnModel.columns);
|
StringBuilder sb = new StringBuilder();
|
||||||
Assert.assertEquals(project.columnModel.columns.size(), numCols);
|
sb.append("<?xml version=\"1.0\"?><library>");
|
||||||
Assert.assertNotNull(project.rows);
|
for(int i = 1; i < 6; i++){
|
||||||
Assert.assertEquals(project.rows.size(), numRows);
|
sb.append(getTypicalElement(i));
|
||||||
}
|
}
|
||||||
|
sb.append("<book id=\"6\">" +
|
||||||
private void PrintProject(Project project){
|
"<author>Author 6, The</author>" +
|
||||||
//some quick and dirty debugging
|
"<title>Book title 6</title>" +
|
||||||
StringBuilder sb = new StringBuilder();
|
"<genre>New element not seen in other records</genre>" +
|
||||||
for(Column c : project.columnModel.columns){
|
"<publish_date>2010-05-26</publish_date>" +
|
||||||
sb.append(c.getName());
|
"</book>");
|
||||||
sb.append("; ");
|
sb.append("</library>");
|
||||||
}
|
return sb.toString();
|
||||||
logger.info(sb.toString());
|
}
|
||||||
for(Row r : project.rows){
|
|
||||||
sb = new StringBuilder();
|
public static String getSampleWithTreeStructure(){
|
||||||
for(Cell c : r.cells){
|
StringBuilder sb = new StringBuilder();
|
||||||
if(c != null){
|
sb.append("<?xml version=\"1.0\"?><library>");
|
||||||
sb.append(c.value);
|
for(int i = 1; i < 7; i++){
|
||||||
sb.append("; ");
|
sb.append("<book id=\"" + i + "\">" +
|
||||||
}else{
|
"<author><author-name>Author " + i + ", The</author-name>" +
|
||||||
sb.append("null; ");
|
"<author-dob>1950-0" + i + "-15</author-dob></author>" +
|
||||||
}
|
"<title>Book title " + i + "</title>" +
|
||||||
}
|
"<publish_date>2010-05-26</publish_date>" +
|
||||||
logger.info(sb.toString());
|
"</book>");
|
||||||
}
|
}
|
||||||
}
|
sb.append("</library>");
|
||||||
}
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RunTest(String testString){
|
||||||
|
try {
|
||||||
|
inputStream = new ByteArrayInputStream( testString.getBytes( "UTF-8" ) );
|
||||||
|
} catch (UnsupportedEncodingException e1) {
|
||||||
|
Assert.fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
SUT.read(inputStream, project, options);
|
||||||
|
} catch (Exception e) {
|
||||||
|
Assert.fail();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user