001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: /*
051: * LobTest.java
052: *
053: * Created on April 19, 2002, 2:22 PM
054: */
055:
056: package org.jaffa.persistence.blackboxtests;
057:
058: import junit.framework.TestCase;
059: import java.util.*;
060: import org.jaffa.persistence.UOW;
061: import org.jaffa.persistence.Criteria;
062: import org.jaffa.persistence.domainobjects.*;
063:
064: /**
065: *
066: * @author GautamJ
067: * @version
068: */
069: public class LobTest extends TestCase {
070:
071: private static StringBuffer buf1 = new StringBuffer();
072: static {
073: for (int i = 0; i < 10000; i++)
074: buf1.append('Z');
075: }
076: private static final String CLOB_FIELD = buf1.toString();
077:
078: private static StringBuffer buf2 = new StringBuffer();
079: static {
080: for (int i = 0; i < 10000; i++)
081: buf2.append('Z');
082: }
083: private static final String BLOB_FIELD = buf2.toString();
084:
085: private UOW m_uow = null;
086:
087: /** Creates new QueryTest
088: * @param name The name of the test case.
089: */
090: public LobTest(String name) {
091: super (name);
092: }
093:
094: /** Sets up the fixture, by creating the UOW. This method is called before a test is executed.
095: */
096: protected void setUp() {
097: try {
098: m_uow = new UOW();
099: } catch (Exception e) {
100: e.printStackTrace();
101: fail("Failed to create a UOW: " + e.toString());
102: }
103: }
104:
105: /** Tears down the fixture, by closing the UOW. This method is called after a test is executed.
106: */
107: protected void tearDown() {
108: try {
109: if (m_uow != null)
110: m_uow.rollback();
111: m_uow = null;
112: } catch (Exception e) {
113: e.printStackTrace();
114: fail("Failed to rollback a UOW: " + e.toString());
115: }
116: }
117:
118: /** Creates ZZ_TEST_PART_REMARKS_PICTURE records having clob and blob elements. It then checks if the data has been added.
119: */
120: public void testAddClobAndBlob() {
121: try {
122: // create 3 records
123: PartRemarksPicture partRemarksPicture = null;
124: partRemarksPicture = (PartRemarksPicture) m_uow
125: .newPersistentInstance(PartRemarksPicture.class);
126: partRemarksPicture.updatePart("Z-TESTPART-01");
127: partRemarksPicture.updateRemarks(CLOB_FIELD + "01");
128: partRemarksPicture.updatePicture((BLOB_FIELD + "01")
129: .getBytes());
130: m_uow.add(partRemarksPicture);
131:
132: partRemarksPicture = (PartRemarksPicture) m_uow
133: .newPersistentInstance(PartRemarksPicture.class);
134: partRemarksPicture.updatePart("Z-TESTPART-02");
135: partRemarksPicture.updateRemarks(CLOB_FIELD + "02");
136: partRemarksPicture.updatePicture((BLOB_FIELD + "02")
137: .getBytes());
138: m_uow.add(partRemarksPicture);
139:
140: partRemarksPicture = (PartRemarksPicture) m_uow
141: .newPersistentInstance(PartRemarksPicture.class);
142: partRemarksPicture.updatePart("Z-TESTPART-03");
143: partRemarksPicture.updateRemarks(CLOB_FIELD + "03");
144: partRemarksPicture.updatePicture((BLOB_FIELD + "03")
145: .getBytes());
146: m_uow.add(partRemarksPicture);
147: m_uow.commit();
148:
149: // now check if they have been added
150: m_uow = new UOW();
151: Criteria c = new Criteria();
152: c.setTable(PartRemarksPictureMeta.getName());
153: c.addCriteria(PartRemarksPictureMeta.PART,
154: Criteria.RELATIONAL_BEGINS_WITH, "Z-");
155: c.addOrderBy(PartRemarksPictureMeta.PART,
156: Criteria.ORDER_BY_ASC);
157: Collection col = m_uow.query(c);
158: // fetch in all the records
159: for (Iterator i = col.iterator(); i.hasNext();)
160: i.next();
161: assertEquals(3, col.size());
162: PartRemarksPicture[] partRemarksPictures = (PartRemarksPicture[]) col
163: .toArray(new PartRemarksPicture[0]);
164: assertEquals("Z-TESTPART-01", partRemarksPictures[0]
165: .getPart());
166: assertEquals(CLOB_FIELD + "01", partRemarksPictures[0]
167: .getRemarks());
168: assertTrue(Arrays.equals((BLOB_FIELD + "01").getBytes(),
169: partRemarksPictures[0].getPicture()));
170:
171: assertEquals("Z-TESTPART-02", partRemarksPictures[1]
172: .getPart());
173: assertEquals(CLOB_FIELD + "02", partRemarksPictures[1]
174: .getRemarks());
175: assertTrue(Arrays.equals((BLOB_FIELD + "02").getBytes(),
176: partRemarksPictures[1].getPicture()));
177:
178: assertEquals("Z-TESTPART-03", partRemarksPictures[2]
179: .getPart());
180: assertEquals(CLOB_FIELD + "03", partRemarksPictures[2]
181: .getRemarks());
182: assertTrue(Arrays.equals((BLOB_FIELD + "03").getBytes(),
183: partRemarksPictures[2].getPicture()));
184:
185: // now delete the records
186: m_uow.delete(partRemarksPictures[0]);
187: m_uow.delete(partRemarksPictures[1]);
188: m_uow.delete(partRemarksPictures[2]);
189: m_uow.commit();
190:
191: } catch (Exception e) {
192: e.printStackTrace();
193: fail();
194: }
195: }
196:
197: /** Creates ZZ_TEST_PART_REMARKS_PICTURE records having clob and blob elements.
198: * It then retrieves and updates the rows. Finally it checks if the data has been updated.
199: */
200: public void testUpdateClob() {
201: try {
202: // create 3 records
203: PartRemarksPicture partRemarksPicture = null;
204: partRemarksPicture = (PartRemarksPicture) m_uow
205: .newPersistentInstance(PartRemarksPicture.class);
206: partRemarksPicture.updatePart("Z-TESTPART-01");
207: partRemarksPicture.updateRemarks(CLOB_FIELD + "01");
208: partRemarksPicture.updatePicture((BLOB_FIELD + "01")
209: .getBytes());
210: m_uow.add(partRemarksPicture);
211:
212: partRemarksPicture = (PartRemarksPicture) m_uow
213: .newPersistentInstance(PartRemarksPicture.class);
214: partRemarksPicture.updatePart("Z-TESTPART-02");
215: partRemarksPicture.updateRemarks(CLOB_FIELD + "02");
216: partRemarksPicture.updatePicture((BLOB_FIELD + "02")
217: .getBytes());
218: m_uow.add(partRemarksPicture);
219:
220: partRemarksPicture = (PartRemarksPicture) m_uow
221: .newPersistentInstance(PartRemarksPicture.class);
222: partRemarksPicture.updatePart("Z-TESTPART-03");
223: partRemarksPicture.updateRemarks(CLOB_FIELD + "03");
224: partRemarksPicture.updatePicture((BLOB_FIELD + "03")
225: .getBytes());
226: m_uow.add(partRemarksPicture);
227: m_uow.commit();
228:
229: // now check if they have been added
230: m_uow = new UOW();
231: Criteria c = new Criteria();
232: c.setTable(PartRemarksPictureMeta.getName());
233: c.addCriteria(PartRemarksPictureMeta.PART,
234: Criteria.RELATIONAL_BEGINS_WITH, "Z-");
235: c.addOrderBy(PartRemarksPictureMeta.PART,
236: Criteria.ORDER_BY_ASC);
237: Collection col = m_uow.query(c);
238: // fetch in all the records
239: for (Iterator i = col.iterator(); i.hasNext();)
240: i.next();
241: assertEquals(3, col.size());
242: PartRemarksPicture[] partRemarksPictures = (PartRemarksPicture[]) col
243: .toArray(new PartRemarksPicture[0]);
244: assertEquals("Z-TESTPART-01", partRemarksPictures[0]
245: .getPart());
246: assertEquals(CLOB_FIELD + "01", partRemarksPictures[0]
247: .getRemarks());
248: assertTrue(Arrays.equals((BLOB_FIELD + "01").getBytes(),
249: partRemarksPictures[0].getPicture()));
250:
251: assertEquals("Z-TESTPART-02", partRemarksPictures[1]
252: .getPart());
253: assertEquals(CLOB_FIELD + "02", partRemarksPictures[1]
254: .getRemarks());
255: assertTrue(Arrays.equals((BLOB_FIELD + "02").getBytes(),
256: partRemarksPictures[1].getPicture()));
257:
258: assertEquals("Z-TESTPART-03", partRemarksPictures[2]
259: .getPart());
260: assertEquals(CLOB_FIELD + "03", partRemarksPictures[2]
261: .getRemarks());
262: assertTrue(Arrays.equals((BLOB_FIELD + "03").getBytes(),
263: partRemarksPictures[2].getPicture()));
264:
265: // now update the records
266: partRemarksPictures[0].updateRemarks(null);
267: partRemarksPictures[1]
268: .updateRemarks("Z-UPDATEDREMARKS-022");
269: m_uow.update(partRemarksPictures[0]);
270: m_uow.update(partRemarksPictures[1]);
271: m_uow.commit();
272:
273: // now check if the updates were successful
274: m_uow = new UOW();
275: c = new Criteria();
276: c.setTable(PartRemarksPictureMeta.getName());
277: c.addCriteria(PartRemarksPictureMeta.PART,
278: Criteria.RELATIONAL_BEGINS_WITH, "Z-");
279: c.addOrderBy(PartRemarksPictureMeta.PART,
280: Criteria.ORDER_BY_ASC);
281: col = m_uow.query(c);
282: // fetch in all the records
283: for (Iterator i = col.iterator(); i.hasNext();)
284: i.next();
285: assertEquals(3, col.size());
286: partRemarksPictures = (PartRemarksPicture[]) col
287: .toArray(new PartRemarksPicture[0]);
288: assertNull(partRemarksPictures[0].getRemarks());
289: assertEquals("Z-UPDATEDREMARKS-022", partRemarksPictures[1]
290: .getRemarks());
291: assertEquals(CLOB_FIELD + "03", partRemarksPictures[2]
292: .getRemarks());
293:
294: // now delete the records
295: m_uow.delete(partRemarksPictures[0]);
296: m_uow.delete(partRemarksPictures[1]);
297: m_uow.delete(partRemarksPictures[2]);
298: m_uow.commit();
299:
300: } catch (Exception e) {
301: e.printStackTrace();
302: fail();
303: }
304: }
305:
306: /** Creates ZZ_TEST_PART_REMARKS_PICTURE records having clob and blob elements.
307: * It then retrieves and updates the rows. Finally it checks if the data has been updated.
308: */
309: public void testUpdateBlob() {
310: try {
311: // create 3 records
312: PartRemarksPicture partRemarksPicture = null;
313: partRemarksPicture = (PartRemarksPicture) m_uow
314: .newPersistentInstance(PartRemarksPicture.class);
315: partRemarksPicture.updatePart("Z-TESTPART-01");
316: partRemarksPicture.updateRemarks(CLOB_FIELD + "01");
317: partRemarksPicture.updatePicture((BLOB_FIELD + "01")
318: .getBytes());
319: m_uow.add(partRemarksPicture);
320:
321: partRemarksPicture = (PartRemarksPicture) m_uow
322: .newPersistentInstance(PartRemarksPicture.class);
323: partRemarksPicture.updatePart("Z-TESTPART-02");
324: partRemarksPicture.updateRemarks(CLOB_FIELD + "02");
325: partRemarksPicture.updatePicture((BLOB_FIELD + "02")
326: .getBytes());
327: m_uow.add(partRemarksPicture);
328:
329: partRemarksPicture = (PartRemarksPicture) m_uow
330: .newPersistentInstance(PartRemarksPicture.class);
331: partRemarksPicture.updatePart("Z-TESTPART-03");
332: partRemarksPicture.updateRemarks(CLOB_FIELD + "03");
333: partRemarksPicture.updatePicture((BLOB_FIELD + "03")
334: .getBytes());
335: m_uow.add(partRemarksPicture);
336: m_uow.commit();
337:
338: // now check if they have been added
339: m_uow = new UOW();
340: Criteria c = new Criteria();
341: c.setTable(PartRemarksPictureMeta.getName());
342: c.addCriteria(PartRemarksPictureMeta.PART,
343: Criteria.RELATIONAL_BEGINS_WITH, "Z-");
344: c.addOrderBy(PartRemarksPictureMeta.PART,
345: Criteria.ORDER_BY_ASC);
346: Collection col = m_uow.query(c);
347: // fetch in all the records
348: for (Iterator i = col.iterator(); i.hasNext();)
349: i.next();
350: assertEquals(3, col.size());
351: PartRemarksPicture[] partRemarksPictures = (PartRemarksPicture[]) col
352: .toArray(new PartRemarksPicture[0]);
353: assertEquals("Z-TESTPART-01", partRemarksPictures[0]
354: .getPart());
355: assertEquals(CLOB_FIELD + "01", partRemarksPictures[0]
356: .getRemarks());
357: assertTrue(Arrays.equals((BLOB_FIELD + "01").getBytes(),
358: partRemarksPictures[0].getPicture()));
359:
360: assertEquals("Z-TESTPART-02", partRemarksPictures[1]
361: .getPart());
362: assertEquals(CLOB_FIELD + "02", partRemarksPictures[1]
363: .getRemarks());
364: assertTrue(Arrays.equals((BLOB_FIELD + "02").getBytes(),
365: partRemarksPictures[1].getPicture()));
366:
367: assertEquals("Z-TESTPART-03", partRemarksPictures[2]
368: .getPart());
369: assertEquals(CLOB_FIELD + "03", partRemarksPictures[2]
370: .getRemarks());
371: assertTrue(Arrays.equals((BLOB_FIELD + "03").getBytes(),
372: partRemarksPictures[2].getPicture()));
373:
374: // now update the records
375: partRemarksPictures[0].updatePicture(null);
376: partRemarksPictures[1].updatePicture("Z-UPDATEDREMARKS-022"
377: .getBytes());
378: m_uow.update(partRemarksPictures[0]);
379: m_uow.update(partRemarksPictures[1]);
380: m_uow.commit();
381:
382: // now check if the updates were successful
383: m_uow = new UOW();
384: c = new Criteria();
385: c.setTable(PartRemarksPictureMeta.getName());
386: c.addCriteria(PartRemarksPictureMeta.PART,
387: Criteria.RELATIONAL_BEGINS_WITH, "Z-");
388: c.addOrderBy(PartRemarksPictureMeta.PART,
389: Criteria.ORDER_BY_ASC);
390: col = m_uow.query(c);
391: // fetch in all the records
392: for (Iterator i = col.iterator(); i.hasNext();)
393: i.next();
394: assertEquals(3, col.size());
395: partRemarksPictures = (PartRemarksPicture[]) col
396: .toArray(new PartRemarksPicture[0]);
397: assertNull(partRemarksPictures[0].getPicture());
398: assertTrue(Arrays.equals("Z-UPDATEDREMARKS-022".getBytes(),
399: partRemarksPictures[1].getPicture()));
400: assertTrue(Arrays.equals((BLOB_FIELD + "03").getBytes(),
401: partRemarksPictures[2].getPicture()));
402:
403: // now delete the records
404: m_uow.delete(partRemarksPictures[0]);
405: m_uow.delete(partRemarksPictures[1]);
406: m_uow.delete(partRemarksPictures[2]);
407: m_uow.commit();
408:
409: } catch (Exception e) {
410: e.printStackTrace();
411: fail();
412: }
413: }
414: }
|