Search in sources :

Example 11 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.

the class AttachDetachReplicateTest method testMoveAcrossDatastores_company.

/**
 * This is a complex testcase using the classes from org.jpox.samples.models.company.
 * It stores a Manager into datastore 1. This Manager has a mapped-by-Set of
 * his employees and a join-Set of his departments. Hence, this test checks for
 * the behaviour of Sets when copied from one
 */
public void testMoveAcrossDatastores_company() {
    PersistenceManagerFactory pmf2 = getPersistenceManagerFactory2();
    try {
        Transaction tx1 = null;
        Transaction tx2 = null;
        // Create a Manager with two Departments and two Employees and persist it
        // into datastore 1.
        PersistenceManager pm1 = pmf.getPersistenceManager();
        Object managerId = null;
        try {
            tx1 = pm1.currentTransaction();
            tx1.begin();
            Manager ds1_manager = new Manager(1L, "Lucifer", "Satan", "Lucifer.Satan@microsoft.hell", 33666.99f, "jsdkhf8z23");
            Employee ds1_employee1 = new Employee(9593L, "McCreevy", "Charlie", "Charlie.McCreevy@microsoft.hell", 9948.57f, "8967bjjhg", new Integer(94));
            Employee ds1_employee2 = new Employee(8723L, "Gates", "Bill", "Bill.Gates@microsoft.hell", 11835.17f, "3894lknsd", new Integer(42));
            Department ds1_department1 = new Department("Brainwashing");
            ds1_department1.setManager(ds1_manager);
            ds1_manager.addDepartment(ds1_department1);
            // TODO Change this to be an inherited type to show up an error
            Department ds1_department2 = new Department("Torture");
            ds1_department2.setManager(ds1_manager);
            ds1_manager.addDepartment(ds1_department2);
            ds1_employee1.setManager(ds1_manager);
            ds1_manager.addSubordinate(ds1_employee1);
            ds1_employee2.setManager(ds1_manager);
            ds1_manager.addSubordinate(ds1_employee2);
            try {
                pm1.makePersistent(ds1_manager);
            } catch (Exception x) {
                LOG.error("Persisting Manager with Departments and Employees into datastore1 failed!", x);
                fail("Persisting Manager with Departments and Employees into datastore1 failed: " + x.getMessage());
            }
            tx1.commit();
            managerId = JDOHelper.getObjectId(ds1_manager);
        } finally {
            if (tx1 != null && tx1.isActive()) {
                tx1.rollback();
            }
            pm1.close();
        }
        // Detach the Manager (with FetchPlan.ALL) from datastore 1.
        Manager detached_manager = null;
        pm1 = pmf.getPersistenceManager();
        try {
            tx1 = pm1.currentTransaction();
            tx1.begin();
            pm1.getFetchPlan().setGroup(FetchPlan.ALL);
            pm1.getFetchPlan().setMaxFetchDepth(-1);
            try {
                Manager ds1_manager = (Manager) pm1.getObjectById(managerId);
                detached_manager = (Manager) pm1.detachCopy(ds1_manager);
            } catch (Exception x) {
                LOG.error("Loading and detaching Manager from datastore1 failed!", x);
                fail("Loading and detaching Manager from datastore1 failed: " + x.getMessage());
            }
            tx1.commit();
        } finally {
            if (tx1 != null && tx1.isActive()) {
                tx1.rollback();
            }
            pm1.close();
        }
        // check, whether the detached data equals the original data
        testMoveAcrossDatastores_company_check("makePersistent or detachCopy (with datastore1) has corrupted data: ", detached_manager, true);
        // put the detached manager into datastore2 using makePersistent
        PersistenceManager pm2 = pmf2.getPersistenceManager();
        try {
            tx2 = pm2.currentTransaction();
            tx2.begin();
            try {
                pm2.makePersistent(detached_manager);
            } catch (Exception x) {
                LOG.error("makePersistent failed on datastore2!", x);
                fail("makePersistent failed on datastore2: " + x.getMessage());
            }
            tx2.commit();
        } finally {
            if (tx2 != null && tx2.isActive()) {
                tx2.rollback();
            }
            pm2.close();
        }
        // load the manager from datastore2 and check whether data is still correct
        pm2 = pmf2.getPersistenceManager();
        try {
            tx2 = pm2.currentTransaction();
            tx2.begin();
            Manager ds2_manager = null;
            try {
                Extent ex = pm2.getExtent(Manager.class);
                Iterator exIter = ex.iterator();
                ds2_manager = (Manager) exIter.next();
                if (exIter.hasNext()) {
                    fail("Returned more than 1 Manager object in second datastore when should only have 1");
                }
            } catch (Exception e) {
                e.printStackTrace();
                LOG.error("Loading Manager from datastore2 failed!", e);
                fail("Loading Manager from datastore2 failed: " + e.getMessage());
            }
            testMoveAcrossDatastores_company_check("makePersistent on datastore2 has corrupted data: ", ds2_manager, false);
            tx2.commit();
        } finally {
            if (tx2 != null && tx2.isActive()) {
                tx2.rollback();
            }
            pm2.close();
        }
    } finally {
        // Clean out our data
        PersistenceManagerFactory[] pmfs = new PersistenceManagerFactory[] { pmf, pmf2 };
        for (int i = 0; i < pmfs.length; ++i) {
            PersistenceManagerFactory pmf = pmfs[i];
            PersistenceManager pm = pmf.getPersistenceManager();
            Transaction tx = pm.currentTransaction();
            tx.begin();
            for (Iterator it = pm.getExtent(Employee.class, false).iterator(); it.hasNext(); ) {
                Employee e = (Employee) it.next();
                e.setManager(null);
            }
            tx.commit();
            tx.begin();
            for (Iterator it = pm.getExtent(Department.class, false).iterator(); it.hasNext(); ) {
                Department d = (Department) it.next();
                d.setManager(null);
            }
            tx.commit();
            pm.close();
            clean(pmf, Manager.class);
            clean(pmf, Employee.class);
            clean(pmf, Department.class);
        }
    }
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) Department(org.jpox.samples.models.company.Department) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator)

Example 12 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.

the class AttachDetachReplicateTest method testReplicateApplicationIdentityWith1toN.

public void testReplicateApplicationIdentityWith1toN() {
    PersistenceManagerFactory pmf1 = null;
    PersistenceManagerFactory pmf2 = null;
    try {
        // Connect to "datastore1"
        Properties props = new Properties();
        props.setProperty(PropertyNames.PROPERTY_ATTACH_SAME_DATASTORE, "false");
        props.setProperty(PropertyNames.PROPERTY_AUTOSTART_MECHANISM, "Classes");
        props.setProperty(PropertyNames.PROPERTY_AUTOSTART_CLASSNAMES, "org.jpox.samples.one_many.unidir_2.GroupMember,org.jpox.samples.one_many.unidir_2.ExpertGroupMember," + "org.jpox.samples.one_many.unidir_2.UserGroup,org.jpox.samples.one_many.unidir_2.ModeratedUserGroup");
        pmf1 = getPMF(1, props);
        // Persist data to "datastore1"
        PersistenceManager pm1 = pmf1.getPersistenceManager();
        Transaction tx1 = pm1.currentTransaction();
        Object holderId = null;
        try {
            tx1.begin();
            ModeratedUserGroup holder = new ModeratedUserGroup(1, "HolderA", "First A");
            List<GroupMember> elements = new ArrayList<GroupMember>();
            elements.add(new ExpertGroupMember(25, "ElementB1", "First B"));
            elements.add(new ExpertGroupMember(26, "ElementB2", "Second B"));
            elements.add(new GroupMember(27, "ElementB3"));
            elements.add(new GroupMember(28, "ElementB4"));
            holder.setMembers(elements);
            pm1.makePersistent(holder);
            tx1.commit();
            holderId = JDOHelper.getObjectId(holder);
        } catch (Exception e) {
            LOG.error("Exception thrown creating data in datastore 1", e);
            e.printStackTrace();
            return;
        } finally {
            if (tx1.isActive()) {
                tx1.rollback();
            }
            pm1.close();
        }
        // Detach holder from "datastore1"
        UserGroup detachedHolder = null;
        pm1 = pmf1.getPersistenceManager();
        tx1 = pm1.currentTransaction();
        try {
            pm1.getFetchPlan().setGroups(new String[] { FetchPlan.DEFAULT, FetchPlan.ALL });
            pm1.getFetchPlan().setMaxFetchDepth(-1);
            tx1.begin();
            ModeratedUserGroup holder = (ModeratedUserGroup) pm1.getObjectById(holderId);
            detachedHolder = (ModeratedUserGroup) pm1.detachCopy(holder);
            tx1.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown detaching data from datastore 1", e);
            fail("Exception in detach with datastore 1 : " + e.getMessage());
        } finally {
            if (tx1.isActive()) {
                tx1.rollback();
            }
            pm1.close();
        }
        // Connect to "datastore2"
        pmf2 = getPMF(2, props);
        // Attach data to "datastore2"
        PersistenceManager pm2 = pmf2.getPersistenceManager();
        Transaction tx2 = pm2.currentTransaction();
        try {
            tx2.begin();
            pm2.makePersistent(detachedHolder);
            tx2.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown attaching data to datastore 2", e);
            fail("Exception in attach with datastore 2 : " + e.getMessage());
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
            pm2.close();
        }
    } catch (Exception e) {
        fail("Exception on attach to datastore 2 : " + e.getMessage());
    } finally {
        // Clean out our data
        clean(ModeratedUserGroup.class);
        clean(UserGroup.class);
        clean(ExpertGroupMember.class);
        clean(GroupMember.class);
    }
}
Also used : ModeratedUserGroup(org.jpox.samples.one_many.unidir_2.ModeratedUserGroup) ExpertGroupMember(org.jpox.samples.one_many.unidir_2.ExpertGroupMember) GroupMember(org.jpox.samples.one_many.unidir_2.GroupMember) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) ExpertGroupMember(org.jpox.samples.one_many.unidir_2.ExpertGroupMember) ArrayList(java.util.ArrayList) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) ModeratedUserGroup(org.jpox.samples.one_many.unidir_2.ModeratedUserGroup) UserGroup(org.jpox.samples.one_many.unidir_2.UserGroup)

Example 13 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.

the class BeanValidationTest method testNonPersistentNotNull.

/**
 * Test for not null annotation on a non-persistent field.
 */
public void testNonPersistentNotNull() {
    Properties userProps = new Properties();
    userProps.setProperty(PropertyNames.PROPERTY_VALIDATION_MODE, "auto");
    PersistenceManagerFactory validationPMF = getPMF(1, userProps);
    try {
        PersistenceManager pm = validationPMF.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Persist with null password
            tx.begin();
            ValidatedPerson2 p1 = new ValidatedPerson2("John", "Smith");
            pm.makePersistent(p1);
            tx.commit();
            fail("Exception should have been thrown, but persisted with null non-persistent field");
        } catch (ConstraintViolationException cve) {
            LOG.info("Exception thrown as expected : " + cve.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = validationPMF.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            ValidatedPerson2 p1 = new ValidatedPerson2("John", "Smith");
            p1.setPassword("secret");
            pm.makePersistent(p1);
            tx.commit();
        } catch (ConstraintViolationException cve) {
            LOG.error("Exception thrown", cve);
            fail("Exception thrown but shouldn't have been");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(validationPMF, ValidatedPerson2.class);
        validationPMF.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) ConstraintViolationException(javax.validation.ConstraintViolationException) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) ValidatedPerson2(org.datanucleus.samples.validation.ValidatedPerson2)

Example 14 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.

the class BeanValidationTest method testNotNull.

/**
 * Test for not null annotation.
 */
public void testNotNull() {
    Properties userProps = new Properties();
    userProps.setProperty(PropertyNames.PROPERTY_VALIDATION_MODE, "auto");
    PersistenceManagerFactory validationPMF = getPMF(1, userProps);
    try {
        PersistenceManager pm = validationPMF.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            ValidatedPerson p1 = new ValidatedPerson("John", null);
            pm.makePersistent(p1);
            tx.commit();
            fail("Should have thrown validation exception on persist but didnt");
        } catch (ConstraintViolationException cve) {
            // expected
            LOG.info("Exception correctly thrown : " + cve.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) ConstraintViolationException(javax.validation.ConstraintViolationException) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) ValidatedPerson(org.datanucleus.samples.validation.ValidatedPerson)

Example 15 with PersistenceManagerFactory

use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.

the class BeanValidationTest method testTransactionalNotNull.

/**
 * Test for not null annotation on a transactional field.
 */
public void testTransactionalNotNull() {
    Properties userProps = new Properties();
    userProps.setProperty(PropertyNames.PROPERTY_VALIDATION_MODE, "auto");
    PersistenceManagerFactory validationPMF = getPMF(1, userProps);
    try {
        PersistenceManager pm = validationPMF.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Persist with null password
            tx.begin();
            ValidatedPerson3 p1 = new ValidatedPerson3("John", "Smith");
            pm.makePersistent(p1);
            tx.commit();
            fail("Exception should have been thrown, but persisted with null transactional field");
        } catch (ConstraintViolationException cve) {
            LOG.info("Exception thrown as expected : " + cve.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = validationPMF.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            ValidatedPerson3 p1 = new ValidatedPerson3("John", "Smith");
            p1.setPassword("secret");
            pm.makePersistent(p1);
            tx.commit();
        } catch (ConstraintViolationException cve) {
            LOG.error("Exception thrown", cve);
            fail("Exception thrown but shouldn't have been");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(validationPMF, ValidatedPerson3.class);
        validationPMF.close();
    }
}
Also used : ValidatedPerson3(org.datanucleus.samples.validation.ValidatedPerson3) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) ConstraintViolationException(javax.validation.ConstraintViolationException) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties)

Aggregations

PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)67 PersistenceManager (javax.jdo.PersistenceManager)52 Transaction (javax.jdo.Transaction)44 Properties (java.util.Properties)40 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)34 Employee (org.jpox.samples.models.company.Employee)18 Query (javax.jdo.Query)15 JDOUserException (javax.jdo.JDOUserException)13 Manager (org.jpox.samples.models.company.Manager)13 Iterator (java.util.Iterator)12 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)11 Extent (javax.jdo.Extent)10 JDODataStoreCache (org.datanucleus.api.jdo.JDODataStoreCache)10 JDOFatalUserException (javax.jdo.JDOFatalUserException)9 Collection (java.util.Collection)8 JDOException (javax.jdo.JDOException)8 DataStoreCache (javax.jdo.datastore.DataStoreCache)8 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)8 Level2Cache (org.datanucleus.cache.Level2Cache)8 SQLException (java.sql.SQLException)7