KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > utility > DBToolTests


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.utility;
66
67 import com.jcorporate.expresso.core.db.DBConnection;
68 import com.jcorporate.expresso.core.db.DBConnectionPool;
69 import com.jcorporate.expresso.core.db.DBException;
70 import com.jcorporate.expresso.core.dbobj.DBObject;
71 import com.jcorporate.expresso.core.dbobj.Schema;
72 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
73 import com.jcorporate.expresso.kernel.ConsoleInstallLog;
74 import com.jcorporate.expresso.kernel.InstallLog;
75 import com.jcorporate.expresso.services.dbobj.SchemaList;
76 import com.jcorporate.expresso.services.test.ExpressoTestCase;
77 import com.jcorporate.expresso.services.test.SchemaDeleter;
78 import com.jcorporate.expresso.services.test.TestSystemInitializer;
79 import junit.framework.TestSuite;
80
81 import java.io.ByteArrayInputStream JavaDoc;
82 import java.io.ByteArrayOutputStream JavaDoc;
83 import java.io.IOException JavaDoc;
84 import java.io.ObjectInputStream JavaDoc;
85 import java.io.ObjectOutputStream JavaDoc;
86 import java.util.Enumeration JavaDoc;
87 import java.util.TreeSet JavaDoc;
88 import java.util.Vector JavaDoc;
89
90
91 /**
92  * What this does:
93  * <p/>
94  * Runs DBCreate on the schemas and makes sure that no exceptions are thrown
95  * while creating.
96  * <p/>
97  * NOTE: Derive a class from this class to test all derived schemas.
98  *
99  * @author Michael Rimov
100  */

101 public class DBToolTests
102         extends ExpressoTestCase {
103
104     /**
105      * Add schemas to this list to verify other schemas
106      */

107     protected Vector JavaDoc schemaList = new Vector JavaDoc();
108     static private boolean alreadyCleared = false;
109     static private boolean failedOne = false;
110     private InstallLog installLog = new ConsoleInstallLog();
111
112     public DBToolTests(String JavaDoc name)
113             throws Exception JavaDoc {
114         super(name);
115     }
116
117     public static void main(String JavaDoc[] args)
118             throws java.lang.Exception JavaDoc {
119
120         //Set the system properties we need
121
junit.textui.TestRunner.run(suite());
122     }
123
124     protected boolean isAlreadyCleared() {
125         return alreadyCleared;
126     }
127
128     protected void setAlreadyCleared(boolean newValue) {
129         alreadyCleared = newValue;
130     }
131
132     protected boolean isFailedOnce() {
133         return failedOne;
134     }
135
136     protected void setFailedOnce(boolean newValue) {
137         failedOne = newValue;
138     }
139
140
141
142     /**
143      * Creates the test cases. Order is important, thus the manual
144      * creation.
145      * @throws Exception upon construction error.
146      * @return constructed Test suite.
147      */

148     public static junit.framework.Test suite()
149             throws Exception JavaDoc {
150
151         //Order Matters here
152
TestSuite ts = new TestSuite("Schema Verification Tests");
153         ts.addTest(new DBToolTests("testCreate"));
154         ts.addTest(new DBToolTests("testAddingSchemas"));
155         ts.addTest(new DBToolTests("testSetupSecurity"));
156         ts.addTest(new DBToolTests("testPopulateTables"));
157         ts.addTest(new DBToolTests("testSetupConfigValues"));
158         ts.addTest(new DBToolTests("testOtherSetups"));
159         ts.addTest(new DBToolTests("testVerify"));
160
161         return ts;
162     }
163
164     /**
165      * Runs the createTables() on the schema list to make sure no exceptions
166      * are getting thrown
167      */

168     public void testCreate() {
169         try {
170             DBTool.createTables(installLog, schemaList, TestSystemInitializer.getTestContext());
171             DBTool.compareTables(installLog, schemaList, TestSystemInitializer.getTestContext());
172         } catch (Exception JavaDoc e) {
173             System.out.println(e.getMessage());
174             e.printStackTrace();
175             this.setFailedOnce(true);
176             fail(e.getMessage());
177         }
178     }
179
180     /**
181      * If testing multiple schemas, this will make sure that the schema has
182      * been added to the dbtable when created.
183      */

184     public void testAddingSchemas() {
185         try {
186             if (!this.isFailedOnce()) {
187                 for (Enumeration JavaDoc e = schemaList.elements();
188                      e.hasMoreElements();) {
189                     Schema s = (Schema) e.nextElement();
190
191                     if (!s.getClass().getName().equals("com.jcorporate.expresso.core.ExpressoSchema")) {
192                         SchemaList sl = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT);
193                         sl.setDataContext(TestSystemInitializer.getTestContext());
194                         sl.setField("SchemaClass", s.getClass().getName());
195                         sl.setField("Descrip", s.getDefaultDescription());
196                         sl.setField("ComponentCode",
197                                 s.getDefaultComponentCode());
198                         sl.add();
199                     }
200                 }
201             } else {
202                 fail("Already Failed Earlier Test - Unable to continue");
203             }
204         } catch (Exception JavaDoc e) {
205             System.out.println(e.getMessage());
206             e.printStackTrace();
207             this.setFailedOnce(true);
208             fail(e.getMessage());
209         }
210     }
211
212     /**
213      * Runs the populateTables() on the schema list to make sure no exceptions
214      * are getting thrown
215      */

216     public void testPopulateTables() {
217         try {
218             if (!this.isFailedOnce()) {
219                 DBTool.populateTables(installLog, schemaList, TestSystemInitializer.getTestContext());
220             } else {
221                 fail("Already Failed Earlier Test - Unable to continue");
222             }
223         } catch (Exception JavaDoc e) {
224             System.out.println(e.getMessage());
225             e.printStackTrace();
226
227             //Failed Default Values don't necessarily mean failed other tests
228
// failedOne = true;
229
fail(e.getMessage());
230         }
231     }
232
233     /**
234      * Runs the setupSecurity() on the schema list to make sure no exceptions
235      * are getting thrown
236      */

237     public void testSetupSecurity() {
238         try {
239             if (!this.isFailedOnce()) {
240                 DBTool.setupSecurity(installLog, schemaList, TestSystemInitializer.getTestContext());
241             } else {
242                 fail("Already Failed Earlier Test - Unable to continue");
243             }
244         } catch (Exception JavaDoc e) {
245             System.out.println(e.getMessage());
246             e.printStackTrace();
247             this.setFailedOnce(true);
248             fail(e.getMessage());
249         }
250     }
251
252     /**
253      * Runs through setting up the configuration values for a particular schema
254      */

255     public void testSetupConfigValues() {
256         try {
257             if (!this.isFailedOnce()) {
258                 DBTool.setupConfig(installLog, schemaList, TestSystemInitializer.getTestContext());
259             } else {
260                 fail("Already Failed Earlier Test - Unable to continue");
261             }
262         } catch (Exception JavaDoc e) {
263             System.out.println(e.getMessage());
264             e.printStackTrace();
265             this.setFailedOnce(true);
266             fail(e.getMessage());
267         }
268     }
269
270     /**
271      * Runs the othersetups() on the schema list to make sure no exceptions
272      * are getting thrown
273      */

274     public void testOtherSetups() {
275         try {
276             if (!this.isFailedOnce()) {
277                 DBTool.otherSetups(installLog, schemaList, TestSystemInitializer.getTestContext());
278             } else {
279                 fail("Already Failed Earlier Test - Unable to continue");
280             }
281         } catch (Exception JavaDoc e) {
282             System.out.println(e.getMessage());
283             e.printStackTrace();
284             this.setFailedOnce(true);
285             fail(e.getMessage());
286         }
287     }
288
289     /**
290      * Call verify on all the dbobjects. Code is borrowed from DBTool
291      * <p/>
292      * Also checks for unique table names. More than one DBOBject shouldn't be
293      * pointing to the same table name.
294      */

295     public void testVerify() {
296         TreeSet JavaDoc tableNames = new TreeSet JavaDoc();
297         DBConnectionPool myPool = null;
298         DBConnection myConnection = null;
299
300         try {
301             if (!this.isFailedOnce()) {
302                 assertTrue("schemaList.size() > 0", schemaList.size() > 0);
303
304                 Schema thisSchema = null;
305
306                 for (Enumeration JavaDoc als = schemaList.elements();
307                      als.hasMoreElements();) {
308                     thisSchema = (Schema) als.nextElement();
309                     System.out.println("Verifying Schema " +
310                             thisSchema.getClass().getName());
311
312                     DBObject oneObject = null;
313
314                     for (Enumeration JavaDoc dbo = thisSchema.getMembers();
315                          dbo.hasMoreElements();) {
316                         oneObject = (DBObject) dbo.nextElement();
317                         oneObject.setDataContext(TestSystemInitializer.getTestContext());
318
319                         //Check for unique table name
320
if (tableNames.contains(oneObject.getJDBCMetaData().getTargetTable())) {
321
322                             //
323
//We have a special case for SingleDBUserInfo since it
324
//is a known duplicate table. Under Normal Circumstances
325
//this should not be the case.
326
//
327
if (oneObject.getClass().getName().equals(
328                                     com.jcorporate.expresso.services.dbobj.DefaultUserInfo.class.getName()) ||
329                                     oneObject.getClass().getName().equals(
330                                             com.jcorporate.expresso.ext.dbobj.SingleDBUserInfo.class.getName())) {
331                             } else {
332                                 fail("Table name: " +
333                                         oneObject.getJDBCMetaData().getTargetTable() +
334                                         " already exists due to another DBObject. Offending DBObject" +
335                                         oneObject.getClass().getName());
336                             }
337                         } else {
338                             tableNames.add(oneObject.getJDBCMetaData().getTargetTable());
339                         }
340                         //oneObject.setConnection(myConnection);
341
try {
342                             oneObject.verify();
343                         } catch (DBException de) {
344                             myPool.release(myConnection);
345                             System.out.println(de.getMessage());
346                             de.printStackTrace();
347                             fail("Error in object " + oneObject.getJDBCMetaData().getName() +
348                                     ":" + de.getMessage());
349                         }
350                     } /* for each database object */
351
352                 }
353
354                 // myPool.release(myConnection);
355
} else { /* for each schema in the list */
356                 fail();
357             }
358         } catch (Exception JavaDoc e) {
359             System.out.println(e.getMessage());
360             e.printStackTrace();
361             this.setFailedOnce(true);
362
363             if (myPool != null && myConnection != null) {
364                 myPool.release(myConnection);
365             }
366
367             fail(e.getMessage());
368         }
369     }
370
371     /**
372      * This testcase makes sure that all DBObjects within all the schemas are
373      * serializable. Helps insure that there aren't any special custom "types"
374      * that might be causing troubles.
375      */

376     public void testSerialization() {
377         try {
378             assertTrue("schemaList.size() > 0", schemaList.size() > 0);
379
380             Schema thisSchema = null;
381
382             for (Enumeration JavaDoc als = schemaList.elements();
383                  als.hasMoreElements();) {
384                 thisSchema = (Schema) als.nextElement();
385                 System.out.println("Testing Schema Serialization: " +
386                         thisSchema.getClass().getName());
387
388                 DBObject oneObject = null;
389
390                 for (Enumeration JavaDoc dbo = thisSchema.getMembers();
391                      dbo.hasMoreElements();) {
392                     oneObject = (DBObject) dbo.nextElement();
393                     oneObject.setDataContext(TestSystemInitializer.getTestContext());
394
395                     //oneObject.setConnection(myConnection);
396
try {
397                         DBObject secondObject = (DBObject) runThroughSerializer(oneObject);
398                     } catch (IOException JavaDoc ioe) {
399                         System.out.println(ioe.getMessage());
400                         ioe.printStackTrace();
401                         fail("Error serializing object " +
402                                 oneObject.getJDBCMetaData().getName() + ":" + ioe.getMessage());
403                     } catch (DBException de) {
404                         System.out.println(de.getMessage());
405                         de.printStackTrace();
406                         fail("Error in object " + oneObject.getJDBCMetaData().getName() + ":" +
407                                 de.getMessage());
408                     }
409                 } /* for each database object */
410
411             } /* for each schema in the list */
412
413         } catch (IOException JavaDoc ioe) {
414             ioe.printStackTrace();
415             fail("Failed Serialization: " + ioe.getMessage());
416         } catch (Exception JavaDoc e) {
417             System.out.println(e.getMessage());
418             e.printStackTrace();
419             fail(e.getMessage());
420         }
421     }
422
423     /**
424      * Helper functino that serializes a DBOBject to a memory stream and then
425      * reads a new instance out from the stream.
426      * @param testObject the serializer test case.
427      * @throws java.lang.Exception upon serialization exception.
428      * @return created dbobject after serializing through a byte array
429      * output stream.
430      */

431     private DBObject runThroughSerializer(DBObject testObject)
432             throws Exception JavaDoc {
433         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
434         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(bos);
435         oos.writeObject(testObject);
436
437         ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(bos.toByteArray());
438         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bis);
439
440         return (DBObject) ois.readObject();
441     }
442
443     protected void setUp()
444             throws Exception JavaDoc {
445         DBConnectionPool.reInitialize();
446         super.setUp();
447
448         Class JavaDoc c = Class.forName("com.jcorporate.expresso.core.ExpressoSchema");
449         schemaList.add(c.newInstance());
450
451         if (this.isAlreadyCleared() == false) {
452             SchemaDeleter.setSchemas(schemaList);
453             SchemaDeleter.deleteSchemas(TestSystemInitializer.getTestContext());
454             this.setAlreadyCleared(true);
455         }
456     }
457 }
458
Popular Tags