KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > dbobj > tests > MultiDBObjectTest


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

64
65 package com.jcorporate.expresso.core.dbobj.tests;
66
67 import com.jcorporate.expresso.core.db.DBException;
68 import com.jcorporate.expresso.core.db.TableCreator;
69 import com.jcorporate.expresso.core.dbobj.MultiDBObject;
70 import com.jcorporate.expresso.core.misc.ConfigManager;
71 import com.jcorporate.expresso.core.misc.ConfigurationException;
72 import com.jcorporate.expresso.services.test.TestSystemInitializer;
73 import junit.framework.Test;
74 import junit.framework.TestCase;
75 import junit.framework.TestSuite;
76 import org.apache.log4j.Logger;
77
78 import java.util.Enumeration JavaDoc;
79 import java.util.Iterator JavaDoc;
80 import java.util.Vector JavaDoc;
81
82
83 /**
84  * A test case to verify the functions of DBObject
85  */

86 public class MultiDBObjectTest
87         extends TestCase {
88     private static Logger log = Logger.getLogger(MultiDBObjectTest.class);
89
90
91     public static void main(String JavaDoc[] args)
92             throws Exception JavaDoc {
93
94         //Set the system properties we need
95
junit.textui.TestRunner.run(suite());
96     }
97
98     /**
99      * Constructs a test case with the given name.
100      *
101      * @param name name of the test
102      */

103     public MultiDBObjectTest(String JavaDoc name) {
104         super(name);
105     } /* MultiDBObjectTest(String) */
106
107     /**
108      * Counts the number of test cases executed by run(TestResult result).
109      *
110      * @return 1
111      */

112     public int countTestCases() {
113         return 1;
114     } /* countTestCases() */
115
116     /**
117      * Sets up the fixture, for example, open a network connection.
118      * This method is called before a test is executed.
119      */

120     protected void setUp()
121             throws Exception JavaDoc {
122         TestSystemInitializer.setUp();
123
124         try {
125             ConfigManager.getContext(TestSystemInitializer.getTestContext());
126         } catch (ConfigurationException ce) {
127             fail("There is no 'test' db/context set up - cannot run db object tests without a test context");
128         }
129
130         TestSchema ts = new TestSchema();
131         Vector JavaDoc v = TableCreator.getInstance().createAsNeeded(ts, TestSystemInitializer.getTestContext());
132
133         for (Enumeration JavaDoc ev = v.elements(); ev.hasMoreElements();) {
134             if (log.isInfoEnabled()) {
135                 log.info((String JavaDoc) ev.nextElement());
136             }
137         }
138         try {
139             Test1 oneTest = new Test1();
140             oneTest.setDataContext(TestSystemInitializer.getTestContext());
141             Test2 oneTest2 = new Test2();
142             oneTest2.setDataContext(TestSystemInitializer.getTestContext());
143             try {
144                 oneTest.deleteAll();
145                 oneTest2.deleteAll();
146             } catch (Exception JavaDoc ex) {
147             }
148             oneTest.setField("TestKey", "1");
149             oneTest.add();
150
151             oneTest2.setField("TestKey", "1");
152             oneTest2.setField("Descrip", "Test Record");
153             oneTest2.add();
154         } catch (DBException de) {
155             de.printStackTrace();
156             log.error(de);
157             fail("Unable to clean up test object tables");
158         }
159     } /* setUp() */
160
161
162     /**
163      * Tears down the fixture, for example, close a network connection.
164      * This method is called after a test is executed.
165      */

166     protected void tearDown()
167             throws Exception JavaDoc {
168         boolean error = false;
169
170         /* Clean out the test tables */
171
172         try {
173             Test1 test1List = new Test1();
174             test1List.setDataContext(TestSystemInitializer.getTestContext());
175
176             Test1 oneTest1 = null;
177             for (Iterator JavaDoc e = test1List.searchAndRetrieveList().iterator();
178                  e.hasNext();) {
179                 oneTest1 = (Test1) e.next();
180                 oneTest1.delete();
181             }
182         } catch (DBException de) {
183             log.error("Error cleaning up test case", de);
184         }
185
186         try {
187             Test2 test2List = new Test2();
188             test2List.setDataContext(TestSystemInitializer.getTestContext());
189
190             Test2 oneTest2 = null;
191
192             for (Iterator JavaDoc e2 = test2List.searchAndRetrieveList().iterator();
193                  e2.hasNext();) {
194                 oneTest2 = (Test2) e2.next();
195                 oneTest2.delete();
196             }
197         } catch (DBException de) {
198             log.error("Error cleaning up test case", de);
199         }
200
201         assertTrue("Error cleaning up test values", !error);
202     } /* tearDown() */
203
204
205     /**
206      * Test adding/updating, etc
207      */

208     public void testMulti() {
209         try {
210             MultiDBObject md = new MultiDBObject();
211             md.setDBName(TestSystemInitializer.getTestContext());
212             md.addDBObj("com.jcorporate.expresso.core.dbobj.tests.Test1",
213                     "test1");
214             md.addDBObj("com.jcorporate.expresso.core.dbobj.tests.Test2",
215                     "test2");
216             md.setForeignKey("test1", "TestKey", "test2", "TestKey");
217
218             int recCount = 0;
219             MultiDBObject oneMD = null;
220
221             for (Iterator JavaDoc ee = md.searchAndRetrieveList().iterator();
222                  ee.hasNext();) {
223                 recCount++;
224                 oneMD = (MultiDBObject) ee.next();
225
226
227                 if (!oneMD.getField("test2", "Descrip").equals("Test Record")) {
228                     fail("Got wrong Descrip field (" +
229                             oneMD.getField("test2", "Descrip") +
230                             ") from retrieve of joined objects.");
231                 }
232             }
233             if (recCount == 0) {
234                 fail("Did not retrieve the joined records correctly");
235             }
236         } catch (DBException ce) {
237             ce.printStackTrace();
238             log.error(ce);
239             fail("DB exception occurred - see log");
240         }
241     }
242
243     /**
244      * Define the suite of tests that verify each function of the cache
245      *
246      * @return an instantiated TestSuite for this class
247      */

248     public static Test suite() {
249         TestSuite suite = new TestSuite(MultiDBObjectTest.class);
250
251         return suite;
252     } /* suite() */
253
254 } /* MultiDBObjectTest */
255
256 /* MultiDBObjectTest */
Popular Tags