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  
22  package org.dbunit.operation;
23  
24  import java.io.FileReader;
25  import java.io.Reader;
26  
27  import org.dbunit.AbstractDatabaseIT;
28  import org.dbunit.Assertion;
29  import org.dbunit.database.MockDatabaseConnection;
30  import org.dbunit.database.statement.MockBatchStatement;
31  import org.dbunit.database.statement.MockStatementFactory;
32  import org.dbunit.dataset.Column;
33  import org.dbunit.dataset.DefaultDataSet;
34  import org.dbunit.dataset.DefaultTable;
35  import org.dbunit.dataset.DefaultTableMetaData;
36  import org.dbunit.dataset.ForwardOnlyDataSet;
37  import org.dbunit.dataset.IDataSet;
38  import org.dbunit.dataset.ITable;
39  import org.dbunit.dataset.LowerCaseDataSet;
40  import org.dbunit.dataset.NoPrimaryKeyException;
41  import org.dbunit.dataset.NoSuchColumnException;
42  import org.dbunit.dataset.datatype.DataType;
43  import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
44  import org.dbunit.testutil.TestUtils;
45  
46  /**
47   * @author Manuel Laflamme
48   * @version $Revision$
49   * @since Feb 19, 2002
50   */
51  public class RefreshOperationIT extends AbstractDatabaseIT
52  {
53      public RefreshOperationIT(String s)
54      {
55          super(s);
56      }
57  
58      public void testExecute() throws Exception
59      {
60          Reader reader = TestUtils.getFileReader("xml/refreshOperationTest.xml");
61          IDataSet dataSet = new FlatXmlDataSetBuilder().build(reader);
62  
63          testExecute(dataSet);
64      }
65  
66      public void testExecuteCaseInsensitive() throws Exception
67      {
68          Reader reader = TestUtils.getFileReader("xml/refreshOperationTest.xml");
69          IDataSet dataSet = new FlatXmlDataSetBuilder().build(reader);
70  
71          testExecute(new LowerCaseDataSet(dataSet));
72      }
73  
74      public void testExecuteForwardOnly() throws Exception
75      {
76          Reader reader = TestUtils.getFileReader("xml/refreshOperationTest.xml");
77          IDataSet dataSet = new FlatXmlDataSetBuilder().build(reader);
78  
79          testExecute(new ForwardOnlyDataSet(dataSet));
80      }
81  
82      private void testExecute(IDataSet dataSet) throws Exception
83      {
84          String[] tableNames = {"PK_TABLE", "ONLY_PK_TABLE"};
85          int[] tableRowCount = {3, 1};
86          String primaryKey = "PK0";
87  
88          // verify table before
89          assertEquals("array lenght", tableNames.length, tableRowCount.length);
90          for (int i = 0; i < tableNames.length; i++)
91          {
92              ITable tableBefore = createOrderedTable(tableNames[i], primaryKey);
93              assertEquals("row count before", tableRowCount[i], tableBefore.getRowCount());
94          }
95  
96          DatabaseOperation.REFRESH.execute(_connection, dataSet);
97  
98          // verify table after
99          IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(
100                 TestUtils.getFileReader("xml/refreshOperationTestExpected.xml"));
101 
102         for (int i = 0; i < tableNames.length; i++)
103         {
104             ITable expectedTable = expectedDataSet.getTable(tableNames[i]);
105             ITable tableAfter = createOrderedTable(tableNames[i], primaryKey);
106             Assertion.assertEquals(expectedTable, tableAfter);
107         }
108     }
109 
110     public void testExecuteAndNoPrimaryKeys() throws Exception
111     {
112         String tableName = "TEST_TABLE";
113 
114         Reader reader = TestUtils.getFileReader("xml/refreshOperationNoPKTest.xml");
115         IDataSet dataSet = new FlatXmlDataSetBuilder().build(reader);
116 
117         // verify table before
118         assertEquals("row count before", 6, _connection.getRowCount(tableName));
119 
120         try
121         {
122             DatabaseOperation.REFRESH.execute(_connection, dataSet);
123             fail("Should not be here!");
124         }
125         catch (NoPrimaryKeyException e)
126         {
127 
128         }
129 
130         // verify table after
131         assertEquals("row count before", 6, _connection.getRowCount(tableName));
132     }
133 
134     public void testExecuteWithEmptyTable() throws Exception
135     {
136         Column[] columns = {new Column("c1", DataType.VARCHAR)};
137         ITable table = new DefaultTable(new DefaultTableMetaData(
138                 "name", columns, columns));
139         IDataSet dataSet = new DefaultDataSet(table);
140 
141         // setup mock objects
142         MockStatementFactory factory = new MockStatementFactory();
143         factory.setExpectedCreatePreparedStatementCalls(0);
144 
145         MockDatabaseConnection connection = new MockDatabaseConnection();
146         connection.setupDataSet(dataSet);
147         connection.setupStatementFactory(factory);
148         connection.setExpectedCloseCalls(0);
149 
150         // execute operation
151         DatabaseOperation.REFRESH.execute(connection, dataSet);
152 
153         factory.verify();
154         connection.verify();
155     }
156 
157     public void testExecuteUnknownColumn() throws Exception
158     {
159         String tableName = "table";
160 
161         // setup table
162         Column[] columns = new Column[]{
163             new Column("unknown", DataType.VARCHAR),
164         };
165         DefaultTable table = new DefaultTable(tableName, columns);
166         table.addRow();
167         table.setValue(0, columns[0].getColumnName(), "value");
168         IDataSet insertDataset = new DefaultDataSet(table);
169 
170         IDataSet databaseDataSet = new DefaultDataSet(
171                 new DefaultTable(tableName, new Column[]{
172                     new Column("column", DataType.VARCHAR),
173                 }));
174 
175         // setup mock objects
176         MockBatchStatement statement = new MockBatchStatement();
177         statement.setExpectedExecuteBatchCalls(0);
178         statement.setExpectedClearBatchCalls(0);
179         statement.setExpectedCloseCalls(0);
180 
181         MockStatementFactory factory = new MockStatementFactory();
182         factory.setExpectedCreatePreparedStatementCalls(0);
183         factory.setupStatement(statement);
184 
185         MockDatabaseConnection connection = new MockDatabaseConnection();
186         connection.setupDataSet(databaseDataSet);
187         connection.setupStatementFactory(factory);
188         connection.setExpectedCloseCalls(0);
189 
190         // execute operation
191         try
192         {
193             new RefreshOperation().execute(connection, insertDataset);
194             fail("Should not be here!");
195         }
196         catch (NoSuchColumnException e)
197         {
198 
199         }
200 
201         statement.verify();
202         factory.verify();
203         connection.verify();
204     }
205 
206 
207 }
208 
209 
210 
211 
212 
213 
214