0001: package org.apache.ojb.odmg;
0002:
0003: import java.io.Serializable;
0004: import java.util.ArrayList;
0005: import java.util.Collection;
0006: import java.util.Iterator;
0007: import java.util.List;
0008:
0009: import org.apache.commons.lang.SerializationUtils;
0010: import org.apache.commons.lang.builder.EqualsBuilder;
0011: import org.apache.commons.lang.builder.ToStringBuilder;
0012: import org.apache.commons.lang.builder.ToStringStyle;
0013: import org.apache.ojb.broker.*;
0014: import org.apache.ojb.junit.ODMGTestCase;
0015: import org.odmg.OQLQuery;
0016: import org.odmg.Transaction;
0017:
0018: /**
0019: * Test inheritance using multiple tables.
0020: * <p/>
0021: * Inner test classes:<br/>
0022: * AddressIF<--Address<br/>
0023: * Employee<--Executive<--Manager
0024: *<p/>
0025: * m:n relation between Employee and Address
0026: * 1:1 relation from Employee to Address
0027: * 1:n relation from Employee to Address
0028: * 1:1 relation from Executive to Manager
0029: * 1:n relation from Manager to Executive
0030: *
0031: * @author <a href="mailto:arminw@apache.org">Armin Waibel</a>
0032: * @version $Id: InheritanceMultipleTableTest.java,v 1.1.2.8 2005/12/13 18:18:25 arminw Exp $
0033: */
0034: public class InheritanceMultipleTableTest extends ODMGTestCase {
0035: public static void main(String[] args) {
0036: junit.textui.TestRunner
0037: .main(new String[] { InheritanceMultipleTableTest.class
0038: .getName() });
0039: }
0040:
0041: public void testQueryUsingReference_1() throws Exception {
0042: long timestamp = System.currentTimeMillis();
0043: Long id_2 = new Long(timestamp);
0044: String name = "testQueryUsingReference_1" + timestamp;
0045:
0046: Manager m_1 = new Manager(id_2, name + "_manager_1");
0047: m_1.setDepartment("m_1");
0048: Address a_1 = new Address("snob allee");
0049: m_1.setAddress(a_1);
0050:
0051: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0052: tx.begin();
0053: database.makePersistent(m_1);
0054: tx.commit();
0055:
0056: tx = (TransactionExt) odmg.newTransaction();
0057: tx.begin();
0058: tx.getBroker().clearCache();
0059: OQLQuery query = odmg.newOQLQuery();
0060: query.create("select objects from " + Manager.class.getName()
0061: + " where name like $1 and address.street like $2");
0062: query.bind(name + "%");
0063: query.bind("snob allee");
0064: Collection result = (Collection) query.execute();
0065: tx.commit();
0066:
0067: assertEquals(1, result.size());
0068: Manager retManager = (Manager) result.iterator().next();
0069: assertNotNull(retManager);
0070: assertEquals(name + "_manager_1", retManager.getName());
0071: assertNotNull(retManager.getAddress());
0072: assertEquals("snob allee", retManager.getAddress().getStreet());
0073:
0074: tx = (TransactionExt) odmg.newTransaction();
0075: tx.begin();
0076: tx.lock(m_1, Transaction.WRITE);
0077: m_1.setName(m_1.getName() + "_updated");
0078: tx.commit();
0079: }
0080:
0081: public void testQueryUsingReference_2() throws Exception {
0082: long timestamp = System.currentTimeMillis();
0083: Long id_2 = new Long(timestamp);
0084: String name = "testQueryUsingReference_2" + timestamp;
0085:
0086: Manager manager = new Manager(id_2, name + "_manager_1");
0087: manager.setDepartment("manager");
0088: Address addressManager = new Address("snob allee 1");
0089: Address addressManagerOld = new Address("snob allee 2");
0090: Address address3 = new Address("snob allee 3");
0091: Address address4 = new Address("snob allee 4");
0092: manager.setAddress(addressManager);
0093: manager.addOldAddress(addressManagerOld);
0094: manager.addCarrel(address3);
0095: manager.addCarrel(address4);
0096:
0097: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0098: tx.begin();
0099: database.makePersistent(manager);
0100: tx.commit();
0101:
0102: tx = (TransactionExt) odmg.newTransaction();
0103: tx.begin();
0104: tx.getBroker().clearCache();
0105: OQLQuery query = odmg.newOQLQuery();
0106: query.create("select objects from " + Manager.class.getName()
0107: + " where name like $1 and address.street like $2");
0108: query.bind(name + "%");
0109: query.bind("snob allee 1");
0110: Collection result = (Collection) query.execute();
0111: tx.commit();
0112:
0113: assertEquals(1, result.size());
0114: Manager retManager = (Manager) result.iterator().next();
0115: assertNotNull(retManager);
0116: assertEquals(name + "_manager_1", retManager.getName());
0117: assertNotNull(retManager.getAddress());
0118: assertEquals("snob allee 1", retManager.getAddress()
0119: .getStreet());
0120:
0121: tx = (TransactionExt) odmg.newTransaction();
0122: tx.begin();
0123: tx.lock(manager, Transaction.WRITE);
0124: manager.setName(manager.getName() + "_updated");
0125: manager.getAddress().setStreet("updated_street");
0126: tx.commit();
0127:
0128: tx = (TransactionExt) odmg.newTransaction();
0129: tx.begin();
0130: tx.getBroker().clearCache();
0131: query = odmg.newOQLQuery();
0132: query.create("select objects from " + Manager.class.getName()
0133: + " where name like $1 and address.street like $2");
0134: query.bind(name + "%");
0135: query.bind("updated_street");
0136: result = (Collection) query.execute();
0137: tx.commit();
0138:
0139: assertEquals(1, result.size());
0140: retManager = (Manager) result.iterator().next();
0141: assertNotNull(retManager);
0142: assertEquals(name + "_manager_1_updated", retManager.getName());
0143: assertNotNull(retManager.getAddress());
0144: assertEquals("updated_street", retManager.getAddress()
0145: .getStreet());
0146: }
0147:
0148: public void testQuery_3() throws Exception {
0149: long timestamp = System.currentTimeMillis();
0150: Long id_2 = new Long(timestamp);
0151: String name = "testInsert" + timestamp;
0152: Manager m_3 = new Manager(id_2, name + "_manager_3");
0153: m_3.setDepartment("none");
0154:
0155: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0156: tx.begin();
0157: database.makePersistent(m_3);
0158: tx.commit();
0159:
0160: tx.begin();
0161: tx.getBroker().clearCache();
0162:
0163: OQLQuery query = odmg.newOQLQuery();
0164: query.create("select objects from " + Manager.class.getName()
0165: + " where name like $1");
0166: query.bind(name + "%");
0167: List newManagers = new ArrayList((Collection) query.execute());
0168: tx.commit();
0169: assertEquals(1, newManagers.size());
0170:
0171: Manager new_m = (Manager) newManagers.get(0);
0172: assertNotNull(new_m.getId());
0173: assertNotNull(new_m.getId_2());
0174: assertEquals(m_3.getName(), new_m.getName());
0175: assertEquals(m_3.getDepartment(), new_m.getDepartment());
0176:
0177: tx = (TransactionExt) odmg.newTransaction();
0178: tx.begin();
0179: database.deletePersistent(m_3);
0180: tx.commit();
0181: }
0182:
0183: public void testQuery_2() throws Exception {
0184: long timestamp = System.currentTimeMillis();
0185: Long id_2 = new Long(timestamp);
0186: String name = "testInsert" + timestamp;
0187: Manager m_1 = new Manager(id_2, name + "_manager_1");
0188: Manager m_2 = new Manager(id_2, name + "_manager_2");
0189: Manager m_3 = new Manager(id_2, name + "_manager_3");
0190: m_3.setDepartment("none");
0191:
0192: Executive ex_1 = new Executive(id_2, name + "_executive",
0193: "department_1", null);
0194: Executive ex_2 = new Executive(id_2, name + "_executive",
0195: "department_1", null);
0196:
0197: Employee em = new Employee(id_2, name + "_employee");
0198:
0199: List executives = new ArrayList();
0200: executives.add(ex_1);
0201: executives.add(ex_2);
0202: m_3.setExecutives(executives);
0203:
0204: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0205: tx.begin();
0206: database.makePersistent(m_1);
0207: database.makePersistent(m_2);
0208: database.makePersistent(m_3);
0209: database.makePersistent(ex_1);
0210: database.makePersistent(ex_2);
0211: database.makePersistent(em);
0212: tx.commit();
0213:
0214: tx.begin();
0215: tx.getBroker().clearCache();
0216: tx.commit();
0217:
0218: OQLQuery query = odmg.newOQLQuery();
0219: query.create("select objects from " + Employee.class.getName()
0220: + " where name like $1");
0221: query.bind(name + "%");
0222: List newEmployees = new ArrayList((Collection) query.execute());
0223: assertEquals(6, newEmployees.size());
0224: int countEmployee = 0;
0225: int countExecutive = 0;
0226: int countManager = 0;
0227: for (int i = 0; i < newEmployees.size(); i++) {
0228: Object o = newEmployees.get(i);
0229: if (o instanceof Employee) {
0230: ++countEmployee;
0231: }
0232: if (o instanceof Executive) {
0233: ++countExecutive;
0234: }
0235: if (o instanceof Manager) {
0236: ++countManager;
0237: }
0238: }
0239: assertEquals(6, countEmployee);
0240: /*
0241: bug:
0242: expect that the real classes will be populated
0243: currently this does not happen, only objects of
0244: type Employee will be returned.
0245: */
0246: assertEquals(5, countExecutive);
0247: assertEquals(3, countManager);
0248: }
0249:
0250: public void testQuery_1() throws Exception {
0251: long timestamp = System.currentTimeMillis();
0252: Long id_2 = new Long(timestamp);
0253: String name = "testInsert" + timestamp;
0254: Manager m_1 = new Manager(id_2, name + "_manager_1");
0255: Manager m_2 = new Manager(id_2, name + "_manager_2");
0256: Manager m_3 = new Manager(id_2, name + "_manager_3");
0257: m_3.setDepartment("none");
0258:
0259: Executive ex_1 = new Executive(id_2, name + "_executive",
0260: "department_1", null);
0261: Executive ex_2 = new Executive(id_2, name + "_executive",
0262: "department_1", null);
0263:
0264: Employee em = new Employee(id_2, name + "_employee");
0265:
0266: List executives = new ArrayList();
0267: executives.add(ex_1);
0268: executives.add(ex_2);
0269: m_3.setExecutives(executives);
0270:
0271: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0272: tx.begin();
0273: database.makePersistent(m_1);
0274: database.makePersistent(m_2);
0275: database.makePersistent(m_3);
0276: database.makePersistent(ex_1);
0277: database.makePersistent(ex_2);
0278: database.makePersistent(em);
0279: tx.commit();
0280:
0281: tx.begin();
0282: tx.getBroker().clearCache();
0283: tx.commit();
0284:
0285: OQLQuery query = odmg.newOQLQuery();
0286: query.create("select objects from " + Manager.class.getName()
0287: + " where name like $1 and department like $2");
0288: query.bind(name + "%");
0289: query.bind("none");
0290: Collection result = (Collection) query.execute();
0291: assertEquals(1, result.size());
0292: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0293: Object o = iterator.next();
0294: assertTrue(o instanceof Manager);
0295: Manager temp = (Manager) o;
0296: assertNotNull(temp.getExecutives());
0297: assertEquals(2, temp.getExecutives().size());
0298: }
0299:
0300: query = odmg.newOQLQuery();
0301: query.create("select objects from " + Employee.class.getName()
0302: + " where name like $1");
0303: query.bind(name + "%");
0304: result = (Collection) query.execute();
0305: assertEquals(6, result.size());
0306: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0307: Object o = iterator.next();
0308: assertTrue(o instanceof Employee);
0309: }
0310:
0311: query = odmg.newOQLQuery();
0312: query.create("select objects from " + Executive.class.getName()
0313: + " where name like $1");
0314: query.bind(name + "%");
0315: result = (Collection) query.execute();
0316: assertEquals(5, result.size());
0317: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0318: Object o = iterator.next();
0319: assertTrue(o instanceof Executive);
0320: }
0321:
0322: query = odmg.newOQLQuery();
0323: query.create("select objects from " + Manager.class.getName()
0324: + " where name like $1");
0325: query.bind(name + "%");
0326: result = (Collection) query.execute();
0327: assertEquals(3, result.size());
0328: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0329: Object o = iterator.next();
0330: assertTrue(o instanceof Manager);
0331: }
0332: }
0333:
0334: public void testQueryWithSerializedObjects() throws Exception {
0335: long timestamp = System.currentTimeMillis();
0336: Long id_2 = new Long(timestamp);
0337: String name = "testInsert" + timestamp;
0338: Manager m_1 = new Manager(id_2, name + "_manager_1");
0339: Manager m_2 = new Manager(id_2, name + "_manager_2");
0340: Manager m_3 = new Manager(id_2, name + "_manager_3");
0341: m_3.setDepartment("none");
0342:
0343: Executive ex_1 = new Executive(id_2, name + "_executive",
0344: "department_1", null);
0345: Executive ex_2 = new Executive(id_2, name + "_executive",
0346: "department_1", null);
0347:
0348: Employee em = new Employee(id_2, name + "_employee");
0349:
0350: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0351: tx.begin();
0352: database.makePersistent(m_1);
0353: database.makePersistent(m_2);
0354: database.makePersistent(m_3);
0355: database.makePersistent(ex_1);
0356: database.makePersistent(ex_2);
0357: database.makePersistent(em);
0358: tx.commit();
0359:
0360: tx.begin();
0361: tx.getBroker().clearCache();
0362: tx.commit();
0363:
0364: OQLQuery query = odmg.newOQLQuery();
0365: query.create("select objects from " + Manager.class.getName()
0366: + " where name like $1 and department like $2");
0367: query.bind(name + "%");
0368: query.bind("none");
0369: Collection result = (Collection) query.execute();
0370: assertEquals(1, result.size());
0371:
0372: query = odmg.newOQLQuery();
0373: query.create("select objects from " + Employee.class.getName()
0374: + " where name like $1");
0375: query.bind(name + "%");
0376: result = (Collection) query.execute();
0377: assertEquals(6, result.size());
0378:
0379: query = odmg.newOQLQuery();
0380: query.create("select objects from " + Executive.class.getName()
0381: + " where name like $1");
0382: query.bind(name + "%");
0383: result = (Collection) query.execute();
0384: assertEquals(5, result.size());
0385:
0386: query = odmg.newOQLQuery();
0387: query.create("select objects from " + Manager.class.getName()
0388: + " where name like $1");
0389: query.bind(name + "%");
0390: result = (Collection) query.execute();
0391: assertEquals(3, result.size());
0392:
0393: em = (Employee) SerializationUtils
0394: .deserialize(SerializationUtils.serialize(em));
0395: m_1 = (Manager) SerializationUtils
0396: .deserialize(SerializationUtils.serialize(m_1));
0397: m_2 = (Manager) SerializationUtils
0398: .deserialize(SerializationUtils.serialize(m_2));
0399: m_3 = (Manager) SerializationUtils
0400: .deserialize(SerializationUtils.serialize(m_3));
0401: ex_1 = (Executive) SerializationUtils
0402: .deserialize(SerializationUtils.serialize(ex_1));
0403: ex_2 = (Executive) SerializationUtils
0404: .deserialize(SerializationUtils.serialize(ex_2));
0405:
0406: tx = (TransactionExt) odmg.newTransaction();
0407: tx.begin();
0408: tx.getBroker().clearCache();
0409: tx.lock(em, Transaction.WRITE);
0410: tx.lock(m_1, Transaction.WRITE);
0411: tx.lock(m_2, Transaction.WRITE);
0412: tx.lock(m_3, Transaction.WRITE);
0413: tx.lock(ex_1, Transaction.WRITE);
0414: tx.lock(ex_2, Transaction.WRITE);
0415:
0416: em.setName(em.getName() + "_updated");
0417: m_1.setName(m_1.getName() + "_updated");
0418: m_2.setName(m_2.getName() + "_updated");
0419: m_3.setName(m_3.getName() + "_updated");
0420: ex_1.setName(ex_1.getName() + "_updated");
0421: ex_2.setName(ex_2.getName() + "_updated");
0422:
0423: tx.commit();
0424:
0425: query = odmg.newOQLQuery();
0426: query.create("select objects from " + Manager.class.getName()
0427: + " where name like $1 and department like $2");
0428: query.bind(name + "%");
0429: query.bind("none");
0430: result = (Collection) query.execute();
0431: assertEquals(
0432: "Expect the same number of objects as before update",
0433: 1, result.size());
0434:
0435: query = odmg.newOQLQuery();
0436: query.create("select objects from " + Employee.class.getName()
0437: + " where name like $1");
0438: query.bind(name + "%");
0439: result = (Collection) query.execute();
0440: assertEquals(
0441: "Expect the same number of objects as before update",
0442: 6, result.size());
0443:
0444: query = odmg.newOQLQuery();
0445: query.create("select objects from " + Executive.class.getName()
0446: + " where name like $1");
0447: query.bind(name + "%");
0448: result = (Collection) query.execute();
0449: assertEquals(
0450: "Expect the same number of objects as before update",
0451: 5, result.size());
0452:
0453: query = odmg.newOQLQuery();
0454: query.create("select objects from " + Manager.class.getName()
0455: + " where name like $1");
0456: query.bind(name + "%");
0457: result = (Collection) query.execute();
0458: assertEquals(
0459: "Expect the same number of objects as before update",
0460: 3, result.size());
0461: }
0462:
0463: private void prepareForQueryTests(Long id_2, String name) {
0464: Manager m_1 = new Manager(id_2, name + "_manager_1");
0465: Manager m_2 = new Manager(id_2, name + "_manager_2");
0466: Manager m_3 = new Manager(id_2, name + "_manager_3");
0467: m_3.setDepartment("none");
0468: Address a_1 = new Address("snob allee");
0469: m_1.setAddress(a_1);
0470:
0471: Executive ex_1 = new Executive(id_2, name + "_executive",
0472: "department_1", null);
0473: Executive ex_2 = new Executive(id_2, name + "_executive",
0474: "department_1", null);
0475:
0476: Employee em = new Employee(id_2, name + "_employee");
0477: Address a_2 = new Address("cockroaches valley");
0478: em.setAddress(a_2);
0479:
0480: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0481: tx.begin();
0482: database.makePersistent(m_1);
0483: database.makePersistent(m_2);
0484: database.makePersistent(m_3);
0485: database.makePersistent(ex_1);
0486: database.makePersistent(ex_2);
0487: database.makePersistent(em);
0488: tx.commit();
0489: }
0490:
0491: public void testQuery_InheritedObjects() throws Exception {
0492: long timestamp = System.currentTimeMillis();
0493: Long id_2 = new Long(timestamp);
0494: String name = "testQuery_InheritedObjects" + timestamp;
0495: prepareForQueryTests(id_2, name);
0496:
0497: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0498: tx.begin();
0499: tx.getBroker().clearCache();
0500:
0501: OQLQuery query = odmg.newOQLQuery();
0502: query.create("select objects from " + Employee.class.getName()
0503: + " where name like $1");
0504: query.bind(name + "%");
0505: Collection result = (Collection) query.execute();
0506: assertEquals(6, result.size());
0507: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0508: Employee obj = (Employee) iterator.next();
0509: assertNotNull(obj.getName());
0510: }
0511:
0512: tx.getBroker().clearCache();
0513: query = odmg.newOQLQuery();
0514: query.create("select objects from " + Executive.class.getName()
0515: + " where name like $1");
0516: query.bind(name + "%");
0517: result = (Collection) query.execute();
0518: assertEquals(5, result.size());
0519: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0520: Executive obj = (Executive) iterator.next();
0521: assertNotNull(obj.getName());
0522: }
0523:
0524: tx.getBroker().clearCache();
0525: query = odmg.newOQLQuery();
0526: query.create("select objects from " + Manager.class.getName()
0527: + " where name like $1");
0528: query.bind(name + "%");
0529: result = (Collection) query.execute();
0530: assertEquals(3, result.size());
0531: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
0532: Manager obj = (Manager) iterator.next();
0533: assertNotNull(obj.getName());
0534: }
0535:
0536: tx.commit();
0537: }
0538:
0539: public void testQuery_InheritedField() throws Exception {
0540: long timestamp = System.currentTimeMillis();
0541: Long id_2 = new Long(timestamp);
0542: String name = "testQuery_InheritedField" + timestamp;
0543: prepareForQueryTests(id_2, name);
0544: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0545: tx.begin();
0546: tx.getBroker().clearCache();
0547: OQLQuery query = odmg.newOQLQuery();
0548: query.create("select objects from " + Manager.class.getName()
0549: + " where name like $1 and department like $2");
0550: query.bind(name + "%");
0551: query.bind("none");
0552: Collection result = (Collection) query.execute();
0553: tx.commit();
0554: assertEquals(1, result.size());
0555: }
0556:
0557: public void testQuery_Reference() throws Exception {
0558: long timestamp = System.currentTimeMillis();
0559: Long id_2 = new Long(timestamp);
0560: String name = "testQuery_Reference" + timestamp;
0561: prepareForQueryTests(id_2, name);
0562:
0563: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0564: tx.begin();
0565: tx.getBroker().clearCache();
0566: OQLQuery query = odmg.newOQLQuery();
0567: query.create("select objects from " + Employee.class.getName()
0568: + " where name like $1 and address.street like $2");
0569: query.bind(name + "%");
0570: query.bind("%valley");
0571: Collection result = (Collection) query.execute();
0572: tx.commit();
0573:
0574: assertEquals(1, result.size());
0575: Employee emp = (Employee) result.iterator().next();
0576: assertNotNull(emp.getAddress());
0577: assertEquals("cockroaches valley", emp.getAddress().getStreet());
0578: }
0579:
0580: public void testQuery_InheritedReference_1() throws Exception {
0581: long timestamp = System.currentTimeMillis();
0582: Long id_2 = new Long(timestamp);
0583: String name = "testQuery_InheritedReference_1" + timestamp;
0584: prepareForQueryTests(id_2, name);
0585:
0586: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0587: tx.begin();
0588: tx.getBroker().clearCache();
0589: OQLQuery query = odmg.newOQLQuery();
0590: query.create("select objects from " + Manager.class.getName()
0591: + " where name like $1 and address.street like $2");
0592: query.bind(name + "%");
0593: query.bind("snob allee");
0594: Collection result = (Collection) query.execute();
0595: tx.commit();
0596:
0597: assertEquals(1, result.size());
0598: Manager retManager = (Manager) result.iterator().next();
0599: assertNotNull(retManager);
0600: assertEquals(name + "_manager_1", retManager.getName());
0601: assertNotNull(retManager.getAddress());
0602: assertEquals("snob allee", retManager.getAddress().getStreet());
0603: }
0604:
0605: public void testQuery_InheritedReference_2() throws Exception {
0606: long timestamp = System.currentTimeMillis();
0607: Long id_2 = new Long(timestamp);
0608: String name = "testQuery_InheritedReference_2" + timestamp;
0609: prepareForQueryTests(id_2, name);
0610:
0611: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0612: tx.begin();
0613: tx.getBroker().clearCache();
0614: OQLQuery query = odmg.newOQLQuery();
0615: query.create("select objects from " + Executive.class.getName()
0616: + " where name like $1 and address.street like $2");
0617: query.bind(name + "%");
0618: query.bind("snob allee");
0619: Collection result = (Collection) query.execute();
0620: tx.commit();
0621:
0622: assertEquals(1, result.size());
0623: Executive retManager = (Executive) result.iterator().next();
0624: assertNotNull(retManager);
0625: assertEquals(name + "_manager_1", retManager.getName());
0626: }
0627:
0628: public void testQuery_InheritedReference_3() throws Exception {
0629: long timestamp = System.currentTimeMillis();
0630: Long id_2 = new Long(timestamp);
0631: String name = "testQuery_InheritedReference_3" + timestamp;
0632: prepareForQueryTests(id_2, name);
0633:
0634: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0635: tx.begin();
0636: tx.getBroker().clearCache();
0637: OQLQuery query = odmg.newOQLQuery();
0638: query.create("select objects from " + Employee.class.getName()
0639: + " where name like $1 and address.street like $2");
0640: query.bind(name + "%");
0641: query.bind("snob allee");
0642: Collection result = (Collection) query.execute();
0643: tx.commit();
0644: assertEquals(1, result.size());
0645: Employee emp = (Employee) result.iterator().next();
0646: assertNotNull(emp);
0647: assertEquals(name + "_manager_1", emp.getName());
0648: }
0649:
0650: public void testInsertQuery() throws Exception {
0651: long timestamp = System.currentTimeMillis();
0652: Long id_2 = new Long(timestamp);
0653: String name = "testInsert" + timestamp;
0654:
0655: Employee em1 = new Employee(id_2, name);
0656: Executive ex1 = new Executive(id_2, name, "department_1", null);
0657: Executive ex2 = new Executive(id_2, name, "department_2", null);
0658: ArrayList list = new ArrayList();
0659: list.add(ex1);
0660: list.add(ex2);
0661: Manager m1 = new Manager(id_2, name);
0662: m1.setExecutives(list);
0663: ex1.setManager(m1);
0664: ex2.setManager(m1);
0665:
0666: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0667: tx.begin();
0668: database.makePersistent(em1);
0669: database.makePersistent(m1);
0670: tx.commit();
0671:
0672: tx.begin();
0673: Identity m1_oid = tx.getBroker().serviceIdentity()
0674: .buildIdentity(m1);
0675: Identity ex1_oid = tx.getBroker().serviceIdentity()
0676: .buildIdentity(ex1);
0677: Identity em1_oid = tx.getBroker().serviceIdentity()
0678: .buildIdentity(em1);
0679:
0680: tx.getBroker().clearCache();
0681:
0682: Employee newEm1 = (Employee) tx.getBroker()
0683: .getObjectByIdentity(em1_oid);
0684: Executive newEx1 = (Executive) tx.getBroker()
0685: .getObjectByIdentity(ex1_oid);
0686: Manager newM1 = (Manager) tx.getBroker().getObjectByIdentity(
0687: m1_oid);
0688:
0689: assertEquals(em1, newEm1);
0690: assertEquals(ex1, newEx1);
0691: assertEquals(m1, newM1);
0692: assertEquals(name, newEx1.getName());
0693: assertEquals(name, newM1.getName());
0694:
0695: assertEquals(2, newM1.getExecutives().size());
0696:
0697: OQLQuery queryEmployee = odmg.newOQLQuery();
0698: queryEmployee.create("select objects from "
0699: + Employee.class.getName() + " where name like $1");
0700: queryEmployee.bind(name);
0701:
0702: OQLQuery queryExecutive = odmg.newOQLQuery();
0703: queryExecutive.create("select objects from "
0704: + Executive.class.getName() + " where name like $1");
0705: queryExecutive.bind(name);
0706:
0707: OQLQuery queryManager = odmg.newOQLQuery();
0708: queryManager.create("select objects from "
0709: + Manager.class.getName() + " where name like $1");
0710: queryManager.bind(name);
0711:
0712: Collection result = (Collection) queryEmployee.execute();
0713: assertEquals(4, result.size());
0714:
0715: result = (Collection) queryExecutive.execute();
0716: assertEquals(3, result.size());
0717:
0718: result = (Collection) queryManager.execute();
0719: assertEquals(1, result.size());
0720: }
0721:
0722: public void testUpdate() throws Exception {
0723: // not all changes are written to DB
0724: // arminw: fixed
0725: // if (ojbSkipKnownIssueProblem()) return;
0726:
0727: long timestamp = System.currentTimeMillis();
0728: Long id_2 = new Long(timestamp);
0729: String name = "testUpdate" + timestamp;
0730: Employee em1 = new Employee(id_2, "employee_" + name);
0731: Executive ex1 = new Executive(id_2, "executive_" + name,
0732: "department_1", null);
0733: Executive ex2 = new Executive(id_2, "executive_" + name,
0734: "department_2", null);
0735: ArrayList list = new ArrayList();
0736: list.add(ex1);
0737: list.add(ex2);
0738: Manager m1 = new Manager(id_2, "manager_" + name);
0739: m1.setExecutives(list);
0740:
0741: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0742: tx.begin();
0743: database.makePersistent(em1);
0744: database.makePersistent(m1);
0745: tx.commit();
0746:
0747: tx.begin();
0748: Identity m1_oid = tx.getBroker().serviceIdentity()
0749: .buildIdentity(m1);
0750: Identity ex1_oid = tx.getBroker().serviceIdentity()
0751: .buildIdentity(ex1);
0752: Identity em1_oid = tx.getBroker().serviceIdentity()
0753: .buildIdentity(em1);
0754:
0755: tx.getBroker().clearCache();
0756:
0757: Employee newEm1 = (Employee) tx.getBroker()
0758: .getObjectByIdentity(em1_oid);
0759: Executive newEx1 = (Executive) tx.getBroker()
0760: .getObjectByIdentity(ex1_oid);
0761: Manager newM1 = (Manager) tx.getBroker().getObjectByIdentity(
0762: m1_oid);
0763: tx.commit();
0764:
0765: assertEquals(2, newM1.getExecutives().size());
0766:
0767: tx.begin();
0768: tx.lock(newEm1, Transaction.WRITE);
0769: tx.lock(newEx1, Transaction.WRITE);
0770: tx.lock(newM1, Transaction.WRITE);
0771: newEm1.setName("**updated_employee_" + name);
0772: newM1.setName("**updated_manager1_" + name);
0773: newM1.setDepartment("**new");
0774: ((Executive) newM1.getExecutives().get(0))
0775: .setName("**updated_executive1_" + name);
0776: ((Executive) newM1.getExecutives().get(1))
0777: .setName("**updated_executive2_" + name);
0778: tx.commit();
0779:
0780: //*************************************
0781: tx.begin();
0782: tx.getBroker().clearCache();
0783: em1 = (Employee) tx.getBroker().getObjectByIdentity(em1_oid);
0784: ex1 = (Executive) tx.getBroker().getObjectByIdentity(ex1_oid);
0785: m1 = (Manager) tx.getBroker().getObjectByIdentity(m1_oid);
0786: tx.commit();
0787: //*************************************
0788:
0789: assertEquals(newEm1, em1);
0790: assertEquals(newM1, m1);
0791: assertEquals(2, m1.getExecutives().size());
0792: }
0793:
0794: public void testDelete() {
0795: // not all objects will be deleted
0796: // arminw: fixed
0797: // if (ojbSkipKnownIssueProblem()) return;
0798:
0799: long timestamp = System.currentTimeMillis();
0800: Long id_2 = new Long(timestamp);
0801: String name = "testUpdate" + timestamp;
0802: Employee em1 = new Employee(id_2, "employee_" + name);
0803: Executive ex1 = new Executive(id_2, "executive_" + name,
0804: "department_1", null);
0805: Executive ex2 = new Executive(id_2, "executive_" + name,
0806: "department_2", null);
0807: ArrayList list = new ArrayList();
0808: list.add(ex1);
0809: list.add(ex2);
0810: Manager m1 = new Manager(id_2, "manager_" + name);
0811: m1.setExecutives(list);
0812:
0813: TransactionExt tx = (TransactionExt) odmg.newTransaction();
0814: tx.begin();
0815: database.makePersistent(em1);
0816: database.makePersistent(m1);
0817: tx.commit();
0818:
0819: tx.begin();
0820: Identity m1_oid = tx.getBroker().serviceIdentity()
0821: .buildIdentity(m1);
0822: Identity ex1_oid = tx.getBroker().serviceIdentity()
0823: .buildIdentity(ex1);
0824: Identity em1_oid = tx.getBroker().serviceIdentity()
0825: .buildIdentity(em1);
0826:
0827: tx.getBroker().clearCache();
0828:
0829: Employee newEm1 = (Employee) tx.getBroker()
0830: .getObjectByIdentity(em1_oid);
0831: Executive newEx1 = (Executive) tx.getBroker()
0832: .getObjectByIdentity(ex1_oid);
0833: Manager newM1 = (Manager) tx.getBroker().getObjectByIdentity(
0834: m1_oid);
0835: tx.commit();
0836:
0837: assertNotNull(newEm1);
0838: assertNotNull(newEx1);
0839: assertNotNull(newM1);
0840: assertEquals(2, newM1.getExecutives().size());
0841:
0842: //*************************************
0843: tx.begin();
0844: database.deletePersistent(newEm1);
0845: database.deletePersistent(newEx1);
0846: database.deletePersistent(newM1);
0847: tx.commit();
0848: //*************************************
0849:
0850: tx.begin();
0851: newEm1 = (Employee) tx.getBroker().getObjectByIdentity(em1_oid);
0852: newEx1 = (Executive) tx.getBroker()
0853: .getObjectByIdentity(ex1_oid);
0854: newM1 = (Manager) tx.getBroker().getObjectByIdentity(m1_oid);
0855: tx.commit();
0856:
0857: assertNull(newEm1);
0858: assertNull(newEx1);
0859: assertNull(newM1);
0860: }
0861:
0862: //************************************************************
0863: // inner classes used for test
0864: //************************************************************
0865: public static class Manager extends Executive {
0866: private List executives = new ArrayList();
0867:
0868: public Manager() {
0869: }
0870:
0871: public Manager(Long id_2, String name) {
0872: super (id_2, name, null, null);
0873: }
0874:
0875: public boolean equals(Object obj) {
0876: if (!(obj instanceof Manager)) {
0877: return false;
0878: }
0879: Manager other = (Manager) obj;
0880: return new EqualsBuilder().append(getExecutives(),
0881: other.getExecutives()).isEquals()
0882: && super .equals(obj);
0883: }
0884:
0885: public List getExecutives() {
0886: return executives;
0887: }
0888:
0889: public void setExecutives(List executives) {
0890: this .executives = executives;
0891: }
0892: }
0893:
0894: public static class Executive extends Employee {
0895: private String department;
0896: private Manager manager;
0897:
0898: public Executive() {
0899: }
0900:
0901: public Executive(Long id_2, String name, String department,
0902: Manager manager) {
0903: super (id_2, name);
0904: this .department = department;
0905: this .manager = manager;
0906: }
0907:
0908: public String getDepartment() {
0909: return department;
0910: }
0911:
0912: public void setDepartment(String department) {
0913: this .department = department;
0914: }
0915:
0916: public Manager getManager() {
0917: return manager;
0918: }
0919:
0920: public void setManager(Manager manager) {
0921: this .manager = manager;
0922: }
0923:
0924: public boolean equals(Object obj) {
0925: if (!(obj instanceof Executive)) {
0926: return false;
0927: }
0928: Executive ex = (Executive) obj;
0929: return new EqualsBuilder()
0930: // avoid endless loop with Manager 1:n relation
0931: .append(
0932: (getManager() != null ? getManager()
0933: .getId() : null),
0934: (ex.getManager() != null ? ex.getManager()
0935: .getId() : null)).append(
0936: (getManager() != null ? getManager()
0937: .getId_2() : null),
0938: (ex.getManager() != null ? ex.getManager()
0939: .getId_2() : null)).append(
0940: getDepartment(), ex.getDepartment())
0941: .isEquals()
0942: && super .equals(obj);
0943: }
0944: }
0945:
0946: public static class Employee implements Serializable {
0947: private Integer id;
0948: private Long id_2;
0949: private String name;
0950: private AddressIF address;
0951: private List oldAddresses = new ArrayList();
0952: private List carrels = new ArrayList();
0953:
0954: public Employee() {
0955: }
0956:
0957: public Employee(Long id_2, String name) {
0958: this .id_2 = id_2;
0959: this .name = name;
0960: }
0961:
0962: public Integer getId() {
0963: return id;
0964: }
0965:
0966: public Long getId_2() {
0967: return id_2;
0968: }
0969:
0970: public void setId_2(Long id_2) {
0971: this .id_2 = id_2;
0972: }
0973:
0974: public void setId(Integer id) {
0975: this .id = id;
0976: }
0977:
0978: public void addOldAddress(AddressIF address) {
0979: if (oldAddresses == null) {
0980: oldAddresses = new ArrayList();
0981: }
0982: oldAddresses.add(address);
0983: }
0984:
0985: public AddressIF getAddress() {
0986: return address;
0987: }
0988:
0989: public void setAddress(AddressIF address) {
0990: this .address = address;
0991: }
0992:
0993: public String getName() {
0994: return name;
0995: }
0996:
0997: public void setName(String name) {
0998: this .name = name;
0999: }
1000:
1001: public List getOldAddresses() {
1002: return oldAddresses;
1003: }
1004:
1005: public void setOldAddresses(List oldAddresses) {
1006: this .oldAddresses = oldAddresses;
1007: }
1008:
1009: public void addCarrel(Address address) {
1010: if (carrels == null) {
1011: carrels = new ArrayList();
1012: }
1013: carrels.add(address);
1014: }
1015:
1016: public List getCarrels() {
1017: return carrels;
1018: }
1019:
1020: public void setCarrels(List carrels) {
1021: this .carrels = carrels;
1022: }
1023:
1024: public boolean equals(Object obj) {
1025: if (!(obj instanceof Employee)) {
1026: return false;
1027: }
1028: Employee em = (Employee) obj;
1029: return new EqualsBuilder().append(getId(), em.getId())
1030: .append(getId_2(), em.getId_2()).append(getName(),
1031: em.getName())
1032: // avoid endless loop with Address
1033: .append(
1034: (getAddress() != null ? getAddress()
1035: .getId() : null),
1036: (em.getAddress() != null ? em.getAddress()
1037: .getId() : null)).append(
1038: getOldAddresses(), em.getOldAddresses())
1039: .append(getCarrels(), em.getCarrels()).isEquals();
1040: }
1041:
1042: public String toString() {
1043: return ToStringBuilder.reflectionToString(this ,
1044: ToStringStyle.MULTI_LINE_STYLE, false,
1045: Employee.class);
1046: }
1047: }
1048:
1049: public static class Address implements AddressIF {
1050: private Integer id;
1051: private String street;
1052: private List employees = new ArrayList();
1053: private Integer fkEmployee1;
1054: private Long fkEmployee2;
1055:
1056: public Address() {
1057: }
1058:
1059: public Address(String street) {
1060: this .street = street;
1061: }
1062:
1063: public boolean equals(Object obj) {
1064: if (!(obj instanceof AddressIF)) {
1065: return false;
1066: }
1067: AddressIF other = (AddressIF) obj;
1068: return new EqualsBuilder().append(getId(), other.getId())
1069: .append(getStreet(), other.getStreet()).append(
1070: getEmployees(), other.getEmployees())
1071: .append(getFkEmployee1(), other.getFkEmployee1())
1072: .append(getFkEmployee2(), other.getFkEmployee2())
1073: .isEquals();
1074: }
1075:
1076: public Integer getId() {
1077: return id;
1078: }
1079:
1080: public void setId(Integer id) {
1081: this .id = id;
1082: }
1083:
1084: public String getStreet() {
1085: return street;
1086: }
1087:
1088: public void setStreet(String street) {
1089: this .street = street;
1090: }
1091:
1092: public Integer getFkEmployee1() {
1093: return fkEmployee1;
1094: }
1095:
1096: public void setFkEmployee1(Integer fkEmployee1) {
1097: this .fkEmployee1 = fkEmployee1;
1098: }
1099:
1100: public Long getFkEmployee2() {
1101: return fkEmployee2;
1102: }
1103:
1104: public void setFkEmployee2(Long fkEmployee2) {
1105: this .fkEmployee2 = fkEmployee2;
1106: }
1107:
1108: public void addEmployee(Employee emp) {
1109: if (employees == null) {
1110: employees = new ArrayList();
1111: }
1112: employees.add(emp);
1113: }
1114:
1115: public List getEmployees() {
1116: return employees;
1117: }
1118:
1119: public void setEmployees(List employees) {
1120: this .employees = employees;
1121: }
1122: }
1123:
1124: public static interface AddressIF {
1125: public Integer getId();
1126:
1127: public void setId(Integer id);
1128:
1129: public String getStreet();
1130:
1131: public void setStreet(String street);
1132:
1133: public List getEmployees();
1134:
1135: public void setEmployees(List employees);
1136:
1137: public Integer getFkEmployee1();
1138:
1139: public void setFkEmployee1(Integer id);
1140:
1141: public Long getFkEmployee2();
1142:
1143: public void setFkEmployee2(Long id);
1144: }
1145: }
|