View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  package org.dbunit.database;
22  
23  import org.dbunit.DatabaseEnvironment;
24  import org.dbunit.dataset.Column;
25  import org.dbunit.dataset.ForwardOnlyTableTest;
26  import org.dbunit.dataset.ITable;
27  import org.dbunit.dataset.MockTableMetaData;
28  import org.dbunit.dataset.RowOutOfBoundsException;
29  import org.dbunit.operation.DatabaseOperation;
30  
31  /**
32   * @author Manuel Laflamme
33   * @since Apr 11, 2003
34   * @version $Revision$
35   */
36  public class ForwardOnlyResultSetTableIT extends ForwardOnlyTableTest
37  {
38      public ForwardOnlyResultSetTableIT(String s)
39      {
40          super(s);
41      }
42  
43      protected ITable createTable() throws Exception
44      {
45          DatabaseEnvironment env = DatabaseEnvironment.getInstance();
46          IDatabaseConnection connection = env.getConnection();
47  
48          DatabaseOperation.CLEAN_INSERT.execute(connection, env.getInitDataSet());
49  
50          String selectStatement = "select * from TEST_TABLE order by COLUMN0";
51          return new ForwardOnlyResultSetTable("TEST_TABLE", selectStatement, connection);
52      }
53  
54      protected String convertString(String str) throws Exception
55      {
56          return DatabaseEnvironment.getInstance().convertString(str);
57      }
58  
59      public void testGetMissingValue() throws Exception
60      {
61          // Do not test this!
62      }
63  
64      public void testGetValueOnLastRowIsClosingResultSet() throws Exception
65      {
66          String tableName = "TABLE";
67          String[] columnNames = {"C0"};
68  //        String[] columnNames = {"C0", "C1", "C2"};
69          Object[][] expectedValues = new Object[][]{
70              new Object[]{"1", "2", "3"},
71              new Object[]{"4", "5", "6"},
72              new Object[]{"7", "8", "9"},
73          };
74  
75          // Setup resultset
76          ExtendedMockMultiRowResultSet resultSet = new ExtendedMockMultiRowResultSet();
77          resultSet.setExpectedCloseCalls(1);
78          resultSet.setupColumnNames(columnNames);
79          resultSet.setupRows(expectedValues);
80  
81          // Create table
82          MockTableMetaData metaData = new MockTableMetaData(tableName, columnNames);
83          ForwardOnlyResultSetTable table =
84                  new ForwardOnlyResultSetTable(metaData, resultSet);
85  
86          // Exercise getValue()
87          try
88          {
89              Column[] columns = table.getTableMetaData().getColumns();
90  
91              for (int i = 0; ; i++)
92              {
93                  for (int j = 0; j < columns.length; j++)
94                  {
95                      String columnName = columns[j].getColumnName();
96                      Object actualValue = table.getValue(i, columnName);
97                      Object expectedValue = expectedValues[i][j];
98                      assertEquals("row=" + i + ", col=" + columnName,
99                              expectedValue, actualValue);
100 
101                 }
102             }
103         }
104         catch(RowOutOfBoundsException e)
105         {
106             // end of table
107         }
108 
109         // Verify that ResultSet have been closed
110         resultSet.verify();
111     }
112 
113 }