KickJava   Java API By Example, From Geeks To Geeks.

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


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.misc.ConfigManager;
70 import com.jcorporate.expresso.core.misc.ConfigurationException;
71 import com.jcorporate.expresso.services.test.TestSystemInitializer;
72 import junit.framework.Test;
73 import junit.framework.TestCase;
74 import junit.framework.TestSuite;
75 import org.apache.log4j.Logger;
76
77 import java.util.Enumeration JavaDoc;
78 import java.util.Iterator JavaDoc;
79 import java.util.Vector JavaDoc;
80
81
82 /**
83  * A test case to verify the functions of "other" DB mapping capabilities in Expresso
84  */

85 public class OtherDBTest
86         extends TestCase {
87     private static Logger log = Logger.getLogger(OtherDBTest.class);
88
89     /**
90      * Constructs a test case with the given name.
91      *
92      * @param name
93      */

94     public OtherDBTest(String JavaDoc name) {
95         super(name);
96     } /* OtherDBTest(String) */
97
98     /**
99      * Counts the number of test cases executed by run(TestResult result).
100      *
101      * @return
102      */

103     public int countTestCases() {
104         return 1;
105     } /* countTestCases() */
106
107     /**
108      * Sets up the fixture, for example, open a network connection.
109      * This method is called before a test is executed.
110      */

111     protected void setUp()
112             throws Exception JavaDoc {
113         TestSystemInitializer.setUp();
114         try {
115             ConfigManager.getContext(TestSystemInitializer.getTestContext());
116         } catch (ConfigurationException ce) {
117             fail("There is no 'test' db/context set up - cannot run db object tests without a test context");
118         }
119
120         TestSchema ts = new TestSchema();
121         Vector JavaDoc v = TableCreator.getInstance().createAsNeeded(ts, TestSystemInitializer.getTestContext());
122
123         for (Enumeration JavaDoc ev = v.elements(); ev.hasMoreElements();) {
124             log.info((String JavaDoc) ev.nextElement());
125         }
126     } /* setUp() */
127
128
129     /**
130      * Tears down the fixture, for example, close a network connection.
131      * This method is called after a test is executed.
132      */

133     protected void tearDown()
134             throws Exception JavaDoc {
135         try {
136
137             /* Clean out the test tables */
138             Test1 test1List = new Test1();
139             test1List.setDataContext(TestSystemInitializer.getTestContext());
140
141             Test1 oneTest1 = null;
142
143             for (Iterator JavaDoc i = test1List.searchAndRetrieveList().iterator();
144                  i.hasNext();) {
145                 oneTest1 = (Test1) i.next();
146                 oneTest1.delete();
147             }
148
149             Test2 test2List = new Test2();
150             test2List.setDataContext(TestSystemInitializer.getTestContext());
151
152             Test2 oneTest2 = null;
153
154             for (Iterator JavaDoc i = test1List.searchAndRetrieveList().iterator();
155                  i.hasNext();) {
156                 oneTest2 = (Test2) i.next();
157                 oneTest2.delete();
158             }
159         } catch (DBException de) {
160             log.error(de);
161             fail("Unable to clean up test object tables");
162         }
163     } /* tearDown() */
164
165
166     /**
167      * Test verifying the object
168      */

169     public void testAdd() {
170         try {
171
172             /* Add a record */
173             Test1 oneTest = new Test1();
174             oneTest.setDataContext(TestSystemInitializer.getTestContext());
175             oneTest.setField("TestKey", "1");
176             oneTest.add();
177         } catch (DBException ce) {
178             log.error(ce);
179             fail("DB exception occurred - see log");
180         }
181         /* Try to add a duplicate - make sure it fails */
182         try {
183
184             /* Add a duplicate record */
185             Test1 oneTest = new Test1();
186             oneTest.setDataContext(TestSystemInitializer.getTestContext());
187             oneTest.setField("TestKey", "1");
188             oneTest.add();
189             fail("Test add should have thrown exception on duplicate key here");
190         } catch (DBException ce) {
191
192             /* Do nothing here - it's good that we throw */
193         }
194         try {
195             Test1 oneTest = new Test1();
196             oneTest.setDataContext(TestSystemInitializer.getTestContext());
197             oneTest.setField("TestKey", "1");
198             oneTest.retrieve();
199         } catch (DBException ce) {
200             log.error(ce);
201             fail("DB exception occurred on retrieve test - see log");
202         }
203         try {
204             Test1 oneTest = new Test1();
205             oneTest.setDataContext(TestSystemInitializer.getTestContext());
206             oneTest.setField("TestKey", "1");
207
208             if (!oneTest.find()) {
209                 fail("DB exception occurred on retrieve test - see log");
210             }
211         } catch (DBException ce) {
212             log.error(ce);
213             fail("DB exception occurred on find test - see log");
214         }
215     } /* testSimpleCaching() */
216
217     /**
218      * Define the suite of tests that verify each function of the cache
219      *
220      * @return
221      */

222     public static Test suite() {
223         TestSuite suite = new TestSuite(OtherDBTest.class);
224
225         return suite;
226     } /* suite() */
227
228 } /* OtherDBTest */
229
230 /* OtherDBTest */
Popular Tags