0001: package org.apache.ojb.broker;
0002:
0003: import java.sql.Statement;
0004: import java.util.Collection;
0005: import java.util.Enumeration;
0006: import java.util.HashSet;
0007: import java.util.Iterator;
0008: import java.util.List;
0009: import java.util.Set;
0010: import java.util.Vector;
0011:
0012: import org.apache.commons.lang.SerializationUtils;
0013: import org.apache.commons.lang.math.NumberRange;
0014: import org.apache.ojb.broker.accesslayer.OJBIterator;
0015: import org.apache.ojb.broker.core.DelegatingPersistenceBroker;
0016: import org.apache.ojb.broker.metadata.ClassDescriptor;
0017: import org.apache.ojb.broker.query.Criteria;
0018: import org.apache.ojb.broker.query.Query;
0019: import org.apache.ojb.broker.query.QueryByCriteria;
0020: import org.apache.ojb.broker.query.QueryByIdentity;
0021: import org.apache.ojb.broker.query.QueryFactory;
0022: import org.apache.ojb.broker.query.ReportQueryByCriteria;
0023: import org.apache.ojb.broker.util.ObjectModification;
0024: import org.apache.ojb.junit.PBTestCase;
0025:
0026: /**
0027: * Junit test driver for elematary PB tests.
0028: */
0029: public class PersistenceBrokerTest extends PBTestCase {
0030: /**
0031: * BrokerTests constructor comment.
0032: * @param name java.lang.String
0033: */
0034: public PersistenceBrokerTest(String name) {
0035: super (name);
0036: }
0037:
0038: public static void main(String[] args) {
0039: String[] arr = { PersistenceBrokerTest.class.getName() };
0040: junit.textui.TestRunner.main(arr);
0041: }
0042:
0043: private boolean checkIdentityEquality(PersistenceBroker p1,
0044: PersistenceBroker p2) {
0045: return ((DelegatingPersistenceBroker) p1)
0046: .getInnermostDelegate() == ((DelegatingPersistenceBroker) p2)
0047: .getInnermostDelegate();
0048: }
0049:
0050: protected Article createArticle(ProductGroup group, String name) {
0051: Article a = new Article();
0052: a.setArticleName(name);
0053: a.setIsSelloutArticle(true);
0054: a.setMinimumStock(100);
0055: a.setOrderedUnits(17);
0056: a.setPrice(0.45);
0057: if (group != null) {
0058: a.setProductGroup(group);
0059: group.add(a);
0060: }
0061: a.setStock(234);
0062: a.setSupplierId(4);
0063: a.setUnit("bottle");
0064: return a;
0065: }
0066:
0067: protected CdArticle createCdArticle(ProductGroup group, String name) {
0068: CdArticle a = new CdArticle();
0069: a.setArticleName(name);
0070: a.setIsSelloutArticle(true);
0071: a.setMinimumStock(100);
0072: a.setOrderedUnits(17);
0073: a.setPrice(9.95);
0074: a.setProductGroup(group);
0075: a.setStock(234);
0076: a.setSupplierId(4);
0077: a.setUnit("cd");
0078: return a;
0079: }
0080:
0081: protected void deleteArticle(Integer id)
0082: throws PersistenceBrokerException {
0083: Article a = new Article();
0084: a.setArticleId(id);
0085: deleteArticle(a);
0086: }
0087:
0088: protected void deleteArticle(Article articleToDelete)
0089: throws PersistenceBrokerException {
0090: boolean needsCommit = false;
0091: if (!broker.isInTransaction()) {
0092: broker.beginTransaction();
0093: needsCommit = true;
0094: }
0095: broker.delete(articleToDelete);
0096: if (needsCommit) {
0097: broker.commitTransaction();
0098: }
0099: }
0100:
0101: protected Article readArticleByExample(Integer id)
0102: throws PersistenceBrokerException {
0103:
0104: Article example = new Article();
0105: example.setArticleId(id);
0106: return (Article) broker.getObjectByQuery(QueryFactory
0107: .newQuery(example));
0108:
0109: }
0110:
0111: protected Article readArticleByIdentity(Article article)
0112: throws PersistenceBrokerException {
0113: return (Article) broker.getObjectByIdentity(broker
0114: .serviceIdentity().buildIdentity(article));
0115: }
0116:
0117: protected Article readArticleByIdentity(Integer id)
0118: throws PersistenceBrokerException {
0119: return (Article) broker.getObjectByIdentity(broker
0120: .serviceIdentity().buildIdentity(Article.class, id));
0121: }
0122:
0123: protected void storeArticle(Article anArticle)
0124: throws PersistenceBrokerException {
0125: boolean needsCommit = false;
0126: if (!broker.isInTransaction()) {
0127: broker.beginTransaction();
0128: needsCommit = true;
0129: }
0130: broker.store(anArticle);
0131: if (needsCommit) {
0132: broker.commitTransaction();
0133: }
0134: }
0135:
0136: public void testReadUncommitedDataWithinSamePB() throws Exception {
0137: String name = "testReadUncommitedDataWithinSamePB"
0138: + System.currentTimeMillis();
0139: ObjectRepository.Component comp = new ObjectRepository.Component();
0140: comp.setName(name);
0141:
0142: broker.beginTransaction();
0143: // store data
0144: broker.store(comp, ObjectModification.INSERT);
0145: Query query = new QueryByCriteria(
0146: ObjectRepository.Component.class, null);
0147: // now we try to read the uncommitted data
0148: Collection all = broker.getCollectionByQuery(query);
0149: Iterator iter = all.iterator();
0150: ObjectRepository.Component temp;
0151: boolean result = false;
0152: while (iter.hasNext()) {
0153: temp = (ObjectRepository.Component) iter.next();
0154: // System.out.println(temp.getName());
0155: if (name.equals(temp.getName())) {
0156: result = true;
0157: break;
0158: }
0159: }
0160: broker.commitTransaction();
0161: assertTrue(
0162: "Can't read uncommitted data within same PB instance",
0163: result);
0164: }
0165:
0166: /**
0167: * PK fields with primitive data types interpret '0' value as
0168: * 'null' by default. But if we don't use primitive data types and read
0169: * an object with 0 as PK value and store such an object without
0170: * changes, nothing should happen.
0171: */
0172: public void testNull_0_Complex() throws Exception {
0173: Class objClass = ObjectRepository.E.class;
0174: ClassDescriptor cld = broker.getClassDescriptor(objClass);
0175: Integer someOtherValue = new Integer(1111111111);
0176: String insert = "INSERT INTO TABLE_E VALUES(0,"
0177: + someOtherValue.intValue() + ")";
0178: String delete = "DELETE FROM TABLE_E WHERE ID=0";
0179: Statement stmt;
0180: try {
0181: broker.beginTransaction();
0182: // cleanup
0183: stmt = broker.serviceStatementManager()
0184: .getGenericStatement(cld, false);
0185: stmt.executeUpdate(delete);
0186: broker.serviceStatementManager().closeResources(stmt, null);
0187: broker.commitTransaction();
0188:
0189: broker.beginTransaction();
0190: // prepare test
0191: stmt = broker.serviceStatementManager()
0192: .getGenericStatement(cld, false);
0193: // insert object with 0 as PK
0194: stmt.executeUpdate(insert);
0195: broker.serviceStatementManager().closeResources(stmt, null);
0196: broker.commitTransaction();
0197:
0198: // find all objects with 'someSubValue' 111111111
0199: Criteria crit = new Criteria();
0200: crit.addEqualTo("someSuperValue", someOtherValue);
0201: Query queryAllSubs = new QueryByCriteria(objClass, crit);
0202: Collection resultBefore = broker
0203: .getCollectionByQuery(queryAllSubs);
0204: int matchesBefore = resultBefore.size();
0205:
0206: // materialize object with 0 PK
0207: Criteria c = new Criteria();
0208: c.addEqualTo("id", new Integer(0));
0209: Query q = new QueryByCriteria(objClass, c);
0210: ObjectRepository.E obj = (ObjectRepository.E) broker
0211: .getObjectByQuery(q);
0212: // store the unchanged read object
0213: broker.beginTransaction();
0214: broker.store(obj);
0215: broker.commitTransaction();
0216:
0217: Collection resultAfter = broker
0218: .getCollectionByQuery(queryAllSubs);
0219: int matchesAfter = resultAfter.size();
0220:
0221: assertEquals(
0222: "We don't store new objects, thus we expect same numbers",
0223: matchesBefore, matchesAfter);
0224: } finally {
0225: broker.beginTransaction();
0226: // cleanup
0227: stmt = broker.serviceStatementManager()
0228: .getGenericStatement(cld, false);
0229: stmt.executeUpdate(delete);
0230: broker.serviceStatementManager().closeResources(stmt, null);
0231: broker.commitTransaction();
0232: }
0233: }
0234:
0235: /**
0236: * Object with autoincrement 'true' and a NON primitive
0237: * data type for the PK field. It should be allowed to set an
0238: * new object with PK 0, because PK field is not primitive
0239: */
0240: public void testNull_0_Complex_2() throws Exception {
0241: Class objClass = ObjectRepository.E.class;
0242: ClassDescriptor cld = broker.getClassDescriptor(objClass);
0243: Integer someOtherValue = new Integer(1111111111);
0244: String delete = "DELETE FROM TABLE_E WHERE ID=0";
0245: Statement stmt;
0246: try {
0247: broker.beginTransaction();
0248: stmt = broker.serviceStatementManager()
0249: .getGenericStatement(cld, false);
0250: stmt.executeUpdate(delete);
0251: broker.serviceStatementManager().closeResources(stmt, null);
0252: broker.commitTransaction();
0253: broker.clearCache();
0254:
0255: // find all objects with 'someSubValue' 111111111
0256: Criteria crit = new Criteria();
0257: crit.addEqualTo("someSuperValue", someOtherValue);
0258: Query queryAllSubs = new QueryByCriteria(objClass, crit);
0259: Collection resultBefore = broker
0260: .getCollectionByQuery(queryAllSubs);
0261: int matchesBefore = resultBefore.size();
0262:
0263: ObjectRepository.E obj = new ObjectRepository.E();
0264: obj.setId(new Integer(0));
0265: obj.setSomeSuperValue(someOtherValue.intValue());
0266: broker.beginTransaction();
0267: broker.store(obj);
0268: broker.commitTransaction();
0269:
0270: broker.clearCache();
0271: Collection resultAfter = broker
0272: .getCollectionByQuery(queryAllSubs);
0273: int matchesAfter = resultAfter.size();
0274: assertEquals(
0275: "We store new object, but was not written to DB",
0276: matchesBefore + 1, matchesAfter);
0277: // lookup object with 0 PK
0278: Criteria c = new Criteria();
0279: c.addEqualTo("id", new Integer(0));
0280: Query q = new QueryByCriteria(objClass, c);
0281: obj = (ObjectRepository.E) broker.getObjectByQuery(q);
0282: assertEquals(
0283: "We should found object with id 0 for PK field",
0284: new Integer(0), obj.getId());
0285: } finally {
0286: broker.beginTransaction();
0287: // cleanup
0288: stmt = broker.serviceStatementManager()
0289: .getGenericStatement(cld, false);
0290: stmt.executeUpdate(delete);
0291: broker.serviceStatementManager().closeResources(stmt, null);
0292: broker.commitTransaction();
0293: }
0294: }
0295:
0296: public void testPBF() throws Exception {
0297: // we don't need this
0298: broker.close();
0299:
0300: PersistenceBroker pb_1 = PersistenceBrokerFactory
0301: .defaultPersistenceBroker();
0302: pb_1.getObjectByQuery(QueryFactory.newQuery(Person.class,
0303: (Criteria) null));
0304: PersistenceBroker pb_2 = PersistenceBrokerFactory
0305: .defaultPersistenceBroker();
0306: pb_2.getObjectByQuery(QueryFactory.newQuery(Person.class,
0307: (Criteria) null));
0308: PersistenceBroker pb_3 = PersistenceBrokerFactory
0309: .defaultPersistenceBroker();
0310: pb_3.getObjectByQuery(QueryFactory.newQuery(Person.class,
0311: (Criteria) null));
0312: pb_1.close();
0313: pb_2.close();
0314: pb_3.close();
0315: PersistenceBrokerFactory.releaseAllInstances();
0316: PersistenceBroker pbNew = PersistenceBrokerFactory
0317: .defaultPersistenceBroker();
0318: if (pbNew instanceof DelegatingPersistenceBroker) {
0319: if (checkIdentityEquality(pbNew, pb_1)
0320: || checkIdentityEquality(pbNew, pb_2)
0321: || checkIdentityEquality(pbNew, pb_3)) {
0322: fail("Reuse of released PB instance");
0323: }
0324: }
0325: assertFalse(pbNew.isClosed());
0326: assertFalse(pbNew.isInTransaction());
0327: pbNew.close();
0328: }
0329:
0330: /**
0331: * test the the PB delete() method.
0332: */
0333: public void testDelete() throws Exception {
0334: String name = "testDelete_" + System.currentTimeMillis();
0335: Article a = createArticle(null, name);
0336: storeArticle(a);
0337: broker.clearCache();
0338: Article b = readArticleByIdentity(a);
0339: assertEquals(
0340: "after inserting an object it should be equal to its re-read pendant",
0341: a.getArticleName(), b.getArticleName());
0342: deleteArticle(b);
0343: b = readArticleByIdentity(a);
0344: assertNull("should be null after deletion", b);
0345: b = readArticleByExample(a.getArticleId());
0346: assertNull("should be null after deletion", b);
0347: }
0348:
0349: public void testPBisClosed() {
0350: PersistenceBroker pb = PersistenceBrokerFactory
0351: .defaultPersistenceBroker();
0352:
0353: assertFalse(pb.isClosed());
0354: pb.beginTransaction();
0355: assertTrue(pb.isInTransaction());
0356: pb.commitTransaction();
0357: assertFalse(pb.isInTransaction());
0358:
0359: pb.beginTransaction();
0360: assertTrue(pb.isInTransaction());
0361: pb.abortTransaction();
0362: assertFalse(pb.isInTransaction());
0363:
0364: pb.close();
0365: assertTrue(pb.isClosed());
0366: assertFalse(pb.isInTransaction());
0367: try {
0368: pb.beginTransaction();
0369: fail("We expect an exception, but was not thrown");
0370: } catch (Exception e) {
0371: assertTrue(true);
0372: }
0373: }
0374:
0375: public void testLocalTransactionDemarcation() {
0376: PersistenceBroker pb = PersistenceBrokerFactory
0377: .defaultPersistenceBroker();
0378:
0379: try {
0380: pb.beginTransaction();
0381: pb.commitTransaction();
0382: pb.close();
0383:
0384: pb = PersistenceBrokerFactory.defaultPersistenceBroker();
0385: pb.beginTransaction();
0386: pb.abortTransaction();
0387: pb.abortTransaction();
0388: pb.close();
0389:
0390: pb = PersistenceBrokerFactory.defaultPersistenceBroker();
0391: pb.beginTransaction();
0392: pb.commitTransaction();
0393: pb.abortTransaction();
0394: pb.close();
0395:
0396: pb = PersistenceBrokerFactory.defaultPersistenceBroker();
0397: try {
0398: pb.commitTransaction();
0399: fail("Commit tx without begin shouldn't be possible");
0400: } catch (TransactionNotInProgressException e) {
0401: assertTrue(true);
0402: }
0403:
0404: try {
0405: pb.beginTransaction();
0406: Query q = QueryFactory.newQuery(Article.class,
0407: "Select * from NOT_EXIST");
0408: pb.getObjectByQuery(q);
0409: pb.commitTransaction();
0410: fail("Query should fail");
0411: } catch (PersistenceBrokerException e) {
0412: pb.abortTransaction();
0413: assertTrue(true);
0414: } finally {
0415: pb.close();
0416: }
0417: } finally {
0418: if (pb != null)
0419: pb.close();
0420: }
0421: }
0422:
0423: /**
0424: * test the the PB deleteByQuery() method.
0425: */
0426: public void testDeleteByQuery() throws Exception {
0427: String name = "Funny_testDelete_" + System.currentTimeMillis();
0428: ProductGroup pg;
0429: pg = new ProductGroup();
0430: pg.setGroupName(name);
0431:
0432: broker.beginTransaction();
0433: broker.store(pg);
0434: broker.commitTransaction();
0435:
0436: Article a = createArticle(pg, name);
0437: Article b = createArticle(pg, name);
0438: CdArticle c = createCdArticle(pg, name);
0439:
0440: storeArticle(a);
0441: storeArticle(b);
0442: storeArticle(c);
0443:
0444: broker.clearCache();
0445:
0446: Criteria crit = new Criteria();
0447: crit.addEqualTo("productGroupId", pg.getId());
0448: crit.addLike("articleName", "%Funny%");
0449: Query q = new QueryByCriteria(Article.class, crit);
0450:
0451: // 1. check for matching items
0452: broker.clearCache();
0453: Collection col = broker.getCollectionByQuery(q);
0454: assertEquals("There should be 3 matching items", 3, col.size());
0455:
0456: // 2. perform delete by query
0457: broker.deleteByQuery(q);
0458:
0459: // 3. recheck for matching elements
0460: col = broker.getCollectionByQuery(q);
0461: assertEquals("there should be no more matching items", 0, col
0462: .size());
0463: }
0464:
0465: /**
0466: * performs a test of the inheritance mapping to one table.
0467: */
0468: public void testMappingToOneTableWithAbstractBaseClass() {
0469: // first delete all ABs from database
0470: Collection abs = null;
0471: Criteria c = null;
0472: Query q = QueryFactory.newQuery(ObjectRepository.AB.class, c);
0473: abs = broker.getCollectionByQuery(q);
0474: broker.beginTransaction();
0475: if (abs != null) {
0476: Iterator iter = abs.iterator();
0477: while (iter.hasNext()) {
0478: broker.delete(iter.next());
0479: }
0480: }
0481: broker.commitTransaction();
0482:
0483: // Insert 2 A, 1 B and 1 B1
0484: ObjectRepository.A a1 = new ObjectRepository.A();
0485: a1.setSomeAField("a A_Field value");
0486: ObjectRepository.A a2 = new ObjectRepository.A();
0487: a1.setSomeAField("another A_Field value");
0488:
0489: ObjectRepository.B b1 = new ObjectRepository.B();
0490: b1.setSomeBField("a B_Field value");
0491: ObjectRepository.B1 b2 = new ObjectRepository.B1();
0492:
0493: broker.beginTransaction();
0494: broker.store(a1);
0495: broker.store(a2);
0496: broker.store(b1);
0497: broker.store(b2);
0498: broker.commitTransaction();
0499:
0500: ObjectRepository.AB ab = null;
0501:
0502: // test retrieval by Identity
0503: Criteria crit = new Criteria();
0504: crit.addEqualTo("id", new Integer(a1.getId()));
0505: q = QueryFactory.newQuery(ObjectRepository.AB.class, crit);
0506: ab = (ObjectRepository.AB) broker.getObjectByQuery(q);
0507:
0508: assertEquals(ObjectRepository.A.class.getName(), ab
0509: .getOjbConcreteClass());
0510: assertEquals(ObjectRepository.A.class, ab.getClass());
0511:
0512: crit = new Criteria();
0513: crit.addEqualTo("id", new Integer(b1.getId()));
0514: q = QueryFactory.newQuery(ObjectRepository.AB.class, crit);
0515: ab = (ObjectRepository.AB) broker.getObjectByQuery(q);
0516:
0517: assertEquals(ObjectRepository.B.class.getName(), ab
0518: .getOjbConcreteClass());
0519: assertEquals(ObjectRepository.B.class, ab.getClass());
0520:
0521: // test retrieval of collections
0522: abs = null;
0523: Criteria selectAll = null;
0524: q = QueryFactory.newQuery(ObjectRepository.AB.class, selectAll);
0525: abs = broker.getCollectionByQuery(q);
0526: assertEquals("collection size", 4, abs.size());
0527: assertEquals("counted size", 4, broker.getCount(q));
0528:
0529: q = QueryFactory.newQuery(ObjectRepository.A.class, selectAll);
0530: abs = broker.getCollectionByQuery(q);
0531: assertEquals("collection size", 2, abs.size());
0532: assertEquals("counted size", 2, broker.getCount(q));
0533:
0534: q = QueryFactory.newQuery(ObjectRepository.B.class, selectAll);
0535: abs = broker.getCollectionByQuery(q);
0536: assertEquals("collection size", 2, abs.size());
0537: assertEquals("counted size", 2, broker.getCount(q));
0538: }
0539:
0540: /**
0541: * performs a test of an extent with one concrete class that uses
0542: * ojbConcreteClass identifier.
0543: */
0544: public void testExtentWithOneConcreteClassWithOjbConcreteClass()
0545: throws Exception {
0546: // first delete all ObjectRepository.ABs from database
0547: Collection as = null;
0548: Criteria c = null;
0549: Query q = QueryFactory.newQuery(ObjectRepository.AB.class, c);
0550: as = broker.getCollectionByQuery(q);
0551: broker.beginTransaction();
0552: if (as != null) {
0553: Iterator iter = as.iterator();
0554: while (iter.hasNext()) {
0555: broker.delete(iter.next());
0556: }
0557: }
0558: broker.commitTransaction();
0559:
0560: // Insert 2 ObjectRepository.A
0561: ObjectRepository.A a1 = new ObjectRepository.A();
0562: ObjectRepository.A a2 = new ObjectRepository.A();
0563:
0564: broker.beginTransaction();
0565: broker.store(a1);
0566: broker.store(a2);
0567: broker.commitTransaction();
0568:
0569: Criteria selectAll = null;
0570:
0571: q = QueryFactory.newQuery(ObjectRepository.AAlone.class,
0572: selectAll);
0573: as = broker.getCollectionByQuery(q);
0574: assertEquals("collection size", 2, as.size());
0575: assertEquals("counted size", 2, broker.getCount(q));
0576: }
0577:
0578: /**
0579: * performs a test of the inheritance mapping to one table.
0580: */
0581: public void testMappingToOneTable() throws Exception {
0582: // first delete all Cs from database
0583: Collection cs = null;
0584: Criteria crit = null;
0585: Query q = QueryFactory.newQuery(ObjectRepository.C.class, crit);
0586: cs = broker.getCollectionByQuery(q);
0587: broker.beginTransaction();
0588: if (cs != null) {
0589: Iterator iter = cs.iterator();
0590: while (iter.hasNext()) {
0591: broker.delete(iter.next());
0592: }
0593: }
0594: broker.commitTransaction();
0595:
0596: ObjectRepository.C c1 = new ObjectRepository.C();
0597: ObjectRepository.C c2 = new ObjectRepository.C();
0598: ObjectRepository.D d1 = new ObjectRepository.D();
0599:
0600: broker.beginTransaction();
0601: broker.store(c1);
0602: broker.store(c2);
0603: broker.store(d1);
0604: broker.commitTransaction();
0605:
0606: ObjectRepository.C candidate = null;
0607:
0608: // test retrieval by Identity
0609: crit = new Criteria();
0610: crit.addEqualTo("id", new Integer(c1.getId()));
0611: q = QueryFactory.newQuery(ObjectRepository.C.class, crit);
0612: candidate = (ObjectRepository.C) broker.getObjectByQuery(q);
0613:
0614: assertEquals(ObjectRepository.C.class.getName(), candidate
0615: .getOjbConcreteClass());
0616: assertEquals(ObjectRepository.C.class, candidate.getClass());
0617:
0618: crit = new Criteria();
0619: crit.addEqualTo("id", new Integer(d1.getId()));
0620: q = QueryFactory.newQuery(ObjectRepository.C.class, crit);
0621: candidate = (ObjectRepository.C) broker.getObjectByQuery(q);
0622: assertEquals(ObjectRepository.D.class.getName(), candidate
0623: .getOjbConcreteClass());
0624: assertEquals(ObjectRepository.D.class, candidate.getClass());
0625:
0626: crit = new Criteria();
0627: crit.addEqualTo("id", new Integer(d1.getId()));
0628: q = QueryFactory.newQuery(ObjectRepository.D.class, crit);
0629: candidate = (ObjectRepository.D) broker.getObjectByQuery(q);
0630: assertEquals(ObjectRepository.D.class.getName(), candidate
0631: .getOjbConcreteClass());
0632: assertEquals(ObjectRepository.D.class, candidate.getClass());
0633:
0634: // test retrieval of collections
0635: cs = null;
0636: Criteria selectAll = null;
0637: q = QueryFactory.newQuery(ObjectRepository.C.class, selectAll);
0638: cs = broker.getCollectionByQuery(q);
0639: assertEquals("collection size", 3, cs.size());
0640: assertEquals("counted size", 3, broker.getCount(q));
0641:
0642: q = QueryFactory.newQuery(ObjectRepository.D.class, selectAll);
0643: cs = broker.getCollectionByQuery(q);
0644: assertEquals("collection size", 1, cs.size());
0645: assertEquals("counted size", 1, broker.getCount(q));
0646: }
0647:
0648: /**
0649: * performs a test to check if metadata can be read
0650: */
0651: public void testGetDescriptor() throws Exception {
0652: ClassDescriptor cld = broker.getClassDescriptor(Article.class);
0653: assertNotNull("classdescriptor should not be null", cld);
0654: }
0655:
0656: /**
0657: * tests the FieldConversion facility
0658: */
0659: public void testGuidFieldConversion() {
0660: GuidTestEntity gte = new GuidTestEntity();
0661: broker.beginTransaction();
0662: broker.store(gte);
0663: broker.commitTransaction();
0664: broker.clearCache();
0665:
0666: GuidTestEntity gte1 = (GuidTestEntity) broker
0667: .getObjectByIdentity(new Identity(gte, broker));
0668:
0669: assertEquals(gte, gte1);
0670: }
0671:
0672: /**
0673: * tests the RowReader mechanism
0674: */
0675: public void testRowReader() {
0676: String name = "testRowReader_" + System.currentTimeMillis();
0677: // a little hack, both classes use the same table, so it's possible
0678: // to insert Article but read ArticleWithStockDetail
0679: Article a = createArticle(null, name);
0680: storeArticle(a);
0681: Criteria crit = new Criteria();
0682: crit.addEqualTo("articleId", a.getArticleId());
0683: Query q = QueryFactory.newQuery(ArticleWithStockDetail.class,
0684: crit);
0685:
0686: broker.clearCache();
0687: ArticleWithStockDetail b = (ArticleWithStockDetail) broker
0688: .getObjectByQuery(q);
0689: StockDetail detail = b.getDetail();
0690: assertNotNull("detail should be loaded by RowReader !", detail);
0691: assertEquals(a.getMinimumStock(), detail.getMinimumStock());
0692: assertEquals(a.getOrderedUnits(), detail.getOrderedUnits());
0693: }
0694:
0695: public void testEscaping() throws Exception {
0696: String name = "testEscaping_" + System.currentTimeMillis();
0697: Article a = createArticle(null, name);
0698: Article b = readArticleByIdentity(a);
0699: assertNull("should be null after deletion", b);
0700: a.setArticleName("Single quote 'article_" + name);
0701: storeArticle(a);
0702: broker.clearCache();
0703: b = readArticleByIdentity(a);
0704: assertEquals(
0705: "after inserting an object it should be equal to its re-read pendant",
0706: a.getArticleName(), b.getArticleName());
0707:
0708: Collection col;
0709: Iterator iter;
0710: String aName = a.getArticleName();
0711: Criteria criteria = new Criteria();
0712: criteria.addEqualTo("articleName", aName);
0713: Query query = QueryFactory.newQuery(InterfaceArticle.class,
0714: criteria);
0715: col = broker.getCollectionByQuery(query);
0716: iter = col.iterator();
0717: assertTrue("should have one element", iter.hasNext());
0718: assertEquals("should be equal", aName, ((InterfaceArticle) iter
0719: .next()).getArticleName());
0720: assertFalse(iter.hasNext());
0721:
0722: a.setArticleName("2 Single quotes 'article'_" + name);
0723: storeArticle(a);
0724: broker.clearCache();
0725: b = readArticleByIdentity(a);
0726: assertEquals(
0727: "after inserting an object it should be equal to its re-read pendant",
0728: a.getArticleName(), b.getArticleName());
0729: aName = a.getArticleName();
0730: criteria = new Criteria();
0731: criteria.addEqualTo("articleName", aName);
0732: query = QueryFactory.newQuery(Article.class, criteria);
0733: col = broker.getCollectionByQuery(query);
0734: iter = col.iterator();
0735: assertTrue("should have one element", iter.hasNext());
0736: assertEquals("should be equal", aName, ((InterfaceArticle) iter
0737: .next()).getArticleName());
0738: assertFalse(iter.hasNext());
0739:
0740: a.setArticleName("double quote \"article_" + name);
0741: storeArticle(a);
0742: broker.clearCache();
0743: b = readArticleByIdentity(a);
0744: assertEquals(
0745: "after inserting an object it should be equal to its re-read pendant",
0746: a.getArticleName(), b.getArticleName());
0747: aName = a.getArticleName();
0748: criteria = new Criteria();
0749: criteria.addEqualTo("articleName", aName);
0750: query = QueryFactory.newQuery(Article.class, criteria);
0751: col = broker.getCollectionByQuery(query);
0752: iter = col.iterator();
0753: assertTrue("should have one element", iter.hasNext());
0754: assertEquals("should be equal", aName, ((InterfaceArticle) iter
0755: .next()).getArticleName());
0756: //
0757: a.setArticleName("2 double quotes \"article\"_" + name);
0758: storeArticle(a);
0759: broker.clearCache();
0760: b = readArticleByIdentity(a);
0761: assertEquals(
0762: "after inserting an object it should be equal to its re-read pendant",
0763: a.getArticleName(), b.getArticleName());
0764: aName = a.getArticleName();
0765: criteria = new Criteria();
0766: criteria.addEqualTo("articleName", aName);
0767: query = QueryFactory.newQuery(Article.class, criteria);
0768: col = broker.getCollectionByQuery(query);
0769: iter = col.iterator();
0770: assertTrue("should have one element", iter.hasNext());
0771: assertEquals("should be equal", aName, ((InterfaceArticle) iter
0772: .next()).getArticleName());
0773: //
0774: a.setArticleName("a comma thing ,article,_" + name);
0775: storeArticle(a);
0776: broker.clearCache();
0777: b = readArticleByIdentity(a);
0778: assertEquals(
0779: "after inserting an object it should be equal to its re-read pendant",
0780: a.getArticleName(), b.getArticleName());
0781: aName = a.getArticleName();
0782: criteria = new Criteria();
0783: criteria.addEqualTo("articleName", aName);
0784: query = QueryFactory.newQuery(Article.class, criteria);
0785: col = broker.getCollectionByQuery(query);
0786: iter = col.iterator();
0787: assertTrue("should have one element", iter.hasNext());
0788: assertEquals("should be equal", aName, ((InterfaceArticle) iter
0789: .next()).getArticleName());
0790: }
0791:
0792: public void testGetByExampleAndGetByIdentity() throws Exception {
0793: String name = "testGetByExampleAndGetByIdentity_"
0794: + System.currentTimeMillis();
0795: Article a = createArticle(null, name);
0796: storeArticle(a);
0797: broker.clearCache();
0798: Article b = readArticleByIdentity(a);
0799: assertEquals(
0800: "after inserting an object it should be equal to its re-read pendant",
0801: a.getArticleName(), b.getArticleName());
0802: broker.clearCache();
0803: Article c = readArticleByExample(a.getArticleId());
0804: assertEquals(
0805: "after inserting an object it should be equal to its re-read pendant",
0806: a.getArticleName(), c.getArticleName());
0807: }
0808:
0809: /**
0810: * Insert the method's description here.
0811: * Creation date: (06.12.2000 21:51:22)
0812: */
0813: public void testGetCollectionByQuery() throws Exception {
0814: String name = "testGetCollectionByQuery_"
0815: + System.currentTimeMillis();
0816:
0817: Criteria criteria = new Criteria();
0818: criteria.addEqualTo("articleName", name);
0819: Query query = QueryFactory.newQuery(Article.class, criteria);
0820: Collection col = broker.getCollectionByQuery(query);
0821: assertEquals("size of collection should be zero", 0, col.size());
0822: //2. insert 3 matching items
0823: Article a1 = createArticle(null, name);
0824: broker.beginTransaction();
0825: broker.store(a1);
0826: Article a2 = createArticle(null, name);
0827: broker.store(a2);
0828: Article a3 = createArticle(null, name);
0829: broker.store(a3);
0830: broker.commitTransaction();
0831:
0832: // 3. check if all items are found
0833: broker.clearCache();
0834: col = broker.getCollectionByQuery(query);
0835: assertEquals("size of collection should be three", 3, col
0836: .size());
0837:
0838: assertEquals("size of count should be three", 3, broker
0839: .getCount(query));
0840:
0841: Iterator iter = col.iterator();
0842: while (iter.hasNext()) {
0843: assertEquals("should be same value", name,
0844: ((InterfaceArticle) iter.next()).getArticleName());
0845: }
0846: }
0847:
0848: public void testGetCollectionByQueryWithStartAndEnd()
0849: throws Exception {
0850: String name = "testGetCollectionByQueryWithStartAndEnd_"
0851: + System.currentTimeMillis();
0852: Criteria criteria = new Criteria();
0853: criteria.addEqualTo("articleName", name);
0854: // criteria.addEqualTo("isSelloutArticle", new Boolean(true));
0855: Query query = QueryFactory.newQuery(Article.class, criteria);
0856: Collection col = broker.getCollectionByQuery(query);
0857: assertEquals("size of collection should be zero", 0, col.size());
0858: //2. insert 5 matching items
0859: broker.beginTransaction();
0860: Article a1 = createArticle(null, name);
0861: broker.store(a1);
0862: Article a2 = createArticle(null, name);
0863: broker.store(a2);
0864: Article a3 = createArticle(null, name);
0865: broker.store(a3);
0866: Article a4 = createArticle(null, name);
0867: broker.store(a4);
0868: Article a5 = createArticle(null, name);
0869: broker.store(a5);
0870: broker.commitTransaction();
0871:
0872: broker.clearCache();
0873: // 3. set query start and end
0874: query.setStartAtIndex(2);
0875: query.setEndAtIndex(5);
0876:
0877: // 4. check if all items are found
0878: col = broker.getCollectionByQuery(query);
0879: assertEquals("size of collection should be four", 4, col.size());
0880:
0881: NumberRange range = new NumberRange(a1.getArticleId(), a5
0882: .getArticleId());
0883: Iterator iter = col.iterator();
0884: while (iter.hasNext()) {
0885: InterfaceArticle testIa = (InterfaceArticle) iter.next();
0886: assertEquals("should be same value", name, testIa
0887: .getArticleName());
0888: Integer id = testIa.getArticleId();
0889: assertTrue(
0890: "Id should be a number of the generated articles",
0891: range.containsInteger(id));
0892: }
0893:
0894: // read one item only
0895: // 1. set query start equals end
0896: query.setStartAtIndex(4);
0897: query.setEndAtIndex(4);
0898:
0899: // 2. check if only one item is found
0900: OJBIterator ojbIter = (OJBIterator) broker
0901: .getIteratorByQuery(query);
0902: assertEquals("size of iterator should be one", 1, ojbIter
0903: .size());
0904: InterfaceArticle test4 = (InterfaceArticle) ojbIter.next();
0905: ojbIter.releaseDbResources();
0906: assertTrue("Id should be a number of the generated articles",
0907: range.containsInteger(test4.getArticleId()));
0908: }
0909:
0910: public void testSorting() throws Exception {
0911: String name = "testSorting_" + System.currentTimeMillis();
0912: Criteria criteria = new Criteria();
0913: criteria.addEqualTo("articleName", name);
0914: QueryByCriteria query = QueryFactory.newQuery(Article.class,
0915: criteria);
0916: query.addOrderByDescending("articleId");
0917: Collection col = broker.getCollectionByQuery(query);
0918: assertEquals("size of collection should be zero", 0, col.size());
0919:
0920: //2. insert 3 matching items
0921: broker.beginTransaction();
0922: Article a1 = createArticle(null, name);
0923: broker.store(a1);
0924: Article a2 = createArticle(null, name);
0925: broker.store(a2);
0926: Article a3 = createArticle(null, name);
0927: broker.store(a3);
0928: broker.commitTransaction();
0929: // 3. check if all items are found
0930: col = broker.getCollectionByQuery(query);
0931: assertEquals("size of collection should be three", 3, col
0932: .size());
0933: Iterator iter = col.iterator();
0934:
0935: assertEquals("should be same value", a3.getArticleId(),
0936: ((InterfaceArticle) iter.next()).getArticleId());
0937: assertEquals("should be same value", a2.getArticleId(),
0938: ((InterfaceArticle) iter.next()).getArticleId());
0939: assertEquals("should be same value", a1.getArticleId(),
0940: ((InterfaceArticle) iter.next()).getArticleId());
0941: }
0942:
0943: /**
0944: * testing the sorted collections feature.)
0945: */
0946: public void testSortedCollectionAttribute() {
0947: String name = "testSortedCollectionAttribute_"
0948: + System.currentTimeMillis();
0949: ProductGroup samplePG = new ProductGroup();
0950: Article a1_ = createArticle(samplePG, name);
0951: Article a2_ = createArticle(samplePG, name);
0952: Article a3_ = createArticle(samplePG, name);
0953: // auto insert of referenced Article is enabled
0954: // and aX_ was added to PG
0955: broker.beginTransaction();
0956: broker.store(samplePG);
0957: broker.commitTransaction();
0958: broker.clearCache();
0959:
0960: InterfaceProductGroup pg = (InterfaceProductGroup) broker
0961: .getObjectByQuery(new QueryByIdentity(samplePG));
0962: List list = pg.getAllArticles();
0963: assertNotNull(list);
0964: assertEquals(3, list.size());
0965: NumberRange range = new NumberRange(a1_.getArticleId(), a3_
0966: .getArticleId());
0967: InterfaceArticle a1 = null;
0968: InterfaceArticle a2 = null;
0969: for (int i = 0; i < list.size(); i++) {
0970: a2 = a1;
0971: a1 = (InterfaceArticle) list.get(i);
0972: if (i > 0)
0973: assertTrue(a1.getArticleId().intValue() < a2
0974: .getArticleId().intValue());
0975: assertTrue(range.containsInteger(a1.getArticleId()));
0976: }
0977: }
0978:
0979: /**
0980: * Test the AutoIncrement facility
0981: */
0982: public void testAutoIncrement() throws Exception {
0983: // create new items for a class with autoincrement PK
0984: ProductGroup pg1 = new ProductGroup();
0985: // Identity id1 = new Identity(pg1, broker);
0986: ProductGroup pg2 = new ProductGroup();
0987: // Identity id2 = new Identity(pg2, broker);
0988: pg1.setName("AutoIncGroup1");
0989: pg2.setName("AutoIncGroup2");
0990: broker.beginTransaction();
0991: broker.store(pg1);
0992: broker.store(pg2);
0993: broker.commitTransaction();
0994: assertEquals("should have assigned to Integers with diff 1", 1,
0995: pg2.getId().intValue() - pg1.getId().intValue());
0996: }
0997:
0998: /**
0999: * do a count by report query
1000: */
1001: public void testCountByReportQuery() throws Exception {
1002: // 7 articles, 2 books, 3 cds
1003: Criteria criteria = new Criteria();
1004: criteria.addEqualTo("productGroupId", new Integer(5));
1005: ReportQueryByCriteria query = QueryFactory.newReportQuery(
1006: Article.class, criteria);
1007: query.setAttributes(new String[] { "count(*)" });
1008: Iterator iter = broker.getReportQueryIteratorByQuery(query);
1009: Object[] row;
1010: int count = 0;
1011:
1012: while (iter.hasNext()) {
1013: row = (Object[]) iter.next();
1014: count += ((Number) row[0]).intValue();
1015: }
1016: assertEquals("Iterator should produce 12 items", 12, count);
1017:
1018: // get count
1019: count = broker.getCount(query);
1020: assertEquals("Count should be 12", 12, count);
1021: }
1022:
1023: public void testMultiKeyCount() throws Exception {
1024: Criteria criteria = new Criteria();
1025: QueryByCriteria query1 = QueryFactory.newQuery(Role.class,
1026: criteria);
1027: QueryByCriteria query2 = QueryFactory.newQuery(Role.class,
1028: criteria, true);
1029:
1030: int count1 = broker.getCount(query1);
1031: int count2 = broker.getCount(query2);
1032:
1033: assertEquals("count and count distinct must match", count1,
1034: count2);
1035: }
1036:
1037: /**
1038: * extent aware iterator
1039: */
1040: public void testExtentAwareIteratorByQuery() throws Exception {
1041: // 7 articles, 2 books, 3 cds
1042: Criteria criteria = new Criteria();
1043: criteria.addEqualTo("productGroupId", new Integer(5));
1044: Query query = QueryFactory.newQuery(Article.class, criteria);
1045: ReportQueryByCriteria reportQuery;
1046: Iterator iter = broker.getIteratorByQuery(query);
1047: Collection result = new Vector();
1048: InterfaceArticle article;
1049: int count;
1050:
1051: while (iter.hasNext()) {
1052: article = (InterfaceArticle) iter.next();
1053: result.add(article);
1054: }
1055: assertEquals("Iterator should produce 12 items", 12, result
1056: .size());
1057:
1058: // get count
1059: count = broker.getCount(query);
1060: assertEquals("Count should be 12", 12, count);
1061:
1062: reportQuery = QueryFactory.newReportQuery(Article.class,
1063: criteria);
1064: reportQuery.setAttributes(new String[] { "count(*)" });
1065: iter = broker.getReportQueryIteratorByQuery(reportQuery);
1066:
1067: while (iter.hasNext()) {
1068: result.add(iter.next());
1069: }
1070: }
1071:
1072: public void testGetIteratorByQuery() throws Exception {
1073: String name = "testGetIteratorByQuery_"
1074: + System.currentTimeMillis();
1075: Criteria criteria = new Criteria();
1076: criteria.addEqualTo("articleName", name);
1077: Query query = QueryFactory.newQuery(Article.class, criteria);
1078: Iterator iter = broker.getIteratorByQuery(query);
1079: assertTrue("size of Iterator should be zero", !iter.hasNext());
1080: //2. insert 3 matching items
1081: broker.beginTransaction();
1082: Article a1 = createArticle(null, name);
1083: broker.store(a1);
1084: Article a2 = createArticle(null, name);
1085: broker.store(a2);
1086: Article a3 = createArticle(null, name);
1087: broker.store(a3);
1088: broker.commitTransaction();
1089:
1090: // 3. check if all items are found
1091: iter = broker.getIteratorByQuery(query);
1092: int count = 0;
1093: while (iter.hasNext()) {
1094: count++;
1095: assertEquals("should be same value", name,
1096: ((InterfaceArticle) iter.next()).getArticleName());
1097: }
1098: assertEquals("Iterator should produce 3 items", 3, count);
1099: }
1100:
1101: /**
1102: * Testing the getIteratorBySQL functionality
1103: */
1104: public void testGetIteratorBySQL() throws Exception {
1105: String name = "testGetIteratorBySQL_"
1106: + System.currentTimeMillis();
1107: // prepare test
1108: ProductGroup pg = new ProductGroup();
1109: pg.setGroupName(name);
1110: Article a1_ = createArticle(pg, name);
1111: Article a2_ = createArticle(pg, name);
1112: Article a3_ = createArticle(pg, name);
1113: // auto insert of referenced Article is enabled
1114: // and aX_ was added to PG
1115: broker.beginTransaction();
1116: broker.store(pg);
1117: broker.commitTransaction();
1118: broker.clearCache();
1119:
1120: Criteria criteria = new Criteria();
1121: criteria.addEqualTo("productGroupId", pg.getId());
1122: Query query = QueryFactory.newQuery(Article.class, criteria);
1123: Iterator iter1 = broker.getIteratorByQuery(query);
1124: String sql = "SELECT A.Artikel_Nr FROM Artikel A, Kategorien PG"
1125: + " WHERE A.Kategorie_Nr = PG.Kategorie_Nr"
1126: + " AND PG.Kategorie_Nr = " + pg.getId();
1127:
1128: Query q2 = QueryFactory.newQuery(Article.class, sql);
1129: Iterator iter2 = broker.getIteratorByQuery(q2);
1130: while (iter1.hasNext()) {
1131: InterfaceArticle a1 = (InterfaceArticle) iter1.next();
1132: InterfaceArticle a2 = (InterfaceArticle) iter2.next();
1133: assertEquals("iterators should return equal objects", a1
1134: .getArticleId(), a2.getArticleId());
1135: }
1136: assertTrue("iter2 should not contain more items than iter1",
1137: !iter2.hasNext());
1138: }
1139:
1140: /**
1141: * Testing the getReportQueryIteratorBySQL functionality
1142: */
1143: public void testGetReportQueryIteratorBySQL() {
1144: String sql = "SELECT * FROM Artikel A, Kategorien PG"
1145: + " WHERE A.Kategorie_Nr = PG.Kategorie_Nr"
1146: + " AND PG.Kategorie_Nr = 2";
1147: Query q = QueryFactory.newQuery(Article.class, sql);
1148:
1149: Iterator iter = broker.getReportQueryIteratorByQuery(q);
1150:
1151: while (iter.hasNext()) {
1152: Object[] arr = (Object[]) iter.next();
1153: for (int i = 0; i < arr.length; i++) {
1154: //System.out.print(arr[i] + ", ");
1155: }
1156: //System.out.println();
1157: }
1158: }
1159:
1160: public void testGetMultipleIteratorsByQuery() throws Exception {
1161: String name = "testGetIteratorBySQL_"
1162: + System.currentTimeMillis();
1163: // 1. ensure there are 0 items matching the query
1164: Criteria criteria = new Criteria();
1165: criteria.addEqualTo("articleName", name);
1166: Query query = QueryFactory.newQuery(Article.class, criteria);
1167: Iterator iter = broker.getIteratorByQuery(query);
1168: assertTrue("size of Iterator should be zero", !iter.hasNext());
1169: //2. insert 3 matching items
1170: broker.beginTransaction();
1171: Article a1 = createArticle(null, name);
1172: broker.store(a1);
1173: Article a2 = createArticle(null, name);
1174: broker.store(a2);
1175: Article a3 = createArticle(null, name);
1176: broker.store(a3);
1177: broker.commitTransaction();
1178:
1179: // 3. return multiple iterators
1180: Iterator i1 = broker.getIteratorByQuery(query);
1181: Iterator i2 = broker.getIteratorByQuery(query);
1182: Iterator i3 = broker.getIteratorByQuery(query);
1183: // 4. TestThreadsNLocks the iterators
1184: for (int i = 0; i < 3; i++) {
1185: assertTrue("should have more elements", i3.hasNext());
1186: assertTrue("should have more elements", i1.hasNext());
1187: assertTrue("should have more elements", i2.hasNext());
1188: assertEquals("should be same value", name,
1189: ((InterfaceArticle) i2.next()).getArticleName());
1190: assertEquals("should be same value", name,
1191: ((InterfaceArticle) i1.next()).getArticleName());
1192: assertEquals("should be same value", name,
1193: ((InterfaceArticle) i3.next()).getArticleName());
1194: }
1195: }
1196:
1197: public void testGetObjectByQuery() throws Exception {
1198: String name = "testGetIteratorBySQL_"
1199: + System.currentTimeMillis();
1200: // ensure article is persistent
1201: Article a = createArticle(null, name);
1202: storeArticle(a);
1203:
1204: // build query-by-example and execute
1205: Query query = QueryFactory.newQuery(a);
1206: Article b = (Article) broker.getObjectByQuery(query);
1207: assertEquals(
1208: "after inserting an object it should be equal to its re-read pendant",
1209: a.getArticleName(), b.getArticleName());
1210:
1211: Article c = readArticleByExample(a.getArticleId());
1212: assertEquals(
1213: "after inserting an object it should be equal to its re-read pendant",
1214: a.getArticleName(), c.getArticleName());
1215:
1216: // now TestThreadsNLocks a criteria query
1217: Criteria crit = new Criteria();
1218: crit.addEqualTo("articleId", a.getArticleId());
1219: Query q = QueryFactory.newQuery(Article.class, crit);
1220: InterfaceArticle d = (InterfaceArticle) broker
1221: .getObjectByQuery(q);
1222: assertEquals(
1223: "after inserting an object it should be equal to its re-read pendant",
1224: a.getArticleName(), d.getArticleName());
1225: }
1226:
1227: public void testGetPKEnumerationByConstraints() throws Exception {
1228: String name = "testGetPKEnumerationByConstraints_"
1229: + System.currentTimeMillis();
1230: // 1. ensure there are 0 items matching the query
1231: Criteria criteria = new Criteria();
1232: criteria.addEqualTo("articleName", name);
1233: Query query = QueryFactory.newQuery(Article.class, criteria);
1234: Enumeration en = ((PersistenceBrokerInternal) broker)
1235: .getPKEnumerationByQuery(ArticlePrimaryKey.class, query);
1236: assertTrue("size of collection should be zero", !en
1237: .hasMoreElements());
1238:
1239: //2. insert 3 matching items
1240: broker.beginTransaction();
1241: Article a1 = createArticle(null, name);
1242: broker.store(a1);
1243: Article a2 = createArticle(null, name);
1244: broker.store(a2);
1245: Article a3 = createArticle(null, name);
1246: broker.store(a3);
1247: broker.commitTransaction();
1248: // 3. check if all items are found
1249: en = ((PersistenceBrokerInternal) broker)
1250: .getPKEnumerationByQuery(ArticlePrimaryKey.class, query);
1251: int count = 0;
1252: while (en.hasMoreElements()) {
1253: count++;
1254: Article tmp = readArticleByIdentity(new Integer(
1255: ((ArticlePrimaryKey) en.nextElement()).id));
1256: assertEquals("should be same value", name, tmp
1257: .getArticleName());
1258: }
1259: assertEquals("Iterator should produce 3 items", 3, count);
1260: }
1261:
1262: public void testInsert() throws Exception {
1263: String name = "testInsert_" + System.currentTimeMillis();
1264: Article a = createArticle(null, name);
1265: Article b = readArticleByIdentity(a.getArticleId());
1266: assertNull("should be null after deletion", b);
1267: storeArticle(a);
1268: b = readArticleByIdentity(a);
1269: assertEquals(
1270: "after inserting an object it should be equal to its re-read pendant",
1271: a.getArticleName(), b.getArticleName());
1272: // check if object is not only stored in cache but also in db
1273: b = null;
1274: broker.clearCache();
1275: b = readArticleByIdentity(a.getArticleId());
1276: assertEquals(
1277: "after inserting and flushing the cache an object should still be equal to its re-read pendant",
1278: a.getArticleName(), b.getArticleName());
1279: }
1280:
1281: public void testUpdate() throws Exception {
1282: String name = "testUpdate_" + System.currentTimeMillis();
1283: Article a = createArticle(null, name);
1284: storeArticle(a);
1285: Article b = readArticleByIdentity(a);
1286: assertEquals(
1287: "after inserting an object it should be equal to its re-read pendant",
1288: a.getArticleName(), b.getArticleName());
1289: String newname = "TESTUPDATE_" + name;
1290: b.setArticleName(newname);
1291: storeArticle(b);
1292: b = readArticleByIdentity(a.getArticleId());
1293: assertEquals("should be equal after update", newname, b
1294: .getArticleName());
1295: // ensure that object is really stored in DB and not only in ObjectCache
1296: broker.clearCache();
1297: b = readArticleByIdentity(a.getArticleId());
1298: assertEquals("should be equal after update and db lookup",
1299: newname, b.getArticleName());
1300: }
1301:
1302: public void testUpdateWithModification() throws Exception {
1303: String name = "testUpdateWithModification_"
1304: + System.currentTimeMillis();
1305: assertFalse("should not be marked for update yet",
1306: ObjectModification.INSERT.needsUpdate());
1307: assertFalse("should not be marked for insert",
1308: ObjectModification.UPDATE.needsInsert());
1309: Article a = createArticle(null, name);
1310:
1311: broker.beginTransaction();
1312: broker.store(a, ObjectModification.INSERT);
1313: broker.commitTransaction();
1314:
1315: Article b = readArticleByIdentity(a.getArticleId());
1316: assertEquals(
1317: "after inserting an object it should be equal to its re-read pendant",
1318: a.getArticleName(), b.getArticleName());
1319: String newname = "TESTUPDATE_" + name;
1320: b.setArticleName(newname);
1321: broker.beginTransaction();
1322: broker.store(b, ObjectModification.UPDATE);
1323: broker.commitTransaction();
1324:
1325: b = null;
1326: b = readArticleByIdentity(a.getArticleId());
1327: assertEquals("should be equal after update", newname, b
1328: .getArticleName());
1329: }
1330:
1331: /**
1332: * test if reference to Proxy is updated
1333: * @throws Exception
1334: */
1335: public void testUpdateReferencedProxy() throws Exception {
1336: String name = "testUpdateReferencedProxy_"
1337: + System.currentTimeMillis();
1338: ProductGroup pg = new ProductGroup();
1339: pg.setGroupName(name);
1340: Article b = createArticle(pg, name);
1341: broker.beginTransaction();
1342: broker.store(pg);
1343: // not needed (auto insert of reference), anyway... we do it
1344: broker.store(b);
1345: broker.commitTransaction();
1346:
1347: broker.clearCache();
1348: b = readArticleByIdentity(b.getArticleId());
1349: InterfaceProductGroup pgb = b.getProductGroup();
1350: assertEquals("should be equal after update", pg.getId(), pgb
1351: .getId());
1352: }
1353:
1354: public void testChangeFieldsWhileStoringObject() {
1355: long timestamp = System.currentTimeMillis();
1356:
1357: broker.beginTransaction();
1358: Person p = new Person();
1359: p.setFirstname("no_1_" + timestamp);
1360: p.setLastname("no_1_" + timestamp);
1361: broker.store(p);
1362: // change fields
1363: p.setFirstname("no_2_" + timestamp);
1364: p.setLastname("no_2_" + timestamp);
1365: // store changed object again
1366: broker.store(p);
1367: broker.commitTransaction();
1368:
1369: Identity id = new Identity(p, broker);
1370: Person result = (Person) broker.getObjectByIdentity(id);
1371: assertNotNull(result);
1372: assertEquals("no_2_" + timestamp, result.getFirstname());
1373: assertEquals("no_2_" + timestamp, result.getLastname());
1374:
1375: /*
1376: same with cleared cache
1377: */
1378: timestamp = System.currentTimeMillis() + 1;
1379: broker.beginTransaction();
1380: p = new Person();
1381: p.setFirstname("no_3_" + timestamp);
1382: p.setLastname("no_3_" + timestamp);
1383: broker.store(p);
1384: broker.clearCache();
1385: p.setFirstname("no_4_" + timestamp);
1386: p.setLastname("no_4_" + timestamp);
1387: broker.store(p);
1388: broker.commitTransaction();
1389:
1390: broker.clearCache();
1391: id = new Identity(p, broker);
1392: broker.clearCache();
1393: result = (Person) broker.getObjectByIdentity(id);
1394: assertNotNull(result);
1395: assertEquals("no_4_" + timestamp, result.getFirstname());
1396: assertEquals("no_4_" + timestamp, result.getLastname());
1397: }
1398:
1399: public void testDoubleStore() {
1400: long timestamp = System.currentTimeMillis();
1401:
1402: Person person = new Person();
1403: person.setFirstname("testDoubleStore_" + timestamp);
1404: person.setLastname("time_" + timestamp);
1405:
1406: broker.beginTransaction();
1407: // Identity used to assign PK of object
1408: Identity oid = new Identity(person, broker);
1409: Person serializedPerson = (Person) SerializationUtils
1410: .clone(person);
1411: broker.store(person);
1412: broker.store(person);
1413: broker.store(serializedPerson);
1414: broker.commitTransaction();
1415:
1416: Criteria crit = new Criteria();
1417: crit.addLike("firstName", "testDoubleStore_" + timestamp);
1418: Query query = QueryFactory.newQuery(Person.class, crit);
1419: Collection result = broker.getCollectionByQuery(query);
1420:
1421: assertEquals("Expect to find exact 1 object for " + oid, 1,
1422: result.size());
1423: }
1424:
1425: public void testDoubleDelete() {
1426: long timestamp = System.currentTimeMillis();
1427:
1428: Person person = new Person();
1429: person.setFirstname("testDoubleDelete_" + timestamp);
1430: person.setLastname("time_" + timestamp);
1431:
1432: broker.beginTransaction();
1433: // Identity oid = new Identity(person, broker);
1434: Person serializedPerson = (Person) SerializationUtils
1435: .clone(person);
1436: broker.store(person);
1437: broker.commitTransaction();
1438:
1439: Criteria crit = new Criteria();
1440: crit.addLike("firstName", "testDoubleDelete_" + timestamp);
1441: Query query = QueryFactory.newQuery(Person.class, crit);
1442: Collection result = broker.getCollectionByQuery(query);
1443: assertEquals("Expect to find exact 1 object", 1, result.size());
1444:
1445: broker.beginTransaction();
1446: broker.delete(person);
1447: broker.delete(serializedPerson);
1448: broker.delete(person);
1449: broker.commitTransaction();
1450:
1451: broker.beginTransaction();
1452: broker.delete(serializedPerson);
1453: broker.commitTransaction();
1454:
1455: result = broker.getCollectionByQuery(query);
1456: assertEquals("Expect to find none objects", 0, result.size());
1457: }
1458:
1459: /**
1460: * Test if only one query is executed for each extent.<br>
1461: * If the same query is run multiple times the result will contain duplicates
1462: */
1463: public void testDuplicateExtentQueries() {
1464: Collection result;
1465: Set set = new HashSet();
1466: Criteria crit = new Criteria();
1467: crit.addGreaterThan("articleId", new Integer(70));
1468: QueryByCriteria qry = new QueryByCriteria(
1469: InterfaceArticle.class, crit);
1470:
1471: broker.clearCache();
1472: result = broker.getCollectionByQuery(qry);
1473: set.addAll(result);
1474:
1475: assertEquals("Both sizes must be equal", set.size(), result
1476: .size());
1477: }
1478:
1479: /**
1480: * Size returned by Iterator must be same as size of Collection
1481: */
1482: public void testIteratorSize() {
1483: OJBIterator ojbIter;
1484: Criteria crit;
1485: QueryByCriteria query;
1486: int collSize;
1487: int iterSize;
1488:
1489: crit = new Criteria();
1490: query = QueryFactory.newQuery(Article.class, crit);
1491:
1492: collSize = broker.getCollectionByQuery(query).size();
1493:
1494: ojbIter = (OJBIterator) broker.getIteratorByQuery(query);
1495: iterSize = ojbIter.size();
1496:
1497: assertEquals("collSize == iterSize", collSize, iterSize);
1498: ojbIter.releaseDbResources();
1499: }
1500:
1501: public void testPaging() {
1502: OJBIterator ojbIter;
1503: Criteria crit;
1504: QueryByCriteria query;
1505:
1506: // All Articles index in range
1507: crit = new Criteria();
1508: query = QueryFactory.newQuery(Article.class, crit);
1509: int fullSize = broker.getCollectionByQuery(query).size();
1510:
1511: query.setStartAtIndex(10);
1512: query.setEndAtIndex(14);
1513: ojbIter = (OJBIterator) broker.getIteratorByQuery(query);
1514: assertEquals("index 10 - 14 expecting 5 rows", 5, ojbIter
1515: .size());
1516: ojbIter.releaseDbResources();
1517: }
1518:
1519: public void testPagingPosition() {
1520: String name = "testPagingPosition_"
1521: + System.currentTimeMillis();
1522: broker.beginTransaction();
1523: for (int i = 1; i < 21; i++) {
1524: Article a = createArticle(null, name);
1525: a.setStock(i);
1526: broker.store(a);
1527: }
1528: broker.commitTransaction();
1529:
1530: OJBIterator ojbIter;
1531: Criteria crit;
1532: QueryByCriteria query;
1533: Collection fullColl, pagedColl;
1534: InterfaceArticle article;
1535:
1536: // All Articles index in range
1537: crit = new Criteria();
1538: crit.addEqualTo("articleName", name);
1539: query = QueryFactory.newQuery(Article.class, crit);
1540: query.addOrderByAscending("stock");
1541: fullColl = broker.getCollectionByQuery(query);
1542: assertEquals(20, fullColl.size());
1543:
1544: // limited query
1545: query.setStartAtIndex(10);
1546: query.setEndAtIndex(14);
1547: pagedColl = broker.getCollectionByQuery(query);
1548:
1549: ojbIter = (OJBIterator) broker.getIteratorByQuery(query);
1550:
1551: assertEquals("collection- and iterator-size must match",
1552: pagedColl.size(), ojbIter.size());
1553: assertEquals("index 10 - 14 expecting 5 rows", 5, ojbIter
1554: .size());
1555:
1556: ojbIter.absolute(2);
1557: article = (InterfaceArticle) ojbIter.next();
1558: assertEquals("Article stock=12", article.getStock(), 12);
1559:
1560: ojbIter.relative(-1);
1561: article = (InterfaceArticle) ojbIter.next();
1562: assertEquals("Article id=12", article.getStock(), 12);
1563:
1564: ojbIter.relative(-1);
1565: article = (InterfaceArticle) ojbIter.next();
1566: assertEquals("Article id=12", article.getStock(), 12);
1567:
1568: // last
1569: ojbIter.absolute(12);
1570: article = (InterfaceArticle) ojbIter.next();
1571: assertEquals("Article id=15", article.getStock(), 15);
1572:
1573: // first
1574: ojbIter.absolute(-12);
1575: article = (InterfaceArticle) ojbIter.next();
1576: assertEquals("Article id=10", article.getStock(), 10);
1577:
1578: ojbIter.releaseDbResources();
1579: }
1580:
1581: public void testPagingIndicesOutOfRange() {
1582: OJBIterator ojbIter;
1583: Criteria crit;
1584: QueryByCriteria query;
1585: int fullSize;
1586:
1587: // All Articles index out of range
1588: crit = new Criteria();
1589: query = QueryFactory.newQuery(Article.class, crit);
1590: fullSize = broker.getCollectionByQuery(query).size();
1591:
1592: query.setStartAtIndex(fullSize + 5);
1593: query.setEndAtIndex(fullSize + 14);
1594: ojbIter = (OJBIterator) broker.getIteratorByQuery(query);
1595: assertEquals("indices out of range expecting 0 rows", 0,
1596: ojbIter.size());
1597: ojbIter.releaseDbResources();
1598: }
1599:
1600: public void testPagingEndIndexOutOfRange() {
1601: OJBIterator ojbIter;
1602: Criteria crit;
1603: QueryByCriteria query;
1604: int fullSize;
1605:
1606: // All Articles index out of range
1607: crit = new Criteria();
1608: query = QueryFactory.newQuery(Article.class, crit);
1609: fullSize = broker.getCollectionByQuery(query).size();
1610:
1611: query.setStartAtIndex(fullSize - 9);
1612: query.setEndAtIndex(fullSize + 9);
1613: ojbIter = (OJBIterator) broker.getIteratorByQuery(query);
1614: assertEquals("end index out of range expecting 10 rows", 10,
1615: ojbIter.size());
1616: ojbIter.releaseDbResources();
1617: }
1618:
1619: public void testPagingEmptyIterator() {
1620: OJBIterator ojbIter;
1621: Criteria crit;
1622: QueryByCriteria query;
1623:
1624: // looking for inexistent Article
1625: crit = new Criteria();
1626: crit.addEqualTo("articleId", new Integer(-777));
1627: query = QueryFactory.newQuery(Article.class, crit);
1628: int fullSize = broker.getCollectionByQuery(query).size();
1629:
1630: query.setStartAtIndex(10);
1631: query.setEndAtIndex(14);
1632: ojbIter = (OJBIterator) broker.getIteratorByQuery(query);
1633: assertEquals(
1634: "index 10 - 14 expecting 0 rows for empty iterator", 0,
1635: ojbIter.size());
1636: ojbIter.releaseDbResources();
1637: }
1638: }
|