KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > js > junit > VersionTest


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.js.junit;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.DriverManager JavaDoc;
21 import java.sql.PreparedStatement JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.sql.Statement JavaDoc;
24
25 import junit.framework.TestCase;
26 import org.apache.ws.jaxme.logging.Logger;
27 import org.apache.ws.jaxme.logging.LoggerAccess;
28 import org.apache.ws.jaxme.sqls.BinaryColumn;
29 import org.apache.ws.jaxme.sqls.Column;
30 import org.apache.ws.jaxme.sqls.ForeignKey;
31 import org.apache.ws.jaxme.sqls.Index;
32 import org.apache.ws.jaxme.sqls.SQLFactory;
33 import org.apache.ws.jaxme.sqls.Schema;
34 import org.apache.ws.jaxme.sqls.StringColumn;
35 import org.apache.ws.jaxme.sqls.Table;
36 import org.apache.ws.jaxme.sqls.hsqldb.HsqlDbSQLFactoryImpl;
37
38
39 /**
40  * @author <a HREF="mailto:jwi@softwareag.com">Jochen Wiedmann</a>
41  */

42 public class VersionTest extends TestCase {
43     private static final Logger logger = LoggerAccess.getLogger(VersionTest.class);
44     public VersionTest(String JavaDoc pName) { super(pName); }
45     private Connection JavaDoc connection;
46
47     private SQLFactory factory;
48     private Schema schema;
49     private Table mainTable, subTable, subsubTable;
50     
51     protected SQLFactory getSQLFactory() {
52         if (factory == null) {
53             factory = new HsqlDbSQLFactoryImpl();
54         }
55         return factory;
56     }
57     
58     protected Table getMainTable() {
59         if (mainTable == null) {
60             Table mt = getSchema().newTable("MAIN");
61             Column mtId = mt.newColumn("ID", Column.Type.BIGINT);
62             Column mtVer = mt.newColumn("VER", Column.Type.INTEGER);
63             StringColumn mtName = (StringColumn) mt.newColumn("NAME", Column.Type.VARCHAR);
64             mtName.setLength(60);
65             BinaryColumn mtSig = (BinaryColumn) mt.newColumn("SIG", Column.Type.BINARY);
66             mtSig.setLength(16);
67             mt.newColumn("DATE", Column.Type.DATE);
68             
69             Index primaryKey = mt.newPrimaryKey();
70             primaryKey.addColumn(mtId);
71             primaryKey.addColumn(mtVer);
72             mainTable = mt;
73         }
74         return mainTable;
75     }
76     
77     protected Table getSubTable() {
78         if (subTable == null) {
79             Table st = getSchema().newTable("SUB");
80             StringColumn stId = (StringColumn) st.newColumn("ID", Column.Type.VARCHAR);
81             stId.setLength(32);
82             
83             Column stMtId = st.newColumn("MTID", Column.Type.BIGINT);
84             Column stMtVer = st.newColumn("MTVER", Column.Type.INTEGER);
85             StringColumn stAddress = (StringColumn) st.newColumn("ADDRESS", Column.Type.VARCHAR);
86             stAddress.setLength(60);
87             StringColumn stEmail = (StringColumn) st.newColumn("EMAIL", Column.Type.VARCHAR);
88             stEmail.setLength(60);
89             stEmail.setNullable(true);
90             
91             Index primaryKey = st.newPrimaryKey();
92             primaryKey.addColumn(stId);
93             
94             ForeignKey foreignKey = st.newForeignKey(getMainTable());
95             foreignKey.addColumnLink(stMtId, getMainTable().getColumn("ID"));
96             foreignKey.addColumnLink(stMtVer, getMainTable().getColumn("VER"));
97             
98             subTable = st;
99         }
100         return subTable;
101     }
102     
103     protected Table getSubSubTable() {
104         if (subsubTable == null) {
105             Table sst = getSchema().newTable("SUBSUB");
106             StringColumn sstId = (StringColumn) sst.newColumn("ID", Column.Type.VARCHAR);
107             sstId.setLength(32);
108             
109             Column sstMtId = sst.newColumn("MTID", Column.Type.BIGINT);
110             Column sstMtVer = sst.newColumn("MTVER", Column.Type.INTEGER);
111             ForeignKey foreignKeySt = sst.newForeignKey(getMainTable());
112             foreignKeySt.addColumnLink(sstMtId, getMainTable().getColumn("ID"));
113             foreignKeySt.addColumnLink(sstMtVer, getMainTable().getColumn("VER"));
114             
115             StringColumn sstStId = (StringColumn) sst.newColumn("SSTID", Column.Type.VARCHAR);
116             sstStId.setLength(32);
117             ForeignKey foreignKeySst = sst.newForeignKey(getSubTable());
118             foreignKeySst.addColumnLink(sstStId, getSubTable().getColumn("ID"));
119             
120             sst.newColumn("MTTS", Column.Type.TIMESTAMP);
121         }
122         return subsubTable;
123     }
124     
125     protected Schema getSchema() {
126         if (schema == null) {
127             schema = getSQLFactory().getDefaultSchema();
128             getMainTable();
129             getSubTable();
130             getSubSubTable();
131         }
132         return schema;
133     }
134     
135     protected Connection JavaDoc getConnection() throws ClassNotFoundException JavaDoc, SQLException JavaDoc {
136         if (connection == null) {
137             final String JavaDoc mName = "getConnection";
138             String JavaDoc driver = System.getProperty("jdbc.driver");
139             if (driver == null) {
140                 driver = "org.hsqldb.jdbcDriver";
141                 logger.fine(mName, "System property 'jdbc.driver' not set, using default JDBC driver: " + driver);
142             } else {
143                 logger.fine(mName, "Using JDBC driver: " + driver);
144             }
145             try {
146                 Class.forName(driver);
147             } catch (ClassNotFoundException JavaDoc e) {
148                 try {
149                     ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
150                     if (cl == null) {
151                         throw new ClassNotFoundException JavaDoc(driver);
152                     }
153                     cl.loadClass(driver);
154                 } catch (ClassNotFoundException JavaDoc ex) {
155                     throw e;
156                 }
157             }
158             
159             String JavaDoc url = System.getProperty("jdbc.url");
160             if (url == null) {
161                 url = "jdbc:hsqldb:build/db/db";
162                 logger.fine(mName, "System property 'jdbc.url' not set, using default JDBC url: " + url);
163             } else {
164                 logger.fine(mName, "Using JDBC url: " + url);
165             }
166             
167             String JavaDoc user = System.getProperty("jdbc.user");
168             String JavaDoc password;
169             if (user == null) {
170                 user = "sa";
171                 password = "";
172                 logger.fine(mName, "System property 'jdbc.user' not set, using default JDBC user: " + user);
173             } else {
174                 password = System.getProperty("jdbc.password");
175                 logger.fine(mName, "Using JDBC user: " + user);
176             }
177             
178             connection = DriverManager.getConnection(url, user, password);
179         }
180         return connection;
181     }
182     
183     private String JavaDoc[] stmts = new String JavaDoc[] {
184         "DELETE FROM SUBSUB",
185         "DELETE FROM SUB",
186         "DELETE FROM MAIN",
187         "INSERT INTO MAIN VALUES (456, 'foo Main', 1, '000102030405060708090a0b0c0d0e0f', '2003-06-09')",
188         "INSERT INTO MAIN VALUES (456, 'foo Main 2', 22, 'a0a1a2a3a4a5a6a7a8a9aaabacadaeaf', '2003-06-10')",
189         "INSERT INTO MAIN VALUES (457, 'bar Main', 3, 'f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff', '2003-06-10')",
190         "INSERT INTO SUB VALUES (456, 1, 'Somewhere', '23423423', 'some@where')",
191         "INSERT INTO SUB VALUES (456, 22, 'Somewhere', '23423434', 'some@where')",
192         "INSERT INTO SUB VALUES (456, 22, 'Someone', '23421425', 'some@one')",
193         "INSERT INTO SUB VALUES (457, 3, 'Somewhere', '23423426', 'some@where')",
194         "INSERT INTO SUB VALUES (457, 3, 'Someone', '23421427', 'some@one')",
195         "INSERT INTO SUBSUB VALUES ('324987', 456, 1, '23423423', '2003-06-10 12:00:00')",
196         "INSERT INTO SUBSUB VALUES ('124987', 456, 22, '23423434', '2003-06-10 12:00:01')",
197         "INSERT INTO SUBSUB VALUES ('124988', 456, 22, '23423434', '2003-06-10 12:00:02')",
198         "INSERT INTO SUBSUB VALUES ('124989', 456, 22, '23421425', '2003-06-10 12:00:03')",
199         "INSERT INTO SUBSUB VALUES ('124990', 457, 3, '23423426', '2003-06-10 12:00:04')",
200         "INSERT INTO SUBSUB VALUES ('124991', 457, 3, '23421427', '2003-06-10 12:00:04')"
201     };
202     
203     public void testCreate() throws ClassNotFoundException JavaDoc, SQLException JavaDoc {
204         Connection JavaDoc conn = getConnection();
205         for (int i = 0; i < stmts.length; i++) {
206             PreparedStatement JavaDoc stmt = conn.prepareStatement(stmts[i]);
207             try {
208                 stmt.executeUpdate();
209             } catch (SQLException JavaDoc e) {
210                 if (stmts[i].startsWith("DROP TABLE ") && "S0002".equals(e.getSQLState())) {
211                     continue;
212                 }
213                 throw e;
214             }
215         }
216     }
217     
218     public void testClone() throws SQLException JavaDoc, ClassNotFoundException JavaDoc {
219         Connection JavaDoc conn = getConnection();
220         Object JavaDoc[] values = new Object JavaDoc[]{new Long JavaDoc(456), new Integer JavaDoc(1)};
221         Object JavaDoc[] updatedValues = (new MAINCloner()).clone(conn, values);
222         assertEquals(456, ((Long JavaDoc) updatedValues[0]).longValue());
223         assertEquals(2, ((Integer JavaDoc) updatedValues[2]).intValue());
224     }
225 }
226
Popular Tags