Search in sources :

Example 41 with Manager

use of org.jpox.samples.models.company.Manager in project tests by datanucleus.

the class PersistenceManagerTest method createNewManager.

private Manager createNewManager(PersistenceManager pm, int dataset) throws Exception {
    boolean successful = false;
    Manager p = new Manager(dataset, FIRSTNAME[dataset], LASTNAME[dataset], EMAIL[dataset], EMP_SALARY[dataset], EMP_SERIAL[dataset]);
    // add many employees as available, excluding the manager previously created
    for (int i = 0; i < FIRSTNAME.length; i++) {
        if (i != dataset) {
            p.addSubordinate(new Employee(i, FIRSTNAME[i], LASTNAME[i], EMAIL[i], EMP_SALARY[i], EMP_SERIAL[i]));
        }
    }
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        pm.makePersistent(p);
        successful = true;
    } finally {
        if (successful)
            tx.commit();
        else
            tx.rollback();
    }
    return p;
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager)

Example 42 with Manager

use of org.jpox.samples.models.company.Manager in project tests by datanucleus.

the class PersistenceManagerTest method testFKCollectionFieldPersistenceByReachability1.

/**
 * Test that when a FK collection is persisted the element is persisted also.
 * TODO Move to reachability tests.
 */
public void testFKCollectionFieldPersistenceByReachability1() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
    try {
        Department d = new Department("Engineering");
        d.setManager(mgr);
        mgr.addDepartment(d);
        tx.begin();
        pm.makePersistent(mgr);
        tx.commit();
    } catch (Exception e) {
        LOG.error("Exception thrown when persisting FK collection using reachability", e);
        fail("Exception thrown when persisting FK collection using reachability " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // get a fresh PM to ensure that any results aren't coming from the cache
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        Extent ext = pm.getExtent(Manager.class, false);
        java.util.Iterator it = ext.iterator();
        assertTrue(it.hasNext());
        mgr = (Manager) it.next();
        Collection c = mgr.getDepartments();
        assertEquals(1, c.size());
        ext = pm.getExtent(Department.class, false);
        it = ext.iterator();
        assertTrue(it.hasNext());
        Department d = (Department) it.next();
        assertTrue(c.contains(d));
    } finally {
        if (tx.isActive())
            tx.rollback();
        pm.close();
    }
}
Also used : Department(org.jpox.samples.models.company.Department) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) TransactionNotActiveException(org.datanucleus.api.jdo.exceptions.TransactionNotActiveException) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) TransactionNotReadableException(org.datanucleus.api.jdo.exceptions.TransactionNotReadableException) SQLException(java.sql.SQLException) JDOUserCallbackException(javax.jdo.JDOUserCallbackException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) TransactionNotWritableException(org.datanucleus.api.jdo.exceptions.TransactionNotWritableException) JDOUnsupportedOptionException(javax.jdo.JDOUnsupportedOptionException)

Example 43 with Manager

use of org.jpox.samples.models.company.Manager in project tests by datanucleus.

the class PersistenceManagerTest method testNormalFCOCollectionFieldPersistence4.

/**
 * Test adding and removing elements from Collections whose members are defined as a non-PC superclass or interface
 */
public void testNormalFCOCollectionFieldPersistence4() {
    if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_SERIALISED_COLLECTION_ELEMENT)) {
        return;
    }
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
    Primitive p = new Primitive();
    setPrimitiveValues(p);
    CollectionFieldTester tester = new CollectionFieldTester();
    try {
        tester.getObjectCollection().add(mgr);
        tx.begin();
        pm.makePersistent(tester);
        tx.commit();
    } catch (Exception e) {
        LOG.error("Exception thrown in test", e);
        fail("Exception in test : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        Extent ex = pm.getExtent(CollectionFieldTester.class, true);
        java.util.Iterator it = ex.iterator();
        assertTrue(it.hasNext());
        tester = (CollectionFieldTester) it.next();
        assertEquals(1, tester.getObjectCollection().size());
        mgr = (Manager) tester.getObjectCollection().iterator().next();
        assertEquals(0, mgr.getPersonNum());
        tx.commit();
    } catch (Exception e) {
        LOG.error(">> Exception thrown in test", e);
        fail("Exception thrown in ObjectCollection test : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
            pm.close();
            fail("Failed to persist object and commit transaction");
        }
        pm.close();
    }
}
Also used : InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Primitive(org.datanucleus.samples.widget.Primitive) CollectionFieldTester(org.datanucleus.samples.widget.CollectionFieldTester) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) TransactionNotActiveException(org.datanucleus.api.jdo.exceptions.TransactionNotActiveException) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) TransactionNotReadableException(org.datanucleus.api.jdo.exceptions.TransactionNotReadableException) SQLException(java.sql.SQLException) JDOUserCallbackException(javax.jdo.JDOUserCallbackException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) TransactionNotWritableException(org.datanucleus.api.jdo.exceptions.TransactionNotWritableException) JDOUnsupportedOptionException(javax.jdo.JDOUnsupportedOptionException)

Example 44 with Manager

use of org.jpox.samples.models.company.Manager in project tests by datanucleus.

the class PersistenceManagerTest method testPersistenceOfOneToOneRelations.

/**
 * Test that persisting a PC will also persist all PCs that are referenced.
 */
public void testPersistenceOfOneToOneRelations() {
    try {
        Manager manager = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
        Employee employee = new Employee(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1], EMP_SALARY[1], EMP_SERIAL[1]);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object managerID = null;
        try {
            tx.begin();
            pm.makePersistent(employee);
            employee.setManager(manager);
            tx.commit();
            managerID = pm.getObjectId(manager);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
                pm.close();
                fail("Failed to persist object and commit transaction");
            }
            pm.close();
        }
        // Check the contents
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Extent clnManager = pm.getExtent(Manager.class, false);
            // should only have one Primitive object
            Iterator it = clnManager.iterator();
            assertTrue(it.hasNext());
            Manager m = (Manager) it.next();
            assertEquals(managerID, pm.getObjectId(m));
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager)

Example 45 with Manager

use of org.jpox.samples.models.company.Manager in project tests by datanucleus.

the class PersistenceManagerTest method tearDown.

/**
 * Invoked after each test is run
 */
public void tearDown() throws java.lang.Exception {
    super.tearDown();
    // TODO Remove all of this when each test cleans out its own data.
    Extent ext = null;
    java.util.Iterator it = null;
    PersistenceManager pm = pmf.getPersistenceManager();
    try {
        // delete all InstanceCallbackContainer objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(InstanceCallbackContainer.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            InstanceCallbackContainer owner = (InstanceCallbackContainer) it.next();
            pm.deletePersistent(owner);
        }
        pm.currentTransaction().commit();
        // delete all InstanceCallbackTester objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(InstanceCallbackTester.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            InstanceCallbackTester tester = (InstanceCallbackTester) it.next();
            // necesaary to avoid exception from jdoPreDelete() for this class only
            tester.setTransientValue("");
            pm.deletePersistent(tester);
        }
        pm.currentTransaction().commit();
        // delete all InversePrimitive objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(org.datanucleus.samples.widget.InversePrimitive.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            InversePrimitive ip = (InversePrimitive) it.next();
            ip.setTester(null);
            pm.deletePersistent(ip);
        }
        pm.currentTransaction().commit();
        // delete all CollectionFieldTester objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(CollectionFieldTester.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            CollectionFieldTester t = (CollectionFieldTester) it.next();
            if (t.getPrimitiveCollection() != null) {
                t.getPrimitiveCollection().clear();
            }
            if (t.getInversePrimitiveCollection() != null) {
                t.getInversePrimitiveCollection().clear();
            }
            pm.deletePersistent(t);
        }
        pm.currentTransaction().commit();
        // delete all Primative objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(org.datanucleus.samples.widget.Primitive.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Primitive p = (Primitive) it.next();
            pm.deletePersistent(p);
        }
        pm.currentTransaction().commit();
        // disassociate all Employees and Departments from their Managers
        pm.currentTransaction().begin();
        ext = pm.getExtent(Manager.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Manager mgr = (Manager) it.next();
            if (mgr.getSubordinates() != null) {
                mgr.getSubordinates().clear();
            }
            if (mgr.getDepartments() != null) {
                mgr.getDepartments().clear();
            }
        }
        pm.currentTransaction().commit();
        // delete all Employee objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(Employee.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Employee emp = (Employee) it.next();
            pm.deletePersistent(emp);
        }
        pm.currentTransaction().commit();
        pm.currentTransaction().begin();
        // dekete all Department objects
        ext = pm.getExtent(Department.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Department d = (Department) it.next();
            pm.deletePersistent(d);
        }
        pm.currentTransaction().commit();
        // delete all Manager objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(Manager.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Manager mgr = (Manager) it.next();
            pm.deletePersistent(mgr);
        }
        pm.currentTransaction().commit();
        // delete all Person objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(Person.class, true);
        it = ext.iterator();
        while (it.hasNext()) {
            Person person = (Person) it.next();
            pm.deletePersistent(person);
        }
        pm.currentTransaction().commit();
    } finally {
        if (pm.currentTransaction().isActive())
            pm.currentTransaction().commit();
        pm.close();
    }
}
Also used : Iterator(java.util.Iterator) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Extent(javax.jdo.Extent) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) InstanceCallbackContainer(org.datanucleus.samples.instancecallback.InstanceCallbackContainer) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) InstanceCallbackTester(org.datanucleus.samples.instancecallback.InstanceCallbackTester) CollectionFieldTester(org.datanucleus.samples.widget.CollectionFieldTester) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Primitive(org.datanucleus.samples.widget.Primitive) Department(org.jpox.samples.models.company.Department) Employee(org.jpox.samples.models.company.Employee) Person(org.jpox.samples.models.company.Person)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)106 Manager (org.jpox.samples.models.company.Manager)106 Transaction (javax.jdo.Transaction)97 Department (org.jpox.samples.models.company.Department)50 Query (javax.jdo.Query)48 Employee (org.jpox.samples.models.company.Employee)45 JDOUserException (javax.jdo.JDOUserException)41 Collection (java.util.Collection)40 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)38 StoreManager (org.datanucleus.store.StoreManager)34 InsuranceDepartment (org.jpox.samples.models.company.InsuranceDepartment)29 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)27 Iterator (java.util.Iterator)22 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)17 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)15 JDOException (javax.jdo.JDOException)14 Extent (javax.jdo.Extent)13 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)13 List (java.util.List)11 Properties (java.util.Properties)10