KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > smallsql > junit > TestJoins


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  * TestJoins.java
29  * ---------------
30  * Author: Volker Berlin
31  *
32  */

33 package smallsql.junit;
34
35 import junit.framework.*;
36
37 import java.math.BigDecimal JavaDoc;
38 import java.sql.*;
39
40 public class TestJoins extends BasicTestCase {
41
42     private TestValue testValue;
43
44     private static final String JavaDoc table = "table_joins";
45     private static final String JavaDoc table2= "table_joins2";
46     private static final String JavaDoc table3= "table_joins3";
47
48     private static final TestValue[] TESTS = new TestValue[]{
49         a("tinyint" , new Byte JavaDoc( (byte)3), new Byte JavaDoc( (byte)4)),
50         a("byte" , new Byte JavaDoc( (byte)3), new Byte JavaDoc( (byte)4)),
51         a("smallint" , new Short JavaDoc( (short)3), new Short JavaDoc( (short)4)),
52         a("int" , new Integer JavaDoc(3), new Integer JavaDoc(4)),
53         a("bigint" , new Long JavaDoc(3), new Long JavaDoc(4)),
54         a("real" , new Float JavaDoc(3.45), new Float JavaDoc(4.56)),
55         a("float" , new Float JavaDoc(3.45), new Float JavaDoc(4.56)),
56         a("double" , new Double JavaDoc(3.45), new Double JavaDoc(4.56)),
57         a("smallmoney" , new Float JavaDoc(3.45), new Float JavaDoc(4.56)),
58         a("money" , new Float JavaDoc(3.45), new Float JavaDoc(4.56)),
59         a("money" , new Double JavaDoc(3.45), new Double JavaDoc(4.56)),
60         a("numeric(19,2)" , new BigDecimal JavaDoc("3.45"), new BigDecimal JavaDoc("4.56")),
61         a("decimal(19,2)" , new BigDecimal JavaDoc("3.45"), new BigDecimal JavaDoc("4.56")),
62         a("varnum(28,2)" , new BigDecimal JavaDoc(3.45), new BigDecimal JavaDoc(4.56)),
63         a("number(28,2)" , new BigDecimal JavaDoc(3.45), new BigDecimal JavaDoc(4.56)),
64         a("varchar(100)" , new String JavaDoc("abc"), new String JavaDoc("qwert")),
65         a("nvarchar(100)" , new String JavaDoc("abc"), new String JavaDoc("qwert")),
66         a("varchar2(100)" , new String JavaDoc("abc"), new String JavaDoc("qwert")),
67         a("nvarchar2(100)" , new String JavaDoc("abc"), new String JavaDoc("qwert")),
68         a("character(100)" , new String JavaDoc("abc"), new String JavaDoc("qwert")),
69         a("char(100)" , new String JavaDoc("abc"), new String JavaDoc("qwert")),
70         a("nchar(100)" , new String JavaDoc("abc"), new String JavaDoc("qwert")),
71         a("text" , new String JavaDoc("abc"), new String JavaDoc("qwert")),
72         a("ntext" , new String JavaDoc("abc"), new String JavaDoc("qwert")),
73         a("date" , new Date(99, 1,1), new Date(99, 2,2)),
74         a("time" , new Time(9, 1,1), new Time(9, 2,2)),
75         a("timestamp" , new Timestamp(99, 1,1,0,0,0,0), new Timestamp(99, 2,2,0,0,0,0)),
76         a("datetime" , new Timestamp(99, 1,1,0,0,0,0), new Timestamp(99, 2,2,0,0,0,0)),
77         a("smalldatetime" , new Timestamp(99, 1,1,0,0,0,0), new Timestamp(99, 2,2,0,0,0,0)),
78         a("binary(100)" , new byte[]{12, 45, 1}, new byte[]{12, 45, 2, 56, 89}),
79         a("varbinary(100)" , new byte[]{12, 45, 1}, new byte[]{12, 45, 2, 56, 89}),
80         a("raw(100)" , new byte[]{12, 45, 1}, new byte[]{12, 45, 2, 56, 89}),
81         a("long raw" , new byte[]{12, 45, 1}, new byte[]{12, 45, 2, 56, 89}),
82         a("longvarbinary" , new byte[]{12, 45, 1}, new byte[]{12, 45, 2, 56, 89}),
83         a("blob" , new byte[]{12, 45, 1}, new byte[]{12, 45, 2, 56, 89}),
84         a("image" , new byte[]{12, 45, 1}, new byte[]{12, 45, 2, 56, 89}),
85         a("boolean" , Boolean.FALSE, Boolean.TRUE),
86         a("bit" , Boolean.FALSE, Boolean.TRUE),
87         a("uniqueidentifier" , "12345678-3445-3445-3445-1234567890ab", "12345679-3445-3445-3445-1234567890ab"),
88     };
89
90
91     TestJoins(TestValue testValue){
92         super(testValue.dataType);
93         this.testValue = testValue;
94     }
95
96     
97     private void clear() throws SQLException{
98         Connection con = AllTests.getConnection();
99         dropTable( con, table );
100         dropTable( con, table2 );
101         dropTable( con, table3 );
102     }
103     
104     
105     public void tearDown() throws SQLException{
106         clear();
107     }
108
109     public void setUp() throws Exception JavaDoc{
110         clear();
111         Connection con = AllTests.getConnection();
112         Statement st = con.createStatement();
113         st.execute("create table " + table + "(a " + testValue.dataType +" PRIMARY KEY, b " + testValue.dataType + ")");
114         st.execute("create table " + table2+ "(c " + testValue.dataType +" PRIMARY KEY, d " + testValue.dataType + ")");
115         st.execute("create table " + table3+ "(c " + testValue.dataType +" PRIMARY KEY, d " + testValue.dataType + ")");
116         st.close();
117         con.close();
118         con = AllTests.getConnection();
119         PreparedStatement pr = con.prepareStatement("INSERT into " + table + "(a,b) Values(?,?)");
120         insertValues( pr );
121         pr.close();
122
123         pr = con.prepareStatement("INSERT into " + table2 + " Values(?,?)");
124         insertValues( pr );
125         pr.close();
126     }
127
128     private void insertValues(PreparedStatement pr ) throws Exception JavaDoc{
129             pr.setObject( 1, testValue.small);
130             pr.setObject( 2, testValue.large);
131             pr.execute();
132
133             pr.setObject( 1, testValue.small);
134             pr.setObject( 2, testValue.small);
135             pr.execute();
136
137             pr.setObject( 1, testValue.large);
138             pr.setObject( 2, testValue.large);
139             pr.execute();
140
141             pr.setObject( 1, testValue.large);
142             pr.setObject( 2, testValue.small);
143             pr.execute();
144
145             pr.setObject( 1, null);
146             pr.setObject( 2, testValue.small);
147             pr.execute();
148
149             pr.setObject( 1, testValue.small);
150             pr.setObject( 2, null);
151             pr.execute();
152
153             pr.setObject( 1, null);
154             pr.setObject( 2, null);
155             pr.execute();
156     }
157
158     public void runTest() throws Exception JavaDoc{
159         Connection con = AllTests.getConnection();
160         Statement st = con.createStatement();
161         ResultSet rs;
162
163         rs = st.executeQuery("Select * from " + table + " where 1 = 0");
164         assertFalse( "To many rows", rs.next() );
165
166         assertRowCount( 7, "Select * from " + table);
167         assertRowCount( 49, "Select * from " + table + ", " + table2);
168         assertRowCount( 0, "Select * from " + table + ", " + table3);
169         assertRowCount( 49, "Select * from ("+ table +"), " + table2);
170         assertRowCount( 49, "Select * from " + table + " Cross Join " + table2);
171         assertRowCount( 13, "Select * from " + table + " INNER JOIN " + table2 + " ON " + table + ".a = " + table2 + ".c");
172         assertRowCount( 13, "Select * from {oj " + table + " INNER JOIN " + table2 + " ON " + table + ".a = " + table2 + ".c}");
173         assertRowCount( 13, "Select * from " + table + " AS t1 INNER JOIN " + table2 + " t2 ON t1.a = t2.c");
174         assertRowCount( 13, "Select * from {oj " + table + " t1 INNER JOIN " + table2 + " t2 ON t1.a = t2.c}");
175         assertRowCount( 4, "Select * from " + table + " t1 INNER JOIN " + table2 + " t2 ON t1.a = t2.c and t1.b=t2.d");
176         assertRowCount( 7, "Select * from " + table + " t1 LEFT OUTER JOIN " + table2 + " t2 ON t1.a = t2.c and t1.b=t2.d");
177         assertRowCount( 15, "Select * from " + table + " t1 LEFT OUTER JOIN " + table2 + " t2 ON t1.a = t2.c");
178         assertRowCount( 7, "Select * from " + table + " t1 LEFT OUTER JOIN " + table3 + " t2 ON t1.a = t2.c");
179         assertRowCount( 7, "Select * from " + table + " t1 RIGHT OUTER JOIN " + table2 + " t2 ON t1.a = t2.c and t1.b=t2.d");
180         assertRowCount( 7, "Select * from " + table + " t1 RIGHT OUTER JOIN " + table2 + " t2 ON false");
181         assertRowCount( 15, "Select * from " + table + " t1 RIGHT OUTER JOIN " + table2 + " t2 ON t1.a = t2.c");
182         assertRowCount( 0, "Select * from " + table + " t1 RIGHT OUTER JOIN " + table3 + " t2 ON t1.a = t2.c");
183         assertRowCount( 14, "Select * from " + table + " t1 FULL OUTER JOIN " + table2 + " t2 ON 1=0");
184         assertRowCount( 17, "Select * from " + table + " t1 FULL OUTER JOIN " + table2 + " t2 ON t1.a = t2.c");
185         assertRowCount( 7, "Select * from " + table + " t1 FULL OUTER JOIN " + table3 + " t2 ON t1.a = t2.c");
186         assertRowCount( 7, "Select * from " + table3 + " t1 FULL OUTER JOIN " + table + " t2 ON t1.c = t2.a");
187         assertRowCount( 5, "Select * from " + table + " INNER JOIN (SELECT DISTINCT c FROM " + table2 + ") t1 ON " + table + ".a = t1.c");
188         
189         st.close();
190     }
191
192     public static Test suite() throws Exception JavaDoc{
193         TestSuite theSuite = new TestSuite("Joins");
194         for(int i=0; i<TESTS.length; i++){
195             theSuite.addTest(new TestJoins( TESTS[i] ) );
196         }
197         return theSuite;
198     }
199
200     public static void main(String JavaDoc[] argv) {
201         junit.swingui.TestRunner.main(new String JavaDoc[]{TestJoins.class.getName()});
202     }
203
204
205
206     private static TestValue a(String JavaDoc dataType, Object JavaDoc small, Object JavaDoc large){
207         TestValue value = new TestValue();
208         value.dataType = dataType;
209         value.small = small;
210         value.large = large;
211         return value;
212     }
213
214     private static class TestValue{
215         String JavaDoc dataType;
216         Object JavaDoc small;
217         Object JavaDoc large;
218     }
219
220 }
Popular Tags