KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > test > TestCascade


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.test;
33
34 /*
35  * CascadeDeleteBug.java
36  *
37  * Created on June 24, 2002, 8:48 AM
38  */

39 import java.io.File JavaDoc;
40 import java.sql.Connection JavaDoc;
41 import java.sql.DriverManager JavaDoc;
42 import java.sql.ResultSet JavaDoc;
43 import java.sql.SQLException JavaDoc;
44 import java.sql.Statement JavaDoc;
45
46 import junit.framework.TestCase;
47
48 /**
49  * Test case to demonstrate catastrophic bug in cascade delete code.
50  *
51  * @version 1.0
52  * @author David Kopp
53  */

54 public class TestCascade extends TestCase {
55
56     Connection JavaDoc con;
57
58     public TestCascade(String JavaDoc name) {
59         super(name);
60     }
61
62     protected void setUp() {
63
64         try {
65             Class.forName("org.hsqldb.jdbcDriver");
66             createDatabase();
67
68             con = DriverManager.getConnection("jdbc:hsqldb:testdb", "sa", "");
69         } catch (Exception JavaDoc e) {
70             e.printStackTrace();
71             System.out.println(this + ".setUp() error: " + e.getMessage());
72         }
73     }
74
75     protected void tearDown() {
76
77         try {
78             con.close();
79         } catch (SQLException JavaDoc e) {}
80     }
81
82     public void testDelete() {
83
84         try {
85             insertData(con);
86
87             Statement JavaDoc stmt = con.createStatement();
88             ResultSet JavaDoc rs =
89                 stmt.executeQuery("SELECT COUNT(EIACODXA) FROM CA");
90
91             rs.next();
92
93             int origCount = rs.getInt(1);
94
95             rs.close();
96             deleteXBRecord(con);
97
98             rs = stmt.executeQuery("SELECT COUNT(EIACODXA) FROM CA");
99
100             rs.next();
101
102             int newCount = rs.getInt(1);
103
104             rs.close();
105             stmt.close();
106             assertEquals(9, newCount);
107         } catch (SQLException JavaDoc e) {
108             this.assertTrue("SQLException thrown", false);
109         }
110     }
111
112     private static void createDatabase() throws SQLException JavaDoc {
113
114         new File JavaDoc("testdb.backup").delete();
115         new File JavaDoc("testdb.data").delete();
116         new File JavaDoc("testdb.properties").delete();
117         new File JavaDoc("testdb.script").delete();
118
119         Connection JavaDoc con = DriverManager.getConnection("jdbc:hsqldb:testdb",
120             "sa", "");
121         String JavaDoc[] saDDL = {
122             "CREATE CACHED TABLE XB (EIACODXA VARCHAR(10) NOT NULL, LSACONXB VARCHAR(18) NOT NULL, ALTLCNXB VARCHAR(2) NOT NULL, LCNTYPXB VARCHAR(1) NOT NULL, LCNINDXB VARCHAR(1), LCNAMEXB VARCHAR(19), UPDT_BY VARCHAR(32), LST_UPDT TIMESTAMP, CONSTRAINT XPKXB PRIMARY KEY (EIACODXA, LSACONXB, ALTLCNXB, LCNTYPXB));",
123
124 // "CREATE INDEX XIF2XB ON XB (EIACODXA);",
125
"CREATE CACHED TABLE CA ( EIACODXA VARCHAR(10) NOT NULL, LSACONXB VARCHAR(18) NOT NULL, ALTLCNXB VARCHAR(2) NOT NULL, LCNTYPXB VARCHAR(1) NOT NULL, TASKCDCA VARCHAR(7) NOT NULL, TSKFRQCA NUMERIC(7,4), UPDT_BY VARCHAR(32), LST_UPDT TIMESTAMP, CONSTRAINT XPKCA PRIMARY KEY (EIACODXA, LSACONXB, ALTLCNXB, LCNTYPXB, TASKCDCA), CONSTRAINT R_XB_CA FOREIGN KEY (EIACODXA, LSACONXB, ALTLCNXB, LCNTYPXB) REFERENCES XB ON DELETE CASCADE);",
126
127 // "CREATE INDEX XIF26CA ON CA ( EIACODXA, LSACONXB, ALTLCNXB, LCNTYPXB);"
128
};
129         Statement JavaDoc stmt = con.createStatement();
130
131         for (int index = 0; index < saDDL.length; index++) {
132             stmt.executeUpdate(saDDL[index]);
133         }
134
135         stmt.execute("SHUTDOWN");
136         con.close();
137     } // createDatabase
138

139     /**
140      * This method demonstrates the bug in cascading deletes. Before this method,
141      * the CA table has 12 records. After, it should have 9, but instead it has
142      * 0.
143      */

144     private static void deleteXBRecord(Connection JavaDoc con) throws SQLException JavaDoc {
145
146         Statement JavaDoc stmt = con.createStatement();
147
148         stmt.executeUpdate(
149             "DELETE FROM XB WHERE LSACONXB = 'LEAA' AND EIACODXA = 'T850' AND LCNTYPXB = 'P' AND ALTLCNXB = '00'");
150         stmt.close();
151     } // deleteXBRecord
152

153     private static void insertData(Connection JavaDoc con) throws SQLException JavaDoc {
154
155         String JavaDoc[] saData = {
156             "INSERT INTO XB VALUES('T850','LEAA','00','P',NULL,'LCN NAME','sa',NOW)",
157             "INSERT INTO XB VALUES('T850','LEAA01','00','P',NULL,'LCN NAME','sa',NOW)",
158             "INSERT INTO XB VALUES('T850','LEAA02','00','P',NULL,'LCN NAME','sa',NOW)",
159             "INSERT INTO XB VALUES('T850','LEAA03','00','P',NULL,'LCN NAME','sa',NOW)",
160             "INSERT INTO CA VALUES('T850','LEAA','00','P','ABCDEFG',3.14,'sa',NOW)",
161             "INSERT INTO CA VALUES('T850','LEAA','00','P','QRSTUJV',3.14,'sa',NOW)",
162             "INSERT INTO CA VALUES('T850','LEAA','00','P','ZZZZZZZ',3.14,'sa',NOW)",
163             "INSERT INTO CA VALUES('T850','LEAA01','00','P','ABCDEFG',3.14,'sa',NOW)",
164             "INSERT INTO CA VALUES('T850','LEAA01','00','P','QRSTUJV',3.14,'sa',NOW)",
165             "INSERT INTO CA VALUES('T850','LEAA01','00','P','ZZZZZZZ',3.14,'sa',NOW)",
166             "INSERT INTO CA VALUES('T850','LEAA02','00','P','ABCDEFG',3.14,'sa',NOW)",
167             "INSERT INTO CA VALUES('T850','LEAA02','00','P','QRSTUJV',3.14,'sa',NOW)",
168             "INSERT INTO CA VALUES('T850','LEAA02','00','P','ZZZZZZZ',3.14,'sa',NOW)",
169             "INSERT INTO CA VALUES('T850','LEAA03','00','P','ABCDEFG',3.14,'sa',NOW)",
170             "INSERT INTO CA VALUES('T850','LEAA03','00','P','QRSTUJV',3.14,'sa',NOW)",
171             "INSERT INTO CA VALUES('T850','LEAA03','00','P','ZZZZZZZ',3.14,'sa',NOW)"
172         };
173         Statement JavaDoc stmt = con.createStatement();
174
175         for (int index = 0; index < saData.length; index++) {
176             stmt.executeUpdate(saData[index]);
177         }
178     } // insertData
179
}
180
Popular Tags