[ 
https://issues.apache.org/jira/browse/ODFTOOLKIT-399?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14641706#comment-14641706
 ] 

Nimarukan edited comment on ODFTOOLKIT-399 at 7/25/15 5:38 PM:
---------------------------------------------------------------

{code:title=TableRowIteratorTestCase.java}
package org.odftoolkit.simple.table;

import java.util.Iterator;
import java.util.NoSuchElementException;

import org.odftoolkit.simple.SpreadsheetDocument;

import org.junit.Before;
import org.junit.Test;

/**
 * Bug: In Table.SimpleRowIterator, the next() method does not 
 * advance correctly if it is called without hasNext(). <p/>
 *
 * (Also, if there are no more elements, it returns null
 * rather than throwing NoSuchElementException as specified
 * by Iterator.) <p/>
 *
 * simple-odf-8.1-incubating.jar, jdk1.8.0.51 <p/>
 *
 * testIteratorRows_withHasNext:
 * <pre>
 * Ok With calls to hasNext:
 * 0: Heading
 * 1: Datum
 * </pre>
 *
 * testIteratorRows_SansHasNext:
 * <pre>
 * java.lang.AssertionError: unexpected result sans hasNext():
 * 0: Heading
 * 0: Heading
 * 1: Datum
 * </pre>
 *
 * testIterateRows_NoSuchElementException:
 * <pre>
 * java.lang.AssertionError: NoSuchElementException not thrown
 * Alpha: Heading
 * Beta: Heading
 * Gamma: Datum
 * Delta: Datum
 * </pre>
 */
public class TableRowIteratorTestCase {
  public static void main(String[] ignore) throws Exception {
    TableRowIteratorTestCase testCase = new TableRowIteratorTestCase();
    testCase.setUp();
    try {
      testCase.testIterateRows_WithHasNext();
    } catch (Throwable th) { th.printStackTrace(System.err); }
    try {
      testCase.testIterateRows_SansHasNext();
    } catch (Throwable th) { th.printStackTrace(System.err); }
    try {
      testCase.testIterateRows_NoSuchElementException();
    } catch (Throwable th) { th.printStackTrace(System.err); }
  }


  Table table;

  @Before
  public void setUp() throws Exception {
    SpreadsheetDocument doc = SpreadsheetDocument.newSpreadsheetDocument();
    Table table = doc.getSheetByIndex(0);

    // put "Heading" into first cell of first row
    Row headRow = table.getRowByIndex(0);
    Cell headCell = headRow.getCellByIndex(0);
    headCell.addParagraph("Heading");

    // put "Datum" into first cell of next row
    assert table.getRowCount() == 1 : table.getRowCount();
    Row firstDataRow = table.appendRow();
    Cell dataCell = firstDataRow.getCellByIndex(0);
    dataCell.addParagraph("Datum");

    this.table = table;
  }

  @Test
  public void testIterateRows_WithHasNext() {
    assert table.getRowCount() == 2 : table.getRowCount();

    StringBuilder valueBuf = new StringBuilder();
    Iterator<Row> rowIter = table.getRowIterator();
    String value;
    do {
      rowIter.hasNext();
      Row row = rowIter.next();
      int rowIndex = row.getRowIndex();
      Cell cell = row.getCellByIndex(0);
      value = cell.getStringValue();
      valueBuf.append(rowIndex+": "+value+"\n");
    } while (!"Datum".equals(value));

    String result = valueBuf.toString();
    String expect = ("0: Heading\n"+
                     "1: Datum\n");

    if (!expect.equals(result)) {
      throw new AssertionError("Unexpected result with hasNext():\n"+result);
    }
    System.err.println("\nOk With calls to hasNext:\n"+result);
  }

  @Test
  public void testIterateRows_SansHasNext() {
    assert table.getRowCount() == 2 : table.getRowCount();

    StringBuilder valueBuf = new StringBuilder();
    Iterator<Row> rowIter = table.getRowIterator();
    String value;
    do {
      Row row = rowIter.next();
      int rowIndex = row.getRowIndex();
      Cell cell = row.getCellByIndex(0);
      value = cell.getStringValue();
      valueBuf.append(rowIndex+": "+value+"\n");
    } while (!"Datum".equals(value));

    String result = valueBuf.toString();
    String expect = ("0: Heading\n"+
                     "1: Datum\n");

    if (!expect.equals(result)) {
      throw new AssertionError("unexpected result sans hasNext():\n"+result);
    }
    System.err.println("\nOk sans calls to hasNext:\n"+result);
  }

  @Test
  public void testIterateRows_NoSuchElementException() {
    assert table.getRowCount() == 2 : table.getRowCount();

    StringBuilder s = new StringBuilder();
    String N = "\n";
    Iterator<Row> rowIter = table.getRowIterator();
    try {
      s.append("Alpha: "+rowIter.next().getCellByIndex(0).getStringValue()+N);
      s.append("Beta: "+rowIter.next().getCellByIndex(0).getStringValue()+N);
      s.append("Gamma: "+rowIter.next().getCellByIndex(0).getStringValue()+N);
      s.append("Delta: "+rowIter.next().getCellByIndex(0).getStringValue()+N);
      s.append("Epsilon: "+rowIter.next().getCellByIndex(0).getStringValue()+N);
    } catch (NoSuchElementException noMoreRows) { 
      System.err.println("\nOk, NoSuchElementException thrown:\n"+s);
    } catch (NullPointerException nullRow) {
      throw new AssertionError("NoSuchElementException not thrown\n"+s);
    }
  }
}
{code}


was (Author: nimarukan):
{code:title=TableRowIteratorTests.java}
package org.odftoolkit.simple.table;

import java.util.Iterator;
import java.util.NoSuchElementException;

import org.odftoolkit.simple.SpreadsheetDocument;

import org.junit.Before;
import org.junit.Test;

/**
 * Bug: In Table.SimpleRowIterator, the next() method does not 
 * advance correctly if it is called without hasNext(). <p/>
 *
 * (Also, if there are no more elements, it returns null
 * rather than throwing NoSuchElementException as specified
 * by Iterator.) <p/>
 *
 * simple-odf-8.1-incubating.jar, jdk1.8.0.51 <p/>
 *
 * testIteratorRows_withHasNext:
 * <pre>
 * Ok With calls to hasNext:
 * 0: Heading
 * 1: Datum
 * </pre>
 *
 * testIteratorRows_SansHasNext:
 * <pre>
 * java.lang.AssertionError: unexpected result sans hasNext():
 * 0: Heading
 * 0: Heading
 * 1: Datum
 * </pre>
 *
 * testIterateRows_NoSuchElementException:
 * <pre>
 * java.lang.AssertionError: NoSuchElementException not thrown
 * Alpha: Heading
 * Beta: Heading
 * Gamma: Datum
 * Delta: Datum
 * </pre>
 */
public class TableRowIteratorTests {
  public static void main(String[] ignore) throws Exception {
    TableRowIteratorTests testCase = new TableRowIteratorTests();
    testCase.setUp();
    try {
      testCase.testIterateRows_WithHasNext();
    } catch (Throwable th) { th.printStackTrace(System.err); }
    try {
      testCase.testIterateRows_SansHasNext();
    } catch (Throwable th) { th.printStackTrace(System.err); }
    try {
      testCase.testIterateRows_NoSuchElementException();
    } catch (Throwable th) { th.printStackTrace(System.err); }
  }


  Table table;

  @Before
  public void setUp() throws Exception {
    SpreadsheetDocument doc = SpreadsheetDocument.newSpreadsheetDocument();
    Table table = doc.getSheetByIndex(0);

    // put "Heading" into first cell of first row
    Row headRow = table.getRowByIndex(0);
    Cell headCell = headRow.getCellByIndex(0);
    headCell.addParagraph("Heading");

    // put "Datum" into first cell of next row
    assert table.getRowCount() == 1 : table.getRowCount();
    Row firstDataRow = table.appendRow();
    Cell dataCell = firstDataRow.getCellByIndex(0);
    dataCell.addParagraph("Datum");

    this.table = table;
  }

  @Test
  public void testIterateRows_WithHasNext() {
    assert table.getRowCount() == 2 : table.getRowCount();

    StringBuilder valueBuf = new StringBuilder();
    Iterator<Row> rowIter = table.getRowIterator();
    String value;
    do {
      rowIter.hasNext();
      Row row = rowIter.next();
      int rowIndex = row.getRowIndex();
      Cell cell = row.getCellByIndex(0);
      value = cell.getStringValue();
      valueBuf.append(rowIndex+": "+value+"\n");
    } while (!"Datum".equals(value));

    String result = valueBuf.toString();
    String expect = ("0: Heading\n"+
                     "1: Datum\n");

    if (!expect.equals(result)) {
      throw new AssertionError("Unexpected result with hasNext():\n"+result);
    }
    System.err.println("\nOk With calls to hasNext:\n"+result);
  }

  @Test
  public void testIterateRows_SansHasNext() {
    assert table.getRowCount() == 2 : table.getRowCount();

    StringBuilder valueBuf = new StringBuilder();
    Iterator<Row> rowIter = table.getRowIterator();
    String value;
    do {
      Row row = rowIter.next();
      int rowIndex = row.getRowIndex();
      Cell cell = row.getCellByIndex(0);
      value = cell.getStringValue();
      valueBuf.append(rowIndex+": "+value+"\n");
    } while (!"Datum".equals(value));

    String result = valueBuf.toString();
    String expect = ("0: Heading\n"+
                     "1: Datum\n");

    if (!expect.equals(result)) {
      throw new AssertionError("unexpected result sans hasNext():\n"+result);
    }
    System.err.println("\nOk sans calls to hasNext:\n"+result);
  }

  @Test
  public void testIterateRows_NoSuchElementException() {
    assert table.getRowCount() == 2 : table.getRowCount();

    StringBuilder s = new StringBuilder();
    String N = "\n";
    Iterator<Row> rowIter = table.getRowIterator();
    try {
      s.append("Alpha: "+rowIter.next().getCellByIndex(0).getStringValue()+N);
      s.append("Beta: "+rowIter.next().getCellByIndex(0).getStringValue()+N);
      s.append("Gamma: "+rowIter.next().getCellByIndex(0).getStringValue()+N);
      s.append("Delta: "+rowIter.next().getCellByIndex(0).getStringValue()+N);
      s.append("Epsilon: "+rowIter.next().getCellByIndex(0).getStringValue()+N);
    } catch (NoSuchElementException noMoreRows) { 
      System.err.println("\nOk, NoSuchElementException thrown:\n"+s);
    } catch (NullPointerException nullRow) {
      throw new AssertionError("NoSuchElementException not thrown\n"+s);
    }
  }
}
{code}

> Table rowIterator next() does not advance correctly if not preceded by 
> hasNext()
> --------------------------------------------------------------------------------
>
>                 Key: ODFTOOLKIT-399
>                 URL: https://issues.apache.org/jira/browse/ODFTOOLKIT-399
>             Project: ODF Toolkit
>          Issue Type: Bug
>          Components: simple api
>    Affects Versions: 0.6.1-incubating
>         Environment: org.apache.odftoolkit:simple-odf:0.8.1-incubating
> jdk1.8.0.51
>            Reporter: Nimarukan
>            Priority: Minor
>              Labels: test
>
> In Table rowIterator the next() method does not advance correctly if it is 
> called multiple times without an intervening call to hasNext().
> (Also, if there are no more elements, it returns null rather than throwing 
> NoSuchElementException as specified by java.util.Iterator.next().)



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to