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: * AddTest.java
052: *
053: * Created on April 1, 2002, 5:47 PM
054: */
055:
056: package org.jaffa.persistence.blackboxtests;
057:
058: import junit.framework.TestCase;
059: import org.jaffa.persistence.domainobjects.*;
060: import org.jaffa.persistence.UOW;
061: import org.jaffa.persistence.Criteria;
062: import java.util.Iterator;
063: import org.jaffa.datatypes.DateTime;
064: import org.jaffa.persistence.AtomicCriteria;
065: import org.jaffa.util.StringHelper;
066:
067: /** Tests for performing inserts through the Jaffa Persistence Engine.
068: *
069: * @author GautamJ
070: */
071: public class AddTest extends TestCase {
072:
073: private static StringBuffer buf = new StringBuffer();
074: static {
075: for (int i = 0; i < 100000; i++)
076: buf.append('Z');
077: }
078: private static final String LONG_FIELD = buf.toString();
079:
080: private UOW m_uow = null;
081:
082: /** Creates new QueryTest
083: * @param name The name of the test case.
084: */
085: public AddTest(String name) {
086: super (name);
087: }
088:
089: /** Sets up the fixture, by creating the UOW. This method is called before a test is executed.
090: */
091: protected void setUp() {
092: try {
093: m_uow = new UOW();
094: } catch (Exception e) {
095: e.printStackTrace();
096: fail("Failed to create a UOW: " + e.toString());
097: }
098: }
099:
100: /** Tears down the fixture, by closing the UOW. This method is called after a test is executed.
101: */
102: protected void tearDown() {
103: try {
104: if (m_uow != null)
105: m_uow.rollback();
106: m_uow = null;
107: } catch (Exception e) {
108: e.printStackTrace();
109: fail("Failed to rollback a UOW: " + e.toString());
110: }
111: }
112:
113: /** Inserts a record into SYCI using the query:
114: * insert into SYCI(CATEGORY_INSTRUMENT, DESCRIPTION) values('Z-TESTCI-02', 'Z-TESTCIDESC-02').
115: * It then checks if the record was added. Finally the record is deleted
116: */
117: public void testCreateCategoryOfInstrument() {
118: try {
119: CategoryOfInstrument obj = (CategoryOfInstrument) m_uow
120: .newPersistentInstance(CategoryOfInstrument.class);
121: obj.updateCategoryInstrument("Z-TESTCI-02");
122: obj.updateDescription("Z-TESTCIDESC-02");
123: obj.updateSupportEquip(Boolean.FALSE);
124: m_uow.add(obj);
125: m_uow.commit();
126:
127: // Now retrieve the added record & check if it was correctly added
128: m_uow = new UOW();
129: Criteria c = new Criteria();
130: c.setTable(CategoryOfInstrumentMeta.getName());
131: c.addCriteria(CategoryOfInstrumentMeta.CATEGORY_INSTRUMENT,
132: "Z-TESTCI-02");
133: Iterator i = m_uow.query(c).iterator();
134: CategoryOfInstrument syci = (CategoryOfInstrument) i.next();
135: assertEquals("Z-TESTCI-02", syci.getCategoryInstrument());
136: assertEquals("Z-TESTCIDESC-02", syci.getDescription());
137: assertEquals(Boolean.FALSE, syci.getSupportEquip());
138: assertNull(syci.getCalculateMtbf());
139:
140: // Now delete the bugger
141: m_uow.delete(syci);
142: m_uow.commit();
143: } catch (Exception e) {
144: e.printStackTrace();
145: fail();
146: }
147: }
148:
149: /** Inserts a record into CATZ using the query:
150: * insert into CATZ(PART, REMARKS) values('Z-TESTPART-02', some 10,000 character string).
151: * It then checks if the record was added. Finally the record is deleted
152: */
153: public void testAddLong() {
154: try {
155: PartRemarks obj = (PartRemarks) m_uow
156: .newPersistentInstance(PartRemarks.class);
157: obj.updatePart("Z-TESTPART-02");
158: obj.updateRemarks(LONG_FIELD);
159: m_uow.add(obj);
160: m_uow.commit();
161:
162: // Now retrieve the added record & check if it was correctly added
163: m_uow = new UOW();
164: Criteria c = new Criteria();
165: c.setTable(PartRemarksMeta.getName());
166: c.addCriteria(PartRemarksMeta.PART, "Z-TESTPART-02");
167: Iterator i = m_uow.query(c).iterator();
168: PartRemarks catz = (PartRemarks) i.next();
169: assertEquals("Z-TESTPART-02", catz.getPart());
170: assertEquals(LONG_FIELD, catz.getRemarks());
171:
172: // Now delete the bugger
173: m_uow.delete(catz);
174: m_uow.commit();
175: } catch (Exception e) {
176: e.printStackTrace();
177: fail();
178: }
179: }
180:
181: /** Inserts a record into SYCI using the query:
182: * insert into SYCI(CATEGORY_INSTRUMENT, DESCRIPTION) values('Z-TESTCI-03', 'Z-TESTCIDESC-03').
183: * It then tries to add the same record. The test is successful, if an exception is raised.
184: * Finally it rollbacks the changes
185: */
186: public void testAddDuplicates() {
187: try {
188: CategoryOfInstrument obj1 = (CategoryOfInstrument) m_uow
189: .newPersistentInstance(CategoryOfInstrument.class);
190: obj1.updateCategoryInstrument("Z-TESTCI-03");
191: obj1.updateDescription("Z-TESTCIDESC-03");
192: m_uow.add(obj1);
193:
194: CategoryOfInstrument obj2 = (CategoryOfInstrument) m_uow
195: .newPersistentInstance(CategoryOfInstrument.class);
196: obj2.updateCategoryInstrument("Z-TESTCI-03");
197: obj2.updateDescription("Z-TESTCIDESC-03");
198: m_uow.add(obj2);
199:
200: m_uow.commit();
201:
202: // The test has failed, if this point is reached
203: fail("No exception was raised while adding duplicates");
204: } catch (Exception e) {
205: // do nothing.. reaching this point is a success !!!
206: } finally {
207: try {
208: m_uow.rollback();
209: } catch (Exception e) {
210: e.printStackTrace();
211: fail();
212: }
213: }
214: }
215:
216: /** Inserts a record into SYCI using the query:
217: * insert into SYCI(CATEGORY_INSTRUMENT, DESCRIPTION) values('Z-TESTCI-03', 'Z-TESTCIDESC-03').
218: * It then does a rollback. This is followed by a commit. The test fails if the record is added.
219: */
220: public void testCheckRollbackAfterAdd() {
221: try {
222: CategoryOfInstrument obj = (CategoryOfInstrument) m_uow
223: .newPersistentInstance(CategoryOfInstrument.class);
224: obj.updateCategoryInstrument("Z-TESTCI-03");
225: obj.updateDescription("Z-TESTCIDESC-03");
226: m_uow.add(obj);
227: m_uow.rollback();
228:
229: // Now ensure the record isn't added
230: m_uow = new UOW();
231: Criteria c = new Criteria();
232: c.setTable(CategoryOfInstrumentMeta.getName());
233: c.addCriteria(CategoryOfInstrumentMeta.CATEGORY_INSTRUMENT,
234: "Z-TESTCI-03");
235: Iterator i = m_uow.query(c).iterator();
236: if (i.hasNext()) {
237: // delete the record & fail the test
238: CategoryOfInstrument syci = (CategoryOfInstrument) i
239: .next();
240: m_uow.delete(syci);
241: m_uow.commit();
242: fail("Record has been added even after a rollback");
243: }
244: } catch (Exception e) {
245: e.printStackTrace();
246: fail();
247: }
248: }
249:
250: /** Inserts a record into ITEM using the query:
251: * insert into ITEM(ITEM_ID, CREATED_DATETIME) values('Z-TESTITEM-04', to_date('2003-09-10 20:30:40', 'yyyy-MM-dd hh24:mi:ss')).
252: * It then checks if the record was added. Finally the record is deleted
253: */
254: public void testAddDateTime() {
255: try {
256: String itemId = "Z-TESTITEM-04";
257: DateTime datetime = new DateTime(2003, DateTime.SEPTEMBER,
258: 10, 20, 30, 40, 0);
259:
260: Item obj = (Item) m_uow.newPersistentInstance(Item.class);
261: obj.updateItemId(itemId);
262: obj.updateCreatedDatetime(datetime);
263: m_uow.add(obj);
264: m_uow.commit();
265:
266: // Now retrieve the added record & check if it was correctly added
267: m_uow = new UOW();
268: Criteria c = new Criteria();
269: c.setTable(ItemMeta.getName());
270: c.addCriteria(ItemMeta.ITEM_ID, itemId);
271: Iterator i = m_uow.query(c).iterator();
272: Item item = (Item) i.next();
273: assertEquals(itemId, item.getItemId());
274: assertEquals(datetime, item.getCreatedDatetime());
275:
276: // Now delete the bugger
277: m_uow.delete(item);
278: m_uow.commit();
279: } catch (Exception e) {
280: e.printStackTrace();
281: fail();
282: }
283: }
284:
285: /** Inserts a record into ASSET, which has a database generated key
286: * It then checks if the record was added. Finally the record is deleted
287: */
288: public void testAddWithAutoKey() {
289: try {
290: String assetId = "Z-TESTASSET-01";
291: DateTime datetime = new DateTime(2003, DateTime.SEPTEMBER,
292: 10, 20, 30, 40, 0);
293:
294: Asset obj = (Asset) m_uow
295: .newPersistentInstance(Asset.class);
296: obj.updateAssetId(assetId);
297: obj.updateCreatedDatetime(datetime);
298: m_uow.add(obj);
299: m_uow.commit();
300:
301: // Now retrieve the added record & check if it was correctly added
302: m_uow = new UOW();
303: Criteria c = new Criteria();
304: c.setTable(AssetMeta.getName());
305: c.addCriteria(AssetMeta.ASSET_ID, assetId);
306: Iterator i = m_uow.query(c).iterator();
307: Asset asset = (Asset) i.next();
308: assertNotNull(asset.getAssetTk());
309: assertEquals(assetId, asset.getAssetId());
310: assertEquals(datetime, asset.getCreatedDatetime());
311:
312: // Now delete the bugger
313: m_uow.delete(asset);
314: m_uow.commit();
315: } catch (Exception e) {
316: e.printStackTrace();
317: fail();
318: }
319: }
320:
321: }
|