KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > dataobjects > jdbc > tests > LobFieldTest


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 package com.jcorporate.expresso.core.dataobjects.jdbc.tests;
65
66 import com.jcorporate.expresso.core.dataobjects.jdbc.LobField;
67 import com.jcorporate.expresso.core.db.DBException;
68 import com.jcorporate.expresso.core.db.TableCreator;
69 import com.jcorporate.expresso.core.dbobj.tests.Test1;
70 import com.jcorporate.expresso.core.dbobj.tests.Test2;
71 import com.jcorporate.expresso.core.dbobj.tests.TestSchema;
72 import com.jcorporate.expresso.core.misc.ConfigManager;
73 import com.jcorporate.expresso.core.misc.ConfigurationException;
74 import com.jcorporate.expresso.services.test.TestSystemInitializer;
75 import junit.framework.Test;
76 import junit.framework.TestCase;
77 import junit.framework.TestSuite;
78 import org.apache.log4j.Logger;
79
80 import java.sql.Blob JavaDoc;
81 import java.util.Enumeration JavaDoc;
82 import java.util.Vector JavaDoc;
83
84 /**
85  * Unit test cases for testing the LobField class.
86  *
87  * @author Michael Rimov
88  * @version $Revision: 1.2 $ on $Date: 2004/11/17 20:48:22 $
89  * @since Expresso 5.1
90  */

91
92 public class LobFieldTest extends TestCase {
93
94     private static Logger log = Logger.getLogger(LobFieldTest.class);
95
96     /**
97      * Constructs a test case with the given name.
98      *
99      * @param name The name of the test case
100      */

101     public LobFieldTest(String JavaDoc name) {
102         super(name);
103     } /* DBObjectTest(String) */
104
105
106     public static void main(String JavaDoc[] args)
107             throws Exception JavaDoc {
108
109         //Set the system properties we need
110
junit.textui.TestRunner.run(suite());
111         ConfigManager.destroy();
112     }
113
114     /**
115      * Sets up the fixture, for example, open a network connection.
116      * This method is called before a test is executed.
117      */

118     public void setUp()
119             throws Exception JavaDoc {
120         TestSystemInitializer.setUp();
121
122         try {
123             ConfigManager.getContext(TestSystemInitializer.getTestContext());
124         } catch (ConfigurationException ce) {
125             fail(
126                     "Specified context to test:" + TestSystemInitializer.getTestContext() + " but couldn't find that context");
127         }
128
129         TestSchema ts = new TestSchema();
130         Vector JavaDoc v = TableCreator.getInstance().createAsNeeded(ts, TestSystemInitializer.getTestContext());
131
132         for (Enumeration JavaDoc ev = v.elements(); ev.hasMoreElements();) {
133             log.info((String JavaDoc) ev.nextElement());
134         }
135
136         Test1 test1List = new Test1();
137         test1List.setDataContext(TestSystemInitializer.getTestContext());
138         test1List.deleteAll();
139
140     } /* setUp() */
141
142
143     /**
144      * Tears down the fixture, for example, close a network connection.
145      * This method is called after a test is executed.
146      */

147     public void tearDown()
148             throws Exception JavaDoc {
149         try {
150
151             /* Clean out the test tables */
152             Test1 test1List = new Test1();
153             test1List.setDataContext(TestSystemInitializer.getTestContext());
154             test1List.deleteAll();
155
156             Test2 test2List = new Test2();
157             test2List.setDataContext(TestSystemInitializer.getTestContext());
158             test2List.deleteAll();
159
160         } catch (DBException de) {
161             de.printStackTrace();
162             log.error(de);
163         }
164     } /* tearDown() */
165
166
167     /**
168      * Define the suite of tests that verify each function of the cache
169      *
170      * @return an instantiated test suite
171      */

172     public static Test suite() {
173         TestSuite suite = new TestSuite(LobFieldTest.class);
174
175         return suite;
176     } /* suite() */
177
178
179     /**
180      * This test demonstrates/tests the basic functionality for binary-based
181      * data fields. You must use the saveBinaryField as a separate method rather
182      * than setField.
183      */

184     public void testBLOBSupport() {
185         LobField lf = new LobField();
186         final Integer JavaDoc testInt = new Integer JavaDoc(-3544);
187         final String JavaDoc testKey = "1";
188         try {
189             java.io.ByteArrayOutputStream JavaDoc bos = new java.io.ByteArrayOutputStream JavaDoc(128);
190             java.io.ObjectOutputStream JavaDoc oos = new java.io.ObjectOutputStream JavaDoc(bos);
191             oos.writeObject(testInt);
192             byte buffer[] = bos.toByteArray();
193             java.io.ByteArrayInputStream JavaDoc bis = new java.io.ByteArrayInputStream JavaDoc(buffer);
194
195
196             Test1 oneTest = new Test1();
197             oneTest.setDataContext(TestSystemInitializer.getTestContext());
198             oneTest.setField("TestKey", testKey);
199             oneTest.add();
200
201             lf.setCriteria(oneTest);
202             lf.saveBlob("BlobTest", bis, buffer.length);
203
204             java.io.InputStream JavaDoc is = lf.getBlobStream("BlobTest");
205
206             assertTrue("Blob Input stream data must not be null", is != null);
207
208             java.io.ObjectInputStream JavaDoc ois = new java.io.ObjectInputStream JavaDoc(is);
209             Object JavaDoc o = ois.readObject();
210             assertTrue("Read object must not be null", o != null);
211             assertTrue("Read object must be of type java.lang.Integer", o instanceof Integer JavaDoc);
212             Integer JavaDoc i = (Integer JavaDoc) o;
213             assertTrue("Binary field data must be equal to original data. Got " + i.intValue() +
214                     " instead of " + testInt.intValue(), i.intValue() == testInt.intValue());
215
216
217         } catch (Throwable JavaDoc t) {
218             t.printStackTrace();
219             fail("Caught exception testing BLOB Support: " + t.getMessage());
220         } finally {
221             lf.close();
222         }
223     }
224
225     /**
226      * Test Blob support with a larger quantitiy of data
227      * <p/>
228      * (Originally needed because Oracle Blob's work with standard jdbc operations for small sizes (~ <4K), but
229      * not with larger sizes)
230      */

231     public void testBLOBSupportLargeData() {
232         LobField lf = new LobField();
233         final Integer JavaDoc testInt = new Integer JavaDoc(-3544);
234         Integer JavaDoc[] testIntArray = new Integer JavaDoc[1000];
235         final String JavaDoc testKey = "1";
236         try {
237             for (int i = 0; i < 1000; i++) {
238                 testIntArray[i] = testInt;
239             }
240
241             java.io.ByteArrayOutputStream JavaDoc bos = new java.io.ByteArrayOutputStream JavaDoc(128000);
242             java.io.ObjectOutputStream JavaDoc oos = new java.io.ObjectOutputStream JavaDoc(bos);
243             oos.writeObject(testIntArray);
244             byte buffer[] = bos.toByteArray();
245             java.io.ByteArrayInputStream JavaDoc bis = new java.io.ByteArrayInputStream JavaDoc(buffer);
246
247
248             Test1 oneTest = new Test1();
249             oneTest.setDataContext(TestSystemInitializer.getTestContext());
250             oneTest.setField("TestKey", testKey);
251             oneTest.add();
252
253             lf.setCriteria(oneTest);
254             lf.saveBlob("BlobTest", bis, buffer.length);
255
256             java.io.InputStream JavaDoc is = lf.getBlobStream("BlobTest");
257
258             assertTrue("Blob Input stream data must not be null", is != null);
259
260             java.io.ObjectInputStream JavaDoc ois = new java.io.ObjectInputStream JavaDoc(is);
261             Object JavaDoc o = ois.readObject();
262             assertTrue("Read object must not be null", o != null);
263             assertTrue("Read object must be of type java.lang.Integer[]", o instanceof Integer JavaDoc[]);
264             Integer JavaDoc[] i = (Integer JavaDoc[]) o;
265
266             boolean objectsEqual = true;
267             for (int j = 0; j < i.length; j++) {
268                 if (i[j].intValue() != testIntArray[j].intValue()) {
269                     objectsEqual = false;
270                     break;
271                 }
272             }
273             assertTrue("Binary field data must be equal to original data.", objectsEqual);
274
275
276         } catch (Throwable JavaDoc t) {
277             t.printStackTrace();
278             fail("Caught exception testing BLOB Support: " + t.getMessage());
279         } finally {
280             lf.close();
281         }
282     }
283
284     /**
285      * Test Blob support with a larger quantitiy of data
286      * <p/>
287      * Specifically, this test tests the LobField.saveBlob(String, Blob) method
288      */

289     public void testBLOBSupportLargeData2() {
290         LobField lf = new LobField();
291         LobField lf2 = new LobField();
292         final Integer JavaDoc testInt = new Integer JavaDoc(-3544);
293         Integer JavaDoc[] testIntArray = new Integer JavaDoc[1000];
294         final String JavaDoc testKey = "1";
295         final String JavaDoc testKey2 = "2";
296         try {
297             for (int i = 0; i < 1000; i++) {
298                 testIntArray[i] = testInt;
299             }
300
301             java.io.ByteArrayOutputStream JavaDoc bos = new java.io.ByteArrayOutputStream JavaDoc(100000);
302             java.io.ObjectOutputStream JavaDoc oos = new java.io.ObjectOutputStream JavaDoc(bos);
303             oos.writeObject(testIntArray);
304             byte buffer[] = bos.toByteArray();
305             java.io.ByteArrayInputStream JavaDoc bis = new java.io.ByteArrayInputStream JavaDoc(buffer);
306
307             Test1 oneTest = new Test1();
308             oneTest.setDataContext(TestSystemInitializer.getTestContext());
309             oneTest.setField("TestKey", testKey);
310             oneTest.add();
311
312             lf.setCriteria(oneTest);
313             lf.saveBlob("BlobTest", bis, buffer.length);
314
315             Blob JavaDoc blob = lf.getBlob("BlobTest");
316             Test1 oneTest2 = new Test1();
317             oneTest2.setDataContext(TestSystemInitializer.getTestContext());
318             oneTest2.setField("TestKey", testKey2);
319             oneTest2.add();
320             lf2.setCriteria(oneTest2);
321             lf2.saveBlob("BlobTest", blob);
322
323             java.io.InputStream JavaDoc is = lf2.getBlobStream("BlobTest");
324
325             assertTrue("Blob Input stream data must not be null", is != null);
326
327             java.io.ObjectInputStream JavaDoc ois = new java.io.ObjectInputStream JavaDoc(is);
328             Object JavaDoc o = ois.readObject();
329             assertTrue("Read object must not be null", o != null);
330             assertTrue("Read object must be of type java.lang.Integer[]", o instanceof Integer JavaDoc[]);
331             Integer JavaDoc[] i = (Integer JavaDoc[]) o;
332
333             boolean objectsEqual = true;
334             for (int j = 0; j < i.length; j++) {
335                 if (i[j].intValue() != testIntArray[j].intValue()) {
336                     objectsEqual = false;
337                     break;
338                 }
339             }
340             assertTrue("Binary field data must be equal to original data.", objectsEqual);
341
342
343         } catch (Throwable JavaDoc t) {
344             t.printStackTrace();
345             fail("Caught exception testing BLOB Support: " + t.getMessage());
346         } finally {
347             lf.close();
348         }
349     }
350
351     /**
352      * This test demonstrates/tests the basic functionality for Text-based
353      * data fields. You set and retrieve the text data as usual and the
354      * underlying framework saves/retrieves the CLOB field when you want it.
355      */

356     public void testCLOBSupport() {
357         final String JavaDoc testString = "LONG Character Update Test";
358         final String JavaDoc testKey = "1";
359         LobField lf = new LobField();
360         try {
361             Test1 oneTest = new Test1();
362             oneTest.setDataContext(TestSystemInitializer.getTestContext());
363             oneTest.setField("TestKey", testKey);
364             oneTest.add();
365
366             lf.setCriteria(oneTest);
367             java.io.InputStream JavaDoc is = lf.getBlobStream("BlobTest");
368             assertTrue("BLOB Stream should be null", is == null);
369
370             lf.saveClob("ClobTest", testString);
371             String JavaDoc fieldData = lf.getClobString("ClobTest");
372
373             assertTrue("CLOB Field data must not be null", fieldData != null);
374
375             assertTrue("CLOB field data must be equal to the original data. Got "
376                     + fieldData + " instead", testString.equals(fieldData));
377
378         } catch (Throwable JavaDoc t) {
379             t.printStackTrace();
380             fail("Caught exception testing CLOB Support: " + t.getMessage());
381         } finally {
382             lf.close();
383         }
384     }
385
386     /**
387      * This test makes sure that the LOB Field support doesn't crash if field
388      * values are null.
389      */

390     public void testNullSupport() {
391         final String JavaDoc testString = "LONG Character Update Test";
392         final String JavaDoc testKey = "1";
393         LobField lf = new LobField();
394         try {
395             Test1 oneTest = new Test1();
396             oneTest.setDataContext(TestSystemInitializer.getTestContext());
397             oneTest.setField("TestKey", testKey);
398             oneTest.add();
399
400             lf.setCriteria(oneTest);
401             lf.saveClob("ClobTest", (String JavaDoc) null);
402             String JavaDoc fieldData = lf.getClobString("ClobTest");
403             assertTrue("Field data should be null", fieldData == null);
404
405         } catch (Throwable JavaDoc t) {
406             t.printStackTrace();
407             fail("Caught exception testing Null Support: " + t.getMessage());
408         } finally {
409             lf.close();
410         }
411
412     }
413
414
415 }
Popular Tags