001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.core.utility;
066:
067: import com.jcorporate.expresso.core.db.DBConnection;
068: import com.jcorporate.expresso.core.db.DBConnectionPool;
069: import com.jcorporate.expresso.core.db.DBException;
070: import com.jcorporate.expresso.core.dbobj.DBObject;
071: import com.jcorporate.expresso.core.dbobj.Schema;
072: import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
073: import com.jcorporate.expresso.kernel.ConsoleInstallLog;
074: import com.jcorporate.expresso.kernel.InstallLog;
075: import com.jcorporate.expresso.services.dbobj.SchemaList;
076: import com.jcorporate.expresso.services.test.ExpressoTestCase;
077: import com.jcorporate.expresso.services.test.SchemaDeleter;
078: import com.jcorporate.expresso.services.test.TestSystemInitializer;
079: import junit.framework.TestSuite;
080:
081: import java.io.ByteArrayInputStream;
082: import java.io.ByteArrayOutputStream;
083: import java.io.IOException;
084: import java.io.ObjectInputStream;
085: import java.io.ObjectOutputStream;
086: import java.util.Enumeration;
087: import java.util.TreeSet;
088: import java.util.Vector;
089:
090: /**
091: * What this does:
092: * <p/>
093: * Runs DBCreate on the schemas and makes sure that no exceptions are thrown
094: * while creating.
095: * <p/>
096: * NOTE: Derive a class from this class to test all derived schemas.
097: *
098: * @author Michael Rimov
099: */
100: public class DBToolTests extends ExpressoTestCase {
101:
102: /**
103: * Add schemas to this list to verify other schemas
104: */
105: protected Vector schemaList = new Vector();
106: static private boolean alreadyCleared = false;
107: static private boolean failedOne = false;
108: private InstallLog installLog = new ConsoleInstallLog();
109:
110: public DBToolTests(String name) throws Exception {
111: super (name);
112: }
113:
114: public static void main(String[] args) throws java.lang.Exception {
115:
116: //Set the system properties we need
117: junit.textui.TestRunner.run(suite());
118: }
119:
120: protected boolean isAlreadyCleared() {
121: return alreadyCleared;
122: }
123:
124: protected void setAlreadyCleared(boolean newValue) {
125: alreadyCleared = newValue;
126: }
127:
128: protected boolean isFailedOnce() {
129: return failedOne;
130: }
131:
132: protected void setFailedOnce(boolean newValue) {
133: failedOne = newValue;
134: }
135:
136: /**
137: * Creates the test cases. Order is important, thus the manual
138: * creation.
139: * @throws Exception upon construction error.
140: * @return constructed Test suite.
141: */
142: public static junit.framework.Test suite() throws Exception {
143:
144: //Order Matters here
145: TestSuite ts = new TestSuite("Schema Verification Tests");
146: ts.addTest(new DBToolTests("testCreate"));
147: ts.addTest(new DBToolTests("testAddingSchemas"));
148: ts.addTest(new DBToolTests("testSetupSecurity"));
149: ts.addTest(new DBToolTests("testPopulateTables"));
150: ts.addTest(new DBToolTests("testSetupConfigValues"));
151: ts.addTest(new DBToolTests("testOtherSetups"));
152: ts.addTest(new DBToolTests("testVerify"));
153:
154: return ts;
155: }
156:
157: /**
158: * Runs the createTables() on the schema list to make sure no exceptions
159: * are getting thrown
160: */
161: public void testCreate() {
162: try {
163: DBTool.createTables(installLog, schemaList,
164: TestSystemInitializer.getTestContext());
165: DBTool.compareTables(installLog, schemaList,
166: TestSystemInitializer.getTestContext());
167: } catch (Exception e) {
168: System.out.println(e.getMessage());
169: e.printStackTrace();
170: this .setFailedOnce(true);
171: fail(e.getMessage());
172: }
173: }
174:
175: /**
176: * If testing multiple schemas, this will make sure that the schema has
177: * been added to the dbtable when created.
178: */
179: public void testAddingSchemas() {
180: try {
181: if (!this .isFailedOnce()) {
182: for (Enumeration e = schemaList.elements(); e
183: .hasMoreElements();) {
184: Schema s = (Schema) e.nextElement();
185:
186: if (!s
187: .getClass()
188: .getName()
189: .equals(
190: "com.jcorporate.expresso.core.ExpressoSchema")) {
191: SchemaList sl = new SchemaList(
192: SecuredDBObject.SYSTEM_ACCOUNT);
193: sl.setDataContext(TestSystemInitializer
194: .getTestContext());
195: sl.setField("SchemaClass", s.getClass()
196: .getName());
197: sl.setField("Descrip", s
198: .getDefaultDescription());
199: sl.setField("ComponentCode", s
200: .getDefaultComponentCode());
201: sl.add();
202: }
203: }
204: } else {
205: fail("Already Failed Earlier Test - Unable to continue");
206: }
207: } catch (Exception e) {
208: System.out.println(e.getMessage());
209: e.printStackTrace();
210: this .setFailedOnce(true);
211: fail(e.getMessage());
212: }
213: }
214:
215: /**
216: * Runs the populateTables() on the schema list to make sure no exceptions
217: * are getting thrown
218: */
219: public void testPopulateTables() {
220: try {
221: if (!this .isFailedOnce()) {
222: DBTool.populateTables(installLog, schemaList,
223: TestSystemInitializer.getTestContext());
224: } else {
225: fail("Already Failed Earlier Test - Unable to continue");
226: }
227: } catch (Exception e) {
228: System.out.println(e.getMessage());
229: e.printStackTrace();
230:
231: //Failed Default Values don't necessarily mean failed other tests
232: // failedOne = true;
233: fail(e.getMessage());
234: }
235: }
236:
237: /**
238: * Runs the setupSecurity() on the schema list to make sure no exceptions
239: * are getting thrown
240: */
241: public void testSetupSecurity() {
242: try {
243: if (!this .isFailedOnce()) {
244: DBTool.setupSecurity(installLog, schemaList,
245: TestSystemInitializer.getTestContext());
246: } else {
247: fail("Already Failed Earlier Test - Unable to continue");
248: }
249: } catch (Exception e) {
250: System.out.println(e.getMessage());
251: e.printStackTrace();
252: this .setFailedOnce(true);
253: fail(e.getMessage());
254: }
255: }
256:
257: /**
258: * Runs through setting up the configuration values for a particular schema
259: */
260: public void testSetupConfigValues() {
261: try {
262: if (!this .isFailedOnce()) {
263: DBTool.setupConfig(installLog, schemaList,
264: TestSystemInitializer.getTestContext());
265: } else {
266: fail("Already Failed Earlier Test - Unable to continue");
267: }
268: } catch (Exception e) {
269: System.out.println(e.getMessage());
270: e.printStackTrace();
271: this .setFailedOnce(true);
272: fail(e.getMessage());
273: }
274: }
275:
276: /**
277: * Runs the othersetups() on the schema list to make sure no exceptions
278: * are getting thrown
279: */
280: public void testOtherSetups() {
281: try {
282: if (!this .isFailedOnce()) {
283: DBTool.otherSetups(installLog, schemaList,
284: TestSystemInitializer.getTestContext());
285: } else {
286: fail("Already Failed Earlier Test - Unable to continue");
287: }
288: } catch (Exception e) {
289: System.out.println(e.getMessage());
290: e.printStackTrace();
291: this .setFailedOnce(true);
292: fail(e.getMessage());
293: }
294: }
295:
296: /**
297: * Call verify on all the dbobjects. Code is borrowed from DBTool
298: * <p/>
299: * Also checks for unique table names. More than one DBOBject shouldn't be
300: * pointing to the same table name.
301: */
302: public void testVerify() {
303: TreeSet tableNames = new TreeSet();
304: DBConnectionPool myPool = null;
305: DBConnection myConnection = null;
306:
307: try {
308: if (!this .isFailedOnce()) {
309: assertTrue("schemaList.size() > 0",
310: schemaList.size() > 0);
311:
312: Schema this Schema = null;
313:
314: for (Enumeration als = schemaList.elements(); als
315: .hasMoreElements();) {
316: this Schema = (Schema) als.nextElement();
317: System.out.println("Verifying Schema "
318: + this Schema.getClass().getName());
319:
320: DBObject oneObject = null;
321:
322: for (Enumeration dbo = this Schema.getMembers(); dbo
323: .hasMoreElements();) {
324: oneObject = (DBObject) dbo.nextElement();
325: oneObject.setDataContext(TestSystemInitializer
326: .getTestContext());
327:
328: //Check for unique table name
329: if (tableNames.contains(oneObject
330: .getJDBCMetaData().getTargetTable())) {
331:
332: //
333: //We have a special case for SingleDBUserInfo since it
334: //is a known duplicate table. Under Normal Circumstances
335: //this should not be the case.
336: //
337: if (oneObject
338: .getClass()
339: .getName()
340: .equals(
341: com.jcorporate.expresso.services.dbobj.DefaultUserInfo.class
342: .getName())
343: || oneObject
344: .getClass()
345: .getName()
346: .equals(
347: com.jcorporate.expresso.ext.dbobj.SingleDBUserInfo.class
348: .getName())) {
349: } else {
350: fail("Table name: "
351: + oneObject.getJDBCMetaData()
352: .getTargetTable()
353: + " already exists due to another DBObject. Offending DBObject"
354: + oneObject.getClass()
355: .getName());
356: }
357: } else {
358: tableNames.add(oneObject.getJDBCMetaData()
359: .getTargetTable());
360: }
361: //oneObject.setConnection(myConnection);
362: try {
363: oneObject.verify();
364: } catch (DBException de) {
365: myPool.release(myConnection);
366: System.out.println(de.getMessage());
367: de.printStackTrace();
368: fail("Error in object "
369: + oneObject.getJDBCMetaData()
370: .getName() + ":"
371: + de.getMessage());
372: }
373: } /* for each database object */
374:
375: }
376:
377: // myPool.release(myConnection);
378: } else { /* for each schema in the list */
379: fail();
380: }
381: } catch (Exception e) {
382: System.out.println(e.getMessage());
383: e.printStackTrace();
384: this .setFailedOnce(true);
385:
386: if (myPool != null && myConnection != null) {
387: myPool.release(myConnection);
388: }
389:
390: fail(e.getMessage());
391: }
392: }
393:
394: /**
395: * This testcase makes sure that all DBObjects within all the schemas are
396: * serializable. Helps insure that there aren't any special custom "types"
397: * that might be causing troubles.
398: */
399: public void testSerialization() {
400: try {
401: assertTrue("schemaList.size() > 0", schemaList.size() > 0);
402:
403: Schema this Schema = null;
404:
405: for (Enumeration als = schemaList.elements(); als
406: .hasMoreElements();) {
407: this Schema = (Schema) als.nextElement();
408: System.out.println("Testing Schema Serialization: "
409: + this Schema.getClass().getName());
410:
411: DBObject oneObject = null;
412:
413: for (Enumeration dbo = this Schema.getMembers(); dbo
414: .hasMoreElements();) {
415: oneObject = (DBObject) dbo.nextElement();
416: oneObject.setDataContext(TestSystemInitializer
417: .getTestContext());
418:
419: //oneObject.setConnection(myConnection);
420: try {
421: DBObject secondObject = (DBObject) runThroughSerializer(oneObject);
422: } catch (IOException ioe) {
423: System.out.println(ioe.getMessage());
424: ioe.printStackTrace();
425: fail("Error serializing object "
426: + oneObject.getJDBCMetaData().getName()
427: + ":" + ioe.getMessage());
428: } catch (DBException de) {
429: System.out.println(de.getMessage());
430: de.printStackTrace();
431: fail("Error in object "
432: + oneObject.getJDBCMetaData().getName()
433: + ":" + de.getMessage());
434: }
435: } /* for each database object */
436:
437: } /* for each schema in the list */
438:
439: } catch (IOException ioe) {
440: ioe.printStackTrace();
441: fail("Failed Serialization: " + ioe.getMessage());
442: } catch (Exception e) {
443: System.out.println(e.getMessage());
444: e.printStackTrace();
445: fail(e.getMessage());
446: }
447: }
448:
449: /**
450: * Helper functino that serializes a DBOBject to a memory stream and then
451: * reads a new instance out from the stream.
452: * @param testObject the serializer test case.
453: * @throws java.lang.Exception upon serialization exception.
454: * @return created dbobject after serializing through a byte array
455: * output stream.
456: */
457: private DBObject runThroughSerializer(DBObject testObject)
458: throws Exception {
459: ByteArrayOutputStream bos = new ByteArrayOutputStream();
460: ObjectOutputStream oos = new ObjectOutputStream(bos);
461: oos.writeObject(testObject);
462:
463: ByteArrayInputStream bis = new ByteArrayInputStream(bos
464: .toByteArray());
465: ObjectInputStream ois = new ObjectInputStream(bis);
466:
467: return (DBObject) ois.readObject();
468: }
469:
470: protected void setUp() throws Exception {
471: DBConnectionPool.reInitialize();
472: super .setUp();
473:
474: Class c = Class
475: .forName("com.jcorporate.expresso.core.ExpressoSchema");
476: schemaList.add(c.newInstance());
477:
478: if (this .isAlreadyCleared() == false) {
479: SchemaDeleter.setSchemas(schemaList);
480: SchemaDeleter.deleteSchemas(TestSystemInitializer
481: .getTestContext());
482: this .setAlreadyCleared(true);
483: }
484: }
485: }
|