KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > smallsql > junit > TestDeleteUpdate


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

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

41 public class TestDeleteUpdate extends BasicTestCase {
42
43     /**
44      *
45      */

46     public TestDeleteUpdate() {
47         super();
48     }
49
50     /**
51      * @param name
52      */

53     public TestDeleteUpdate(String JavaDoc name) {
54         super(name);
55     }
56
57
58     public void testDelete() throws Exception JavaDoc{
59         Connection con = AllTests.getConnection();
60         dropTable(con,"testDelete");
61         Statement st = con.createStatement();
62         st.execute("create table testDelete(a int default 15)");
63         for(int i=0; i<10; i++){
64             st.execute("Insert into testDelete Values("+i+")");
65         }
66         assertRowCount( 10, "Select * from testDelete");
67         
68         st.execute("delete from testDelete Where a=3");
69         assertRowCount( 9, "Select * from testDelete");
70         
71         st.execute("delete from testDelete Where a<5");
72         assertRowCount( 5, "Select * from testDelete");
73         
74         st.execute("delete from testDelete");
75         assertRowCount( 0, "Select * from testDelete");
76         dropTable(con,"testDelete");
77     }
78     
79     public void testUpdate1() throws Exception JavaDoc{
80         Connection con = AllTests.getConnection();
81         dropTable(con,"testUpdate");
82         Statement st = con.createStatement();
83         st.execute("create table testUpdate(id int default 15, value int)");
84         for(int i=0; i<10; i++){
85             st.execute("Insert into testUpdate Values("+i+','+i+")");
86         }
87         assertRowCount( 10, "Select * from testUpdate");
88         
89         int updateCount;
90         updateCount = st.executeUpdate("update testUpdate set value=103 Where id=3");
91         assertEqualsRsValue( new Integer JavaDoc(103), "Select value from testUpdate Where id=3");
92         assertRowCount( 10, "Select value from testUpdate");
93         assertEquals( 1, updateCount);
94         
95         updateCount = st.executeUpdate("update testUpdate set value=104 Where id=3");
96         assertEqualsRsValue( new Integer JavaDoc(104), "Select value from testUpdate Where id=3");
97         assertRowCount( 10, "Select value from testUpdate");
98         assertEquals( 1, updateCount);
99         
100         updateCount = st.executeUpdate("delete from testUpdate Where id=3");
101         assertRowCount( 9, "Select * from testUpdate");
102         assertEquals( 1, updateCount);
103
104         updateCount = st.executeUpdate("update testUpdate set value=27 Where id<5");
105         assertEquals( 4, updateCount);
106
107         dropTable(con,"testUpdate");
108     }
109     
110     public void testUpdate2() throws Exception JavaDoc{
111         Connection con = AllTests.getConnection();
112         dropTable(con,"testUpdate");
113         Statement st = con.createStatement();
114         st.execute("create table testUpdate(id int default 15, value1 varchar(100), value2 int)");
115         for(int i=0; i<10; i++){
116             st.execute("Insert into testUpdate Values("+i+','+(i*100)+','+i+")");
117         }
118         assertRowCount( 10, "Select * from testUpdate");
119         
120         st.execute("update testUpdate set value1=13 Where id=3");
121         assertEqualsRsValue( "13", "Select value1 from testUpdate Where id=3");
122         assertRowCount( 10, "Select * from testUpdate");
123         
124         st.execute("update testUpdate set value1=1040 Where id=3");
125         assertEqualsRsValue( "1040", "Select value1 from testUpdate Where id=3");
126         assertRowCount( 10, "Select * from testUpdate");
127         
128         st.execute("update testUpdate set value1=10400 Where id=3");
129         assertEqualsRsValue( "10400", "Select value1 from testUpdate Where id=3");
130         assertRowCount( 10, "Select * from testUpdate");
131         
132         st.execute("update testUpdate set value1=13,id=3 Where id=3");
133         assertEqualsRsValue( "13", "Select value1 from testUpdate Where id=3");
134         assertRowCount( 10, "Select * from testUpdate");
135         
136         st.execute("delete from testUpdate Where id=3");
137         assertRowCount( 9, "Select * from testUpdate");
138
139         dropTable(con,"testUpdate");
140     }
141     
142
143     public void testUpdateMultiTables() throws Exception JavaDoc{
144         Connection con = AllTests.getConnection();
145         dropTable(con,"testUpdate1");
146         dropTable(con,"testUpdate2");
147         Statement st = con.createStatement();
148         st.execute("create table testUpdate1(id1 int, value1 varchar(100))");
149         st.execute("create table testUpdate2(id2 int, value2 varchar(100))");
150         st.execute("Insert into testUpdate1 Values(11, 'qwert1')");
151         st.execute("Insert into testUpdate2 Values(11, 'qwert2')");
152         
153         st.execute("update testUpdate1 inner join testUpdate2 on id1=id2 Set value1=value1+'update', value2=value2+'update'");
154         ResultSet rs = st.executeQuery("Select * From testUpdate1 inner join testUpdate2 on id1=id2");
155         assertTrue( rs.next() );
156         assertEquals( "qwert1update", rs.getString("value1"));
157         assertEquals( "qwert2update", rs.getString("value2"));
158
159         dropTable(con,"testUpdate1");
160         dropTable(con,"testUpdate2");
161     }
162 }
163
Popular Tags