KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > operation > RefreshOperationTest


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 org.dbunit.AbstractDatabaseTest;
25 import org.dbunit.Assertion;
26 import org.dbunit.database.MockDatabaseConnection;
27 import org.dbunit.database.statement.MockBatchStatement;
28 import org.dbunit.database.statement.MockStatementFactory;
29 import org.dbunit.dataset.*;
30 import org.dbunit.dataset.datatype.DataType;
31 import org.dbunit.dataset.xml.FlatXmlDataSet;
32
33 import java.io.FileReader JavaDoc;
34 import java.io.Reader JavaDoc;
35
36 /**
37  * @author Manuel Laflamme
38  * @version $Revision: 1.18 $
39  * @since Feb 19, 2002
40  */

41 public class RefreshOperationTest extends AbstractDatabaseTest
42 {
43     public RefreshOperationTest(String JavaDoc s)
44     {
45         super(s);
46     }
47
48     public void testExecute() throws Exception JavaDoc
49     {
50         Reader JavaDoc reader = new FileReader JavaDoc("src/xml/refreshOperationTest.xml");
51         IDataSet dataSet = new FlatXmlDataSet(reader);
52
53         testExecute(dataSet);
54     }
55
56     public void testExecuteCaseInsensitive() throws Exception JavaDoc
57     {
58         Reader JavaDoc reader = new FileReader JavaDoc("src/xml/refreshOperationTest.xml");
59         IDataSet dataSet = new FlatXmlDataSet(reader);
60
61         testExecute(new LowerCaseDataSet(dataSet));
62     }
63
64     public void testExecuteForwardOnly() throws Exception JavaDoc
65     {
66         Reader JavaDoc reader = new FileReader JavaDoc("src/xml/refreshOperationTest.xml");
67         IDataSet dataSet = new FlatXmlDataSet(reader);
68
69         testExecute(new ForwardOnlyDataSet(dataSet));
70     }
71
72     private void testExecute(IDataSet dataSet) throws Exception JavaDoc
73     {
74         String JavaDoc[] tableNames = {"PK_TABLE", "ONLY_PK_TABLE"};
75         int[] tableRowCount = {3, 1};
76         String JavaDoc primaryKey = "PK0";
77
78         // verify table before
79
assertEquals("array lenght", tableNames.length, tableRowCount.length);
80         for (int i = 0; i < tableNames.length; i++)
81         {
82             ITable tableBefore = createOrderedTable(tableNames[i], primaryKey);
83             assertEquals("row count before", tableRowCount[i], tableBefore.getRowCount());
84         }
85
86         DatabaseOperation.REFRESH.execute(_connection, dataSet);
87
88         // verify table after
89
IDataSet expectedDataSet = new FlatXmlDataSet(
90                 new FileReader JavaDoc("src/xml/refreshOperationTestExpected.xml"));
91
92         for (int i = 0; i < tableNames.length; i++)
93         {
94             ITable expectedTable = expectedDataSet.getTable(tableNames[i]);
95             ITable tableAfter = createOrderedTable(tableNames[i], primaryKey);
96             Assertion.assertEquals(expectedTable, tableAfter);
97         }
98     }
99
100     public void testExecuteAndNoPrimaryKeys() throws Exception JavaDoc
101     {
102         String JavaDoc tableName = "test_table";
103
104         Reader JavaDoc reader = new FileReader JavaDoc("src/xml/refreshOperationNoPKTest.xml");
105         IDataSet dataSet = new FlatXmlDataSet(reader);
106
107         // verify table before
108
assertEquals("row count before", 6, _connection.getRowCount(tableName));
109
110         try
111         {
112             DatabaseOperation.REFRESH.execute(_connection, dataSet);
113             fail("Should not be here!");
114         }
115         catch (NoPrimaryKeyException e)
116         {
117
118         }
119
120         // verify table after
121
assertEquals("row count before", 6, _connection.getRowCount(tableName));
122     }
123
124     public void testExecuteWithDuplicateTables() throws Exception JavaDoc
125     {
126         String JavaDoc[] tableNames = {"PK_TABLE", "ONLY_PK_TABLE"};
127         int[] tableRowCount = {3, 1};
128         String JavaDoc primaryKey = "PK0";
129
130         IDataSet xmlDataSet = new FlatXmlDataSet(
131                 new FileReader JavaDoc("src/xml/refreshOperationDuplicateTest.xml"));
132         assertEquals("table count", xmlDataSet.getTableNames().length, 4);
133
134         // verify table before
135
assertEquals("array lenght", tableNames.length, tableRowCount.length);
136         for (int i = 0; i < tableNames.length; i++)
137         {
138             ITable tableBefore = createOrderedTable(tableNames[i], primaryKey);
139             assertEquals("row count before", tableRowCount[i], tableBefore.getRowCount());
140         }
141
142         DatabaseOperation.REFRESH.execute(_connection, xmlDataSet);
143
144         // verify table after
145
IDataSet expectedDataSet = new FlatXmlDataSet(
146                 new FileReader JavaDoc("src/xml/refreshOperationTestExpected.xml"));
147
148         for (int i = 0; i < tableNames.length; i++)
149         {
150             ITable expectedTable = expectedDataSet.getTable(tableNames[i]);
151             ITable tableAfter = createOrderedTable(tableNames[i], primaryKey);
152             Assertion.assertEquals(expectedTable, tableAfter);
153         }
154     }
155
156     public void testExecuteWithEmptyTable() throws Exception JavaDoc
157     {
158         Column[] columns = {new Column("c1", DataType.VARCHAR)};
159         ITable table = new DefaultTable(new DefaultTableMetaData(
160                 "name", columns, columns));
161         IDataSet dataSet = new DefaultDataSet(table);
162
163         // setup mock objects
164
MockStatementFactory factory = new MockStatementFactory();
165         factory.setExpectedCreatePreparedStatementCalls(0);
166
167         MockDatabaseConnection connection = new MockDatabaseConnection();
168         connection.setupDataSet(dataSet);
169         connection.setupStatementFactory(factory);
170         connection.setExpectedCloseCalls(0);
171
172         // execute operation
173
DatabaseOperation.REFRESH.execute(connection, dataSet);
174
175         factory.verify();
176         connection.verify();
177     }
178
179     public void testExecuteUnknownColumn() throws Exception JavaDoc
180     {
181         String JavaDoc tableName = "table";
182
183         // setup table
184
Column[] columns = new Column[]{
185             new Column("unknown", DataType.VARCHAR),
186         };
187         DefaultTable table = new DefaultTable(tableName, columns);
188         table.addRow();
189         table.setValue(0, columns[0].getColumnName(), "value");
190         IDataSet insertDataset = new DefaultDataSet(table);
191
192         IDataSet databaseDataSet = new DefaultDataSet(
193                 new DefaultTable(tableName, new Column[]{
194                     new Column("column", DataType.VARCHAR),
195                 }));
196
197         // setup mock objects
198
MockBatchStatement statement = new MockBatchStatement();
199         statement.setExpectedExecuteBatchCalls(0);
200         statement.setExpectedClearBatchCalls(0);
201         statement.setExpectedCloseCalls(0);
202
203         MockStatementFactory factory = new MockStatementFactory();
204         factory.setExpectedCreatePreparedStatementCalls(0);
205         factory.setupStatement(statement);
206
207         MockDatabaseConnection connection = new MockDatabaseConnection();
208         connection.setupDataSet(databaseDataSet);
209         connection.setupStatementFactory(factory);
210         connection.setExpectedCloseCalls(0);
211
212         // execute operation
213
try
214         {
215             new RefreshOperation().execute(connection, insertDataset);
216             fail("Should not be here!");
217         }
218         catch (NoSuchColumnException e)
219         {
220
221         }
222
223         statement.verify();
224         factory.verify();
225         connection.verify();
226     }
227
228
229 }
230
231
232
233
234
235
236
237
Popular Tags