KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > smallsql > junit > TestGroupBy


1 /* =============================================================
2  * SmallSQL : a free Java DBMS library for the Java(tm) platform
3  * =============================================================
4  *
5  * (C) Copyright 2004-2006, by Volker Berlin.
6  *
7  * Project Info: http://www.smallsql.de/
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ---------------
28  * TestGroupBy.java
29  * ---------------
30  * Author: Volker Berlin
31  *
32  */

33 package smallsql.junit;
34
35 import java.math.BigDecimal JavaDoc;
36 import java.sql.*;
37
38
39 /**
40  * @author Volker Berlin
41  *
42  */

43 public class TestGroupBy extends BasicTestCase {
44
45     private static final String JavaDoc table1 = "table_GroupBy1";
46     
47     private static final String JavaDoc STR_VALUE1 = "name1";
48     private static final String JavaDoc STR_VALUE2 = "name2";
49
50     private boolean init;
51     public TestGroupBy() {
52         super();
53     }
54
55     public TestGroupBy(String JavaDoc name) {
56         super(name);
57     }
58
59     public void init(){
60         if(init) return;
61         try{
62             Connection con = AllTests.getConnection();
63             dropTable( con, table1 );
64             Statement st = con.createStatement();
65             st.execute("create table " + table1 + "(name varchar(30), id int )");
66             //st.execute("create table " + table2 + "(c " + testValue.dataType +", d " + testValue.dataType + ")");
67
//st.execute("create table " + table3 + "(c " + testValue.dataType +", d " + testValue.dataType + ")");
68
st.close();
69             PreparedStatement pr = con.prepareStatement("INSERT into " + table1 + "(name, id) Values(?,?)");
70             pr.setString( 1, STR_VALUE1);
71             pr.setInt( 2, 1 );
72             pr.execute();
73             pr.setString( 1, STR_VALUE1);
74             pr.setInt( 2, 2 );
75             pr.execute();
76             pr.setString( 1, STR_VALUE1);
77             pr.setNull( 2, Types.INTEGER );
78             pr.execute();
79             pr.setString( 1, STR_VALUE2);
80             pr.setInt( 2, 1 );
81             pr.execute();
82
83             pr.close();
84
85             init = true;
86         }catch(Throwable JavaDoc e){
87             e.printStackTrace();
88         }
89     }
90
91     public void testTest() throws Exception JavaDoc{
92         init();
93         Connection con = AllTests.getConnection();
94         Statement st = con.createStatement();
95         ResultSet rs;
96         /*
97         rs = st.executeQuery("Select name FROM " + table1 + " Group By name");
98         while(rs.next()){
99             System.out.println( rs.getObject(1) );
100         }*/

101         
102         rs = st.executeQuery("Select count(id) FROM " + table1 + " Group By name");
103         while(rs.next()){
104             rs.getObject(1);
105         }
106
107         rs = st.executeQuery("Select count(*) FROM " + table1 + " Group By name");
108         while(rs.next()){
109             rs.getObject(1);
110         }
111
112         rs = st.executeQuery("Select count(*) FROM " + table1);
113         assertTrue(rs.next());
114         assertEquals( 4, rs.getInt(1));
115
116         rs = st.executeQuery("Select count(id) FROM " + table1);
117         assertTrue(rs.next());
118         assertEquals( 3, rs.getInt(1));
119
120         rs = st.executeQuery("Select count(*)+1 FROM " + table1);
121         assertTrue(rs.next());
122         assertEquals( 5, rs.getInt(1));
123     }
124
125     public void testCountWhere() throws Exception JavaDoc{
126         init();
127         assertEqualsRsValue( new Integer JavaDoc(0), "Select count(*) FROM " + table1 + " Where id=-1234");
128     }
129     
130     public void testCountWherePrepare() throws Exception JavaDoc{
131         init();
132         Connection con = AllTests.getConnection();
133         PreparedStatement pr = con.prepareStatement("Select count(*) FROM " + table1 + " Where id=-1234");
134         for(int i=1; i<=3; i++){
135             ResultSet rs = pr.executeQuery();
136             assertTrue( "No row produce in loop:"+i, rs.next());
137             assertEquals( "loop:"+i, 0, rs.getInt(1));
138         }
139     }
140     
141     public void testCountOrderBy() throws Exception JavaDoc{
142         init();
143         Connection con = AllTests.getConnection();
144         PreparedStatement pr = con.prepareStatement("Select count(*) FROM " + table1 + " Group By name Order By name DESC");
145         for(int i=1; i<=3; i++){
146             ResultSet rs = pr.executeQuery( );
147             assertTrue ( "loop:"+i, rs.next());
148             assertEquals( "loop:"+i, 1, rs.getInt(1));
149             assertTrue ( "loop:"+i, rs.next());
150             assertEquals( "loop:"+i, 3, rs.getInt(1));
151         }
152     }
153     
154     public void testGroupByWithExpression() throws Exception JavaDoc{
155         init();
156         Connection con = AllTests.getConnection();
157         PreparedStatement pr = con.prepareStatement("Select sum(id), name+'a' as ColumnName FROM " + table1 + " Group By name+'a' Order By Name+'a'");
158         for(int i=1; i<=3; i++){
159             ResultSet rs = pr.executeQuery( );
160             assertTrue ( "loop:"+i, rs.next());
161             assertEquals( "loop:"+i, 3, rs.getInt(1));
162             assertTrue ( "loop:"+i, rs.next());
163             assertEquals( "loop:"+i, 1, rs.getInt(1));
164             assertEquals( "loop:"+i+" Alias name von Expression", "ColumnName", rs.getMetaData().getColumnName(2));
165         }
166     }
167     
168     public void testComplex() throws Exception JavaDoc{
169         init();
170         Connection con = AllTests.getConnection();
171         PreparedStatement pr = con.prepareStatement("Select abs(sum(abs(3-id))+2) FROM " + table1 + " Group By name+'a' Order By 'b'+(Name+'a')");
172         for(int i=1; i<=3; i++){
173             ResultSet rs = pr.executeQuery( );
174             assertTrue ( "loop:"+i, rs.next());
175             assertEquals( "loop:"+i, 5, rs.getInt(1));
176             assertTrue ( "loop:"+i, rs.next());
177             assertEquals( "loop:"+i, 4, rs.getInt(1));
178         }
179     }
180     
181     public void testWithNullValue() throws Exception JavaDoc{
182         init();
183         assertEqualsRsValue(new Integer JavaDoc(4), "Select count(*) FROM " + table1 + " Group By name+null" );
184     }
185     
186     public void testSumInt() throws Exception JavaDoc{
187         init();
188         assertEqualsRsValue( new Integer JavaDoc(4), "Select sum(id) FROM " + table1);
189     }
190     
191     public void testSumLong() throws Exception JavaDoc{
192         init();
193         assertEqualsRsValue( new Long JavaDoc(4), "Select sum(cast(id as BigInt)) FROM " + table1);
194     }
195     
196     public void testSumReal() throws Exception JavaDoc{
197         init();
198         assertEqualsRsValue( new Float JavaDoc(4), "Select sum(cast(id as real)) FROM " + table1);
199     }
200     
201     public void testSumDouble() throws Exception JavaDoc{
202         init();
203         assertEqualsRsValue( new Double JavaDoc(4), "Select sum(cast(id as double)) FROM " + table1);
204     }
205     
206     public void testSumDecimal() throws Exception JavaDoc{
207         init();
208         assertEqualsRsValue( new BigDecimal JavaDoc("4.00"), "Select sum(cast(id as decimal(38,2))) FROM " + table1);
209     }
210     
211     public void testMaxInt() throws Exception JavaDoc{
212         init();
213         assertEqualsRsValue( new Integer JavaDoc(2), "Select max(id) FROM " + table1);
214     }
215     
216     public void testMaxBigInt() throws Exception JavaDoc{
217         init();
218         assertEqualsRsValue( new Long JavaDoc(2), "Select max(cast(id as BigInt)) FROM " + table1);
219     }
220     
221     public void testMaxString() throws Exception JavaDoc{
222         init();
223         assertEqualsRsValue( STR_VALUE2, "Select max(name) FROM " + table1);
224     }
225     
226     
227     public void testMaxTinyint() throws Exception JavaDoc{
228         init();
229         assertEqualsRsValue( new Integer JavaDoc(2), "Select max(convert(tinyint,id)) FROM " + table1);
230     }
231     
232     
233     public void testMaxReal() throws Exception JavaDoc{
234         init();
235         assertEqualsRsValue( new Float JavaDoc(2), "Select max(convert(real,id)) FROM " + table1);
236     }
237     
238     
239     public void testMaxFloat() throws Exception JavaDoc{
240         init();
241         assertEqualsRsValue( new Double JavaDoc(2), "Select max(convert(float,id)) FROM " + table1);
242     }
243     
244     
245     public void testMaxDouble() throws Exception JavaDoc{
246         init();
247         assertEqualsRsValue( new Double JavaDoc(2), "Select max(convert(double,id)) FROM " + table1);
248     }
249     
250     
251     public void testMaxMoney() throws Exception JavaDoc{
252         init();
253         assertEqualsRsValue( new java.math.BigDecimal JavaDoc("2.0000"), "Select max(convert(money,id)) FROM " + table1);
254     }
255     
256     
257     public void testMaxNumeric() throws Exception JavaDoc{
258         init();
259         assertEqualsRsValue( new java.math.BigDecimal JavaDoc("2"), "Select max(convert(numeric,id)) FROM " + table1);
260     }
261     
262     
263     public void testMaxDate() throws Exception JavaDoc{
264         init();
265         assertEqualsRsValue( java.sql.Date.valueOf("2345-01-23"), "Select max({d '2345-01-23'}) FROM " + table1);
266     }
267     
268     
269     public void testMaxTime() throws Exception JavaDoc{
270         init();
271         assertEqualsRsValue( java.sql.Time.valueOf("12:34:56"), "Select max({t '12:34:56'}) FROM " + table1);
272     }
273     
274     public void testMaxTimestamp() throws Exception JavaDoc{
275         init();
276         assertEqualsRsValue( java.sql.Timestamp.valueOf("2345-01-23 12:34:56.123"), "Select max({ts '2345-01-23 12:34:56.123'}) FROM " + table1);
277     }
278     
279     
280     public void testMaxUniqueidentifier() throws Exception JavaDoc{
281         init();
282         assertEqualsRsValue( "12345678-3445-3445-3445-1234567890AB", "Select max(convert(uniqueidentifier, '12345678-3445-3445-3445-1234567890ab')) FROM " + table1);
283     }
284     
285     
286     public void testMaxOfNull() throws Exception JavaDoc{
287         init();
288         assertEqualsRsValue( null, "Select max(id) FROM " + table1 + " Where id is null");
289     }
290     
291     public void testMin() throws Exception JavaDoc{
292         init();
293         assertEqualsRsValue( new Integer JavaDoc(1), "Select min(id) FROM " + table1);
294     }
295     
296     public void testMinString() throws Exception JavaDoc{
297         init();
298         assertEqualsRsValue( STR_VALUE1, "Select min(name) FROM " + table1);
299     }
300     
301     public void testMinOfNull() throws Exception JavaDoc{
302         init();
303         assertEqualsRsValue( null, "Select min(id) FROM " + table1 + " Where id is null");
304     }
305     
306     public void testFirst1() throws Exception JavaDoc{
307         init();
308         assertEqualsRsValue( new Integer JavaDoc(1), "Select first(id) FROM " + table1);
309     }
310     
311     public void testFirst2() throws Exception JavaDoc{
312         init();
313         assertEqualsRsValue( "name1", "Select first(name) FROM " + table1);
314     }
315     
316     public void testLast1() throws Exception JavaDoc{
317         init();
318         assertEqualsRsValue( new Integer JavaDoc(1), "Select last(id) FROM " + table1);
319     }
320     
321     public void testLast2() throws Exception JavaDoc{
322         init();
323         assertEqualsRsValue( "name2", "Select last(name) FROM " + table1);
324     }
325     
326     
327     public void testAvg() throws Exception JavaDoc{
328         init();
329         assertEqualsRsValue( new Integer JavaDoc(1), "Select avg(id) FROM " + table1);
330     }
331     
332     
333     public void testGroupBy() throws Exception JavaDoc{
334         init();
335         Connection con = AllTests.getConnection();
336         Statement st = con.createStatement();
337         ResultSet rs;
338         rs = st.executeQuery("Select name FROM " + table1 + " Group By name");
339         assertTrue(rs.next());
340         assertEquals( STR_VALUE1, rs.getObject(1) );
341         assertTrue(rs.next());
342         assertEquals( STR_VALUE2, rs.getObject(1) );
343         
344     }
345     
346     
347     /**
348      * A problem can be the metadata from a View.
349      * @throws Exception
350      */

351     public void testViewWidthGroupBy() throws Exception JavaDoc{
352         init();
353         Connection con = AllTests.getConnection();
354         Statement st = con.createStatement();
355         try{
356             ResultSet rs;
357             st.execute("Create View qry" + table1 + " as Select name, name as name2, count(*) as count FROM " + table1 + " Group By name");
358             rs = st.executeQuery("Select * from qry" + table1);
359             assertEquals( "name", rs.getMetaData().getColumnLabel(1) );
360             assertEquals( "name2", rs.getMetaData().getColumnLabel(2) );
361             assertEquals( "count", rs.getMetaData().getColumnLabel(3) );
362         }finally{
363             st.execute("Drop View qry" + table1);
364         }
365     }
366     
367     
368     public void testCountNoRow() throws Exception JavaDoc{
369         init();
370     
371         // test count(*) without any row
372
Connection con = AllTests.getConnection();
373         Statement st = con.createStatement();
374         st.execute("Delete FROM " + table1);
375         init = false;
376         assertEqualsRsValue( new Integer JavaDoc(0), "Select count(*) FROM " + table1);
377     }
378     
379 }
380
Popular Tags