KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > database > QueryDataSetTest


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.database;
23
24 import org.dbunit.DatabaseEnvironment;
25 import org.dbunit.dataset.AbstractDataSetTest;
26 import org.dbunit.dataset.IDataSet;
27 import org.dbunit.dataset.ITable;
28 import org.dbunit.dataset.NoSuchColumnException;
29 import org.dbunit.operation.DatabaseOperation;
30
31 /**
32  * @author Manuel Laflamme
33  * @version $Revision: 1.8 $
34  * @since Feb 18, 2002
35  */

36 public class QueryDataSetTest extends AbstractDataSetTest
37 {
38     private IDatabaseConnection _connection;
39
40     public QueryDataSetTest(String JavaDoc s)
41     {
42         super(s);
43     }
44
45     ////////////////////////////////////////////////////////////////////////////
46
// TestCase class
47

48     protected void setUp() throws Exception JavaDoc
49     {
50         super.setUp();
51
52         DatabaseEnvironment env = DatabaseEnvironment.getInstance();
53         _connection = env.getConnection();
54
55         DatabaseOperation.CLEAN_INSERT.execute(_connection, env.getInitDataSet());
56     }
57
58     protected void tearDown() throws Exception JavaDoc
59     {
60         super.tearDown();
61
62         _connection = null;
63     }
64
65     ////////////////////////////////////////////////////////////////////////////
66
// AbstractDataSetTest class
67

68     protected String JavaDoc[] getExpectedNames() throws Exception JavaDoc
69     {
70         return getExpectedLowerNames();
71     }
72
73     protected IDataSet createDataSet() throws Exception JavaDoc
74     {
75         String JavaDoc[] names = getExpectedNames();
76
77         QueryDataSet dataSet = new QueryDataSet(_connection);
78         for (int i = 0; i < names.length; i++)
79         {
80             String JavaDoc name = names[i];
81             String JavaDoc query = "select * from " + name;
82             dataSet.addTable(name, query);
83 /*
84             if (i % 2 == 0)
85             {
86                 String query = "select * from " + name;
87                 dataSet.addTable(name, query);
88             }
89             else
90             {
91                 dataSet.addTable(name);
92             }
93 */

94         }
95         return dataSet;
96     }
97
98     protected IDataSet createDuplicateDataSet() throws Exception JavaDoc
99     {
100         QueryDataSet dataSet = new QueryDataSet(_connection);
101         String JavaDoc[] names = getExpectedDuplicateNames();
102
103         // first table expect 1 row
104
String JavaDoc queryOneRow = "select * from only_pk_table";
105         dataSet.addTable(names[0], queryOneRow);
106
107         // second table expect 0 row
108
String JavaDoc queryNoRow = "select * from empty_table";
109         dataSet.addTable(names[1], queryNoRow);
110
111         // third table expect 2 row
112
String JavaDoc queryTwoRow = "select * from pk_table where PK0=0 or PK0=1";
113         dataSet.addTable(names[2], queryTwoRow);
114
115         return dataSet;
116     }
117
118     ////////////////////////////////////////////////////////////////////////////
119
// Test methods
120

121     public void testGetSelectPartialData() throws Exception JavaDoc
122     {
123
124         QueryDataSet ptds = new QueryDataSet(_connection);
125         ptds.addTable("PK_TABLE", "SELECT PK0, PK1 FROM pk_table where PK0 = 0");
126
127         ITable table = ptds.getTable("PK_TABLE");
128         assertEquals("", "0", table.getValue(0, "PK0").toString());
129         assertEquals("", "1", new String JavaDoc(table.getRowCount() + ""));
130
131     }
132
133     public void testGetAllColumnsWithStar() throws Exception JavaDoc
134     {
135
136         QueryDataSet ptds = new QueryDataSet(_connection);
137         ptds.addTable("PK_TABLE", "SELECT * FROM pk_table where PK0 = 0");
138
139         ITable table = ptds.getTable("PK_TABLE");
140         assertEquals("", "0", table.getValue(0, "PK0").toString());
141         assertEquals("", "1", new String JavaDoc(table.getRowCount() + ""));
142
143     }
144
145     public void testGetAllRowsSingleColumn() throws Exception JavaDoc
146     {
147
148         QueryDataSet ptds = new QueryDataSet(_connection);
149         ptds.addTable("PK_TABLE", "SELECT PK0 FROM pk_table");
150
151         ITable table = ptds.getTable("PK_TABLE");
152         assertEquals("", "0", table.getValue(0, "PK0").toString());
153         assertEquals("", "3", new String JavaDoc(table.getRowCount() + ""));
154     }
155
156
157     public void testOnlySpecifiedColumnsReturned() throws Exception JavaDoc
158     {
159
160         QueryDataSet ptds = new QueryDataSet(_connection);
161         ptds.addTable("PK_TABLE", "SELECT PK0 FROM pk_table");
162
163         ITable table = ptds.getTable("PK_TABLE");
164         assertEquals("", "0", table.getValue(0, "PK0").toString());
165
166         try
167         {
168             table.getValue(0, "PK1").toString();
169             fail("Should not have reached here, we should have thrown a NoSuchColumnException");
170         }
171         catch (NoSuchColumnException nsce)
172         {
173             String JavaDoc errorMsg = "org.dbunit.dataset.NoSuchColumnException: PK_TABLE.PK1";
174             assertTrue("Find text:" + errorMsg, nsce.toString().indexOf(errorMsg) >= 0);
175         }
176     }
177
178     public void testGetSelectPartialData2() throws Exception JavaDoc
179     {
180
181         QueryDataSet ptds = new QueryDataSet(_connection);
182         ptds.addTable("SECOND_TABLE",
183                 "SELECT * FROM second_table where COLUMN0='row 0 col 0'");
184
185         ITable table = ptds.getTable("SECOND_TABLE");
186         assertEquals("", "row 0 col 0", table.getValue(0, "COLUMN0").toString());
187         assertEquals("", "row 0 col 3", table.getValue(0, "COLUMN3").toString());
188         assertEquals("", "1", new String JavaDoc(table.getRowCount() + ""));
189
190     }
191
192     public void testCombinedWhere() throws Exception JavaDoc
193     {
194
195         QueryDataSet ptds = new QueryDataSet(_connection);
196         ptds.addTable("SECOND_TABLE",
197                 "SELECT COLUMN0, COLUMN3 FROM second_table where COLUMN0='row 0 col 0' and COLUMN2='row 0 col 2'");
198
199         ITable table = ptds.getTable("SECOND_TABLE");
200         assertEquals("", "row 0 col 0", table.getValue(0, "COLUMN0").toString());
201         assertEquals("", "row 0 col 3", table.getValue(0, "COLUMN3").toString());
202         assertEquals("", "1", new String JavaDoc(table.getRowCount() + ""));
203
204     }
205
206     public void testMultipleTables() throws Exception JavaDoc
207     {
208         ITable table = null;
209
210         QueryDataSet ptds = new QueryDataSet(_connection);
211         ptds.addTable("SECOND_TABLE",
212                 "SELECT * from second_table where COLUMN0='row 0 col 0' and COLUMN2='row 0 col 2'");
213         ptds.addTable("PK_TABLE",
214                 "SELECT * FROM pk_table where PK0 = 0");
215
216         table = ptds.getTable("SECOND_TABLE");
217         assertEquals("", "row 0 col 0", table.getValue(0, "COLUMN0").toString());
218         assertEquals("", "row 0 col 3", table.getValue(0, "COLUMN3").toString());
219         assertEquals("", "1", new String JavaDoc(table.getRowCount() + ""));
220
221         table = ptds.getTable("PK_TABLE");
222         assertEquals("", "0", table.getValue(0, "PK0").toString());
223         assertEquals("", "1", new String JavaDoc(table.getRowCount() + ""));
224
225     }
226
227     public void testMultipleTablesWithMissingWhere() throws Exception JavaDoc
228     {
229         QueryDataSet ptds = new QueryDataSet(_connection);
230         ptds.addTable("SECOND_TABLE",
231                 "SELECT * from second_table where COLUMN0='row 0 col 0' and COLUMN2='row 0 col 2'");
232         ptds.addTable("PK_TABLE", null);
233     }
234 }
235
236
237
238
239
240
241
242
243
244
245
246
Popular Tags