[cvs] expresso commit by rimovm: API Refactoring: New
ExpressoThread and
JCorporate Ltd
jcorp at jcorp2.servlets.net
Tue Nov 9 07:19:23 PST 2004
Log Message:
-----------
API Refactoring: New ExpressoThread and ExpressoThreadContext class to allow for propagation of the RequestRegistry across threads.
New Unit Tests to verify it.
Unit tests for RowSecuredDBObject.
Moved to only access the RequestRegistry during getDataContext() for DBObjects.
Modified Files:
--------------
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/cache:
CacheCleaner.java
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dbobj:
RowSecuredDBObject.java
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/job:
Job.java
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/logging:
LogHandler.java
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/misc:
EventHandler.java
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/registry:
RequestRegistry.java
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/security:
User.java
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/utility:
JobHandler.java
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/ext/controller:
RunTestThread.java
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/ext/tests:
StressThread.java
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/services/asyncprocess:
ProcessThread.java
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/services/crontab:
CronDaemon.java
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/services/job:
ClearOldValidation.java
ControllerJob.java
ValidationJob.java
expresso/expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core:
ExpressoTestSuite.java
expresso/expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/cache:
CacheTestThread.java
expresso/expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/registry:
TestMutableRequestRegistry.java
Added Files:
-----------
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/registry:
ExpressoThread.java
ExpressoThreadContext.java
expresso/expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/registry:
ExpressoThreadTestModel.java
TestExpressoThread.java
expresso/expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/dbobj/tests:
AutoDBObjTest.java
DBIndexTest.java
DBObjectTest.java
MultiDBObjectTest.java
OtherDBTest.java
RowSecuredDBObjectTest.java
RowSecuredDBObjectTest1.java
RowSecuredDBObjectTestSchema.java
SerializationTest.java
Test1.java
Test2.java
TestSchema.java
Removed Files:
-------------
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dbobj/tests:
AutoDBObjTest.java
DBIndexTest.java
DBObjectTest.java
MultiDBObjectTest.java
OtherDBTest.java
SerializationTest.java
Test1.java
Test2.java
TestSchema.java
Revision Data
-------------
Index: CacheCleaner.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/cache/CacheCleaner.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/core/cache/CacheCleaner.java -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/core/cache/CacheCleaner.java -u -r1.10 -r1.11
--- expresso-web/WEB-INF/src/com/jcorporate/expresso/core/cache/CacheCleaner.java
+++ expresso-web/WEB-INF/src/com/jcorporate/expresso/core/cache/CacheCleaner.java
@@ -63,13 +63,11 @@
*/
package com.jcorporate.expresso.core.cache;
+import java.util.Iterator;
import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArrayList;
+import com.jcorporate.expresso.core.registry.ExpressoThread;
import org.apache.log4j.Logger;
-import java.util.Iterator;
-import com.jcorporate.expresso.core.security.SuperUser;
-import com.jcorporate.expresso.core.registry.MutableRequestRegistry;
-
/**
* This thread is initialized by CacheManager and slowly iterates through the
* Caches clearing out expired items. It often is given a lower thread priority
@@ -94,7 +92,7 @@
* @version $Revision$ on $Date$
*/
-public class CacheCleaner extends Thread {
+public class CacheCleaner extends ExpressoThread {
private static final transient Logger log = Logger.getLogger(CacheCleaner.class);
private CopyOnWriteArrayList cacheSystems = new CopyOnWriteArrayList();
@@ -125,15 +123,11 @@
* automatically runs every 30 seconds.
*/
public void run() {
+ super.run();
+
if (log.isDebugEnabled()) {
log.debug("Starting Cache Cleaning");
}
-
- //Since we're in a new thread, we have to give it context, even
- //if it is inside superuser.
- new MutableRequestRegistry("default"
- ,SuperUser.SUPER_USER);
-
while (!this.isInterrupted() && !done) {
for (Iterator i = cacheSystems.iterator(); i.hasNext();) {
Index: RunTestThread.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/ext/controller/RunTestThread.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/ext/controller/RunTestThread.java -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/ext/controller/RunTestThread.java -u -r1.6 -r1.7
--- expresso-web/WEB-INF/src/com/jcorporate/expresso/ext/controller/RunTestThread.java
+++ expresso-web/WEB-INF/src/com/jcorporate/expresso/ext/controller/RunTestThread.java
@@ -69,6 +69,7 @@
import java.util.Enumeration;
import java.util.Vector;
+import com.jcorporate.expresso.core.registry.ExpressoThread;
/**
@@ -79,7 +80,7 @@
* @author Michael Nash
*/
public class RunTestThread
- extends Thread {
+ extends ExpressoThread {
private Vector setsToRun = null;
private int repeats = 1;
private PerfTestResult setResults = new PerfTestResult();
@@ -115,6 +116,7 @@
}
public void run() {
+ super.run();
String currentSessionId = null;
PerfTestResult overallResult = new PerfTestResult();
@@ -157,4 +159,4 @@
} /* RunTestThread */
-/* RunTestThread */
\ No newline at end of file
+/* RunTestThread */
Index: StressThread.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/ext/tests/StressThread.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/ext/tests/StressThread.java -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/ext/tests/StressThread.java -u -r1.9 -r1.10
--- expresso-web/WEB-INF/src/com/jcorporate/expresso/ext/tests/StressThread.java
+++ expresso-web/WEB-INF/src/com/jcorporate/expresso/ext/tests/StressThread.java
@@ -69,6 +69,7 @@
import java.util.ArrayList;
import java.util.Iterator;
+import com.jcorporate.expresso.core.registry.ExpressoThread;
/**
@@ -81,7 +82,7 @@
* @version $Revision$ $Date$
*/
public class StressThread
- extends Thread {
+ extends ExpressoThread {
static final int NUM_RECORDS = 60;
ArrayList myIds = new ArrayList(NUM_RECORDS);
int numLoops;
@@ -99,6 +100,7 @@
}
public void run() {
+ super.run();
try {
for (int i = 0; i < numLoops; i++) {
addTestObjects();
@@ -194,7 +196,7 @@
sto.delete();
}
}
-
+
myIds = new ArrayList(NUM_RECORDS);
}
-}
\ No newline at end of file
+}
Index: ProcessThread.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/services/asyncprocess/ProcessThread.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/services/asyncprocess/ProcessThread.java -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/services/asyncprocess/ProcessThread.java -u -r1.4 -r1.5
--- expresso-web/WEB-INF/src/com/jcorporate/expresso/services/asyncprocess/ProcessThread.java
+++ expresso-web/WEB-INF/src/com/jcorporate/expresso/services/asyncprocess/ProcessThread.java
@@ -72,8 +72,9 @@
import java.util.LinkedList;
import com.jcorporate.expresso.core.security.SuperUser;
import com.jcorporate.expresso.core.registry.MutableRequestRegistry;
+import com.jcorporate.expresso.core.registry.ExpressoThread;
-public class ProcessThread extends Thread {
+public class ProcessThread extends ExpressoThread {
private boolean done = false;
/**
@@ -122,14 +123,7 @@
* we exit.
*/
public void run() {
-
- /**
- * @todo This might eventually become a security issue to
- * malevolent coding practices. We need to be able to set
- * the request for the process.
- */
- new MutableRequestRegistry("default"
- ,SuperUser.SUPER_USER);
+ super.run();
LinkedList queue = parent.getQueue();
while (!done) {
Index: CronDaemon.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/services/crontab/CronDaemon.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/services/crontab/CronDaemon.java -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/services/crontab/CronDaemon.java -u -r1.11 -r1.12
--- expresso-web/WEB-INF/src/com/jcorporate/expresso/services/crontab/CronDaemon.java
+++ expresso-web/WEB-INF/src/com/jcorporate/expresso/services/crontab/CronDaemon.java
@@ -66,6 +66,7 @@
import com.jcorporate.expresso.core.security.SuperUser;
import com.jcorporate.expresso.core.registry.MutableRequestRegistry;
+import com.jcorporate.expresso.core.registry.ExpressoThread;
/**
@@ -84,7 +85,7 @@
public CronDaemon(Crontab mgr, boolean isDaemon, String waiterName) {
this.mgr = mgr;
- thread = new Thread(this, waiterName);
+ thread = new ExpressoThread(this, waiterName);
thread.setDaemon(isDaemon);
thread.start();
}
Index: ValidationJob.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/services/job/ValidationJob.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/services/job/ValidationJob.java -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/services/job/ValidationJob.java -u -r1.11 -r1.12
--- expresso-web/WEB-INF/src/com/jcorporate/expresso/services/job/ValidationJob.java
+++ expresso-web/WEB-INF/src/com/jcorporate/expresso/services/job/ValidationJob.java
@@ -174,8 +174,7 @@
*
*/
public void run() {
- // remove super call to run; does nothing, right? 7/2003 Larry Hamel
-// super.run();
+ super.run();
//This is the default description of the job, we try to get a more specific one later
String desc = getTitle();
@@ -224,4 +223,4 @@
}
}
-}
\ No newline at end of file
+}
Index: ControllerJob.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/services/job/ControllerJob.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/services/job/ControllerJob.java -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/services/job/ControllerJob.java -u -r1.12 -r1.13
--- expresso-web/WEB-INF/src/com/jcorporate/expresso/services/job/ControllerJob.java
+++ expresso-web/WEB-INF/src/com/jcorporate/expresso/services/job/ControllerJob.java
@@ -104,6 +104,7 @@
* outputs generated in the "finish" email.
*/
public void run() {
+ super.run();
try {
StringBuffer mailMsg = new StringBuffer("");
String controllerName = StringUtil.notNull(getJobParameter(
Index: ClearOldValidation.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/services/job/ClearOldValidation.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/services/job/ClearOldValidation.java -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/services/job/ClearOldValidation.java -u -r1.2 -r1.3
--- expresso-web/WEB-INF/src/com/jcorporate/expresso/services/job/ClearOldValidation.java
+++ expresso-web/WEB-INF/src/com/jcorporate/expresso/services/job/ClearOldValidation.java
@@ -109,6 +109,7 @@
* Deletes all expired validation queue entries.
*/
public void run() {
+ super.run();
try {
if (log.isInfoEnabled()) {
log.info("Beginning job " + THIS_CLASS);
Index: ExpressoTestSuite.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/ExpressoTestSuite.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -Lexpresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/ExpressoTestSuite.java -Lexpresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/ExpressoTestSuite.java -u -r1.1 -r1.2
--- expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/ExpressoTestSuite.java
+++ expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/ExpressoTestSuite.java
@@ -112,13 +112,14 @@
ts.addTestSuite(com.jcorporate.expresso.kernel.util.test.TestLocatorUtils.class);
ts.addTestSuite(com.jcorporate.expresso.core.security.filters.FilterTreeTest.class);
ts.addTestSuite(com.jcorporate.expresso.core.db.tests.DBConnectionPoolTest.class);
- ts.addTestSuite(com.jcorporate.expresso.core.dbobj.tests.AutoDBObjTest.class);
ts.addTestSuite(com.jcorporate.expresso.core.controller.tests.InputTests.class);
ts.addTestSuite(com.jcorporate.expresso.core.controller.tests.OutputTests.class);
ts.addTestSuite(com.jcorporate.expresso.core.controller.tests.TransitionTests.class);
ts.addTestSuite(com.jcorporate.expresso.core.controller.tests.BlockTests.class);
ts.addTestSuite(com.jcorporate.expresso.core.controller.tests.ControllerResponseTests.class);
ts.addTestSuite(com.jcorporate.expresso.core.registry.TestMutableRequestRegistry.class);
+ ts.addTestSuite(com.jcorporate.expresso.core.registry.TestExpressoThread.class);
+ ts.addTestSuite(com.jcorporate.expresso.core.i18n.MessagesTest.class);
ts.addTestSuite(com.jcorporate.expresso.core.misc.tests.TestXmlDocTypeFilter.class);
ts.addTestSuite(com.jcorporate.expresso.core.misc.tests.Base64Test.class);
@@ -133,11 +134,15 @@
ts.addTestSuite(com.jcorporate.expresso.core.security.filters.FilterTest.class);
ts.addTestSuite(com.jcorporate.expresso.core.cache.CacheTest.class);
ts.addTestSuite(com.jcorporate.expresso.core.cache.tests.TTLTest.class);
+
+ //DBObject and related tests.
ts.addTestSuite(com.jcorporate.expresso.core.dbobj.tests.DBObjectTest.class);
ts.addTestSuite(com.jcorporate.expresso.core.dbobj.tests.SerializationTest.class);
ts.addTestSuite(com.jcorporate.expresso.core.dataobjects.test.TestDataTransferObject.class);
ts.addTestSuite(com.jcorporate.expresso.core.dbobj.tests.MultiDBObjectTest.class);
- ts.addTestSuite(com.jcorporate.expresso.core.i18n.MessagesTest.class);
+ ts.addTestSuite(com.jcorporate.expresso.services.dbobj.tests.DBObjSecurityTests.class);
+ ts.addTestSuite(com.jcorporate.expresso.core.dbobj.tests.RowSecuredDBObjectTest.class);
+ ts.addTestSuite(com.jcorporate.expresso.core.dbobj.tests.AutoDBObjTest.class);
ts.addTestSuite(com.jcorporate.expresso.core.security.tests.UserGroupTest.class);
ts.addTestSuite(com.jcorporate.expresso.core.security.tests.UserTest.class);
@@ -145,7 +150,6 @@
ts.addTestSuite(com.jcorporate.expresso.services.crontab.tests.TestCrontabEntry.class);
ts.addTestSuite(com.jcorporate.expresso.services.dbobj.tests.MimeTypesTests.class);
ts.addTestSuite(com.jcorporate.expresso.services.dbobj.tests.SetupTests.class);
- ts.addTestSuite(com.jcorporate.expresso.services.dbobj.tests.DBObjSecurityTests.class);
return ts;
}
Index: CacheTestThread.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/cache/CacheTestThread.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -Lexpresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/cache/CacheTestThread.java -Lexpresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/cache/CacheTestThread.java -u -r1.1 -r1.2
--- expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/cache/CacheTestThread.java
+++ expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/cache/CacheTestThread.java
@@ -71,10 +71,11 @@
*/
import org.apache.log4j.Logger;
+import com.jcorporate.expresso.core.registry.ExpressoThread;
public class CacheTestThread
- extends Thread {
+ extends ExpressoThread {
private static Logger log = Logger.getLogger(CacheTestThread.class);
private String myName = null;
private static final int NUM_ITERATIONS = 30;
@@ -93,6 +94,7 @@
*
*/
public void run() {
+ super.run();
try {
CacheTest myTest = new CacheTest("thread");
myTest.setCacheName(myName);
@@ -133,4 +135,4 @@
} /* CacheTestThread */
-/* CacheTestThread */
\ No newline at end of file
+/* CacheTestThread */
--- /dev/null
+++ expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/registry/ExpressoThreadTestModel.java
@@ -0,0 +1,119 @@
+/* The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
+*
+* Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following conditions
+* are met:
+*
+* 1. Redistributions of source code must retain the above copyright
+* notice, this list of conditions and the following disclaimer.
+*
+* 2. Redistributions in binary form must reproduce the above copyright
+* notice, this list of conditions and the following disclaimer in
+* the documentation and/or other materials provided with the
+* distribution.
+*
+* 3. The end-user documentation included with the redistribution,
+* if any, must include the following acknowledgment:
+* "This product includes software developed by Jcorporate Ltd.
+* (http://www.jcorporate.com/)."
+* Alternately, this acknowledgment may appear in the software itself,
+* if and wherever such third-party acknowledgments normally appear.
+*
+* 4. "Jcorporate" and product names such as "Expresso" must
+* not be used to endorse or promote products derived from this
+* software without prior written permission. For written permission,
+* please contact info at jcorporate.com.
+*
+* 5. Products derived from this software may not be called "Expresso",
+* or other Jcorporate product names; nor may "Expresso" or other
+* Jcorporate product names appear in their name, without prior
+* written permission of Jcorporate Ltd.
+*
+* 6. No product derived from this software may compete in the same
+* market space, i.e. framework, without prior written permission
+* of Jcorporate Ltd. For written permission, please contact
+* partners at jcorporate.com.
+*
+* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+* DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
+* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+* SUCH DAMAGE.
+* ====================================================================
+*
+* This software consists of voluntary contributions made by many
+* individuals on behalf of the Jcorporate Ltd. Contributions back
+* to the project(s) are encouraged when you make modifications.
+* Please send them to support at jcorporate.com. For more information
+* on Jcorporate Ltd. and its products, please see
+* <http://www.jcorporate.com/>.
+*
+* Portions of this software are based upon other open source
+* products and are subject to their respective licenses.
+*/
+
+package com.jcorporate.expresso.core.registry;
+
+import com.jcorporate.expresso.core.security.User;
+
+/**
+ * Test model to verify Expresso Threads. When the thread is run and
+ * exists, it will have set the values of the RequestRegistry for that
+ * thread.
+ * @author Michael Rimov
+ * @version $Revision: 1.1 $ on $Date: 2004/11/09 15:19:20 $
+ */
+public class ExpressoThreadTestModel extends ExpressoThread
+{
+ private String dataContext;
+
+ private User user;
+
+ public ExpressoThreadTestModel()
+ {
+ super();
+ }
+
+ public void setDataContext(String dataContext)
+ {
+ this.dataContext = dataContext;
+ }
+
+ public void setUser(User user)
+ {
+ this.user = user;
+ }
+
+ public String getDataContext()
+ {
+ return dataContext;
+ }
+
+ public User getUser()
+ {
+ return user;
+ }
+
+ /**
+ * When an object implementing interface <code>Runnable</code> is used to
+ * create a thread, starting the thread causes the object's
+ * <code>run</code> method to be called in that separately executing
+ * thread.
+ */
+ public void run()
+ {
+ super.run();
+
+ this.setDataContext(RequestRegistry.getDataContext());
+ this.setUser(RequestRegistry.getUser());
+ }
+}
Index: TestMutableRequestRegistry.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/registry/TestMutableRequestRegistry.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -Lexpresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/registry/TestMutableRequestRegistry.java -Lexpresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/registry/TestMutableRequestRegistry.java -u -r1.1 -r1.2
--- expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/registry/TestMutableRequestRegistry.java
+++ expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/registry/TestMutableRequestRegistry.java
@@ -64,6 +64,7 @@
package com.jcorporate.expresso.core.registry;
import com.jcorporate.expresso.core.security.User;
+import com.jcorporate.expresso.core.security.SuperUser;
import com.jcorporate.expresso.services.test.TestSystemInitializer;
import junit.framework.TestCase;
@@ -90,6 +91,11 @@
mutableRequestRegistry.releaseSettings();
mutableRequestRegistry = null;
super.tearDown();
+ //Reset to administrator. Cleanup so that the following
+ //threads can continue on nicely.
+ new MutableRequestRegistry(TestSystemInitializer
+ .getTestContext(),
+ SuperUser.SUPER_USER);
}
public void testReleaseSettings() {
--- /dev/null
+++ expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/registry/TestExpressoThread.java
@@ -0,0 +1,48 @@
+package com.jcorporate.expresso.core.registry;
+
+import junit.framework.*;
+import com.jcorporate.expresso.services.test.TestSystemInitializer;
+import com.jcorporate.expresso.core.security.SuperUser;
+
+/**
+ * Test case to make sure Expresso credentials are properly getting
+ * sent between threads in classes derived from ExpressoThread.
+ * @author Michael Rimov
+ * @version $Revision: 1.1 $ on $Date: 2004/11/09 15:19:20 $
+ */
+public class TestExpressoThread extends TestCase {
+ private ExpressoThread expressoThread = null;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ expressoThread = new ExpressoThread();
+ //Increase credentials for the test.
+ RequestRegistry.superUser(SuperUser.SUPER_USER);
+ }
+
+ protected void tearDown() throws Exception {
+ expressoThread = null;
+ //Reduce credentials for the test.
+ RequestRegistry.revertUser();
+ super.tearDown();
+ }
+
+ public void testExpressoThreadRequestRegistry()
+ throws InterruptedException
+ {
+ ExpressoThreadTestModel model = new ExpressoThreadTestModel();
+ model.start();
+ model.join(5000); //Wait up to five seconds.
+ //Bail if we don't get there
+ //by then.
+ if (model.isAlive()) {
+ fail("Test Thread Model didn't end in 5 seconds");
+ }
+
+ assertEquals(TestSystemInitializer.getTestContext(),
+ model.getDataContext());
+
+ assertEquals(SuperUser.SUPER_USER, model.getUser());
+ }
+
+}
--- /dev/null
+++ expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/dbobj/tests/Test2.java
@@ -0,0 +1,118 @@
+/* ====================================================================
+ * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
+ *
+ * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by Jcorporate Ltd.
+ * (http://www.jcorporate.com/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. "Jcorporate" and product names such as "Expresso" must
+ * not be used to endorse or promote products derived from this
+ * software without prior written permission. For written permission,
+ * please contact info at jcorporate.com.
+ *
+ * 5. Products derived from this software may not be called "Expresso",
+ * or other Jcorporate product names; nor may "Expresso" or other
+ * Jcorporate product names appear in their name, without prior
+ * written permission of Jcorporate Ltd.
+ *
+ * 6. No product derived from this software may compete in the same
+ * market space, i.e. framework, without prior written permission
+ * of Jcorporate Ltd. For written permission, please contact
+ * partners at jcorporate.com.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Jcorporate Ltd. Contributions back
+ * to the project(s) are encouraged when you make modifications.
+ * Please send them to support at jcorporate.com. For more information
+ * on Jcorporate Ltd. and its products, please see
+ * <http://www.jcorporate.com/>.
+ *
+ * Portions of this software are based upon other open source
+ * products and are subject to their respective licenses.
+ */
+
+package com.jcorporate.expresso.core.dbobj.tests;
+
+import com.jcorporate.expresso.core.db.DBConnection;
+import com.jcorporate.expresso.core.db.DBException;
+import com.jcorporate.expresso.core.dbobj.DBObject;
+
+
+/**
+ *
+ * A basic DBObject used to verify the functions of the DBObject class
+ *
+ * @version $Revision: 1.1 $ $Date: 2004/11/09 15:19:21 $
+ * @author Michael Nash
+ */
+public class Test2
+ extends DBObject {
+ /**
+ * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject#SecuredDBObject
+ * @throws DBException upon error
+ */
+ public Test2()
+ throws DBException {
+ super();
+ } /* Test2() */
+
+
+ /**
+ * Creates a new object hooked with a particular DBConnection. Useful
+ * for transactional capabilities
+ * @param oneConnection a DBConnection obtained from the ConnectionPool
+ * @throws DBException upon error
+ */
+ public Test2(DBConnection oneConnection) throws DBException {
+ super(oneConnection);
+ }
+
+ /**
+ * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject#setupFields
+ * @throws DBException upon error.
+ */
+ public void setupFields()
+ throws DBException {
+ setTargetTable("TEST2");
+ setDescription("Test Two");
+ setCharset("ISO-8859-1");
+ addField("TestKey", "int", 0, false, "Test Table Key");
+ addField("Descrip", "varchar", 30, true, "Description");
+ this.setDefaultValue("Descrip","This is a test");
+ addKey("TestKey");
+ } /* setupFields() */
+
+
+}
+
--- /dev/null
+++ expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/dbobj/tests/MultiDBObjectTest.java
@@ -0,0 +1,260 @@
+/* ====================================================================
+ * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
+ *
+ * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by Jcorporate Ltd.
+ * (http://www.jcorporate.com/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. "Jcorporate" and product names such as "Expresso" must
+ * not be used to endorse or promote products derived from this
+ * software without prior written permission. For written permission,
+ * please contact info at jcorporate.com.
+ *
+ * 5. Products derived from this software may not be called "Expresso",
+ * or other Jcorporate product names; nor may "Expresso" or other
+ * Jcorporate product names appear in their name, without prior
+ * written permission of Jcorporate Ltd.
+ *
+ * 6. No product derived from this software may compete in the same
+ * market space, i.e. framework, without prior written permission
+ * of Jcorporate Ltd. For written permission, please contact
+ * partners at jcorporate.com.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Jcorporate Ltd. Contributions back
+ * to the project(s) are encouraged when you make modifications.
+ * Please send them to support at jcorporate.com. For more information
+ * on Jcorporate Ltd. and its products, please see
+ * <http://www.jcorporate.com/>.
+ *
+ * Portions of this software are based upon other open source
+ * products and are subject to their respective licenses.
+ */
+
+package com.jcorporate.expresso.core.dbobj.tests;
+
+import com.jcorporate.expresso.core.db.DBException;
+import com.jcorporate.expresso.core.db.TableCreator;
+import com.jcorporate.expresso.core.dbobj.MultiDBObject;
+import com.jcorporate.expresso.core.misc.ConfigManager;
+import com.jcorporate.expresso.core.misc.ConfigurationException;
+import com.jcorporate.expresso.services.test.TestSystemInitializer;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import org.apache.log4j.Logger;
+
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.Vector;
+
+
+/**
+ * A test case to verify the functions of DBObject
+ */
+public class MultiDBObjectTest
+ extends TestCase {
+ private static Logger log = Logger.getLogger(MultiDBObjectTest.class);
+
+
+ public static void main(String[] args)
+ throws Exception {
+
+ //Set the system properties we need
+ junit.textui.TestRunner.run(suite());
+ }
+
+ /**
+ * Constructs a test case with the given name.
+ *
+ *@param name name of the test
+ */
+ public MultiDBObjectTest(String name) {
+ super(name);
+ } /* MultiDBObjectTest(String) */
+
+ /**
+ * Counts the number of test cases executed by run(TestResult result).
+ *
+ * @return 1
+ */
+ public int countTestCases() {
+ return 1;
+ } /* countTestCases() */
+
+ /**
+ * Sets up the fixture, for example, open a network connection.
+ * This method is called before a test is executed.
+ *
+ * @throws Exception
+ */
+ protected void setUp()
+ throws Exception {
+ TestSystemInitializer.setUp();
+
+ try {
+ ConfigManager.getContext(TestSystemInitializer.getTestContext());
+ } catch (ConfigurationException ce) {
+ fail("There is no 'test' db/context set up - cannot run db object tests without a test context");
+ }
+
+ TestSchema ts = new TestSchema();
+ Vector v = TableCreator.getInstance().createAsNeeded(ts, TestSystemInitializer.getTestContext());
+
+ for (Enumeration ev = v.elements(); ev.hasMoreElements();) {
+ if (log.isInfoEnabled()) {
+ log.info((String) ev.nextElement());
+ }
+ }
+ try {
+ Test1 oneTest = new Test1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ Test2 oneTest2 = new Test2();
+ oneTest2.setDataContext(TestSystemInitializer.getTestContext());
+ try {
+ oneTest.deleteAll();
+ oneTest2.deleteAll();
+ } catch (Exception ex) {
+ }
+ oneTest.setField("TestKey", "1");
+ oneTest.add();
+
+ oneTest2.setField("TestKey", "1");
+ oneTest2.setField("Descrip", "Test Record");
+ oneTest2.add();
+ } catch (DBException de) {
+ de.printStackTrace();
+ log.error(de);
+ fail("Unable to clean up test object tables");
+ }
+ } /* setUp() */
+
+
+ /**
+ * Tears down the fixture, for example, close a network connection.
+ * This method is called after a test is executed.
+ *
+ * @throws Exception
+ */
+ protected void tearDown()
+ throws Exception {
+ boolean error = false;
+
+ /* Clean out the test tables */
+
+ try {
+ Test1 test1List = new Test1();
+ test1List.setDataContext(TestSystemInitializer.getTestContext());
+
+ Test1 oneTest1 = null;
+ for (Iterator e = test1List.searchAndRetrieveList().iterator();
+ e.hasNext();) {
+ oneTest1 = (Test1) e.next();
+ oneTest1.delete();
+ }
+ } catch (DBException de) {
+ log.error("Error cleaning up test case", de);
+ }
+
+ try {
+ Test2 test2List = new Test2();
+ test2List.setDataContext(TestSystemInitializer.getTestContext());
+
+ Test2 oneTest2 = null;
+
+ for (Iterator e2 = test2List.searchAndRetrieveList().iterator();
+ e2.hasNext();) {
+ oneTest2 = (Test2) e2.next();
+ oneTest2.delete();
+ }
+ } catch (DBException de) {
+ log.error("Error cleaning up test case", de);
+ }
+
+ assertTrue("Error cleaning up test values", !error);
+ } /* tearDown() */
+
+
+ /**
+ * Test adding/updating, etc
+ */
+ public void testMulti() {
+ try {
+ MultiDBObject md = new MultiDBObject();
+ md.setDBName(TestSystemInitializer.getTestContext());
+ md.addDBObj("com.jcorporate.expresso.core.dbobj.tests.Test1",
+ "test1");
+ md.addDBObj("com.jcorporate.expresso.core.dbobj.tests.Test2",
+ "test2");
+ md.setForeignKey("test1", "TestKey", "test2", "TestKey");
+
+ int recCount = 0;
+ MultiDBObject oneMD = null;
+
+ for (Iterator ee = md.searchAndRetrieveList().iterator();
+ ee.hasNext();) {
+ recCount++;
+ oneMD = (MultiDBObject) ee.next();
+
+
+ if (!oneMD.getField("test2", "Descrip").equals("Test Record")) {
+ fail("Got wrong Descrip field (" +
+ oneMD.getField("test2", "Descrip") +
+ ") from retrieve of joined objects.");
+ }
+ }
+ if (recCount == 0) {
+ fail("Did not retrieve the joined records correctly");
+ }
+ } catch (DBException ce) {
+ ce.printStackTrace();
+ log.error(ce);
+ fail("DB exception occurred - see log");
+ }
+ }
+
+ /**
+ * Define the suite of tests that verify each function of the cache
+ *
+ * @return an instantiated TestSuite for this class
+ */
+ public static Test suite() {
+ TestSuite suite = new TestSuite(MultiDBObjectTest.class);
+
+ return suite;
+ } /* suite() */
+
+} /* MultiDBObjectTest */
+
+/* MultiDBObjectTest */
\ No newline at end of file
--- /dev/null
+++ expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/dbobj/tests/RowSecuredDBObjectTestSchema.java
@@ -0,0 +1,131 @@
+/* ====================================================================
+ * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
+ *
+ * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by Jcorporate Ltd.
+ * (http://www.jcorporate.com/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. "Jcorporate" and product names such as "Expresso" must
+ * not be used to endorse or promote products derived from this
+ * software without prior written permission. For written permission,
+ * please contact info at jcorporate.com.
+ *
+ * 5. Products derived from this software may not be called "Expresso",
+ * or other Jcorporate product names; nor may "Expresso" or other
+ * Jcorporate product names appear in their name, without prior
+ * written permission of Jcorporate Ltd.
+ *
+ * 6. No product derived from this software may compete in the same
+ * market space, i.e. framework, without prior written permission
+ * of Jcorporate Ltd. For written permission, please contact
+ * partners at jcorporate.com.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Jcorporate Ltd. Contributions back
+ * to the project(s) are encouraged when you make modifications.
+ * Please send them to support at jcorporate.com. For more information
+ * on Jcorporate Ltd. and its products, please see
+ * <http://www.jcorporate.com/>.
+ *
+ * Portions of this software are based upon other open source
+ * products and are subject to their respective licenses.
+ */
+
+package com.jcorporate.expresso.core.dbobj.tests;
+
+/*
+ * TestSchema.java
+ *
+ * Copyright 2001 Jcorporate Ltd.
+ */
+
+import com.jcorporate.expresso.core.db.DBException;
+import com.jcorporate.expresso.core.dbobj.Schema;
+
+
+/**
+ * Test Schema object - used by the test cases to create appropriate tables for tests
+ *
+ * @version $Revision: 1.1 $ $Date: 2004/11/09 15:19:20 $
+ * @author Michael Nash
+ */
+public class RowSecuredDBObjectTestSchema
+ extends Schema {
+
+ /**
+ * Constructor
+ *
+ * @throws DBException
+ */
+ public RowSecuredDBObjectTestSchema()
+ throws DBException {
+ super();
+
+ try {
+
+ /* Database objects */
+ addDBObject(com.jcorporate.expresso.core.dbobj
+ .tests.RowSecuredDBObjectTest1.class);
+ } catch (Exception e) {
+ throw new DBException(e);
+ }
+ } /* TestSchema() */
+
+ /**
+ *
+ *
+ * @return
+ */
+ public String getMessageBundlePath() {
+ return "com/jcorporate/expresso/core";
+ } /* getMessageBundlePath() */
+
+ /**
+ * Returns the default component code for this schema. Useful for automated
+ * component testing/installation
+ */
+ public String getDefaultComponentCode() {
+ return ("tests");
+ }
+
+ /**
+ * Returns the default description for this schema. Useful for automated
+ * component testing/installation
+ */
+ public String getDefaultDescription() {
+ return ("Test Schema");
+ }
+} /* TestSchema */
+
+/* TestSchema */
--- /dev/null
+++ expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/dbobj/tests/RowSecuredDBObjectTest1.java
@@ -0,0 +1,141 @@
+/* ====================================================================
+ * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
+ *
+ * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by Jcorporate Ltd.
+ * (http://www.jcorporate.com/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. "Jcorporate" and product names such as "Expresso" must
+ * not be used to endorse or promote products derived from this
+ * software without prior written permission. For written permission,
+ * please contact info at jcorporate.com.
+ *
+ * 5. Products derived from this software may not be called "Expresso",
+ * or other Jcorporate product names; nor may "Expresso" or other
+ * Jcorporate product names appear in their name, without prior
+ * written permission of Jcorporate Ltd.
+ *
+ * 6. No product derived from this software may compete in the same
+ * market space, i.e. framework, without prior written permission
+ * of Jcorporate Ltd. For written permission, please contact
+ * partners at jcorporate.com.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Jcorporate Ltd. Contributions back
+ * to the project(s) are encouraged when you make modifications.
+ * Please send them to support at jcorporate.com. For more information
+ * on Jcorporate Ltd. and its products, please see
+ * <http://www.jcorporate.com/>.
+ *
+ * Portions of this software are based upon other open source
+ * products and are subject to their respective licenses.
+ */
+
+package com.jcorporate.expresso.core.dbobj.tests;
+
+import com.jcorporate.expresso.core.db.DBConnection;
+import com.jcorporate.expresso.core.db.DBException;
+import com.jcorporate.expresso.core.dbobj.RowSecuredDBObject;
+
+
+/**
+ *
+ * A basic DBObject used to verify the functions of the DBObject class
+ *
+ * @author Michael Nash
+ */
+public class RowSecuredDBObjectTest1
+ extends RowSecuredDBObject {
+ /**
+ * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject#SecuredDBObject
+ * @throws DBException upon error
+ */
+ public RowSecuredDBObjectTest1()
+ throws DBException {
+ super();
+ } /* Test1() */
+
+ /**
+ * Creates a new object hooked with a particular DBConnection. Useful
+ * for transactional capabilities
+ * @param oneConnection a DBConnection obtained from the ConnectionPool
+ * @param securityUid The security uid
+ * @throws DBException upon error
+ */
+ public RowSecuredDBObjectTest1(DBConnection oneConnection, int securityUid)
+ throws DBException {
+ super(oneConnection, securityUid);
+ }
+
+
+ /**
+ * Setup fields for the test object. The commented out fields are fields
+ * that are rare and not necessarily supported by most databases.
+ * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject#setupFields
+ * @throws DBException upon error
+ */
+ public void setupFields()
+ throws DBException {
+ setTargetTable("ROWSECTEST1");
+ setDescription("Test One");
+ setCharset("ISO-8859-1");
+ addField("TestKey", "int", 0, false, "Test Table Key");
+ addField("BigIntTest", "bigint", 0, true, "Big Int Test");
+ addField("BinaryTest", "binary", 0, true, "Binary Test");
+ addField("BitTest", "bit", 0, true, "Bit Test");
+ addField("BooleanTest", "boolean", 0, true, "Boolean Test");
+ addField("BlobTest", "blob", 0, true, "Blob Test");
+ addField("CharTest", "char", 30, true, "Char Test");
+ addField("ClobTest", "clob", 0, true, "Clob Test");
+ addField("DateTest", "date", 0, true, "Date Test");
+ addField("DecimalTest", "decimal", 0, true, "Decimal Test");
+ addField("DoubleTest", "double", 0, true, "Double Test");
+ addField("FloatTest", "float", 9, 3, true, "Float Test");
+ addField("IntTest", "int", 0, true, "Int Test");
+ addField("NumericTest", "numeric", 0, true, "Numeric Test");
+ addField("RealTest", "real", 9, true, "Real Test");
+ addField("SmallIntTest", "smallint", 0, true, "Smallint Test");
+ addField("TimeTest", "time", 0, true, "Time Test");
+ addField("TimeStampTest", "timestamp", 0, true, "Time Stampt Test");
+ addField("DateTimeTest", "datetime", 0, true, "Date Time Test");
+ addField("TinyIntTest", "tinyint", 0, true, "Tiny Int Test");
+ addField("VarCharTest", "varchar", 30, true, "Varchar Test");
+ addField("TextTest", "text", 0, true, "Text Test");
+ addField("AutoIncTest", "auto-inc", 0, true, "AutoIncrement Test");
+
+ addKey("TestKey");
+ } /* setupFields() */
+
+
+}
--- /dev/null
+++ expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/dbobj/tests/RowSecuredDBObjectTest.java
@@ -0,0 +1,697 @@
+/* ====================================================================
+ * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
+ *
+ * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by Jcorporate Ltd.
+ * (http://www.jcorporate.com/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. "Jcorporate" and product names such as "Expresso" must
+ * not be used to endorse or promote products derived from this
+ * software without prior written permission. For written permission,
+ * please contact info at jcorporate.com.
+ *
+ * 5. Products derived from this software may not be called "Expresso",
+ * or other Jcorporate product names; nor may "Expresso" or other
+ * Jcorporate product names appear in their name, without prior
+ * written permission of Jcorporate Ltd.
+ *
+ * 6. No product derived from this software may compete in the same
+ * market space, i.e. framework, without prior written permission
+ * of Jcorporate Ltd. For written permission, please contact
+ * partners at jcorporate.com.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Jcorporate Ltd. Contributions back
+ * to the project(s) are encouraged when you make modifications.
+ * Please send them to support at jcorporate.com. For more information
+ * on Jcorporate Ltd. and its products, please see
+ * <http://www.jcorporate.com/>.
+ *
+ * Portions of this software are based upon other open source
+ * products and are subject to their respective licenses.
+ */
+
+package com.jcorporate.expresso.core.dbobj.tests;
+
+import com.jcorporate.expresso.core.db.DBConnection;
+import com.jcorporate.expresso.core.db.DBConnectionPool;
+import com.jcorporate.expresso.core.db.DBException;
+import com.jcorporate.expresso.core.db.TableCreator;
+import com.jcorporate.expresso.core.db.exception.DBRecordNotFoundException;
+import com.jcorporate.expresso.core.misc.ConfigManager;
+import com.jcorporate.expresso.core.misc.ConfigurationException;
+import com.jcorporate.expresso.core.misc.DateTime;
+import com.jcorporate.expresso.services.test.TestSystemInitializer;
+import com.jcorporate.expresso.core.security.User;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import org.apache.log4j.Logger;
+import java.util.Enumeration;
+import java.util.Vector;
+import com.jcorporate.expresso.core.registry.RequestRegistry;
+
+/**
+ * A test case to verify the functions of DBObject.
+ * @todo Create test case where you add in one security context,
+ * and try to modify it in another.
+ */
+public class RowSecuredDBObjectTest
+ extends TestCase {
+
+ private static Logger log = Logger.getLogger(RowSecuredDBObjectTest.class);
+
+ /**
+ * Constructs a test case with the given name.
+ *
+ * @param name The name of the test case
+ */
+ public RowSecuredDBObjectTest(String name) {
+ super(name);
+ } /* DBObjectTest(String) */
+
+
+ public static void main(String[] args)
+ throws Exception {
+
+ //Set the system properties we need
+ junit.textui.TestRunner.run(suite());
+ ConfigManager.destroy();
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection.
+ * This method is called before a test is executed.
+ * @throws Exception upon error
+ */
+ public void setUp()
+ throws Exception {
+ TestSystemInitializer.setUp();
+
+ try {
+ ConfigManager.getContext(TestSystemInitializer.getTestContext());
+ } catch (ConfigurationException ce) {
+ fail(
+ "Specified context to test:" + TestSystemInitializer.getTestContext() + " but couldn't find that context");
+ }
+
+ RowSecuredDBObjectTestSchema ts = new RowSecuredDBObjectTestSchema();
+ Vector v = TableCreator.getInstance().createAsNeeded(ts, TestSystemInitializer.getTestContext());
+
+ for (Enumeration ev = v.elements(); ev.hasMoreElements();) {
+ log.info((String) ev.nextElement());
+ }
+
+ RequestRegistry.superUser(User.getAdmin(TestSystemInitializer.getTestContext()));
+ } /* setUp() */
+
+
+ /**
+ * Tears down the fixture, for example, close a network connection.
+ * This method is called after a test is executed.
+ * @throws Exception upon error
+ */
+ public void tearDown()
+ throws Exception {
+ try {
+
+ /* Clean out the test tables */
+ RowSecuredDBObjectTest1 test1List = new RowSecuredDBObjectTest1();
+ test1List.setDataContext(TestSystemInitializer.getTestContext());
+ test1List.deleteAll();
+ RequestRegistry.revertUser();
+
+ } catch (DBException de) {
+ de.printStackTrace();
+ log.error(de);
+ }
+ } /* tearDown() */
+
+ /**
+ * First test: Make sure we're getting permissions transferred to the
+ * RowSecuredDBObject
+ * @throws DBException upon error.
+ */
+ public void testRequestRegistryWithRowSecuredDBObject() throws DBException {
+ RowSecuredDBObjectTest1 test = new RowSecuredDBObjectTest1();
+ assertEquals(TestSystemInitializer.getTestContext(),
+ test.getDataContext());
+ assertEquals(User.getAdminId(TestSystemInitializer.getTestContext()),
+ test.getRequestingUid());
+ }
+
+
+ /**
+ * Tests to make sure get/set fields is working properly Specifically
+ * we also test that fields are appropriately set for null values.
+ */
+ public void testGetSetFields() {
+ try {
+ RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", "1");
+
+ String value = oneTest.getField("CharTest");
+ assertTrue("Should be getting blank from value",
+ value == null || value.length() == 0);
+
+ value = oneTest.getField("DecimalTest");
+ assertTrue("Should be getting blank from value",
+ value == null || value.length() == 0);
+
+ value = oneTest.getField("IntTest");
+ assertTrue("Should be getting blank from value",
+ value == null || value.length() == 0);
+
+ int testInt = oneTest.getFieldInt("IntTest");
+ assertTrue("Must get zero for blank field IntTest", testInt == 0);
+
+ boolean testBoolean = oneTest.getFieldBoolean("BooleanTest");
+ assertTrue("Must get false for blank field BooleanTest", testBoolean == false);
+
+ java.util.Date dt = oneTest.getFieldDate("DateTest");
+ assertTrue("Should get null date for blank field", dt == null);
+
+ float fl = oneTest.getFieldFloat("FloatTest");
+ assertTrue("Should get zero back for a blank float field", fl == 0.0);
+
+ value = oneTest.getFieldDecimalFormatted("DecimalTest", "######");
+ assertTrue("Should get null back for decimal formatted", value == null);
+
+
+ oneTest.setField("TextTest", "abcdefg");
+ oneTest.setField("BooleanTest", true);
+ oneTest.setField("CharTest", "abcd");
+ oneTest.setField("DecimalTest", "3");
+ oneTest.setField("FloatTest", "3.141");
+ oneTest.setField("IntTest", 123985);
+ oneTest.setField("VarCharTest", "lmnop");
+ oneTest.setField("DateTest", com.jcorporate.expresso.core.misc
+ .DateTime.getDateForDB());
+
+
+ value = oneTest.getField("CharTest");
+ assertTrue("Should be getting abcd from CharTest got"
+ + value + " instead", value.equals("abcd"));
+
+ value = oneTest.getField("DecimalTest");
+ assertTrue("Should be getting 3 from value got + " + value + " instead",
+ value.equals("3"));
+
+ testInt = oneTest.getFieldInt("IntTest");
+ assertTrue("Must get 123985 for blank field IntTest got "
+ + testInt + " instead", testInt == 123985);
+
+ testBoolean = oneTest.getFieldBoolean("BooleanTest");
+ assertTrue("Must get true for set field BooleanTest", testBoolean = true);
+
+ dt = oneTest.getFieldDate("DateTest");
+ assertTrue("Should not get a null date for set field", dt != null);
+
+ fl = oneTest.getFieldFloat("FloatTest");
+
+ assertTrue("Should get 3.141 for float value got " + fl + " instead",
+ java.lang.Math.abs(fl - 3.141) < .001);
+
+ } catch (DBException ex) {
+ ex.printStackTrace();
+ fail("Caught exception testing update support" + ex.getMessage());
+ }
+ }
+
+
+ /**
+ * Test Adding the object to the database. We also test adding duplicate
+ * keys to the database
+ */
+ public void testAdd() {
+
+ try {
+
+ /* Add a record */
+ RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", "1");
+ oneTest.setField("DateTest", "1999-10-01");
+ oneTest.setField("BooleanTest", true);
+ oneTest.setField("CharTest", "abcd");
+ oneTest.setField("DecimalTest", "3");
+ oneTest.setField("FloatTest", "3.141");
+ oneTest.setField("IntTest", 123985);
+ oneTest.setField("VarCharTest", "lmnop");
+
+ oneTest.add();
+ oneTest.clear();
+ } catch (DBException ce) {
+ log.error(ce);
+ fail("DB exception occurred - see log");
+ }
+ /* Try to add a duplicate - make sure it fails */
+ try {
+
+ /* Add a duplicate record */
+ RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", "1");
+ log.warn(
+ "About to throw intentional exception having to do with duplicate key. \nIgnore this dup-key exception.");
+ oneTest.add();
+ fail("Test add should have thrown exception on duplicate key here");
+ } catch (DBException ce) {
+
+ /* Do nothing here - it's good that we throw */
+ }
+
+ try {
+ RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", "1");
+ oneTest.retrieve();
+ } catch (DBException ce) {
+ log.error(ce);
+ fail("DB exception occurred on retrieve test - see log");
+ }
+ try {
+ RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", "1");
+
+ if (!oneTest.find()) {
+ fail("DB exception occurred on find test - see log");
+ }
+ } catch (DBException ce) {
+ ce.printStackTrace();
+ log.error(ce);
+ fail("DB exception occurred on find test - see log");
+ }
+ }
+
+ /**
+ * Tests to make sure that we can work with custom where clauses and nothing
+ * breaks.
+ */
+ public void testCustomWhereClause() {
+ try {
+ /* Add a record */
+ RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", "1");
+ oneTest.add();
+
+ oneTest.clear();
+ oneTest.setCustomWhereClause("TESTKEY = 1");
+ boolean found = oneTest.find();
+ assertTrue("Must find a record", found == true);
+
+ oneTest.clear();
+ oneTest.setCustomWhereClause("TESTKEY = 1");
+ int numRecords = oneTest.count();
+ assertTrue("Must have one record in the database table", numRecords == 1);
+ } catch (DBException ce) {
+ log.error(ce);
+ fail("DB exception occurred - see log");
+ }
+
+ }
+
+ /**
+ * Tests retrieve We also make sure that the exceptions we expect to be
+ * thrown are thrown... such as for bogus key values, keys not set.
+ */
+ public void testRetrieve() {
+ try {
+ /* Add a record */
+ RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", "1");
+ oneTest.add();
+
+ oneTest.clear();
+ oneTest.setField("TestKey", "1");
+ oneTest.retrieve();
+ assertTrue("Should have proper return value for testkey",
+ "1".equals(oneTest.getField("TestKey")));
+
+
+ oneTest.clear();
+ oneTest.setField("TestKey", "232");
+ try {
+ oneTest.retrieve();
+ fail("Retrieve for bogus key should have thrown DBRecordNotFoundException");
+ } catch (DBRecordNotFoundException ex) {
+ //we're ok here.
+ }
+
+ oneTest.clear();
+ try {
+ oneTest.retrieve();
+ fail("Retrieve with no keys set should have thrown an exception");
+ } catch (DBException dbe) {
+ //we're ok if we get here.
+ }
+ } catch (DBException ce) {
+ log.error(ce);
+ fail("DB exception occurred - see log");
+ }
+ }
+
+ /**
+ * Tests the 'setFieldsToRetrieve()' functionality.
+ */
+ public void testFieldsToRetrieve() {
+ try {
+ /* Add a record */
+ RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", "1");
+ oneTest.setField("IntTest", 24);
+ oneTest.setField("FloatTest", "3.141592");
+ oneTest.add();
+
+ oneTest.clear();
+ oneTest.setField("TestKey", "1");
+ oneTest.setFieldsToRetrieve("TestKey|IntTest");
+ oneTest.retrieve();
+ assertTrue("Should have proper return value for testkey",
+ "1".equals(oneTest.getField("TestKey")));
+
+ assertTrue("Should have retrieved TestInt field:",
+ 24 == oneTest.getFieldInt("IntTest"));
+
+ assertTrue("Should not have retrieved FloatTest:",
+ oneTest.getDataField("FloatTest").isNull());
+
+ //Now make sure clear() properly resets the fields to retrieve
+ oneTest.clear();
+ oneTest.setField("TestKey", "1");
+ oneTest.clearFieldsToRetrieve();
+ oneTest.retrieve();
+ assertTrue("Should have proper return value for testkey",
+ "1".equals(oneTest.getField("TestKey")));
+
+ assertTrue("Should have retrieved TestInt field:",
+ 24 == oneTest.getFieldInt("IntTest"));
+
+ assertTrue("Should have retrieved FloatTest:",
+ !oneTest.getDataField("FloatTest").isNull());
+
+
+ } catch (DBException ce) {
+ log.error(ce);
+ fail("DB exception occurred - see log");
+ }
+
+ }
+
+ /**
+ * This test demonstrates/tests the basic functionality for Text-based
+ * data fields. You set and retrieve the text data as usual and the
+ * underlying framework saves/retrieves the CLOB field when you want it.
+ */
+ public void testCLOBSupport() {
+ final String testString = "LONG Character Update Test";
+ final String testKey = "1";
+ try {
+ RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", testKey);
+ oneTest.setField("TextTest", testString);
+ oneTest.add();
+
+ oneTest.clear();
+ oneTest.setField("TestKey", testKey);
+ oneTest.retrieve();
+ String fieldData = oneTest.getField("TextTest");
+ assertTrue("CLOB Field data must not be null", fieldData != null);
+
+ assertTrue("CLOB field data must be equal to the original data. Got "
+ + fieldData + " instead", testString.equals(fieldData));
+
+ } catch (Throwable t) {
+ t.printStackTrace();
+ fail("Caught exception testing CLOB Support: " + t.getMessage());
+ }
+ }
+
+ /**
+ * This test makes sure that special characters such as backslashes
+ * or single quotes get properly escaped so we read them back properly
+ */
+ public void testSpecialCharacters() {
+ final String testString = "' \\ abcdefg";
+ final String testKey = "1";
+
+ try {
+ RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", testKey);
+ oneTest.setField("VarCharTest", testString);
+ oneTest.setField("CharTest", testString);
+ oneTest.add();
+
+ oneTest.clear();
+ oneTest.setField("TestKey", testKey);
+ oneTest.retrieve();
+ String fieldData = oneTest.getField("VarCharTest");
+ assertTrue("Varchar Field data must not be null", fieldData != null);
+
+ assertTrue("Varchar field data must be equal to the original data. Got "
+ + fieldData + " instead", testString.equals(fieldData));
+ fieldData = oneTest.getField("CharTest");
+ assertTrue("CharTest Field data must not be null", fieldData != null);
+
+ assertTrue("CharTest field data must be equal to the original data. Got "
+ + fieldData + " instead", testString.equals(fieldData));
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ fail("Caught exception processing Special Characters "
+ + ex.getMessage());
+ }
+
+ }
+
+
+ /**
+ * Test Transaction Capabilities
+ */
+ public void testTransactions() {
+ try {
+ DBConnectionPool pool = DBConnectionPool.getInstance(TestSystemInitializer.getTestContext());
+ DBConnection oneConnection = pool.getConnection("Test Transaction");
+ if (!oneConnection.supportsTransactions()) {
+ log.warn("Cannot test transactions because this DB does not support them.");
+ oneConnection.release();
+ return;
+ }
+
+
+ try {
+
+ // before test, make sure no object exists in db (during autocommit)
+ RowSecuredDBObjectTest1 test1 = new RowSecuredDBObjectTest1(oneConnection,
+ User.getAdminId(TestSystemInitializer.getTestContext()));
+ test1.setField("TestKey", 1);
+ if (test1.find()) test1.delete();
+
+ //
+ // turn on transaction
+ //
+ oneConnection.setAutoCommit(false);
+
+ // add item during transaction
+ test1 = new RowSecuredDBObjectTest1(oneConnection,
+ User.getAdminId(TestSystemInitializer.getTestContext()));
+ test1.setField("TestKey", 1);
+ test1.setField("TextTest", "Long Test");
+ test1.add();
+
+ //
+ // ok, rollback, which should remove objects
+ //
+ oneConnection.rollback();
+
+ assertTrue(test1.count() == 0);
+
+ //
+ //Now add and commit.
+ //
+ test1.setField("TestKey", 1);
+ test1.setField("TextTest", "Long Test");
+ test1.add();
+
+ oneConnection.commit();
+ assertTrue(test1.count() == 1);
+
+ //
+ //Try clear, retrieve, update, clear, retrieve, update
+ //
+ test1.clear();
+ test1.setField("TestKey", 1);
+ test1.retrieve();
+
+ test1.setField("TextTest", "Some other value");
+ test1.update();
+
+ test1.clear();
+ test1.setField("TestKey", 1);
+ test1.setField("TextTest", "Yet another Value");
+ test1.update();
+
+ test1.clear();
+ test1.setField("TestKey", 1);
+ test1.retrieve();
+
+
+ } catch (DBException dbe) {
+ try {
+ oneConnection.rollback();
+ } catch (DBException ex) {
+ log.error("Error rolling back transaction");
+ }
+ dbe.printStackTrace();
+ fail("Caught DBException attempting to execute statements");
+ } finally {
+ pool.release(oneConnection);
+ }
+
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ fail("Caught exception processing TestTransaction "
+ + ex.getMessage());
+ }
+ }
+
+
+
+ /**
+ * Tests the DBObject update routine.
+ */
+ public void testUpdate() {
+ try {
+ /* Add a record */
+ RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", "1");
+ oneTest.add();
+
+ oneTest.setField("TextTest", "abcdefg");
+ oneTest.setField("BooleanTest", true);
+ oneTest.setField("CharTest", "abcd");
+ oneTest.setField("DecimalTest", "3");
+ oneTest.setField("FloatTest", "3.141");
+ oneTest.setField("IntTest", 123985);
+ oneTest.setField("VarCharTest", "lmnop");
+ oneTest.setField("DateTest", "1999-10-01");
+ oneTest.update();
+
+ //Make sure it really was there.
+ oneTest.clear();
+ oneTest.setField("TestKey", "1");
+ if (!oneTest.find()) {
+ fail("Unable to locate record we just updated");
+ }
+ oneTest.delete();
+ } catch (DBException ex) {
+ ex.printStackTrace();
+ fail("Caught exception testing update support" + ex.getMessage());
+ }
+
+ }
+
+ /**
+ * This function tests adding blank dates since some databases don't tolerate
+ * it very well.
+ */
+ public void testDateAdd() {
+ final String testStringValue = "";
+ final String testKey = "1";
+ try {
+ RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", testKey);
+ oneTest.setField("DateTest", testStringValue);
+ oneTest.add();
+
+ oneTest.clear();
+ oneTest.setField("TestKey", testKey);
+ oneTest.retrieve();
+
+ String fieldData = oneTest.getField("DateTest");
+ assertTrue(fieldData.equals(testStringValue));
+
+ } catch (Throwable t) {
+ t.printStackTrace();
+ fail("Caught exception testing CLOB Support: " + t.getMessage());
+ }
+ }
+
+ public void testDateSetField() {
+ final String testKey = "1";
+ try {
+ RowSecuredDBObjectTest1 oneTest = new RowSecuredDBObjectTest1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", testKey);
+ oneTest.setField("DateTest", new java.util.Date());
+ oneTest.setField("DateTimeTest", new java.util.Date());
+ oneTest.setField("TimeTest", new java.util.Date());
+ oneTest.add();
+
+ oneTest.clear();
+ oneTest.setField("TestKey", testKey);
+ oneTest.retrieve();
+
+ System.out.println("Date: " + oneTest.getField("DateTest"));
+ System.out.println("DateTime: " + oneTest.getField("DateTimeTest"));
+ System.out.println("Time: " + oneTest.getField("TimeTest"));
+// assertTrue(fieldData.equals(testStringValue));
+
+ } catch (Throwable t) {
+ t.printStackTrace();
+ fail("Caught exception testing CLOB Support: " + t.getMessage());
+ }
+ }
+
+
+ /**
+ * Define the suite of tests that verify each function of the cache
+ *
+ * @return an instantiated test suite
+ */
+ public static Test suite() {
+ TestSuite suite = new TestSuite(RowSecuredDBObjectTest.class);
+
+ return suite;
+ } /* suite() */
+
+} /* DBObjectTest */
--- /dev/null
+++ expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/dbobj/tests/AutoDBObjTest.java
@@ -0,0 +1,145 @@
+/* ====================================================================
+ * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
+ *
+ * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by Jcorporate Ltd.
+ * (http://www.jcorporate.com/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. "Jcorporate" and product names such as "Expresso" must
+ * not be used to endorse or promote products derived from this
+ * software without prior written permission. For written permission,
+ * please contact info at jcorporate.com.
+ *
+ * 5. Products derived from this software may not be called "Expresso",
+ * or other Jcorporate product names; nor may "Expresso" or other
+ * Jcorporate product names appear in their name, without prior
+ * written permission of Jcorporate Ltd.
+ *
+ * 6. No product derived from this software may compete in the same
+ * market space, i.e. framework, without prior written permission
+ * of Jcorporate Ltd. For written permission, please contact
+ * partners at jcorporate.com.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Jcorporate Ltd. Contributions back
+ * to the project(s) are encouraged when you make modifications.
+ * Please send them to support at jcorporate.com. For more information
+ * on Jcorporate Ltd. and its products, please see
+ * <http://www.jcorporate.com/>.
+ *
+ * Portions of this software are based upon other open source
+ * products and are subject to their respective licenses.
+ */
+
+package com.jcorporate.expresso.core.dbobj.tests;
+
+import com.jcorporate.expresso.core.dbobj.AutoDBObject;
+import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
+import com.jcorporate.expresso.services.dbobj.MimeTypes;
+import com.jcorporate.expresso.services.test.ControllerTestSuite;
+import com.jcorporate.expresso.services.test.ExpressoTestCase;
+import com.jcorporate.expresso.services.test.TestSystemInitializer;
+import com.jcorporate.expresso.core.misc.ConfigManager;
+
+import java.util.ArrayList;
+
+
+/**
+ * Quick sanity check to make sure that the AutoDBObject is working properly
+ * @author Michael Rimov
+ * @version $Revision: 1.1 $ on $Date: 2004/11/09 15:19:20 $
+ */
+public class AutoDBObjTest
+ extends ExpressoTestCase {
+ public AutoDBObjTest(String name)
+ throws Exception {
+ super(name);
+ }
+
+ public static void main(String[] args)
+ throws java.lang.Exception {
+
+ //Set the system properties we need
+ junit.textui.TestRunner.run(suite());
+ }
+
+ /**
+ * Creates the test cases. Order is important, thus the manual
+ * creation.
+ */
+ public static junit.framework.Test suite()
+ throws Exception {
+ ControllerTestSuite cts = new ControllerTestSuite();
+ cts.addTestSuite(AutoDBObjTest.class);
+ cts.addReadOnlySchemaDependency("com.jcorporate.expresso.core.ExpressoSchema");
+
+ return cts;
+ }
+
+ /**
+ * Quick sanity check. This was originally adapted from the unitTest
+ * function that no longer exists within AutoDBOBject itself. This
+ * unit test will fail if AutoDBObject tests against a DBOBject with
+ * virtual fields in it.
+ */
+ public void testAutoDBObject()
+ throws Exception {
+ if ("org.hsqldb.jdbcDriver".equals(ConfigManager
+ .getContext(TestSystemInitializer
+ .getTestContext()).getJdbc().getDriver())) {
+ return;
+ }
+ AutoDBObject ad = new AutoDBObject();
+ ad.setDataContext(TestSystemInitializer.getTestContext());
+ ad.setTargetTable("MIMETYPES");
+
+ ArrayList al = ad.getMetaData().getFieldListArray();
+ assertTrue("Cannot Have a Null FieldListArray", al != null);
+ assertTrue("Must have a > zero length field list", al.size() > 0);
+
+ SecuredDBObject sl = new MimeTypes(SecuredDBObject.SYSTEM_ACCOUNT);
+ sl.setDataContext(TestSystemInitializer.getTestContext());
+
+ ArrayList al2 = sl.getMetaData().getFieldListArray();
+ assertTrue("MimeTypes cannot return a null FieldList", al2 != null);
+ assertTrue("MimeTypes and AutoDBObject Field List Sizes must be " +
+ "equal", al.size() == al2.size());
+
+ //Check to make sure both field lists are identical.
+ for (int i = 0; i < al.size(); i++) {
+ assertTrue("Field List must be equal for i=" + i,
+ ((String) al.get(i)).equalsIgnoreCase((String) al2.get(i)));
+ }
+ }
+}
--- /dev/null
+++ expresso-web/WEB-INF/test-src/com/jcorporate/expresso/core/dbobj/tests/DBObjectTest.java
@@ -0,0 +1,1209 @@
+/* ====================================================================
+ * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
+ *
+ * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by Jcorporate Ltd.
+ * (http://www.jcorporate.com/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. "Jcorporate" and product names such as "Expresso" must
+ * not be used to endorse or promote products derived from this
+ * software without prior written permission. For written permission,
+ * please contact info at jcorporate.com.
+ *
+ * 5. Products derived from this software may not be called "Expresso",
+ * or other Jcorporate product names; nor may "Expresso" or other
+ * Jcorporate product names appear in their name, without prior
+ * written permission of Jcorporate Ltd.
+ *
+ * 6. No product derived from this software may compete in the same
+ * market space, i.e. framework, without prior written permission
+ * of Jcorporate Ltd. For written permission, please contact
+ * partners at jcorporate.com.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Jcorporate Ltd. Contributions back
+ * to the project(s) are encouraged when you make modifications.
+ * Please send them to support at jcorporate.com. For more information
+ * on Jcorporate Ltd. and its products, please see
+ * <http://www.jcorporate.com/>.
+ *
+ * Portions of this software are based upon other open source
+ * products and are subject to their respective licenses.
+ */
+
+package com.jcorporate.expresso.core.dbobj.tests;
+
+import com.jcorporate.expresso.core.dataobjects.jdbc.LobField;
+import com.jcorporate.expresso.core.db.DBConnection;
+import com.jcorporate.expresso.core.db.DBConnectionPool;
+import com.jcorporate.expresso.core.db.DBException;
+import com.jcorporate.expresso.core.db.TableCreator;
+import com.jcorporate.expresso.core.db.exception.DBRecordNotFoundException;
+import com.jcorporate.expresso.core.misc.ConfigManager;
+import com.jcorporate.expresso.core.misc.ConfigurationException;
+import com.jcorporate.expresso.core.misc.DateTime;
+import com.jcorporate.expresso.core.misc.StringUtil;
+import com.jcorporate.expresso.services.test.TestSystemInitializer;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import org.apache.log4j.Logger;
+
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.Vector;
+
+
+/**
+ * A test case to verify the functions of DBObject.
+ */
+public class DBObjectTest
+ extends TestCase {
+ private static Logger log = Logger.getLogger(DBObjectTest.class);
+
+ /**
+ * Constructs a test case with the given name.
+ *
+ * @param name The name of the test case
+ */
+ public DBObjectTest(String name) {
+ super(name);
+ } /* DBObjectTest(String) */
+
+
+ public static void main(String[] args)
+ throws Exception {
+
+ //Set the system properties we need
+ junit.textui.TestRunner.run(suite());
+ ConfigManager.destroy();
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection.
+ * This method is called before a test is executed.
+ * @throws Exception upon error
+ */
+ public void setUp()
+ throws Exception {
+ TestSystemInitializer.setUp();
+
+ try {
+ ConfigManager.getContext(TestSystemInitializer.getTestContext());
+ } catch (ConfigurationException ce) {
+ fail(
+ "Specified context to test:" + TestSystemInitializer.getTestContext() + " but couldn't find that context");
+ }
+
+ TestSchema ts = new TestSchema();
+ Vector v = TableCreator.getInstance().createAsNeeded(ts, TestSystemInitializer.getTestContext());
+
+ for (Enumeration ev = v.elements(); ev.hasMoreElements();) {
+ log.info((String) ev.nextElement());
+ }
+ } /* setUp() */
+
+
+ /**
+ * Tears down the fixture, for example, close a network connection.
+ * This method is called after a test is executed.
+ * @throws Exception upon error
+ */
+ public void tearDown()
+ throws Exception {
+ try {
+
+ /* Clean out the test tables */
+ Test1 test1List = new Test1();
+ test1List.setDataContext(TestSystemInitializer.getTestContext());
+
+ test1List.deleteAll();
+
+ Test2 test2List = new Test2();
+ test2List.setDataContext(TestSystemInitializer.getTestContext());
+ test2List.deleteAll();
+
+ } catch (DBException de) {
+ de.printStackTrace();
+ log.error(de);
+ }
+ } /* tearDown() */
+
+ /**
+ * Tests to make sure get/set fields is working properly Specifically
+ * we also test that fields are appropriately set for null values.
+ */
+ public void testGetSetFields() {
+ try {
+ Test1 oneTest = new Test1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", "1");
+
+ String value = oneTest.getField("CharTest");
+ assertTrue("Should be getting blank from value",
+ value == null || value.length() == 0);
+
+ value = oneTest.getField("DecimalTest");
+ assertTrue("Should be getting blank from value",
+ value == null || value.length() == 0);
+
+ value = oneTest.getField("IntTest");
+ assertTrue("Should be getting blank from value",
+ value == null || value.length() == 0);
+
+ int testInt = oneTest.getFieldInt("IntTest");
+ assertTrue("Must get zero for blank field IntTest", testInt == 0);
+
+ boolean testBoolean = oneTest.getFieldBoolean("BooleanTest");
+ assertTrue("Must get false for blank field BooleanTest", testBoolean == false);
+
+ java.util.Date dt = oneTest.getFieldDate("DateTest");
+ assertTrue("Should get null date for blank field", dt == null);
+
+ float fl = oneTest.getFieldFloat("FloatTest");
+ assertTrue("Should get zero back for a blank float field", fl == 0.0);
+
+ value = oneTest.getFieldDecimalFormatted("DecimalTest", "######");
+ assertTrue("Should get null back for decimal formatted", value == null);
+
+
+ oneTest.setField("TextTest", "abcdefg");
+ oneTest.setField("BooleanTest", true);
+ oneTest.setField("CharTest", "abcd");
+ oneTest.setField("DecimalTest", "3");
+ oneTest.setField("FloatTest", "3.141");
+ oneTest.setField("IntTest", 123985);
+ oneTest.setField("VarCharTest", "lmnop");
+ oneTest.setField("DateTest", com.jcorporate.expresso.core.misc
+ .DateTime.getDateForDB());
+
+
+ value = oneTest.getField("CharTest");
+ assertTrue("Should be getting abcd from CharTest got"
+ + value + " instead", value.equals("abcd"));
+
+ value = oneTest.getField("DecimalTest");
+ assertTrue("Should be getting 3 from value got + " + value + " instead",
+ value.equals("3"));
+
+ testInt = oneTest.getFieldInt("IntTest");
+ assertTrue("Must get 123985 for blank field IntTest got "
+ + testInt + " instead", testInt == 123985);
+
+ testBoolean = oneTest.getFieldBoolean("BooleanTest");
+ assertTrue("Must get true for set field BooleanTest", testBoolean = true);
+
+ dt = oneTest.getFieldDate("DateTest");
+ assertTrue("Should not get a null date for set field", dt != null);
+
+ fl = oneTest.getFieldFloat("FloatTest");
+
+ assertTrue("Should get 3.141 for float value got " + fl + " instead",
+ java.lang.Math.abs(fl - 3.141) < .001);
+
+ } catch (DBException ex) {
+ ex.printStackTrace();
+ fail("Caught exception testing update support" + ex.getMessage());
+ }
+ }
+
+
+ /**
+ * Test Adding the object to the database. We also test adding duplicate
+ * keys to the database
+ */
+ public void testAdd() {
+
+ try {
+
+ /* Add a record */
+ Test1 oneTest = new Test1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", "1");
+ oneTest.setField("DateTest", "1999-10-01");
+ oneTest.setField("BooleanTest", true);
+ oneTest.setField("CharTest", "abcd");
+ oneTest.setField("DecimalTest", "3");
+ oneTest.setField("FloatTest", "3.141");
+ oneTest.setField("IntTest", 123985);
+ oneTest.setField("VarCharTest", "lmnop");
+
+ oneTest.add();
+ oneTest.clear();
+ } catch (DBException ce) {
+ log.error(ce);
+ fail("DB exception occurred - see log");
+ }
+ /* Try to add a duplicate - make sure it fails */
+ try {
+
+ /* Add a duplicate record */
+ Test1 oneTest = new Test1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", "1");
+ log.warn(
+ "About to throw intentional exception having to do with duplicate key. \nIgnore this dup-key exception.");
+ oneTest.add();
+ fail("Test add should have thrown exception on duplicate key here");
+ } catch (DBException ce) {
+
+ /* Do nothing here - it's good that we throw */
+ }
+
+ try {
+ Test1 oneTest = new Test1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", "1");
+ oneTest.retrieve();
+ } catch (DBException ce) {
+ log.error(ce);
+ fail("DB exception occurred on retrieve test - see log");
+ }
+ try {
+ Test1 oneTest = new Test1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", "1");
+
+ if (!oneTest.find()) {
+ fail("DB exception occurred on find test - see log");
+ }
+ } catch (DBException ce) {
+ ce.printStackTrace();
+ log.error(ce);
+ fail("DB exception occurred on find test - see log");
+ }
+ }
+
+ /**
+ * Tests to make sure that we can work with custom where clauses and nothing
+ * breaks.
+ */
+ public void testCustomWhereClause() {
+ try {
+ /* Add a record */
+ Test1 oneTest = new Test1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", "1");
+ oneTest.add();
+
+ oneTest.clear();
+ oneTest.setCustomWhereClause("TESTKEY = 1");
+ boolean found = oneTest.find();
+ assertTrue("Must find a record", found == true);
+
+ oneTest.clear();
+ oneTest.setCustomWhereClause("TESTKEY = 1");
+ int numRecords = oneTest.count();
+ assertTrue("Must have one record in the database table", numRecords == 1);
+ } catch (DBException ce) {
+ log.error(ce);
+ fail("DB exception occurred - see log");
+ }
+
+ }
+
+ /**
+ * Tests retrieve We also make sure that the exceptions we expect to be
+ * thrown are thrown... such as for bogus key values, keys not set.
+ */
+ public void testRetrieve() {
+ try {
+ /* Add a record */
+ Test1 oneTest = new Test1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", "1");
+ oneTest.add();
+
+ oneTest.clear();
+ oneTest.setField("TestKey", "1");
+ oneTest.retrieve();
+ assertTrue("Should have proper return value for testkey",
+ "1".equals(oneTest.getField("TestKey")));
+
+
+ oneTest.clear();
+ oneTest.setField("TestKey", "232");
+ try {
+ oneTest.retrieve();
+ fail("Retrieve for bogus key should have thrown DBRecordNotFoundException");
+ } catch (DBRecordNotFoundException ex) {
+ //we're ok here.
+ }
+
+ oneTest.clear();
+ try {
+ oneTest.retrieve();
+ fail("Retrieve with no keys set should have thrown an exception");
+ } catch (DBException dbe) {
+ //we're ok if we get here.
+ }
+ } catch (DBException ce) {
+ log.error(ce);
+ fail("DB exception occurred - see log");
+ }
+ }
+
+ /**
+ * Tests the 'setFieldsToRetrieve()' functionality.
+ */
+ public void testFieldsToRetrieve() {
+ try {
+ /* Add a record */
+ Test1 oneTest = new Test1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", "1");
+ oneTest.setField("IntTest", 24);
+ oneTest.setField("FloatTest", "3.141592");
+ oneTest.add();
+
+ oneTest.clear();
+ oneTest.setField("TestKey", "1");
+ oneTest.setFieldsToRetrieve("TestKey|IntTest");
+ oneTest.retrieve();
+ assertTrue("Should have proper return value for testkey",
+ "1".equals(oneTest.getField("TestKey")));
+
+ assertTrue("Should have retrieved TestInt field:",
+ 24 == oneTest.getFieldInt("IntTest"));
+
+ assertTrue("Should not have retrieved FloatTest:",
+ oneTest.getDataField("FloatTest").isNull());
+
+ //Now make sure clear() properly resets the fields to retrieve
+ oneTest.clear();
+ oneTest.setField("TestKey", "1");
+ oneTest.clearFieldsToRetrieve();
+ oneTest.retrieve();
+ assertTrue("Should have proper return value for testkey",
+ "1".equals(oneTest.getField("TestKey")));
+
+ assertTrue("Should have retrieved TestInt field:",
+ 24 == oneTest.getFieldInt("IntTest"));
+
+ assertTrue("Should have retrieved FloatTest:",
+ !oneTest.getDataField("FloatTest").isNull());
+
+
+ } catch (DBException ce) {
+ log.error(ce);
+ fail("DB exception occurred - see log");
+ }
+
+ }
+
+ /**
+ * This test demonstrates/tests the basic functionality for Text-based
+ * data fields. You set and retrieve the text data as usual and the
+ * underlying framework saves/retrieves the CLOB field when you want it.
+ */
+ public void testCLOBSupport() {
+ final String testString = "LONG Character Update Test";
+ final String testKey = "1";
+ try {
+ Test1 oneTest = new Test1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", testKey);
+ oneTest.setField("TextTest", testString);
+ oneTest.add();
+
+ oneTest.clear();
+ oneTest.setField("TestKey", testKey);
+ oneTest.retrieve();
+ String fieldData = oneTest.getField("TextTest");
+ assertTrue("CLOB Field data must not be null", fieldData != null);
+
+ assertTrue("CLOB field data must be equal to the original data. Got "
+ + fieldData + " instead", testString.equals(fieldData));
+
+ } catch (Throwable t) {
+ t.printStackTrace();
+ fail("Caught exception testing CLOB Support: " + t.getMessage());
+ }
+ }
+
+ /**
+ * This test makes sure that special characters such as backslashes
+ * or single quotes get properly escaped so we read them back properly
+ */
+ public void testSpecialCharacters() {
+ final String testString = "' \\ abcdefg";
+ final String testKey = "1";
+
+ try {
+ Test1 oneTest = new Test1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", testKey);
+ oneTest.setField("VarCharTest", testString);
+ oneTest.setField("CharTest", testString);
+ oneTest.add();
+
+ oneTest.clear();
+ oneTest.setField("TestKey", testKey);
+ oneTest.retrieve();
+ String fieldData = oneTest.getField("VarCharTest");
+ assertTrue("Varchar Field data must not be null", fieldData != null);
+
+ assertTrue("Varchar field data must be equal to the original data. Got "
+ + fieldData + " instead", testString.equals(fieldData));
+ fieldData = oneTest.getField("CharTest");
+ assertTrue("CharTest Field data must not be null", fieldData != null);
+
+ assertTrue("CharTest field data must be equal to the original data. Got "
+ + fieldData + " instead", testString.equals(fieldData));
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ fail("Caught exception processing Special Characters "
+ + ex.getMessage());
+ }
+
+ }
+
+
+ /**
+ * Test Transaction Capabilities
+ */
+ public void testTransactions() {
+ try {
+ DBConnectionPool pool = DBConnectionPool.getInstance(TestSystemInitializer.getTestContext());
+ DBConnection oneConnection = pool.getConnection("Test Transaction");
+ if (!oneConnection.supportsTransactions()) {
+ log.warn("Cannot test transactions because this DB does not support them.");
+ oneConnection.release();
+ return;
+ }
+
+
+ try {
+
+ // before test, make sure no object exists in db (during autocommit)
+ Test1 test1 = new Test1(oneConnection);
+ test1.setField("TestKey", 1);
+ if (test1.find()) test1.delete();
+
+ //
+ // turn on transaction
+ //
+ oneConnection.setAutoCommit(false);
+
+ // add item during transaction
+ test1 = new Test1(oneConnection);
+ test1.setField("TestKey", 1);
+ test1.setField("TextTest", "Long Test");
+ test1.add();
+
+ // different object
+ Test2 test2 = new Test2(oneConnection);
+ test2.setField("TestKey", 1);
+ test2.add();
+
+ //
+ // ok, rollback, which should remove objects
+ //
+ oneConnection.rollback();
+
+ assertTrue(test1.count() == 0);
+ assertTrue(test2.count() == 0);
+
+ //
+ //Now add and commit.
+ //
+ test1.setField("TestKey", 1);
+ test1.setField("TextTest", "Long Test");
+ test1.add();
+
+ test2.setField("TestKey", 1);
+ test2.add();
+ oneConnection.commit();
+ assertTrue(test1.count() == 1);
+ assertTrue(test2.count() == 1);
+
+ //
+ //Try clear, retrieve, update, clear, retrieve, update
+ //
+ test1.clear();
+ test1.setField("TestKey", 1);
+ test1.retrieve();
+
+ test1.setField("TextTest", "Some other value");
+ test1.update();
+
+ test1.clear();
+ test1.setField("TestKey", 1);
+ test1.setField("TextTest", "Yet another Value");
+ test1.update();
+
+ test1.clear();
+ test1.setField("TestKey", 1);
+ test1.retrieve();
+
+
+ } catch (DBException dbe) {
+ try {
+ oneConnection.rollback();
+ } catch (DBException ex) {
+ log.error("Error rolling back transaction");
+ }
+ dbe.printStackTrace();
+ fail("Caught DBException attempting to execute statements");
+ } finally {
+ pool.release(oneConnection);
+ }
+
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ fail("Caught exception processing TestTransaction "
+ + ex.getMessage());
+ }
+ }
+
+ /**
+ * This test demonstrates/tests the basic functionality for binary-based
+ * data fields. You must use the saveBinaryField as a separate method rather
+ * than setField.
+ */
+ public void testBLOBSupport() {
+ final Integer testInt = new Integer(-3544);
+ final String testKey = "1";
+ try {
+ java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream(128);
+ java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(bos);
+ oos.writeObject(testInt);
+ byte buffer[] = bos.toByteArray();
+
+
+ Test1 oneTest = new Test1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", testKey);
+ oneTest.add();
+ oneTest.saveBinaryField("BinaryTest", buffer);
+
+ oneTest.clear();
+ oneTest.setField("TestKey", testKey);
+ LobField query = new LobField();
+ query.setCriteria(oneTest);
+ java.io.InputStream is = query.getBlobStream("BinaryTest");
+ assertTrue("Blob Input stream data must not be null", is != null);
+
+ java.io.ObjectInputStream ois = new java.io.ObjectInputStream(is);
+ Object o = ois.readObject();
+ assertTrue("Read object must not be null", o != null);
+ assertTrue("Read object must be of type java.lang.Integer", o instanceof Integer);
+ Integer i = (Integer) o;
+ assertTrue("Binary field data must be equal to original data. Got " + i.intValue() +
+ " instead of " + testInt.intValue(), i.intValue() == testInt.intValue());
+
+
+ } catch (Throwable t) {
+ t.printStackTrace();
+ fail("Caught exception testing BLOB Support: " + t.getMessage());
+ }
+ }
+
+ /**
+ * Tests to make sure that after setting a null integer and adding it to the
+ * database, that the field is still null after a retrieval.
+ */
+ public void testNullFields() {
+ try {
+ /* Add a record */
+ Test1 oneTest = new Test1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", "1");
+ oneTest.add();
+ oneTest.clear();
+
+ oneTest.setField("TestKey", "1");
+ if (!oneTest.find()) {
+ fail("Unable to locate record we just updated");
+ }
+
+ assertTrue("Test Integer field must be null", oneTest.isFieldNull("IntTest"));
+ assertTrue("NumericTest field must be null", oneTest.isFieldNull("NumericTest"));
+ assertTrue("BigIntTestfield must be null", oneTest.isFieldNull("BigIntTest"));
+ assertTrue("BooleanTest must be null", oneTest.isFieldNull("BooleanTest"));
+ assertTrue("CharTest field must be null", oneTest.isFieldNull("CharTest"));
+ assertTrue("DateTest field must be null", oneTest.isFieldNull("DateTest"));
+ assertTrue("DecimalTest field must be null", oneTest.isFieldNull("DecimalTest"));
+ assertTrue("DoubleTest field must be null", oneTest.isFieldNull("DoubleTest"));
+ assertTrue("FloatTest field must be null", oneTest.isFieldNull("FloatTest"));
+ assertTrue("RealTest field must be null", oneTest.isFieldNull("RealTest"));
+ assertTrue("SmallIntTest field must be null", oneTest.isFieldNull("SmallIntTest"));
+ assertTrue("TimeTest field must be null", oneTest.isFieldNull("TimeTest"));
+ assertTrue("TimeStampTest field must be null", oneTest.isFieldNull("TimeStampTest"));
+ assertTrue("DateTimeTest field must be null", oneTest.isFieldNull("DateTimeTest"));
+ assertTrue("TinyIntTest field must be null", oneTest.isFieldNull("TinyIntTest"));
+ assertTrue("VarCharTest field must be null", oneTest.isFieldNull("VarCharTest"));
+ assertTrue("TextTest field must be null", oneTest.isFieldNull("TextTest"));
+ assertTrue("AutoIncTest field must NOT be null", !oneTest.isFieldNull("AutoIncTest"));
+
+ oneTest.setField("NumericTest", "3.1415");
+ oneTest.clear();
+ oneTest.setField("TestKey", "1");
+ //This is invalid because after we've done an update to a field that
+ //doesn't do autoinc'ing we lose data.
+// oneTest.update(); //We do this update to clear the cache.
+ oneTest.removeFromCache(oneTest);
+ oneTest.clear();
+ oneTest.setField("TestKey", "1");
+ oneTest.retrieve();
+ assertTrue("Test Integer field must be null", oneTest.isFieldNull("IntTest"));
+ assertTrue("NumericTest field must be null", oneTest.isFieldNull("NumericTest"));
+ assertTrue("BigIntTestfield must be null", oneTest.isFieldNull("BigIntTest"));
+ assertTrue("BooleanTest must be null", oneTest.isFieldNull("BooleanTest"));
+ assertTrue("CharTest field must be null", oneTest.isFieldNull("CharTest"));
+ assertTrue("DateTest field must be null", oneTest.isFieldNull("DateTest"));
+ assertTrue("DecimalTest field must be null", oneTest.isFieldNull("DecimalTest"));
+ assertTrue("DoubleTest field must be null", oneTest.isFieldNull("DoubleTest"));
+ assertTrue("FloatTest field must be null", oneTest.isFieldNull("FloatTest"));
+ assertTrue("RealTest field must be null", oneTest.isFieldNull("RealTest"));
+ assertTrue("SmallIntTest field must be null", oneTest.isFieldNull("SmallIntTest"));
+ assertTrue("TimeTest field must be null", oneTest.isFieldNull("TimeTest"));
+ assertTrue("TimeStampTest field must be null", oneTest.isFieldNull("TimeStampTest"));
+ assertTrue("DateTimeTest field must be null", oneTest.isFieldNull("DateTimeTest"));
+ assertTrue("TinyIntTest field must be null", oneTest.isFieldNull("TinyIntTest"));
+ assertTrue("VarCharTest field must be null", oneTest.isFieldNull("VarCharTest"));
+ assertTrue("TextTest field must be null", oneTest.isFieldNull("TextTest"));
+ assertTrue("AutoIncTest field must NOT be null", !oneTest.isFieldNull("AutoIncTest"));
+
+
+ oneTest.delete();
+ } catch (DBException ex) {
+ ex.printStackTrace();
+ fail("Caught exception testing null Integer" + ex.getMessage());
+ }
+
+ }
+
+ public void testNullValues() {
+ String resourceId = null;
+ try {
+ log.info("Test null values");
+ Test1 resource = new Test1();
+ resource.setDataContext(TestSystemInitializer.getTestContext());
+ resource.setField("TestKey", 1);
+ resource.setField("CharTest", "FZI");
+ resource.setField("DateTimeTest",
+ DateTime.getDateTimeForDB(resource.getDataContext()));
+ resource.add();
+ resourceId = resource.getField("TestKey");
+ resource = new Test1();
+ resource.setDataContext(TestSystemInitializer.getTestContext());
+ resource.setField("TestKey", resourceId);
+ resource.retrieve();
+ assertTrue(resource.isFieldNull("VarCharTest"));
+ assertTrue(resource.isFieldNull("IntTest"));
+ resource.retrieve();
+ assertTrue(resource.isFieldNull("VarCharTest"));
+ assertTrue(resource.isFieldNull("IntTest"));
+ resource.addOrUpdate();
+ resource = new Test1();
+ resource.setDataContext(TestSystemInitializer.getTestContext());
+ resource.setField("TestKey", resourceId);
+ resource.retrieve();
+ assertTrue(resource.isFieldNull("VarCharTest"));
+ assertTrue(resource.isFieldNull("IntTest"));
+ resource.setField("VarCharTest", "Hi!");
+ resource.addOrUpdate();
+ resource.retrieve();
+ assertFalse(resource.isFieldNull("VarCharTest"));
+ assertTrue(resource.isFieldNull("IntTest"));
+ resource.setField("VarCharTest", (String) null);
+ resource.addOrUpdate();
+ resource.retrieve();
+ assertTrue(resource.isFieldNull("VarCharTest"));
+ assertTrue(resource.isFieldNull("IntTest"));
+ } catch (Exception e) {
+ log.error(e);
+ fail("Exception occured:" + e.getMessage());
+ }
+ }
+
+
+ /**
+ * Tests the DBObject update routine.
+ */
+ public void testUpdate() {
+ try {
+ /* Add a record */
+ Test1 oneTest = new Test1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", "1");
+ oneTest.add();
+
+ oneTest.setField("TextTest", "abcdefg");
+ oneTest.setField("BooleanTest", true);
+ oneTest.setField("CharTest", "abcd");
+ oneTest.setField("DecimalTest", "3");
+ oneTest.setField("FloatTest", "3.141");
+ oneTest.setField("IntTest", 123985);
+ oneTest.setField("VarCharTest", "lmnop");
+ oneTest.setField("DateTest", "1999-10-01");
+ oneTest.update();
+
+ //Make sure it really was there.
+ oneTest.clear();
+ oneTest.setField("TestKey", "1");
+ if (!oneTest.find()) {
+ fail("Unable to locate record we just updated");
+ }
+ oneTest.delete();
+ } catch (DBException ex) {
+ ex.printStackTrace();
+ fail("Caught exception testing update support" + ex.getMessage());
+ }
+
+ }
+
+ /**
+ * This function tests adding blank dates since some databases don't tolerate
+ * it very well.
+ */
+ public void testDateAdd() {
+ final String testStringValue = "";
+ final String testKey = "1";
+ try {
+ Test1 oneTest = new Test1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", testKey);
+ oneTest.setField("DateTest", testStringValue);
+ oneTest.add();
+
+ oneTest.clear();
+ oneTest.setField("TestKey", testKey);
+ oneTest.retrieve();
+
+ String fieldData = oneTest.getField("DateTest");
+ assertTrue(fieldData.equals(testStringValue));
+
+ } catch (Throwable t) {
+ t.printStackTrace();
+ fail("Caught exception testing CLOB Support: " + t.getMessage());
+ }
+ }
+
+ public void testDateSetField() {
+ final String testKey = "1";
+ try {
+ Test1 oneTest = new Test1();
+ oneTest.setDataContext(TestSystemInitializer.getTestContext());
+ oneTest.setField("TestKey", testKey);
+ oneTest.setField("DateTest", new java.util.Date());
+ oneTest.set