Skip to content

Commit

Permalink
fix the ExcelSheetReader + wrote test
Browse files Browse the repository at this point in the history
  • Loading branch information
RoanKanninga committed Feb 7, 2013
1 parent 777935c commit 3eebf26
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/org/molgenis/io/excel/ExcelSheetReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ public Iterator<Tuple> iterator()
if (!it.hasNext()) return Collections.<Tuple> emptyList().iterator();

// create column header index once and reuse
final Map<String, Integer> colNamesMap = hasHeader ? (this.colNamesMap == null ? toColNamesMap(it.next())
: this.colNamesMap) : null;
final Map<String, Integer> colNamesMap;
if (hasHeader)
{
Row headerRow = it.next();
colNamesMap = this.colNamesMap == null ? toColNamesMap(headerRow) : this.colNamesMap;
}
else
colNamesMap = null;

return new Iterator<Tuple>()
{
Expand Down
24 changes: 24 additions & 0 deletions test/java/org/molgenis/io/csv/CsvReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,30 @@ public void iterator_separator() throws IOException
}
}

@Test
public void colNamesIteratorAndIterator() throws IOException
{
CsvReader csvReader = new CsvReader(new StringReader("col1,col2\nval1,val2"), ',', true);
try
{
Iterator<String> colNamesIt = csvReader.colNamesIterator();
assertTrue(colNamesIt.hasNext());
assertEquals(colNamesIt.next(), "col1");
assertTrue(colNamesIt.hasNext());
assertEquals(colNamesIt.next(), "col2");

Iterator<Tuple> it = csvReader.iterator();
Tuple t0 = it.next();
assertEquals(t0.get("col1"), "val1");
assertEquals(t0.get("col2"), "val2");
assertFalse(it.hasNext());
}
finally
{
csvReader.close();
}
}

@Test
public void close() throws IOException
{
Expand Down
34 changes: 34 additions & 0 deletions test/java/org/molgenis/io/excel/ExcelSheetReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,38 @@ public void iterator()
assertEquals(row4.get("col2"), "2.4");
assertFalse(it.hasNext());
}

@Test
public void colNamesIteratorAndIterator() throws IOException
{
Iterator<String> colNamesIt = excelSheetReader.colNamesIterator();
assertTrue(colNamesIt.hasNext());
assertEquals(colNamesIt.next(), "col1");
assertTrue(colNamesIt.hasNext());
assertEquals(colNamesIt.next(), "col2");

Iterator<Tuple> it = excelSheetReader.iterator();
assertTrue(it.hasNext());

Tuple row1 = it.next();
assertEquals(row1.get("col1"), "val1");
assertEquals(row1.get("col2"), "val2");
assertTrue(it.hasNext());

Tuple row2 = it.next();
assertEquals(row2.get("col1"), "val3");
assertEquals(row2.get("col2"), "val4");
assertTrue(it.hasNext());

Tuple row3 = it.next();
assertEquals(row3.get("col1"), "XXX");
assertEquals(row3.get("col2"), "val6");
assertTrue(it.hasNext());

// test number cell (col1) and formula cell (col2)
Tuple row4 = it.next();
assertEquals(row4.get("col1"), "1.2");
assertEquals(row4.get("col2"), "2.4");
assertFalse(it.hasNext());
}
}

0 comments on commit 3eebf26

Please sign in to comment.