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: * RawTest.java
052: *
053: * Created on April 19, 2002, 9:44 AM
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 RawTest extends TestCase {
070:
071: private static StringBuffer buf = new StringBuffer();
072: static {
073: for (int i = 0; i < 100000; i++)
074: buf.append('Z');
075: }
076: private static final String LONG_FIELD = buf.toString();
077:
078: private UOW m_uow = null;
079:
080: /** Creates new QueryTest
081: * @param name The name of the test case.
082: */
083: public RawTest(String name) {
084: super (name);
085: }
086:
087: /** Sets up the fixture, by creating the UOW. This method is called before a test is executed.
088: */
089: protected void setUp() {
090: try {
091: m_uow = new UOW();
092: } catch (Exception e) {
093: e.printStackTrace();
094: fail("Failed to create a UOW: " + e.toString());
095: }
096: }
097:
098: /** Tears down the fixture, by closing the UOW. This method is called after a test is executed.
099: */
100: protected void tearDown() {
101: try {
102: if (m_uow != null)
103: m_uow.rollback();
104: m_uow = null;
105: } catch (Exception e) {
106: e.printStackTrace();
107: fail("Failed to rollback a UOW: " + e.toString());
108: }
109: }
110:
111: /** Creates ZZ_TEST_PART_PICTURE records having raw and longraw elements. It then checks if the data has been added.
112: */
113: public void testAddRawAndLongRaw() {
114: try {
115: // create 3 records
116: PartPicture partPicture = null;
117: partPicture = (PartPicture) m_uow
118: .newPersistentInstance(PartPicture.class);
119: partPicture.updatePart("Z-TESTPART-01");
120: partPicture.updateSmallPicture("Z-TESTSMALLPICTURE-01"
121: .getBytes());
122: partPicture.updatePicture((LONG_FIELD + "01").getBytes());
123: m_uow.add(partPicture);
124:
125: partPicture = (PartPicture) m_uow
126: .newPersistentInstance(PartPicture.class);
127: partPicture.updatePart("Z-TESTPART-02");
128: partPicture.updateSmallPicture("Z-TESTSMALLPICTURE-02"
129: .getBytes());
130: partPicture.updatePicture((LONG_FIELD + "02").getBytes());
131: m_uow.add(partPicture);
132:
133: partPicture = (PartPicture) m_uow
134: .newPersistentInstance(PartPicture.class);
135: partPicture.updatePart("Z-TESTPART-03");
136: partPicture.updateSmallPicture("Z-TESTSMALLPICTURE-03"
137: .getBytes());
138: partPicture.updatePicture((LONG_FIELD + "03").getBytes());
139: m_uow.add(partPicture);
140: m_uow.commit();
141:
142: // now check if they have been added
143: m_uow = new UOW();
144: Criteria c = new Criteria();
145: c.setTable(PartPictureMeta.getName());
146: c.addCriteria(PartPictureMeta.PART,
147: Criteria.RELATIONAL_BEGINS_WITH, "Z");
148: c.addOrderBy(PartPictureMeta.PART, Criteria.ORDER_BY_ASC);
149: Collection col = m_uow.query(c);
150: // fetch in all the records
151: for (Iterator i = col.iterator(); i.hasNext();)
152: i.next();
153: assertEquals(3, col.size());
154: PartPicture[] partPictures = (PartPicture[]) col
155: .toArray(new PartPicture[0]);
156: assertEquals("Z-TESTPART-01", partPictures[0].getPart());
157: assertTrue(Arrays.equals(
158: "Z-TESTSMALLPICTURE-01".getBytes(), partPictures[0]
159: .getSmallPicture()));
160: assertTrue(Arrays.equals((LONG_FIELD + "01").getBytes(),
161: partPictures[0].getPicture()));
162:
163: assertEquals("Z-TESTPART-02", partPictures[1].getPart());
164: assertTrue(Arrays.equals(
165: "Z-TESTSMALLPICTURE-02".getBytes(), partPictures[1]
166: .getSmallPicture()));
167: assertTrue(Arrays.equals((LONG_FIELD + "02").getBytes(),
168: partPictures[1].getPicture()));
169:
170: assertEquals("Z-TESTPART-03", partPictures[2].getPart());
171: assertTrue(Arrays.equals(
172: "Z-TESTSMALLPICTURE-03".getBytes(), partPictures[2]
173: .getSmallPicture()));
174: assertTrue(Arrays.equals((LONG_FIELD + "03").getBytes(),
175: partPictures[2].getPicture()));
176:
177: // now delete the records
178: m_uow.delete(partPictures[0]);
179: m_uow.delete(partPictures[1]);
180: m_uow.delete(partPictures[2]);
181: m_uow.commit();
182:
183: } catch (Exception e) {
184: e.printStackTrace();
185: fail();
186: }
187: }
188:
189: /** Creates ZZ_TEST_PART_PICTURE records having raw and longraw elements.
190: * It then retrieves and updates the rows. Finally it checks if the data has been updated.
191: */
192: public void testUpdateRawAndLongRaw() {
193: try {
194: // create 3 records
195: PartPicture partPicture = null;
196: partPicture = (PartPicture) m_uow
197: .newPersistentInstance(PartPicture.class);
198: partPicture.updatePart("Z-TESTPART-01");
199: partPicture.updateSmallPicture("Z-TESTSMALLPICTURE-01"
200: .getBytes());
201: partPicture.updatePicture((LONG_FIELD + "01").getBytes());
202: m_uow.add(partPicture);
203:
204: partPicture = (PartPicture) m_uow
205: .newPersistentInstance(PartPicture.class);
206: partPicture.updatePart("Z-TESTPART-02");
207: partPicture.updateSmallPicture("Z-TESTSMALLPICTURE-02"
208: .getBytes());
209: partPicture.updatePicture((LONG_FIELD + "02").getBytes());
210: m_uow.add(partPicture);
211:
212: partPicture = (PartPicture) m_uow
213: .newPersistentInstance(PartPicture.class);
214: partPicture.updatePart("Z-TESTPART-03");
215: partPicture.updateSmallPicture("Z-TESTSMALLPICTURE-03"
216: .getBytes());
217: partPicture.updatePicture((LONG_FIELD + "03").getBytes());
218: m_uow.add(partPicture);
219: m_uow.commit();
220:
221: // now check if they have been added
222: m_uow = new UOW();
223: Criteria c = new Criteria();
224: c.setTable(PartPictureMeta.getName());
225: c.addCriteria(PartPictureMeta.PART,
226: Criteria.RELATIONAL_BEGINS_WITH, "Z");
227: c.addOrderBy(PartPictureMeta.PART, Criteria.ORDER_BY_ASC);
228: Collection col = m_uow.query(c);
229: // fetch in all the records
230: for (Iterator i = col.iterator(); i.hasNext();)
231: i.next();
232: assertEquals(3, col.size());
233: PartPicture[] partPictures = (PartPicture[]) col
234: .toArray(new PartPicture[0]);
235: assertEquals("Z-TESTPART-01", partPictures[0].getPart());
236: assertTrue(Arrays.equals(
237: "Z-TESTSMALLPICTURE-01".getBytes(), partPictures[0]
238: .getSmallPicture()));
239: assertTrue(Arrays.equals((LONG_FIELD + "01").getBytes(),
240: partPictures[0].getPicture()));
241:
242: assertEquals("Z-TESTPART-02", partPictures[1].getPart());
243: assertTrue(Arrays.equals(
244: "Z-TESTSMALLPICTURE-02".getBytes(), partPictures[1]
245: .getSmallPicture()));
246: assertTrue(Arrays.equals((LONG_FIELD + "02").getBytes(),
247: partPictures[1].getPicture()));
248:
249: assertEquals("Z-TESTPART-03", partPictures[2].getPart());
250: assertTrue(Arrays.equals(
251: "Z-TESTSMALLPICTURE-03".getBytes(), partPictures[2]
252: .getSmallPicture()));
253: assertTrue(Arrays.equals((LONG_FIELD + "03").getBytes(),
254: partPictures[2].getPicture()));
255:
256: // now update the records
257: partPictures[0].updateSmallPicture(null);
258: partPictures[0].updatePicture(null);
259: partPictures[1].updateSmallPicture("Z-TESTSMALLPICTURE-022"
260: .getBytes());
261: partPictures[1].updatePicture("Z-TESTPICTURE-022"
262: .getBytes());
263: m_uow.update(partPictures[0]);
264: m_uow.update(partPictures[1]);
265: m_uow.commit();
266:
267: // now check if the updates were successful
268: m_uow = new UOW();
269: c = new Criteria();
270: c.setTable(PartPictureMeta.getName());
271: c.addCriteria(PartPictureMeta.PART,
272: Criteria.RELATIONAL_BEGINS_WITH, "Z");
273: c.addOrderBy(PartPictureMeta.PART, Criteria.ORDER_BY_ASC);
274: col = m_uow.query(c);
275: // fetch in all the records
276: for (Iterator i = col.iterator(); i.hasNext();)
277: i.next();
278: assertEquals(3, col.size());
279: partPictures = (PartPicture[]) col
280: .toArray(new PartPicture[0]);
281: assertEquals("Z-TESTPART-01", partPictures[0].getPart());
282: assertNull(partPictures[0].getSmallPicture());
283: assertNull(partPictures[0].getPicture());
284:
285: assertEquals("Z-TESTPART-02", partPictures[1].getPart());
286: assertTrue(Arrays.equals("Z-TESTSMALLPICTURE-022"
287: .getBytes(), partPictures[1].getSmallPicture()));
288: assertTrue(Arrays.equals("Z-TESTPICTURE-022".getBytes(),
289: partPictures[1].getPicture()));
290:
291: assertEquals("Z-TESTPART-03", partPictures[2].getPart());
292: assertTrue(Arrays.equals(
293: "Z-TESTSMALLPICTURE-03".getBytes(), partPictures[2]
294: .getSmallPicture()));
295: assertTrue(Arrays.equals((LONG_FIELD + "03").getBytes(),
296: partPictures[2].getPicture()));
297:
298: // now delete the records
299: m_uow.delete(partPictures[0]);
300: m_uow.delete(partPictures[1]);
301: m_uow.delete(partPictures[2]);
302: m_uow.commit();
303:
304: } catch (Exception e) {
305: e.printStackTrace();
306: fail();
307: }
308: }
309: }
|