0001: package org.apache.ojb.broker;
0002:
0003: import java.io.Serializable;
0004: import java.util.ArrayList;
0005: import java.util.Collection;
0006: import java.util.Collections;
0007: import java.util.Iterator;
0008: import java.util.List;
0009:
0010: import org.apache.commons.lang.SerializationUtils;
0011: import org.apache.commons.lang.builder.EqualsBuilder;
0012: import org.apache.commons.lang.builder.ToStringBuilder;
0013: import org.apache.commons.lang.builder.ToStringStyle;
0014: import org.apache.ojb.broker.metadata.ClassDescriptor;
0015: import org.apache.ojb.broker.query.Criteria;
0016: import org.apache.ojb.broker.query.Query;
0017: import org.apache.ojb.broker.query.QueryByCriteria;
0018: import org.apache.ojb.broker.query.QueryFactory;
0019: import org.apache.ojb.broker.util.ObjectModification;
0020: import org.apache.ojb.junit.PBTestCase;
0021:
0022: /**
0023: * These tests check inheritance using multiple tables via 1:1 reference and "super" keyword in
0024: * reference descriptor. The test objects use a composite PK.
0025: * One autoincrement PK field - Integer. One non-autoincrement PK field with manually set PK- Long.
0026: *
0027: * @author <a href="mailto:arminw@apache.org">Armin Waibel</a>
0028: * @version $Id: InheritanceMultipleTableTest.java,v 1.7.2.20 2005/12/29 22:18:21 arminw Exp $
0029: */
0030: public class InheritanceMultipleTableTest extends PBTestCase {
0031: public static void main(String[] args) {
0032: junit.textui.TestRunner
0033: .main(new String[] { InheritanceMultipleTableTest.class
0034: .getName() });
0035: }
0036:
0037: public void testLookupByIdentity() {
0038: long timestamp = System.currentTimeMillis();
0039: Long id_2 = new Long(timestamp);
0040: String name = "testLookupByIdentity_" + timestamp;
0041: Employee em1 = new Employee(id_2, "employee_" + name);
0042: Executive ex1 = new Executive(id_2, "executive_" + name,
0043: "department_1", null);
0044: Executive ex2 = new Executive(id_2, "executive_" + name,
0045: "department_2", null);
0046: ArrayList list = new ArrayList();
0047: list.add(ex1);
0048: list.add(ex2);
0049: Manager m1 = new Manager(id_2, "manager_" + name);
0050: m1.setExecutives(list);
0051:
0052: broker.beginTransaction();
0053: broker.store(em1);
0054: broker.store(m1);
0055: broker.commitTransaction();
0056:
0057: Identity m1_oid = broker.serviceIdentity().buildIdentity(m1);
0058: Identity ex1_oid = broker.serviceIdentity().buildIdentity(ex1);
0059: Identity em1_oid = broker.serviceIdentity().buildIdentity(em1);
0060:
0061: broker.clearCache();
0062:
0063: Employee newEm1 = (Employee) broker
0064: .getObjectByIdentity(em1_oid);
0065: Executive newEx1 = (Executive) broker
0066: .getObjectByIdentity(ex1_oid);
0067: Manager newM1 = (Manager) broker.getObjectByIdentity(m1_oid);
0068:
0069: assertNotNull(newEm1);
0070: assertNotNull(newEx1);
0071: assertNotNull(newM1);
0072:
0073: assertEquals(em1.getId(), newEm1.getId());
0074: assertEquals(em1.getId_2(), newEm1.getId_2());
0075: assertEquals(2, newM1.getExecutives().size());
0076:
0077: assertEquals(m1.getId(), newM1.getId());
0078: assertEquals(m1.getId_2(), newM1.getId_2());
0079: assertEquals(2, newM1.getExecutives().size());
0080:
0081: assertEquals(ex1.getId(), newEx1.getId());
0082: assertEquals(ex1.getId_2(), newEx1.getId_2());
0083: assertEquals(ex1.getDepartment(), newEx1.getDepartment());
0084: }
0085:
0086: public void testLookupByQuery() {
0087: long timestamp = System.currentTimeMillis();
0088: Long id_2 = new Long(timestamp);
0089: String name = "testLookupByIdentity_" + timestamp;
0090: Employee em1 = new Employee(id_2, "employee_" + name);
0091: Executive ex1 = new Executive(id_2, "executive_" + name,
0092: "department_1", null);
0093: Executive ex2 = new Executive(id_2, "executive_" + name,
0094: "department_2", null);
0095: ArrayList list = new ArrayList();
0096: list.add(ex1);
0097: list.add(ex2);
0098: Manager m1 = new Manager(id_2, "manager_" + name);
0099: m1.setExecutives(list);
0100:
0101: broker.beginTransaction();
0102: broker.store(em1);
0103: broker.store(m1);
0104: broker.commitTransaction();
0105:
0106: Criteria crit = new Criteria();
0107: crit.addEqualTo("name", "employee_" + name);
0108: Query q = QueryFactory.newQuery(Employee.class, crit);
0109: Employee newEm1 = (Employee) broker.getObjectByQuery(q);
0110:
0111: crit = new Criteria();
0112: crit.addEqualTo("name", "executive_" + name);
0113: q = QueryFactory.newQuery(Employee.class, crit);
0114: Executive newEx1 = (Executive) broker.getObjectByQuery(q);
0115:
0116: crit = new Criteria();
0117: crit.addEqualTo("name", "manager_" + name);
0118: q = QueryFactory.newQuery(Employee.class, crit);
0119: Manager newM1 = (Manager) broker.getObjectByQuery(q);
0120:
0121: broker.clearCache();
0122:
0123: assertNotNull(newEm1);
0124: assertNotNull(newEx1);
0125: assertNotNull(newM1);
0126: assertEquals(2, newM1.getExecutives().size());
0127: assertEquals(em1.getId(), newEm1.getId());
0128: assertEquals(em1.getId_2(), newEm1.getId_2());
0129:
0130: assertEquals(m1.getId(), newM1.getId());
0131: assertEquals(m1.getId_2(), newM1.getId_2());
0132:
0133: assertEquals(ex1.getId(), newEx1.getId());
0134: assertEquals(ex1.getId_2(), newEx1.getId_2());
0135: }
0136:
0137: public void testQueryInheritancedObjects() {
0138: if (ojbSkipKnownIssueProblem("Classes mapped to multiple joined tables will always be instantiated "
0139: + " with the class type of the query, instead of the real type")) {
0140: return;
0141: }
0142:
0143: long timestamp = System.currentTimeMillis();
0144: String name = "testQueryInheritancedObjects_" + timestamp;
0145: // store company with Employee/Executive/Manager
0146: Company company = prepareTestDataWithCompany(name);
0147: Long id_2 = company.getId();
0148:
0149: // add Shareholder too
0150: Shareholder shareholder = new Shareholder(id_2, name);
0151: shareholder.setShare(77);
0152: shareholder.setDepartment("none");
0153: AddressIF ad = new Address(name);
0154: shareholder.setAddress(ad);
0155:
0156: broker.beginTransaction();
0157: broker.store(shareholder);
0158: broker.commitTransaction();
0159:
0160: broker.clearCache();
0161: // now we expect 7 objects when query for all Employee (this is the base class)
0162: Criteria crit = new Criteria();
0163: crit.addEqualTo("id_2", id_2);
0164: crit.addLike("name", "%" + name + "%");
0165: Query query = QueryFactory.newQuery(Employee.class, crit);
0166: Collection result = broker.getCollectionByQuery(query);
0167: assertEquals(7, result.size());
0168: int employeeCount = 0;
0169: int executiveCount = 0;
0170: int managerCount = 0;
0171: int shareholderCount = 0;
0172: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0173: Object obj = iterator.next();
0174: if (obj instanceof Employee)
0175: ++employeeCount;
0176: if (obj instanceof Executive)
0177: ++executiveCount;
0178: if (obj instanceof Manager)
0179: ++managerCount;
0180: if (obj instanceof Shareholder)
0181: ++shareholderCount;
0182: }
0183: assertEquals(7, employeeCount);
0184: assertEquals(6, executiveCount);
0185: assertEquals(4, managerCount);
0186: assertEquals(1, shareholderCount);
0187:
0188: broker.clearCache();
0189: // now we expect 4 objects when query for all Manager
0190: crit = new Criteria();
0191: crit.addEqualTo("id_2", id_2);
0192: crit.addLike("name", "%" + name + "%");
0193: query = QueryFactory.newQuery(Manager.class, crit);
0194: result = broker.getCollectionByQuery(query);
0195: assertEquals(4, result.size());
0196: employeeCount = 0;
0197: executiveCount = 0;
0198: managerCount = 0;
0199: shareholderCount = 0;
0200: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0201: Object obj = iterator.next();
0202: if (obj instanceof Employee)
0203: ++employeeCount;
0204: if (obj instanceof Executive)
0205: ++executiveCount;
0206: if (obj instanceof Manager)
0207: ++managerCount;
0208: if (obj instanceof Shareholder)
0209: ++shareholderCount;
0210: }
0211: assertEquals(4, employeeCount);
0212: assertEquals(4, executiveCount);
0213: assertEquals(4, managerCount);
0214: assertEquals(1, shareholderCount);
0215:
0216: broker.clearCache();
0217: // now we expect 1 objects when query for all Shareholder
0218: crit = new Criteria();
0219: crit.addEqualTo("id_2", id_2);
0220: crit.addLike("name", "%" + name + "%");
0221: query = QueryFactory.newQuery(Shareholder.class, crit);
0222: result = broker.getCollectionByQuery(query);
0223: assertEquals(1, result.size());
0224: employeeCount = 0;
0225: executiveCount = 0;
0226: managerCount = 0;
0227: shareholderCount = 0;
0228: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0229: Object obj = iterator.next();
0230: if (obj instanceof Employee)
0231: ++employeeCount;
0232: if (obj instanceof Executive)
0233: ++executiveCount;
0234: if (obj instanceof Manager)
0235: ++managerCount;
0236: if (obj instanceof Shareholder)
0237: ++shareholderCount;
0238: }
0239: assertEquals(1, employeeCount);
0240: assertEquals(1, executiveCount);
0241: assertEquals(1, managerCount);
0242: assertEquals(1, shareholderCount);
0243: }
0244:
0245: public void testQueryInheritancedObjectsById() {
0246: long timestamp = System.currentTimeMillis();
0247: String name = "testQueryInheritancedObjectsByPk_" + timestamp;
0248: Long id_2 = new Long(timestamp);
0249:
0250: List insertedObjs = prepareForQueryTests(id_2, name);
0251:
0252: // add Shareholder
0253: Shareholder shareholder = new Shareholder(id_2, name);
0254: shareholder.setShare(77);
0255: shareholder.setDepartment("none");
0256: AddressIF ad = new Address(name);
0257: shareholder.setAddress(ad);
0258:
0259: broker.beginTransaction();
0260: broker.store(shareholder);
0261: broker.commitTransaction();
0262:
0263: broker.clearCache();
0264:
0265: Employee emp1;
0266: Identity ident;
0267: Employee retrievedEmp;
0268:
0269: // retrieve Manager by pk
0270: emp1 = (Employee) insertedObjs.get(0);
0271: ident = broker.serviceIdentity().buildIdentity(emp1);
0272: retrievedEmp = (Employee) broker.getObjectByIdentity(ident);
0273: assertNotNull(retrievedEmp);
0274: assertSame(Manager.class, retrievedEmp.getClass());
0275: assertEquals(emp1, retrievedEmp);
0276:
0277: // retrieve Executive by pk
0278: emp1 = (Employee) insertedObjs.get(3);
0279: ident = broker.serviceIdentity().buildIdentity(emp1);
0280: retrievedEmp = (Employee) broker.getObjectByIdentity(ident);
0281: assertNotNull(retrievedEmp);
0282: assertSame(Executive.class, retrievedEmp.getClass());
0283: assertEquals(emp1, retrievedEmp);
0284:
0285: // retrieve Employee by pk
0286: emp1 = (Employee) insertedObjs.get(5);
0287: ident = broker.serviceIdentity().buildIdentity(emp1);
0288: retrievedEmp = (Employee) broker.getObjectByIdentity(ident);
0289: assertNotNull(retrievedEmp);
0290: assertSame(Employee.class, retrievedEmp.getClass());
0291: assertEquals(emp1, retrievedEmp);
0292:
0293: // retrieve Shareholder by pk
0294: emp1 = shareholder;
0295: ident = broker.serviceIdentity().buildIdentity(emp1);
0296: retrievedEmp = (Employee) broker.getObjectByIdentity(ident);
0297: assertNotNull(retrievedEmp);
0298: assertSame(Shareholder.class, retrievedEmp.getClass());
0299: assertEquals(emp1, retrievedEmp);
0300: }
0301:
0302: public void testJavaInheritance() {
0303: ojbSkipKnownIssueProblem("Declared inheritance (without java inheritance)"
0304: + "of classes is currently not supported and will be difficult to implement");
0305:
0306: String name = "testWithoutJavaInheritance_tmp"
0307: + System.currentTimeMillis();
0308: Animal animal = new Animal(name, 55);
0309: Food f1 = new Food(name + "fruit1");
0310: Food f2 = new Food(name + "fruit2");
0311: animal.addFood(f1);
0312: animal.addFood(f2);
0313: // animal.setParent(animal);
0314:
0315: broker.beginTransaction();
0316: broker.store(animal);
0317: broker.commitTransaction();
0318: Identity oid = broker.serviceIdentity().buildIdentity(animal);
0319:
0320: broker.clearCache();
0321: Animal newAnimal = (Animal) broker.getObjectByIdentity(oid);
0322: assertTrue(animal.equals(newAnimal));
0323:
0324: Criteria crit = new Criteria();
0325: crit.addEqualTo("name", name);
0326: Query q = QueryFactory.newQuery(Animal.class, crit);
0327: Collection result = broker.getCollectionByQuery(q);
0328: assertNotNull(result);
0329: assertEquals(1, result.size());
0330: newAnimal = (Animal) result.iterator().next();
0331: assertTrue(animal.equals(newAnimal));
0332: }
0333:
0334: public void testInheritancedObjectsInCollectionReferences() {
0335: if (ojbSkipKnownIssueProblem("References of classes (1:1, 1:n) mapped to multiple joined tables only"
0336: + " return base class type instances")) {
0337: return;
0338: }
0339:
0340: long timestamp = System.currentTimeMillis();
0341: String name = "testInheritancedObjectsInCollectionReferences_"
0342: + timestamp;
0343: Company company = prepareTestDataWithCompany(name);
0344: Long id_2 = company.getId();
0345:
0346: broker.clearCache();
0347: Criteria crit = new Criteria();
0348: crit.addEqualTo("id", id_2);
0349: Query query = QueryFactory.newQuery(Company.class, crit);
0350: Collection result = broker.getCollectionByQuery(query);
0351: assertEquals(1, result.size());
0352: Company newCompany = (Company) result.iterator().next();
0353: List newEmployees = newCompany.getEmployees();
0354: assertNotNull(newEmployees);
0355: assertEquals(company.getEmployees().size(), newEmployees.size());
0356:
0357: List newExecutives = newCompany.getExecutives();
0358: assertNotNull(newExecutives);
0359: assertEquals(company.getExecutives().size(), newExecutives
0360: .size());
0361:
0362: int countEmployee = 0;
0363: int countExecutive = 0;
0364: int countManager = 0;
0365: for (int i = 0; i < newEmployees.size(); i++) {
0366: Object o = newEmployees.get(i);
0367: if (o instanceof Employee) {
0368: ++countEmployee;
0369: }
0370: if (o instanceof Executive) {
0371: ++countExecutive;
0372: }
0373: if (o instanceof Manager) {
0374: ++countManager;
0375: }
0376: }
0377: assertEquals(6, countEmployee);
0378: assertEquals(5, countExecutive);
0379: assertEquals(3, countManager);
0380: }
0381:
0382: public void testInheritedReferences() throws Exception {
0383: // TODO: fix this bug
0384: if (ojbSkipKnownIssueProblem("[OJB-84] Will be fixed in next version"))
0385: return;
0386:
0387: long timestamp = System.currentTimeMillis();
0388: Long id_2 = new Long(timestamp);
0389: String name = "testInheritedReferences_" + timestamp;
0390: Shareholder s1 = new Shareholder(id_2, name + "_shareholder");
0391: s1.setShare(23);
0392: Shareholder s2 = new Shareholder(id_2, name + "_shareholder");
0393: s2.setShare(24);
0394: List sh = new ArrayList();
0395: sh.add(s1);
0396: sh.add(s2);
0397: Consortium consortium = new Consortium();
0398: consortium.setName(name);
0399: consortium.setShareholders(sh);
0400:
0401: Identity oidCon;
0402: Identity oidSH;
0403: broker.beginTransaction();
0404: broker.store(consortium);
0405: broker.commitTransaction();
0406: oidCon = broker.serviceIdentity().buildIdentity(consortium);
0407: oidSH = broker.serviceIdentity().buildIdentity(s1);
0408: broker.clearCache();
0409: Consortium con = (Consortium) broker
0410: .getObjectByIdentity(oidCon);
0411: assertNotNull(con);
0412: assertNotNull(con.getShareholders());
0413: assertEquals(2, con.getShareholders().size());
0414:
0415: broker.clearCache();
0416: Shareholder s1_new = (Shareholder) broker
0417: .getObjectByIdentity(oidSH);
0418: assertNotNull(s1_new.getConsortiumKey());
0419:
0420: broker.clearCache();
0421: Criteria crit = new Criteria();
0422: crit.addEqualTo("name", consortium.getName());
0423: crit.addEqualTo("shareholders.share", new Integer(24));
0424: crit.addEqualTo("shareholders.name", name + "_shareholder");
0425: Query q = QueryFactory.newQuery(Consortium.class, crit);
0426: Collection result = broker.getCollectionByQuery(q);
0427: assertEquals(1, result.size());
0428: assertEquals(consortium, result.iterator().next());
0429: }
0430:
0431: public void testQuery() {
0432: long timestamp = System.currentTimeMillis();
0433: Long id_2 = new Long(timestamp);
0434: String name = "testQuery_" + timestamp;
0435: String s_name = name + "_Shareholder_3";
0436:
0437: Shareholder shareholder = new Shareholder(id_2, s_name);
0438: shareholder.setName(name);
0439: shareholder.setShare(77);
0440: shareholder.setDepartment("none");
0441: AddressIF ad = new Address(name);
0442: shareholder.setAddress(ad);
0443:
0444: broker.beginTransaction();
0445: broker.store(shareholder);
0446: broker.commitTransaction();
0447:
0448: Identity oid_shareholder = broker.serviceIdentity()
0449: .buildIdentity(shareholder);
0450: broker.clearCache();
0451:
0452: Shareholder new_shareholder = (Shareholder) broker
0453: .getObjectByIdentity(oid_shareholder);
0454: assertNotNull(new_shareholder);
0455: assertEquals(shareholder, new_shareholder);
0456:
0457: Criteria c = new Criteria();
0458: c.addEqualTo("name", shareholder.getName());
0459: c.addEqualTo("share", new Integer(shareholder.getShare()));
0460: c.addEqualTo("department", shareholder.getDepartment());
0461: c.addEqualTo("address.street", shareholder.getAddress()
0462: .getStreet());
0463: Query q = QueryFactory.newQuery(Shareholder.class, c);
0464: Collection result = broker.getCollectionByQuery(q);
0465: assertEquals(1, result.size());
0466: assertEquals(shareholder, result.iterator().next());
0467: }
0468:
0469: public void testStoreDelete_2() {
0470: long timestamp = System.currentTimeMillis();
0471: Long id_2 = new Long(timestamp);
0472: String name = "testStoreDelete_" + timestamp;
0473: String s_name = name + "_Shareholder_3";
0474:
0475: Shareholder shareholder = new Shareholder(id_2, s_name);
0476: shareholder.setShare(77);
0477: shareholder.setDepartment("none");
0478: AddressIF ad = new Address(name);
0479: shareholder.setAddress(ad);
0480:
0481: broker.beginTransaction();
0482: broker.store(shareholder);
0483: broker.commitTransaction();
0484:
0485: Identity oid_shareholder = broker.serviceIdentity()
0486: .buildIdentity(shareholder);
0487: broker.clearCache();
0488:
0489: Shareholder new_shareholder = (Shareholder) broker
0490: .getObjectByIdentity(oid_shareholder);
0491: assertNotNull(new_shareholder);
0492:
0493: assertEquals(s_name, new_shareholder.getName());
0494: assertNotNull(new_shareholder.getAddress());
0495: assertEquals(name, new_shareholder.getAddress().getStreet());
0496: assertEquals(77, new_shareholder.getShare());
0497:
0498: shareholder.getAddress().setStreet(name + "_updated");
0499: shareholder.setShare(1313);
0500: shareholder.setName(name + "_updated");
0501:
0502: // use serialized version of object
0503: shareholder = (Shareholder) SerializationUtils
0504: .clone(shareholder);
0505: broker.beginTransaction();
0506: broker.store(shareholder);
0507: broker.commitTransaction();
0508:
0509: oid_shareholder = broker.serviceIdentity().buildIdentity(
0510: shareholder);
0511: broker.clearCache();
0512:
0513: new_shareholder = (Shareholder) broker
0514: .getObjectByIdentity(oid_shareholder);
0515: assertNotNull(new_shareholder);
0516:
0517: assertEquals(1313, new_shareholder.getShare());
0518: assertEquals(name + "_updated", new_shareholder.getName());
0519: assertNotNull(new_shareholder.getAddress());
0520: assertEquals(name + "_updated", new_shareholder.getAddress()
0521: .getStreet());
0522:
0523: broker.beginTransaction();
0524: broker.delete(shareholder);
0525: broker.commitTransaction();
0526:
0527: new_shareholder = (Shareholder) broker
0528: .getObjectByIdentity(oid_shareholder);
0529: assertNull(new_shareholder);
0530: }
0531:
0532: public void testStoreDelete() {
0533:
0534: long timestamp = System.currentTimeMillis();
0535: Long id_2 = new Long(timestamp);
0536: String name = "testInheritancedObjectsInCollectionReferences_"
0537: + timestamp;
0538: String m_name = name + "_manager_3";
0539:
0540: Manager m = new Manager(id_2, m_name);
0541: m.setDepartment("none");
0542: AddressIF ad = new Address(name);
0543: m.setAddress(ad);
0544:
0545: String ex_name = name + "_executive";
0546: Executive ex = new Executive(id_2, ex_name, "department_1",
0547: null);
0548:
0549: String em_name = name + "_employee";
0550: Employee em = new Employee(id_2, em_name);
0551:
0552: broker.beginTransaction();
0553: broker.store(em);
0554: broker.store(ex);
0555: broker.store(m);
0556: broker.commitTransaction();
0557:
0558: Identity oid_em = broker.serviceIdentity().buildIdentity(em);
0559: Identity oid_ex = broker.serviceIdentity().buildIdentity(ex);
0560: Identity oid_m = broker.serviceIdentity().buildIdentity(m);
0561: broker.clearCache();
0562:
0563: Employee new_em = (Employee) broker.getObjectByIdentity(oid_em);
0564: Executive new_ex = (Executive) broker
0565: .getObjectByIdentity(oid_ex);
0566: Manager new_m = (Manager) broker.getObjectByIdentity(oid_m);
0567:
0568: assertNotNull(new_em);
0569: assertNotNull(new_ex);
0570: assertNotNull(new_m);
0571:
0572: assertEquals(em_name, new_em.getName());
0573: assertEquals(ex_name, new_ex.getName());
0574: assertEquals(m_name, new_m.getName());
0575: assertNotNull(new_m.getAddress());
0576: assertEquals(name, new_m.getAddress().getStreet());
0577:
0578: broker.beginTransaction();
0579: broker.delete(m);
0580: broker.delete(ex);
0581: broker.delete(em);
0582: broker.commitTransaction();
0583:
0584: new_em = (Employee) broker.getObjectByIdentity(oid_em);
0585: new_ex = (Executive) broker.getObjectByIdentity(oid_ex);
0586: new_m = (Manager) broker.getObjectByIdentity(oid_m);
0587:
0588: assertNull(new_em);
0589: assertNull(new_ex);
0590: assertNull(new_m);
0591: }
0592:
0593: public void testStoreUpdateQuerySerialized_2() {
0594: long timestamp = System.currentTimeMillis();
0595: Long id_2 = new Long(timestamp);
0596: String name = "testStoreUpdateQuerySerialized_" + timestamp;
0597: Manager m_1 = new Manager(id_2, name + "_manager_1");
0598: Manager m_2 = new Manager(id_2, name + "_manager_2");
0599: Manager m_3 = new Manager(id_2, name + "_manager_3");
0600: m_3.setDepartment("none");
0601:
0602: Executive ex_1 = new Executive(id_2, name + "_executive",
0603: "department_1", null);
0604: Executive ex_2 = new Executive(id_2, name + "_executive",
0605: "department_1", null);
0606:
0607: Employee em = new Employee(id_2, name + "_employee");
0608:
0609: broker.beginTransaction();
0610: broker.store(em);
0611: broker.store(m_1);
0612: broker.store(m_3);
0613: broker.store(ex_1);
0614: broker.store(m_2);
0615: broker.store(ex_2);
0616: broker.commitTransaction();
0617:
0618: broker.clearCache();
0619: Criteria crit = new Criteria();
0620: crit.addLike("name", name + "%");
0621: crit.addLike("department", "none");
0622: Query query = QueryFactory.newQuery(Manager.class, crit);
0623: Collection result = broker.getCollectionByQuery(query);
0624: assertEquals(1, result.size());
0625:
0626: crit = new Criteria();
0627: crit.addLike("name", name + "%");
0628: query = QueryFactory.newQuery(Employee.class, crit);
0629: result = broker.getCollectionByQuery(query);
0630: assertEquals(6, result.size());
0631:
0632: crit = new Criteria();
0633: crit.addLike("name", name + "%");
0634: query = QueryFactory.newQuery(Executive.class, crit);
0635: result = broker.getCollectionByQuery(query);
0636: assertEquals(5, result.size());
0637:
0638: crit = new Criteria();
0639: crit.addLike("name", name + "%");
0640: query = QueryFactory.newQuery(Manager.class, crit);
0641: result = broker.getCollectionByQuery(query);
0642: assertEquals(3, result.size());
0643:
0644: em = (Employee) SerializationUtils.clone(em);
0645: m_1 = (Manager) SerializationUtils.clone(m_1);
0646: m_2 = (Manager) SerializationUtils.clone(m_2);
0647: m_3 = (Manager) SerializationUtils.clone(m_3);
0648: ex_1 = (Executive) SerializationUtils.clone(ex_1);
0649: ex_2 = (Executive) SerializationUtils.clone(ex_2);
0650:
0651: em.setName(em.getName() + "_updated");
0652: m_1.setName(m_1.getName() + "_updated");
0653: m_1.setDepartment("_updated_Dep");
0654: m_2.setName(m_2.getName() + "_updated");
0655: m_3.setName(m_3.getName() + "_updated");
0656: ex_1.setName(ex_1.getName() + "_updated");
0657: ex_2.setName(ex_2.getName() + "_updated");
0658:
0659: broker.clearCache();
0660: broker.beginTransaction();
0661: //========================================
0662: // update fields
0663: broker.store(em, ObjectModification.UPDATE);
0664: broker.store(m_1, ObjectModification.UPDATE);
0665: broker.store(m_3, ObjectModification.UPDATE);
0666: broker.store(ex_1, ObjectModification.UPDATE);
0667: broker.store(m_2, ObjectModification.UPDATE);
0668: broker.store(ex_2, ObjectModification.UPDATE);
0669: //========================================
0670: broker.commitTransaction();
0671:
0672: /*
0673: after de/serialization and update we expect the same row count in
0674: each table
0675: */
0676: broker.clearCache();
0677:
0678: crit = new Criteria();
0679: crit.addLike("name", name + "%");
0680: crit.addLike("department", "_updated_Dep");
0681: query = QueryFactory.newQuery(Manager.class, crit);
0682: result = broker.getCollectionByQuery(query);
0683: assertEquals(
0684: "Expect the same number of objects as before update",
0685: 1, result.size());
0686: Manager newMan = (Manager) result.iterator().next();
0687: assertEquals(m_1.getName(), newMan.getName());
0688: assertEquals(m_1.getDepartment(), newMan.getDepartment());
0689:
0690: crit = new Criteria();
0691: crit.addLike("name", name + "%");
0692: crit.addLike("department", "none");
0693: query = QueryFactory.newQuery(Manager.class, crit);
0694: result = broker.getCollectionByQuery(query);
0695: assertEquals(
0696: "Expect the same number of objects as before update",
0697: 1, result.size());
0698:
0699: crit = new Criteria();
0700: crit.addLike("name", name + "%");
0701: query = QueryFactory.newQuery(Employee.class, crit);
0702: result = broker.getCollectionByQuery(query);
0703: assertEquals(
0704: "Expect the same number of objects as before update",
0705: 6, result.size());
0706:
0707: crit = new Criteria();
0708: crit.addLike("name", name + "%");
0709: query = QueryFactory.newQuery(Executive.class, crit);
0710: result = broker.getCollectionByQuery(query);
0711: assertEquals(
0712: "Expect the same number of objects as before update",
0713: 5, result.size());
0714:
0715: crit = new Criteria();
0716: crit.addLike("name", name + "%");
0717: query = QueryFactory.newQuery(Manager.class, crit);
0718: result = broker.getCollectionByQuery(query);
0719: assertEquals(
0720: "Expect the same number of objects as before update",
0721: 3, result.size());
0722: }
0723:
0724: public void testObjectExistence() {
0725: Manager target_1 = new Manager(new Long(1),
0726: "testObjectExistence");
0727: Manager target_2 = new Manager(new Long(System
0728: .currentTimeMillis()), "testObjectExistence");
0729:
0730: Identity oid_1 = broker.serviceIdentity().buildIdentity(
0731: target_1);
0732: Identity oid_2 = broker.serviceIdentity().buildIdentity(
0733: target_2);
0734:
0735: ClassDescriptor cld = broker.getClassDescriptor(Manager.class);
0736:
0737: boolean b_1 = broker.serviceBrokerHelper().doesExist(cld,
0738: oid_1, target_1);
0739: boolean b_2 = broker.serviceBrokerHelper().doesExist(cld,
0740: oid_2, target_2);
0741: assertFalse(b_1);
0742: assertFalse(b_2);
0743: }
0744:
0745: public void testStoreUpdateQuerySerialized() {
0746: long timestamp = System.currentTimeMillis();
0747: Long id_2 = new Long(timestamp);
0748: String name = "testStoreUpdateQuerySerialized_" + timestamp;
0749: Manager m_1 = new Manager(id_2, name + "_manager_1");
0750: Manager m_2 = new Manager(id_2, name + "_manager_2");
0751: Manager m_3 = new Manager(id_2, name + "_manager_3");
0752: m_3.setDepartment("none");
0753:
0754: Executive ex_1 = new Executive(id_2, name + "_executive",
0755: "department_1", null);
0756: Executive ex_2 = new Executive(id_2, name + "_executive",
0757: "department_1", null);
0758:
0759: Employee em = new Employee(id_2, name + "_employee");
0760:
0761: broker.beginTransaction();
0762: broker.store(em);
0763: broker.store(m_1);
0764: broker.store(m_3);
0765: broker.store(ex_1);
0766: broker.store(m_2);
0767: broker.store(ex_2);
0768: broker.commitTransaction();
0769:
0770: broker.clearCache();
0771: Criteria crit = new Criteria();
0772: crit.addLike("name", name + "%");
0773: crit.addLike("department", "none");
0774: Query query = QueryFactory.newQuery(Manager.class, crit);
0775: Collection result = broker.getCollectionByQuery(query);
0776: assertEquals(1, result.size());
0777:
0778: crit = new Criteria();
0779: crit.addLike("name", name + "%");
0780: query = QueryFactory.newQuery(Employee.class, crit);
0781: result = broker.getCollectionByQuery(query);
0782: assertEquals(6, result.size());
0783:
0784: crit = new Criteria();
0785: crit.addLike("name", name + "%");
0786: query = QueryFactory.newQuery(Executive.class, crit);
0787: result = broker.getCollectionByQuery(query);
0788: assertEquals(5, result.size());
0789:
0790: crit = new Criteria();
0791: crit.addLike("name", name + "%");
0792: query = QueryFactory.newQuery(Manager.class, crit);
0793: result = broker.getCollectionByQuery(query);
0794: assertEquals(3, result.size());
0795:
0796: em = (Employee) SerializationUtils.clone(em);
0797: m_1 = (Manager) SerializationUtils.clone(m_1);
0798: m_2 = (Manager) SerializationUtils.clone(m_2);
0799: m_3 = (Manager) SerializationUtils.clone(m_3);
0800: ex_1 = (Executive) SerializationUtils.clone(ex_1);
0801: ex_2 = (Executive) SerializationUtils.clone(ex_2);
0802:
0803: em.setName(em.getName() + "_updated");
0804: m_1.setName(m_1.getName() + "_updated");
0805: m_2.setName(m_2.getName() + "_updated");
0806: m_3.setName(m_3.getName() + "_updated");
0807: ex_1.setName(ex_1.getName() + "_updated");
0808: ex_2.setName(ex_2.getName() + "_updated");
0809:
0810: broker.clearCache();
0811: broker.beginTransaction();
0812: broker.store(em);
0813: broker.store(m_1);
0814: broker.store(m_3);
0815: broker.store(ex_1);
0816: broker.store(m_2);
0817: broker.store(ex_2);
0818: broker.commitTransaction();
0819:
0820: /*
0821: after de/serialization and update we expect the same row count in
0822: each table
0823: */
0824: broker.clearCache();
0825: crit = new Criteria();
0826: crit.addLike("name", name + "%");
0827: crit.addLike("department", "none");
0828: query = QueryFactory.newQuery(Manager.class, crit);
0829: result = broker.getCollectionByQuery(query);
0830: assertEquals(
0831: "Expect the same number of objects as before update",
0832: 1, result.size());
0833:
0834: crit = new Criteria();
0835: crit.addLike("name", name + "%");
0836: query = QueryFactory.newQuery(Employee.class, crit);
0837: result = broker.getCollectionByQuery(query);
0838: assertEquals(
0839: "Expect the same number of objects as before update",
0840: 6, result.size());
0841:
0842: crit = new Criteria();
0843: crit.addLike("name", name + "%");
0844: query = QueryFactory.newQuery(Executive.class, crit);
0845: result = broker.getCollectionByQuery(query);
0846: assertEquals(
0847: "Expect the same number of objects as before update",
0848: 5, result.size());
0849:
0850: crit = new Criteria();
0851: crit.addLike("name", name + "%");
0852: query = QueryFactory.newQuery(Manager.class, crit);
0853: result = broker.getCollectionByQuery(query);
0854: assertEquals(
0855: "Expect the same number of objects as before update",
0856: 3, result.size());
0857: }
0858:
0859: private List prepareForQueryTests(Long id_2, String name) {
0860: List result = new ArrayList();
0861:
0862: Manager m_1 = new Manager(id_2, name + "_manager_1");
0863: Manager m_2 = new Manager(id_2, name + "_manager_2");
0864: Manager m_3 = new Manager(id_2, name + "_manager_3");
0865: m_3.setDepartment("none");
0866: Address a_1 = new Address("snob allee");
0867: m_1.setAddress(a_1);
0868:
0869: Executive ex_1 = new Executive(id_2, name + "_executive",
0870: "department_1", null);
0871: Executive ex_2 = new Executive(id_2, name + "_executive",
0872: "department_1", null);
0873:
0874: Employee em = new Employee(id_2, name + "_employee");
0875: Address a_2 = new Address("cockroaches valley");
0876: em.setAddress(a_2);
0877:
0878: result.add(m_1);
0879: result.add(m_2);
0880: result.add(m_3);
0881: result.add(ex_1);
0882: result.add(ex_2);
0883: result.add(em);
0884:
0885: broker.beginTransaction();
0886: broker.store(m_1);
0887: broker.store(m_2);
0888: broker.store(m_3);
0889: broker.store(ex_1);
0890: broker.store(ex_2);
0891: broker.store(em);
0892: broker.commitTransaction();
0893:
0894: return result;
0895: }
0896:
0897: private Company prepareTestDataWithCompany(String name) {
0898: Long id_2 = null;
0899: Manager m_1 = new Manager(id_2, name + "_manager_1");
0900: Manager m_2 = new Manager(id_2, name + "_manager_2");
0901: Manager m_3 = new Manager(id_2, name + "_manager_3");
0902: m_3.setDepartment("none");
0903: Executive ex_1 = new Executive(id_2, name + "_executive",
0904: "department_1", null);
0905: Executive ex_2 = new Executive(id_2, name + "_executive",
0906: "department_1", null);
0907: Employee em = new Employee(id_2, name + "_employee");
0908:
0909: ArrayList employees = new ArrayList();
0910: employees.add(m_1);
0911: employees.add(m_2);
0912: employees.add(m_3);
0913: employees.add(ex_1);
0914: employees.add(ex_2);
0915: employees.add(em);
0916:
0917: ArrayList executives = new ArrayList();
0918: executives.add(m_1);
0919: executives.add(m_2);
0920: executives.add(m_3);
0921: executives.add(ex_1);
0922: executives.add(ex_2);
0923:
0924: Company company = new Company(null, name, employees, executives);
0925: broker.beginTransaction();
0926: broker.store(company);
0927: broker.commitTransaction();
0928: return company;
0929: }
0930:
0931: public void testQuery_InheritedObjects() {
0932: long timestamp = System.currentTimeMillis();
0933: Long id_2 = new Long(timestamp);
0934: String name = "testQuery_InheritedObjects" + timestamp;
0935: prepareForQueryTests(id_2, name);
0936: broker.clearCache();
0937:
0938: Criteria crit = new Criteria();
0939: crit.addLike("name", name + "%");
0940: Query query = QueryFactory.newQuery(Employee.class, crit);
0941: Collection result = broker.getCollectionByQuery(query);
0942: assertEquals(6, result.size());
0943: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0944: Employee obj = (Employee) iterator.next();
0945: assertNotNull(obj.getName());
0946: }
0947:
0948: broker.clearCache();
0949: crit = new Criteria();
0950: crit.addLike("name", name + "%");
0951: query = QueryFactory.newQuery(Executive.class, crit);
0952: result = broker.getCollectionByQuery(query);
0953: assertEquals(5, result.size());
0954: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0955: Executive obj = (Executive) iterator.next();
0956: assertNotNull(obj.getName());
0957: }
0958:
0959: broker.clearCache();
0960: crit = new Criteria();
0961: crit.addLike("name", name + "%");
0962: query = QueryFactory.newQuery(Manager.class, crit);
0963: result = broker.getCollectionByQuery(query);
0964: assertEquals(3, result.size());
0965: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0966: Manager obj = (Manager) iterator.next();
0967: assertNotNull(obj.getName());
0968: }
0969: }
0970:
0971: public void testQuery_InheritedField() {
0972: long timestamp = System.currentTimeMillis();
0973: Long id_2 = new Long(timestamp);
0974: String name = "testQuery_InheritedField" + timestamp;
0975: prepareForQueryTests(id_2, name);
0976: broker.clearCache();
0977:
0978: broker.clearCache();
0979: Criteria crit = new Criteria();
0980: crit.addLike("name", name + "%");
0981: crit.addLike("department", "none");
0982: Query query = QueryFactory.newQuery(Manager.class, crit);
0983: Collection result = broker.getCollectionByQuery(query);
0984: assertEquals(1, result.size());
0985: }
0986:
0987: public void testQuery_Reference() {
0988: long timestamp = System.currentTimeMillis();
0989: Long id_2 = new Long(timestamp);
0990: String name = "testQuery_Reference" + timestamp;
0991: prepareForQueryTests(id_2, name);
0992: broker.clearCache();
0993:
0994: Criteria crit = new Criteria();
0995: crit.addLike("name", name + "%");
0996: crit.addLike("address.street", "%valley");
0997: Query query = QueryFactory.newQuery(Employee.class, crit);
0998: Collection result = broker.getCollectionByQuery(query);
0999: assertEquals(1, result.size());
1000: Employee emp = (Employee) result.iterator().next();
1001: assertNotNull(emp.getAddress());
1002: assertEquals("cockroaches valley", emp.getAddress().getStreet());
1003: }
1004:
1005: public void testQuery_InheritedReference_1() {
1006: long timestamp = System.currentTimeMillis();
1007: Long id_2 = new Long(timestamp);
1008: String name = "testQuery_InheritedReference_1" + timestamp;
1009: prepareForQueryTests(id_2, name);
1010: broker.clearCache();
1011:
1012: Criteria crit = new Criteria();
1013: crit.addLike("name", name + "%");
1014: crit.addEqualTo("address.street", "snob allee");
1015: Query query = QueryFactory.newQuery(Manager.class, crit);
1016: Collection result = broker.getCollectionByQuery(query);
1017: assertEquals(1, result.size());
1018: Manager retManager = (Manager) result.iterator().next();
1019: assertNotNull(retManager);
1020: assertEquals(name + "_manager_1", retManager.getName());
1021: assertNotNull(retManager.getAddress());
1022: assertEquals("snob allee", retManager.getAddress().getStreet());
1023: }
1024:
1025: public void testQuery_InheritedReference_2() {
1026: long timestamp = System.currentTimeMillis();
1027: Long id_2 = new Long(timestamp);
1028: String name = "testQuery_InheritedReference_2" + timestamp;
1029: prepareForQueryTests(id_2, name);
1030: broker.clearCache();
1031:
1032: Criteria crit = new Criteria();
1033: crit.addLike("name", name + "%");
1034: crit.addEqualTo("address.street", "snob allee");
1035: Query query = QueryFactory.newQuery(Executive.class, crit);
1036: Collection result = broker.getCollectionByQuery(query);
1037: assertEquals(1, result.size());
1038:
1039: Executive retManager = (Executive) result.iterator().next();
1040: assertNotNull(retManager);
1041: assertEquals(name + "_manager_1", retManager.getName());
1042: }
1043:
1044: public void testQuery_InheritedReference_3() {
1045: long timestamp = System.currentTimeMillis();
1046: Long id_2 = new Long(timestamp);
1047: String name = "testQuery_InheritedReference_3" + timestamp;
1048: prepareForQueryTests(id_2, name);
1049: broker.clearCache();
1050:
1051: Criteria crit = new Criteria();
1052: crit.addLike("name", name + "%");
1053: crit.addEqualTo("address.street", "snob allee");
1054: Query query = QueryFactory.newQuery(Employee.class, crit);
1055: Collection result = broker.getCollectionByQuery(query);
1056: assertEquals(1, result.size());
1057: }
1058:
1059: public void testQuery_ReferenceOuterJoin() {
1060: long timestamp = System.currentTimeMillis();
1061: String name = "testQuery_ReferenceOuterJoin_" + timestamp;
1062: prepareTestDataWithCompany(name);
1063: //Long id_2 = company.getId();
1064:
1065: // Store a dummy company
1066: Company dummyComp = new Company(null, name + "_dummy",
1067: Collections.EMPTY_LIST, Collections.EMPTY_LIST);
1068: broker.beginTransaction();
1069: broker.store(dummyComp);
1070: broker.commitTransaction();
1071:
1072: broker.clearCache();
1073:
1074: Criteria crit = new Criteria();
1075: crit.addLike("name", name + "%");
1076: QueryByCriteria query = QueryFactory.newQuery(Company.class,
1077: crit, true);
1078: Collection result = broker.getCollectionByQuery(query);
1079: // retrieve both companies
1080: assertEquals(2, result.size());
1081:
1082: crit = new Criteria();
1083: crit.addLike("name", name + "%");
1084:
1085: Criteria nameCrit1 = new Criteria();
1086: nameCrit1.addLike("executives.name", name + "%");
1087: Criteria nameCrit2 = new Criteria();
1088: nameCrit2.addIsNull("executives.name");
1089: nameCrit1.addOrCriteria(nameCrit2);
1090: crit.addAndCriteria(nameCrit1);
1091:
1092: query = QueryFactory.newQuery(Company.class, crit, true);
1093: query.addOrderByAscending("id");
1094: query.setPathOuterJoin("executives");
1095: result = broker.getCollectionByQuery(query);
1096: // should retrieve both companies
1097: assertEquals(2, result.size());
1098: }
1099:
1100: public void testInsertQuery() {
1101: long timestamp = System.currentTimeMillis();
1102: Long id_2 = new Long(timestamp);
1103: String name = "testInsert" + timestamp;
1104: Employee em1 = new Employee(id_2, name);
1105: Executive ex1 = new Executive(id_2, name, "department_1", null);
1106: Executive ex2 = new Executive(id_2, name, "department_2", null);
1107: ArrayList list = new ArrayList();
1108: list.add(ex1);
1109: list.add(ex2);
1110: Manager m1 = new Manager(id_2, name);
1111: m1.setExecutives(list);
1112:
1113: broker.beginTransaction();
1114: broker.store(em1);
1115: broker.store(m1);
1116: broker.commitTransaction();
1117:
1118: Identity m1_oid = broker.serviceIdentity().buildIdentity(m1);
1119: Identity ex1_oid = broker.serviceIdentity().buildIdentity(ex1);
1120: Identity em1_oid = broker.serviceIdentity().buildIdentity(em1);
1121:
1122: broker.clearCache();
1123:
1124: Employee newEm1 = (Employee) broker
1125: .getObjectByIdentity(em1_oid);
1126: Executive newEx1 = (Executive) broker
1127: .getObjectByIdentity(ex1_oid);
1128: Manager newM1 = (Manager) broker.getObjectByIdentity(m1_oid);
1129:
1130: assertEquals(em1, newEm1);
1131: assertEquals(ex1, newEx1);
1132: assertEquals(m1, newM1);
1133: assertEquals(name, newEx1.getName());
1134: assertEquals(name, newM1.getName());
1135:
1136: assertEquals(2, newM1.getExecutives().size());
1137:
1138: Criteria crit = new Criteria();
1139: crit.addEqualTo("name", name);
1140: Query queryEmployee = QueryFactory.newQuery(Employee.class,
1141: crit);
1142: Query queryExecutive = QueryFactory.newQuery(Executive.class,
1143: crit);
1144: Query queryManager = QueryFactory.newQuery(Manager.class, crit);
1145:
1146: Collection result = broker.getCollectionByQuery(queryEmployee);
1147: assertEquals(4, result.size());
1148:
1149: result = broker.getCollectionByQuery(queryExecutive);
1150: assertEquals(3, result.size());
1151:
1152: result = broker.getCollectionByQuery(queryManager);
1153: assertEquals(1, result.size());
1154: }
1155:
1156: public void testUpdate() {
1157: long timestamp = System.currentTimeMillis();
1158: Long id_2 = new Long(timestamp);
1159: String name = "testUpdate_" + timestamp;
1160: Employee em1 = new Employee(id_2, "employee_" + name);
1161: Executive ex1 = new Executive(id_2, "executive_" + name,
1162: "department_1", null);
1163: Executive ex2 = new Executive(id_2, "executive_" + name,
1164: "department_2", null);
1165: ArrayList list = new ArrayList();
1166: list.add(ex1);
1167: list.add(ex2);
1168: Manager m1 = new Manager(id_2, "manager_" + name);
1169: m1.setExecutives(list);
1170:
1171: broker.beginTransaction();
1172: broker.store(em1);
1173: broker.store(m1);
1174: broker.commitTransaction();
1175:
1176: Identity m1_oid = broker.serviceIdentity().buildIdentity(m1);
1177: Identity ex1_oid = broker.serviceIdentity().buildIdentity(ex1);
1178: Identity em1_oid = broker.serviceIdentity().buildIdentity(em1);
1179:
1180: broker.clearCache();
1181:
1182: Employee newEm1 = (Employee) broker
1183: .getObjectByIdentity(em1_oid);
1184: Executive newEx1 = (Executive) broker
1185: .getObjectByIdentity(ex1_oid);
1186: Manager newM1 = (Manager) broker.getObjectByIdentity(m1_oid);
1187:
1188: assertEquals(2, newM1.getExecutives().size());
1189:
1190: newEm1.setName("**updated_" + name);
1191: newM1.setName("**updated_" + name);
1192: ((Executive) newM1.getExecutives().get(0)).setName("**updated_"
1193: + name);
1194:
1195: broker.beginTransaction();
1196: broker.store(newEm1);
1197: broker.store(newM1);
1198: broker.store(newEx1);
1199: broker.commitTransaction();
1200:
1201: broker.clearCache();
1202:
1203: em1 = (Employee) broker.getObjectByIdentity(em1_oid);
1204: ex1 = (Executive) broker.getObjectByIdentity(ex1_oid);
1205: m1 = (Manager) broker.getObjectByIdentity(m1_oid);
1206:
1207: assertEquals(newEm1, em1);
1208: assertEquals(newEx1, ex1);
1209: assertEquals(newM1, m1);
1210:
1211: assertEquals(2, m1.getExecutives().size());
1212: }
1213:
1214: public void testDelete() {
1215: long timestamp = System.currentTimeMillis();
1216: Long id_2 = new Long(timestamp);
1217: String name = "testDelete_" + timestamp;
1218: Employee em1 = new Employee(id_2, "employee_" + name);
1219: Executive ex1 = new Executive(id_2, "executive_" + name,
1220: "department_1", null);
1221: Executive ex2 = new Executive(id_2, "executive_" + name,
1222: "department_2", null);
1223: ArrayList list = new ArrayList();
1224: list.add(ex1);
1225: list.add(ex2);
1226: Manager m1 = new Manager(id_2, "manager_" + name);
1227: m1.setExecutives(list);
1228:
1229: broker.beginTransaction();
1230: broker.store(em1);
1231: broker.store(m1);
1232: broker.commitTransaction();
1233:
1234: Identity m1_oid = broker.serviceIdentity().buildIdentity(m1);
1235: Identity ex1_oid = broker.serviceIdentity().buildIdentity(ex1);
1236: Identity em1_oid = broker.serviceIdentity().buildIdentity(em1);
1237:
1238: broker.clearCache();
1239:
1240: Employee newEm1 = (Employee) broker
1241: .getObjectByIdentity(em1_oid);
1242: Executive newEx1 = (Executive) broker
1243: .getObjectByIdentity(ex1_oid);
1244: Manager newM1 = (Manager) broker.getObjectByIdentity(m1_oid);
1245:
1246: assertNotNull(newEm1);
1247: assertNotNull(newEx1);
1248: assertNotNull(newM1);
1249: assertEquals(2, newM1.getExecutives().size());
1250:
1251: broker.beginTransaction();
1252: broker.delete(newEm1);
1253: broker.delete(newEx1);
1254: broker.delete(newM1);
1255: broker.commitTransaction();
1256:
1257: newEm1 = (Employee) broker.getObjectByIdentity(em1_oid);
1258: newEx1 = (Executive) broker.getObjectByIdentity(ex1_oid);
1259: newM1 = (Manager) broker.getObjectByIdentity(m1_oid);
1260:
1261: assertNull(newEm1);
1262: assertNull(newEx1);
1263: assertNull(newM1);
1264: }
1265:
1266: /**
1267: * Check backward compatibility with 'old' super-reference handling using explicite
1268: * anonymous field for FK to super class/table.
1269: */
1270: public void testInheritanceViaAnonymousField() {
1271: ObjectRepository.G obj_1 = new ObjectRepository.G();
1272: obj_1.setSomeValue(1);
1273: obj_1.setSomeSuperValue(2);
1274: obj_1.setSomeSubValue(3);
1275:
1276: broker.beginTransaction();
1277: broker.store(obj_1);
1278: broker.commitTransaction();
1279:
1280: Identity oid = broker.serviceIdentity().buildIdentity(obj_1);
1281: broker.clearCache();
1282:
1283: ObjectRepository.G obj_2 = (ObjectRepository.G) broker
1284: .getObjectByIdentity(oid);
1285: assertEquals(obj_1.getId(), obj_2.getId());
1286: assertEquals(obj_1.getSomeSubValue(), obj_2.getSomeSubValue());
1287: assertEquals(obj_1.getSomeSuperValue(), obj_2
1288: .getSomeSuperValue());
1289: assertEquals(obj_1.getSomeValue(), obj_2.getSomeValue());
1290:
1291: broker.beginTransaction();
1292: obj_1.setSomeValue(11);
1293: obj_1.setSomeSuperValue(22);
1294: obj_1.setSomeSubValue(33);
1295: broker.store(obj_1);
1296: broker.commitTransaction();
1297:
1298: broker.clearCache();
1299:
1300: ObjectRepository.G obj_3 = (ObjectRepository.G) broker
1301: .getObjectByIdentity(oid);
1302: assertEquals(obj_1.getId(), obj_3.getId());
1303: assertEquals(obj_1.getSomeSubValue(), obj_3.getSomeSubValue());
1304: assertEquals(obj_1.getSomeSuperValue(), obj_3
1305: .getSomeSuperValue());
1306: assertEquals(obj_1.getSomeValue(), obj_3.getSomeValue());
1307:
1308: assertEquals(obj_2.getId(), obj_3.getId());
1309: assertFalse(obj_2.getSomeSubValue() == obj_3.getSomeSubValue());
1310: assertFalse(obj_2.getSomeSuperValue() == obj_3
1311: .getSomeSuperValue());
1312: assertFalse(obj_2.getSomeValue() == obj_3.getSomeValue());
1313: }
1314:
1315: // /**
1316: // * TODO: Should we support some kind of "declarative inheritance"? This test
1317: // * try to use this kind of inheritance as class Dog expects some fields from a
1318: // * "declarated super class" (no java inheritance, only declared in metadata).
1319: // * In class {@link org.apache.ojb.broker.metadata.SuperReferenceDescriptor} the
1320: // * support is comment out (but only works for simple objects without references).
1321: // */
1322: // public void YYYtestWithoutJavaInheritance_1()
1323: // {
1324: // if (ojbSkipKnownIssueProblem("Declarative Inheritance not supported"))
1325: // {
1326: // return;
1327: // }
1328: //
1329: // String name = "testWithoutJavaInheritance_1" + System.currentTimeMillis();
1330: // Dog dog = new Dog(name, 35, 4);
1331: // broker.beginTransaction();
1332: // broker.store(dog);
1333: // broker.commitTransaction();
1334: //
1335: // broker.clearCache();
1336: // Criteria crit = new Criteria();
1337: // crit.addEqualTo("name", name);
1338: // Query q = QueryFactory.newQuery(Dog.class, crit);
1339: // Collection result = broker.getCollectionByQuery(q);
1340: // assertNotNull(result);
1341: // assertEquals(1, result.size());
1342: // Dog newDog = (Dog) result.iterator().next();
1343: // assertTrue(dog.equals(newDog));
1344: //
1345: // broker.beginTransaction();
1346: // newDog.setWeight(1000);
1347: // newDog.setLegs(10);
1348: // broker.store(newDog);
1349: // broker.commitTransaction();
1350: //
1351: // broker.clearCache();
1352: // result = broker.getCollectionByQuery(q);
1353: // assertNotNull(result);
1354: // assertEquals(1, result.size());
1355: // Dog newDog2 = (Dog) result.iterator().next();
1356: // assertTrue(newDog.equals(newDog2));
1357: //
1358: // broker.beginTransaction();
1359: // broker.delete(dog);
1360: // broker.commitTransaction();
1361: // }
1362: //
1363: // /**
1364: // * TODO: Should we support some kind of "declarative inheritance"? This test
1365: // * try to use this kind of inheritance as class Dog expects some fields from a
1366: // * "declarated super class" (no java inheritance, only declared in metadata).
1367: // * In class {@link org.apache.ojb.broker.metadata.SuperReferenceDescriptor} the
1368: // * support is comment out (but only works for simple objects without references).
1369: // */
1370: // public void YYYtestWithoutJavaInheritance_2()
1371: // {
1372: // if (ojbSkipKnownIssueProblem("Declarative Inheritance not supported"))
1373: // {
1374: // return;
1375: // }
1376: //
1377: // String name = "testWithoutJavaInheritance_2" + System.currentTimeMillis();
1378: // Dog dog = new Dog(name, 35, 4);
1379: // Animal parent = new Animal(name + "_parent", 55);
1380: // Food f1 = new Food(name + "fruit1");
1381: // Food f2 = new Food(name + "fruit2");
1382: // dog.addFood(f1);
1383: // dog.addFood(f2);
1384: // dog.setParent(parent);
1385: //
1386: // broker.beginTransaction();
1387: // broker.store(dog);
1388: // broker.commitTransaction();
1389: //
1390: // broker.clearCache();
1391: // Criteria crit = new Criteria();
1392: // crit.addEqualTo("name", name);
1393: // Query q = QueryFactory.newQuery(Dog.class, crit);
1394: // Collection result = broker.getCollectionByQuery(q);
1395: // assertNotNull(result);
1396: // assertEquals(1, result.size());
1397: // Dog newDog = (Dog) result.iterator().next();
1398: // assertEquals(dog, newDog);
1399: //
1400: // broker.beginTransaction();
1401: // newDog.setWeight(1000);
1402: // newDog.setLegs(10);
1403: // newDog.addFood(new Food(name + "_new"));
1404: // broker.store(newDog);
1405: // broker.commitTransaction();
1406: //
1407: // broker.clearCache();
1408: // result = broker.getCollectionByQuery(q);
1409: // assertNotNull(result);
1410: // assertEquals(1, result.size());
1411: // Dog newDog2 = (Dog) result.iterator().next();
1412: // assertTrue(newDog.equals(newDog2));
1413: //
1414: // broker.beginTransaction();
1415: // broker.delete(dog);
1416: // broker.commitTransaction();
1417: // }
1418:
1419: //************************************************************
1420: // inner classes used for test
1421: //************************************************************
1422: public static class Shareholder extends Manager {
1423: private int share;
1424:
1425: public Shareholder() {
1426: }
1427:
1428: public Shareholder(Long id_2, String name) {
1429: super (id_2, name);
1430: }
1431:
1432: public int getShare() {
1433: return share;
1434: }
1435:
1436: public void setShare(int share) {
1437: this .share = share;
1438: }
1439:
1440: public boolean equals(Object obj) {
1441: if (!(obj instanceof Shareholder)) {
1442: return false;
1443: }
1444: Shareholder s = (Shareholder) obj;
1445: return new EqualsBuilder().append(getShare(), s.getShare())
1446: .isEquals()
1447: && super .equals(obj);
1448: }
1449: }
1450:
1451: public static class Manager extends Executive {
1452: private List executives;
1453: private Integer consortiumKey;
1454:
1455: public Manager() {
1456: }
1457:
1458: public Manager(Long id_2, String name) {
1459: super (id_2, name, null, null);
1460: }
1461:
1462: public List getExecutives() {
1463: return executives;
1464: }
1465:
1466: public void setExecutives(List executives) {
1467: this .executives = executives;
1468: }
1469:
1470: public Integer getConsortiumKey() {
1471: return consortiumKey;
1472: }
1473:
1474: public void setConsortiumKey(Integer consortiumKey) {
1475: this .consortiumKey = consortiumKey;
1476: }
1477:
1478: public boolean equals(Object obj) {
1479: if (!(obj instanceof Manager)) {
1480: return false;
1481: }
1482: Manager m = (Manager) obj;
1483: return new EqualsBuilder().append(getConsortiumKey(),
1484: m.getConsortiumKey()).isEquals()
1485: && super .equals(obj);
1486: }
1487: }
1488:
1489: public static class Executive extends Employee {
1490: private String department;
1491: private Manager manager;
1492:
1493: public Executive() {
1494: }
1495:
1496: public Executive(Long id_2, String name, String department,
1497: Manager manager) {
1498: super (id_2, name);
1499: this .department = department;
1500: this .manager = manager;
1501: }
1502:
1503: public String getDepartment() {
1504: return department;
1505: }
1506:
1507: public void setDepartment(String department) {
1508: this .department = department;
1509: }
1510:
1511: public Manager getManager() {
1512: return manager;
1513: }
1514:
1515: public void setManager(Manager manager) {
1516: this .manager = manager;
1517: }
1518:
1519: public boolean equals(Object obj) {
1520: if (!(obj instanceof Executive)) {
1521: return false;
1522: }
1523: Executive ex = (Executive) obj;
1524: return new EqualsBuilder().append(getDepartment(),
1525: ex.getDepartment()).isEquals()
1526: && super .equals(obj);
1527: }
1528: }
1529:
1530: public static class Employee implements Serializable {
1531: private Integer id;
1532: private Long id_2;
1533: private String name;
1534: private AddressIF address;
1535:
1536: public Employee() {
1537: }
1538:
1539: public Employee(Long id_2, String name) {
1540: this .id_2 = id_2;
1541: this .name = name;
1542: }
1543:
1544: public Integer getId() {
1545: return id;
1546: }
1547:
1548: public Long getId_2() {
1549: return id_2;
1550: }
1551:
1552: public void setId_2(Long id_2) {
1553: this .id_2 = id_2;
1554: }
1555:
1556: public void setId(Integer id) {
1557: this .id = id;
1558: }
1559:
1560: public AddressIF getAddress() {
1561: return address;
1562: }
1563:
1564: public void setAddress(AddressIF address) {
1565: this .address = address;
1566: }
1567:
1568: public String getName() {
1569: return name;
1570: }
1571:
1572: public void setName(String name) {
1573: this .name = name;
1574: }
1575:
1576: public boolean equals(Object obj) {
1577: if (!(obj instanceof Employee)) {
1578: return false;
1579: }
1580: Employee em = (Employee) obj;
1581: return new EqualsBuilder().append(getId(), em.getId())
1582: .append(getId_2(), em.getId_2()).append(getName(),
1583: em.getName()).append(getAddress(),
1584: em.getAddress()).isEquals();
1585: }
1586:
1587: public String toString() {
1588: return ToStringBuilder.reflectionToString(this ,
1589: ToStringStyle.MULTI_LINE_STYLE, false,
1590: Employee.class);
1591: }
1592: }
1593:
1594: public static class Address implements AddressIF {
1595: private Integer id;
1596: private String street;
1597:
1598: public Address() {
1599: }
1600:
1601: public Address(String street) {
1602: this .street = street;
1603: }
1604:
1605: public Integer getId() {
1606: return id;
1607: }
1608:
1609: public void setId(Integer id) {
1610: this .id = id;
1611: }
1612:
1613: public String getStreet() {
1614: return street;
1615: }
1616:
1617: public void setStreet(String street) {
1618: this .street = street;
1619: }
1620:
1621: public boolean equals(Object obj) {
1622: if (!(obj instanceof Address)) {
1623: return false;
1624: }
1625: Address adr = (Address) obj;
1626: return new EqualsBuilder().append(getId(), adr.getId())
1627: .append(getStreet(), adr.getStreet()).isEquals();
1628: }
1629:
1630: public String toString() {
1631: return ToStringBuilder.reflectionToString(this ,
1632: ToStringStyle.MULTI_LINE_STYLE, false,
1633: Address.class);
1634: }
1635: }
1636:
1637: public static interface AddressIF extends Serializable {
1638: public Integer getId();
1639:
1640: public void setId(Integer id);
1641:
1642: public String getStreet();
1643:
1644: public void setStreet(String street);
1645: }
1646:
1647: public static class Company {
1648: private Long id;
1649: private String name;
1650: private List employees;
1651: private List executives;
1652:
1653: public Company() {
1654: }
1655:
1656: public Company(Long id, String name, List employees,
1657: List executives) {
1658: this .id = id;
1659: this .name = name;
1660: this .employees = employees;
1661: this .executives = executives;
1662: }
1663:
1664: public Long getId() {
1665: return id;
1666: }
1667:
1668: public void setId(Long id) {
1669: this .id = id;
1670: }
1671:
1672: public String getName() {
1673: return name;
1674: }
1675:
1676: public void setName(String name) {
1677: this .name = name;
1678: }
1679:
1680: public List getEmployees() {
1681: return employees;
1682: }
1683:
1684: public void setEmployees(List employees) {
1685: this .employees = employees;
1686: }
1687:
1688: public List getExecutives() {
1689: return executives;
1690: }
1691:
1692: public void setExecutives(List executives) {
1693: this .executives = executives;
1694: }
1695: }
1696:
1697: public static class Consortium {
1698: private Integer id;
1699: private String name;
1700: private List shareholders;
1701:
1702: public Consortium() {
1703: }
1704:
1705: public Integer getId() {
1706: return id;
1707: }
1708:
1709: public void setId(Integer id) {
1710: this .id = id;
1711: }
1712:
1713: public String getName() {
1714: return name;
1715: }
1716:
1717: public void setName(String name) {
1718: this .name = name;
1719: }
1720:
1721: public List getShareholders() {
1722: return shareholders;
1723: }
1724:
1725: public void setShareholders(List shareholders) {
1726: this .shareholders = shareholders;
1727: }
1728:
1729: public boolean equals(Object obj) {
1730: if (!(obj instanceof Consortium)) {
1731: return false;
1732: }
1733: Consortium c = (Consortium) obj;
1734: return new EqualsBuilder().append(getId(), c.getId())
1735: // todo: this could be problematic, if so remove it
1736: .append(getShareholders(), c.getShareholders())
1737: .append(getName(), c.getName()).isEquals();
1738: }
1739:
1740: public String toString() {
1741: return ToStringBuilder.reflectionToString(this ,
1742: ToStringStyle.MULTI_LINE_STYLE, false,
1743: Consortium.class);
1744: }
1745: }
1746:
1747: public static class Entity {
1748: private Integer id;
1749: private String name;
1750:
1751: public Entity() {
1752: }
1753:
1754: public Entity(String name) {
1755: this .name = name;
1756: }
1757:
1758: public boolean equals(Object obj) {
1759: if (!(obj instanceof Entity)) {
1760: return false;
1761: }
1762: Entity other = (Entity) obj;
1763: return new EqualsBuilder().append(getId(), other.getId())
1764: .append(getName(), other.getName()).isEquals();
1765: }
1766:
1767: public Integer getId() {
1768: return id;
1769: }
1770:
1771: public void setId(Integer id) {
1772: this .id = id;
1773: }
1774:
1775: public String getName() {
1776: return name;
1777: }
1778:
1779: public void setName(String name) {
1780: this .name = name;
1781: }
1782: }
1783:
1784: public static class Animal {
1785: private Integer id;
1786: private int weight;
1787: private String name;
1788: private Animal parent;
1789: private List foods = new ArrayList();
1790:
1791: public Animal() {
1792: }
1793:
1794: public Animal(String name, int weight) {
1795: this .name = name;
1796: this .weight = weight;
1797: }
1798:
1799: public boolean equals(Object obj) {
1800: if (!(obj instanceof Animal)) {
1801: return false;
1802: }
1803: Animal other = (Animal) obj;
1804: return new EqualsBuilder().append(getId(), other.getId())
1805: .append(getName(), other.getName()).append(
1806: getWeight(), other.getWeight()).append(
1807: getParent(), other.getParent()).append(
1808: getFoods(), other.getFoods()).isEquals();
1809: }
1810:
1811: public String toString() {
1812: return ToStringBuilder
1813: .reflectionToString(this ,
1814: ToStringStyle.MULTI_LINE_STYLE, false,
1815: Entity.class);
1816: }
1817:
1818: public Integer getId() {
1819: return id;
1820: }
1821:
1822: public void setId(Integer id) {
1823: this .id = id;
1824: }
1825:
1826: public String getName() {
1827: return name;
1828: }
1829:
1830: public void setName(String name) {
1831: this .name = name;
1832: }
1833:
1834: public int getWeight() {
1835: return weight;
1836: }
1837:
1838: public void setWeight(int weight) {
1839: this .weight = weight;
1840: }
1841:
1842: public Animal getParent() {
1843: return parent;
1844: }
1845:
1846: public void setParent(Animal parent) {
1847: this .parent = parent;
1848: }
1849:
1850: public void addFood(Food food) {
1851: foods.add(food);
1852: }
1853:
1854: public List getFoods() {
1855: return foods;
1856: }
1857:
1858: public void setFoods(List foods) {
1859: this .foods = foods;
1860: }
1861: }
1862:
1863: public static class Dog {
1864: private Integer id;
1865: private int legs;
1866:
1867: // these fields should be mapped to a super table
1868: private String name;
1869: private int weight;
1870: private Animal parent;
1871: private List foods = new ArrayList();
1872:
1873: public Dog() {
1874: }
1875:
1876: public Dog(String name, int weight, int legs) {
1877: this .name = name;
1878: this .weight = weight;
1879: this .legs = legs;
1880: }
1881:
1882: public boolean equals(Object obj) {
1883: if (!(obj instanceof Dog)) {
1884: return false;
1885: }
1886: Dog other = (Dog) obj;
1887: return new EqualsBuilder().append(getId(), other.getId())
1888: .append(getName(), other.getName()).append(
1889: getLegs(), other.getLegs()).append(
1890: getWeight(), other.getWeight()).append(
1891: getParent(), other.getParent()).append(
1892: (getFoods() != null ? new Integer(
1893: getFoods().size()) : null),
1894: ((other.getFoods() != null ? new Integer(
1895: other.getFoods().size()) : null)))
1896: .isEquals();
1897: }
1898:
1899: public String toString() {
1900: return ToStringBuilder.reflectionToString(this ,
1901: ToStringStyle.MULTI_LINE_STYLE, false, Dog.class);
1902: }
1903:
1904: public Integer getId() {
1905: return id;
1906: }
1907:
1908: public void setId(Integer id) {
1909: this .id = id;
1910: }
1911:
1912: public String getName() {
1913: return name;
1914: }
1915:
1916: public void setName(String name) {
1917: this .name = name;
1918: }
1919:
1920: public int getWeight() {
1921: return weight;
1922: }
1923:
1924: public void setWeight(int weight) {
1925: this .weight = weight;
1926: }
1927:
1928: public int getLegs() {
1929: return legs;
1930: }
1931:
1932: public void setLegs(int legs) {
1933: this .legs = legs;
1934: }
1935:
1936: public Animal getParent() {
1937: return parent;
1938: }
1939:
1940: public void setParent(Animal parent) {
1941: this .parent = parent;
1942: }
1943:
1944: public void addFood(Food food) {
1945: this .foods.add(food);
1946: }
1947:
1948: public List getFoods() {
1949: return foods;
1950: }
1951:
1952: public void setFoods(List foods) {
1953: this .foods = foods;
1954: }
1955: }
1956:
1957: public static class Food extends Entity {
1958: private Integer fkAnimal;
1959:
1960: public Food() {
1961: }
1962:
1963: public boolean equals(Object obj) {
1964: if (!(obj instanceof Food)) {
1965: return false;
1966: }
1967: Food other = (Food) obj;
1968: return new EqualsBuilder().append(getFkAnimal(),
1969: other.getFkAnimal()).isEquals()
1970: && super .equals(obj);
1971: }
1972:
1973: public String toString() {
1974: return ToStringBuilder.reflectionToString(this ,
1975: ToStringStyle.MULTI_LINE_STYLE, false, Food.class);
1976: }
1977:
1978: public Food(String name) {
1979: super (name);
1980: }
1981:
1982: public Integer getFkAnimal() {
1983: return fkAnimal;
1984: }
1985:
1986: public void setFkAnimal(Integer fkAnimal) {
1987: this.fkAnimal = fkAnimal;
1988: }
1989: }
1990: }
|