diff --git a/main/src/com/metaweb/gridworks/importers/XmlImportUtilities.java b/main/src/com/metaweb/gridworks/importers/XmlImportUtilities.java index 89de297a2..78ab92ebe 100644 --- a/main/src/com/metaweb/gridworks/importers/XmlImportUtilities.java +++ b/main/src/com/metaweb/gridworks/importers/XmlImportUtilities.java @@ -72,8 +72,6 @@ public class XmlImportUtilities { } static public String[] detectPathFromTag(InputStream inputStream, String tag) { - //List candidates = new ArrayList(); - try { XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream); @@ -99,6 +97,8 @@ public class XmlImportUtilities { } static protected List detectRecordElement(XMLStreamReader parser, String tag) throws XMLStreamException { + if(parser.getEventType() == XMLStreamConstants.START_DOCUMENT) + parser.next(); String localName = parser.getLocalName(); String fullName = composeName(parser.getPrefix(), localName); if (tag.equals(parser.getLocalName()) || tag.equals(fullName)) { @@ -327,6 +327,10 @@ public class XmlImportUtilities { int pathIndex, ImportColumnGroup rootColumnGroup ) throws XMLStreamException { + if(parser.getEventType() == XMLStreamConstants.START_DOCUMENT){ + logger.warn("Cannot use findRecord method for START_DOCUMENT event"); + return; + } String tagName = parser.getLocalName(); if (tagName.equals(recordPath[pathIndex])) { if (pathIndex < recordPath.length - 1) { @@ -466,7 +470,7 @@ public class XmlImportUtilities { ImportRecord record, String columnLocalName, String text, - int commonStaringRowIndex + int commonStartingRowIndex ) { if (text == null || ((String) text).isEmpty()) { return; @@ -478,7 +482,7 @@ public class XmlImportUtilities { int cellIndex = column.cellIndex; while (cellIndex >= record.columnEmptyRowIndices.size()) { - record.columnEmptyRowIndices.add(commonStaringRowIndex); + record.columnEmptyRowIndices.add(commonStartingRowIndex); } int rowIndex = record.columnEmptyRowIndices.get(cellIndex); @@ -491,7 +495,9 @@ public class XmlImportUtilities { 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); diff --git a/main/src/com/metaweb/gridworks/importers/XmlImporter.java b/main/src/com/metaweb/gridworks/importers/XmlImporter.java index 23c9a6369..6a2f42001 100644 --- a/main/src/com/metaweb/gridworks/importers/XmlImporter.java +++ b/main/src/com/metaweb/gridworks/importers/XmlImporter.java @@ -57,6 +57,9 @@ public class XmlImporter implements Importer { } } + if(recordPath == null) + return; + ImportColumnGroup rootColumnGroup = new ImportColumnGroup(); XmlImportUtilities.importXml(pis, project, recordPath, rootColumnGroup); diff --git a/main/tests/server/src/com/metaweb/gridworks/tests/importers/TestTools.java b/main/tests/server/src/com/metaweb/gridworks/tests/importers/TestTools.java new file mode 100644 index 000000000..c1148623a --- /dev/null +++ b/main/tests/server/src/com/metaweb/gridworks/tests/importers/TestTools.java @@ -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()); + } + } +} diff --git a/main/tests/server/src/com/metaweb/gridworks/tests/importers/XmlImportUtilitiesStub.java b/main/tests/server/src/com/metaweb/gridworks/tests/importers/XmlImportUtilitiesStub.java new file mode 100644 index 000000000..ed0943dab --- /dev/null +++ b/main/tests/server/src/com/metaweb/gridworks/tests/importers/XmlImportUtilitiesStub.java @@ -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 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); + } +} diff --git a/main/tests/server/src/com/metaweb/gridworks/tests/importers/XmlImportUtilitiesTests.java b/main/tests/server/src/com/metaweb/gridworks/tests/importers/XmlImportUtilitiesTests.java new file mode 100644 index 000000000..a5b8a9176 --- /dev/null +++ b/main/tests/server/src/com/metaweb/gridworks/tests/importers/XmlImportUtilitiesTests.java @@ -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("author1genre1"); + 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("author1genre1"); + 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("author1genre1"); + createParser(); + String tag="library"; + + List response = new ArrayList(); + 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("author1genre1"); + createParser(); + String tag="book"; + + List response = new ArrayList(); + 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("author1genre1"); + createParser(); + String tag=""; + + List response = new ArrayList(); + 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("author1genre1"); + 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("author1author2genre1"); + 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("author1a dategenre1"); + 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("author1genre1"); + 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(); + } + } +} diff --git a/main/tests/server/src/com/metaweb/gridworks/tests/importers/XmlImporterTests.java b/main/tests/server/src/com/metaweb/gridworks/tests/importers/XmlImporterTests.java index e4272b6bf..613d5c80d 100644 --- a/main/tests/server/src/com/metaweb/gridworks/tests/importers/XmlImporterTests.java +++ b/main/tests/server/src/com/metaweb/gridworks/tests/importers/XmlImporterTests.java @@ -1,207 +1,225 @@ -package com.metaweb.gridworks.tests.importers; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.util.Properties; - -import static org.mockito.Mockito.mock; - -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.XmlImporter; -import com.metaweb.gridworks.model.Cell; -import com.metaweb.gridworks.model.Column; -import com.metaweb.gridworks.model.Project; -import com.metaweb.gridworks.model.Row; - - -public class XmlImporterTests { - final static Logger logger = LoggerFactory.getLogger("XmlImporterTests"); - - //dependencies - Project project = null; - Properties options = null; - ByteArrayInputStream inputStream = null; - - //System Under Test - XmlImporter SUT = null; - - - @BeforeMethod - public void SetUp(){ - SUT = new XmlImporter(); - project = new Project(); - options = mock(Properties.class); - } - - @AfterMethod - public void TearDown(){ - SUT = null; - project = null; - options = null; - } - - @Test - public void canParseSample(){ - - RunTest(getSample()); - - AssertGridCreate(project, 4, 6); - PrintProject(project); - - Row row = project.rows.get(0); - Assert.assertNotNull(row); - Assert.assertNotNull(row.cells); - Assert.assertNotNull(row.cells.get(2)); - Assert.assertEquals(row.cells.get(2).value, "Author 1, The"); - - - } - - @Test - public void testCanParseLineBreak(){ - - RunTest(getSampleWithLineBreak()); - - AssertGridCreate(project, 4, 6); - PrintProject(project); - - Row row = project.rows.get(3); - Assert.assertNotNull(row); - Assert.assertNotNull(row.cells); - Assert.assertNotNull(row.cells.get(2)); - Assert.assertEquals(row.cells.get(2).value, "With line\n break"); - } - - @Test(groups={"broken"}) - public void testElementsWithVaryingStructure(){ - - - RunTest(getSampleWithVaryingStructure()); - - AssertGridCreate(project, 5, 6); - PrintProject(project); - - Row row0 = project.rows.get(0); - Assert.assertNotNull(row0); - Assert.assertNotNull(row0.cells); - Assert.assertEquals(row0.cells.size(),6); - - Row row5 = project.rows.get(5); - Assert.assertNotNull(row5); - Assert.assertNotNull(row5.cells); - Assert.assertEquals(row5.cells.size(),6); - - - } - - //------------helper methods--------------- - - protected String getTypicalElement(int id){ - return "" + - "Author " + id + ", The" + - "Book title " + id + "" + - "2010-05-26" + - ""; - } - - protected String getSample(){ - StringBuilder sb = new StringBuilder(); - sb.append(""); - for(int i = 1; i < 7; i++){ - sb.append(getTypicalElement(i)); - } - sb.append(""); - return sb.toString(); - } - - protected String getSampleWithLineBreak(){ - StringBuilder sb = new StringBuilder(); - sb.append(""); - for(int i = 1; i < 4; i++){ - sb.append(getTypicalElement(i)); - } - sb.append("" + - "With line\n break" + - "Book title 4" + - "2010-05-26" + - ""); - sb.append(getTypicalElement(5)); - sb.append(getTypicalElement(6)); - sb.append(""); - return sb.toString(); - } - - protected String getSampleWithVaryingStructure(){ - StringBuilder sb = new StringBuilder(); - sb.append(""); - for(int i = 1; i < 6; i++){ - sb.append(getTypicalElement(i)); - } - sb.append("" + - "With line\n break" + - "Book title 6" + - "New element not seen in other records" + - "2010-05-26" + - ""); - sb.append(""); - 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(); - } - - try { - inputStream.close(); - } catch (IOException e) { - Assert.fail(); - } - } - - private void AssertGridCreate(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); - } - - private 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(Cell c : r.cells){ - if(c != null){ - sb.append(c.value); - sb.append("; "); - }else{ - sb.append("null; "); - } - } - logger.info(sb.toString()); - } - } -} +package com.metaweb.gridworks.tests.importers; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.Properties; + +import static org.mockito.Mockito.mock; + +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.XmlImporter; +import com.metaweb.gridworks.model.Project; +import com.metaweb.gridworks.model.Row; + + +public class XmlImporterTests { + final static Logger logger = LoggerFactory.getLogger("XmlImporterTests"); + + //dependencies + Project project = null; + Properties options = null; + ByteArrayInputStream inputStream = null; + + //System Under Test + XmlImporter SUT = null; + + + @BeforeMethod + public void SetUp(){ + SUT = new XmlImporter(); + project = new Project(); + options = mock(Properties.class); + } + + @AfterMethod + public void TearDown() throws IOException{ + SUT = null; + project = null; + options = null; + inputStream.close(); + inputStream = null; + } + + @Test + public void canParseSample(){ + RunTest(getSample()); + + TestTools.AssertGridCreated(project, 4, 6); + TestTools.PrintProject(project); + + Row row = project.rows.get(0); + Assert.assertNotNull(row); + Assert.assertNotNull(row.getCell(1)); + Assert.assertEquals(row.getCell(1).value, "Author 1, The"); + } + + @Test + public void canParseSampleWithDuplicateNestedElements(){ + RunTest(getSampleWithDuplicateNestedElements()); + + TestTools.PrintProject(project); + TestTools.AssertGridCreated(project, 4, 12); + + Row row = project.rows.get(0); + Assert.assertNotNull(row); + Assert.assertEquals(row.cells.size(), 4); + Assert.assertNotNull(row.getCell(2)); + Assert.assertEquals(row.getCell(1).value, "Author 1, The"); + Assert.assertEquals(project.rows.get(1).getCell(1).value, "Author 1, Another"); + } + + @Test + public void testCanParseLineBreak(){ + + RunTest(getSampleWithLineBreak()); + + TestTools.AssertGridCreated(project, 4, 6); + TestTools.PrintProject(project); + + Row row = project.rows.get(3); + Assert.assertNotNull(row); + Assert.assertEquals(row.cells.size(), 4); + Assert.assertNotNull(row.getCell(1)); + Assert.assertEquals(row.getCell(1).value, "With line\n break"); + } + + @Test(groups={"broken"}) + public void testElementsWithVaryingStructure(){ + RunTest(getSampleWithVaryingStructure()); + + TestTools.AssertGridCreated(project, 5, 6); + TestTools.PrintProject(project); + + Row row0 = project.rows.get(0); + Assert.assertNotNull(row0); + Assert.assertEquals(row0.cells.size(),6); + + Row row5 = project.rows.get(5); + Assert.assertNotNull(row5); + Assert.assertEquals(row5.cells.size(),6); + } + + @Test + public void testElementWithNestedTree(){ + RunTest(getSampleWithTreeStructure()); + TestTools.AssertGridCreated(project, 5, 6); + TestTools.PrintProject(project); + Assert.assertEquals(project.columnModel.columnGroups.size(),1); + Assert.assertEquals(project.columnModel.columnGroups.get(0).keyColumnIndex, 2); + Assert.assertEquals(project.columnModel.columnGroups.get(0).startColumnIndex, 2); + Assert.assertNull(project.columnModel.columnGroups.get(0).parentGroup); + Assert.assertEquals(project.columnModel.columnGroups.get(0).subgroups.size(),0); + Assert.assertEquals(project.columnModel.columnGroups.get(0).columnSpan,2); + } + + //------------helper methods--------------- + + public static String getTypicalElement(int id){ + return "" + + "Author " + id + ", The" + + "Book title " + id + "" + + "2010-05-26" + + ""; + } + + public static String getElementWithDuplicateSubElement(int id){ + return "" + + "Author " + id + ", The" + + "Author " + id + ", Another" + + "Book title " + id + "" + + "2010-05-26" + + ""; + } + + public static String getSample(){ + StringBuilder sb = new StringBuilder(); + sb.append(""); + for(int i = 1; i < 7; i++){ + sb.append(getTypicalElement(i)); + } + sb.append(""); + return sb.toString(); + } + + public static String getSampleWithDuplicateNestedElements(){ + StringBuilder sb = new StringBuilder(); + sb.append(""); + for(int i = 1; i < 7; i++){ + sb.append(getElementWithDuplicateSubElement(i)); + } + sb.append(""); + return sb.toString(); + + } + + public static String getSampleWithLineBreak(){ + StringBuilder sb = new StringBuilder(); + sb.append(""); + for(int i = 1; i < 4; i++){ + sb.append(getTypicalElement(i)); + } + sb.append("" + + "With line\n break" + + "Book title 4" + + "2010-05-26" + + ""); + sb.append(getTypicalElement(5)); + sb.append(getTypicalElement(6)); + sb.append(""); + return sb.toString(); + } + + public static String getSampleWithVaryingStructure(){ + StringBuilder sb = new StringBuilder(); + sb.append(""); + for(int i = 1; i < 6; i++){ + sb.append(getTypicalElement(i)); + } + sb.append("" + + "Author 6, The" + + "Book title 6" + + "New element not seen in other records" + + "2010-05-26" + + ""); + sb.append(""); + return sb.toString(); + } + + public static String getSampleWithTreeStructure(){ + StringBuilder sb = new StringBuilder(); + sb.append(""); + for(int i = 1; i < 7; i++){ + sb.append("" + + "Author " + i + ", The" + + "1950-0" + i + "-15" + + "Book title " + i + "" + + "2010-05-26" + + ""); + } + sb.append(""); + 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(); + } + } + + +}