Search in sources :

Example 56 with PersistenceManagerFactory

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

the class SchemaTest method testFixedDatastore.

/**
 * Test of the fixed datastore facility.
 * Should prevent all attempts to change tables in the datastore, yet allow insert/delete of rows.
 */
public void testFixedDatastore() {
    try {
        // Create the necessary table and create a few objects
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e = new Employee(123, "Barney", "Rubble", "barney.rubble@warnerbros.com", (float) 123.45, "1245C");
            pm.makePersistent(e);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Create a PMF for our read-only schema
        Properties userProps = new Properties();
        userProps.setProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES, "false");
        PersistenceManagerFactory pmf2 = getPMF(1, userProps);
        assertFalse("The PMF should have had AutoCreate property as false, yet hasn't", getConfigurationForPMF(pmf2).getBooleanProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES));
        PersistenceManager pm2 = pmf2.getPersistenceManager();
        // a). Try makePersistent
        Transaction tx2 = pm2.currentTransaction();
        try {
            tx2.begin();
            Employee e = new Employee(123, "Barney", "Rubble", "barney.rubble@warnerbros.com", (float) 123.45, "1245D");
            pm2.makePersistent(e);
            tx2.commit();
        } catch (Exception e) {
            assertTrue("Should not have thrown an exception when trying makePersistent on fixed datastore", false);
            LOG.error(e);
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
        }
        // b). Try deletePersistent
        tx2 = pm2.currentTransaction();
        try {
            tx2.begin();
            Extent ex = pm2.getExtent(Employee.class, true);
            Iterator iter = ex.iterator();
            while (iter.hasNext()) {
                Employee e = (Employee) iter.next();
                pm2.deletePersistent(e);
            }
            tx2.commit();
        } catch (Exception e) {
            assertTrue("Should not have thrown an exception when trying deletePersistent on fixed datastore", false);
            LOG.error(e);
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
        }
        // c). Try update
        tx2 = pm2.currentTransaction();
        try {
            tx2.begin();
            Extent ex = pm2.getExtent(Employee.class, true);
            Iterator iter = ex.iterator();
            while (iter.hasNext()) {
                Employee e = (Employee) iter.next();
                e.setAge(21);
            }
            tx2.commit();
        } catch (Exception e) {
            assertTrue("Should not have thrown an exception when modifying an object on fixed datastore", false);
            LOG.error(e);
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
        }
        // d). Try query
        tx2 = pm2.currentTransaction();
        try {
            tx2.begin();
            Query q = pm2.newQuery(Employee.class);
            Collection results = (Collection) q.execute();
            Iterator resultsIter = results.iterator();
            while (resultsIter.hasNext()) {
                resultsIter.next();
            }
            tx2.commit();
        } catch (Exception e) {
            assertTrue("Should have been able to access objects on a fixed datastore", false);
            LOG.error(e);
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
        }
        pm2.close();
        pmf2.close();
    } finally {
        clean(Employee.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) Collection(java.util.Collection) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) JDOUserException(javax.jdo.JDOUserException) JDOFatalUserException(javax.jdo.JDOFatalUserException) SQLException(java.sql.SQLException) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 57 with PersistenceManagerFactory

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

the class AttachDetachReplicateTest method testSetDetachedObjectOnFieldInPCNewObject.

/**
 * Test checks if we can detach a graph of objects where there are
 * persistent and/or detached objects inside this graph
 *
 * Test adding a detached object to a PC_NEW object graph (after it has been made persistent)
 * TODO Change the sample to be Company
 */
@TransactionMode(PESSIMISTIC)
public void testSetDetachedObjectOnFieldInPCNewObject() {
    PersistenceManagerFactory pmf2 = getPersistenceManagerFactory2();
    try {
        PersistenceManager pm = null;
        Transaction tx = null;
        Master detachedMaster = null;
        try {
            pm = pmf.getPersistenceManager();
            tx = pm.currentTransaction();
            tx.begin();
            Master master = new Master();
            master.setId("Master3");
            pm.makePersistent(master);
            pm.getFetchPlan().setMaxFetchDepth(2);
            detachedMaster = (Master) pm.detachCopy(master);
            tx.commit();
        } catch (JDOUserException ue) {
            LOG.error(ue);
            fail("Exception thrown while performing test : " + ue.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Associate with the detached master in a different datastore
        try {
            pm = pmf2.getPersistenceManager();
            tx = pm.currentTransaction();
            tx.begin();
            tx.setRetainValues(true);
            Detail detail = new Detail();
            detail.setId("Detail3");
            pm.makePersistent(detail);
            OtherDetail otherDetail = new OtherDetail();
            otherDetail.setId("OtherDetail3");
            pm.makePersistent(otherDetail);
            assertTrue(JDOHelper.isDetached(detachedMaster));
            // set a detached object to a field in a PC_NEW instance
            detail.setMaster(detachedMaster);
            detachedMaster.addDetail(detail);
            otherDetail.setMaster(detachedMaster);
            detachedMaster.addOtherDetail(otherDetail);
            tx.commit();
            assertFalse("detail object is still detached, but should have been attached", JDOHelper.isDetached(detail));
            assertNotNull("detail object has master which is null!", detail.getMaster());
            assertFalse("detached has master that has not been attached", JDOHelper.isDetached(detail.getMaster()));
            assertTrue("detail object has master but number of other details is 0!", detail.getMaster().getOtherDetails().size() > 0);
            assertFalse("detail object has master that has otherdetail that is detached still!", JDOHelper.isDetached(detail.getMaster().getOtherDetails().iterator().next()));
            assertFalse("otherdetail object is still detached, but should have been attached", JDOHelper.isDetached(otherDetail));
            assertNotNull("otherdetail object has master which is null!", otherDetail.getMaster());
            assertFalse("otherdetail has master that has not been attached!", JDOHelper.isDetached(otherDetail.getMaster()));
            // Detach the detail
            tx.begin();
            pm.getFetchPlan().addGroup("all");
            pm.getFetchPlan().setMaxFetchDepth(2);
            Detail detachedDetail = (Detail) pm.detachCopy(detail);
            tx.commit();
            assertTrue(JDOHelper.isDetached(detachedDetail));
            assertTrue(JDOHelper.isDetached(detachedDetail.getMaster()));
            assertTrue(JDOHelper.isDetached(detachedDetail.getMaster().getOtherDetails().iterator().next()));
        } catch (JDOUserException ue) {
            LOG.error(ue);
            fail("Exception thrown while performing test : " + ue.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean up our data in the main DB
        clean(OtherDetail.class);
        clean(Detail.class);
        clean(Master.class);
        // Clean up data in the other DB
        PersistenceManager pm = pmf2.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // delete all Detail objects
            tx.begin();
            Extent ext = pm.getExtent(Detail.class, true);
            Iterator it = ext.iterator();
            while (it.hasNext()) {
                Object o = it.next();
                pm.deletePersistent(o);
            }
            tx.commit();
            // delete all OtherDetail objects
            tx.begin();
            ext = pm.getExtent(OtherDetail.class, true);
            it = ext.iterator();
            while (it.hasNext()) {
                Object o = it.next();
                pm.deletePersistent(o);
            }
            tx.commit();
            // delete all Master objects
            tx.begin();
            ext = pm.getExtent(Master.class, true);
            it = ext.iterator();
            while (it.hasNext()) {
                Object o = it.next();
                pm.deletePersistent(o);
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    }
}
Also used : Master(org.datanucleus.samples.models.hashsetcollection.Master) OtherDetail(org.datanucleus.samples.models.hashsetcollection.OtherDetail) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOUserException(javax.jdo.JDOUserException) OtherDetail(org.datanucleus.samples.models.hashsetcollection.OtherDetail) Detail(org.datanucleus.samples.models.hashsetcollection.Detail) TransactionMode(org.datanucleus.tests.annotations.TransactionMode)

Example 58 with PersistenceManagerFactory

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

the class AttachDetachReplicateTest method testReplicateSimple.

/**
 * Test of replication, with no relations.
 * Just detaches simple objectsm abd attaches them to a different datastore
 * and so should retain the ids.
 */
public void testReplicateSimple() {
    PersistenceManagerFactory pmf2 = getPersistenceManagerFactory2();
    try {
        PersistenceManager pm1 = pmf.getPersistenceManager();
        Product prod = null;
        Object id = null;
        // Persist in first DB
        Transaction tx = null;
        try {
            tx = pm1.currentTransaction();
            tx.begin();
            Product prod1 = new Product("1", "Cup", "A tea cup", "http://www.jpox.org", "GBP", 12.50, 0.00, 0.00, 17.5, 1);
            prod1 = (Product) pm1.makePersistent(prod1);
            // Detach it for copying
            prod = (Product) pm1.detachCopy(prod1);
            tx.commit();
            id = pm1.getObjectId(prod1);
        } catch (JDOUserException ue) {
            LOG.error(ue);
            fail("Exception thrown while creating object in first datastore : " + ue.getMessage());
        } finally {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
            pm1.close();
        }
        // Check the detached object
        if (!JDOHelper.isDetached(prod)) {
            fail("Product has not been detached!");
        }
        // Copy to other DB
        PersistenceManager pm2 = pmf2.getPersistenceManager();
        try {
            tx = pm2.currentTransaction();
            tx.begin();
            pm2.makePersistent(prod);
            tx.commit();
        } catch (JDOUserException ue) {
            LOG.error(ue);
            fail("Exception thrown while copying object into second datastore : " + ue.getMessage());
        } finally {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
        }
        // Check the persistence in the second datastore
        try {
            tx = pm2.currentTransaction();
            tx.begin();
            // Use Extent since PM may have just put object in cache.
            Extent e = pm2.getExtent(Product.class);
            Iterator iter = e.iterator();
            boolean copied = false;
            while (iter.hasNext()) {
                Product p = (Product) iter.next();
                if (pm2.getObjectId(p).equals(id)) {
                    copied = true;
                    break;
                }
            }
            assertTrue("Product was not copied to second datastore!", copied);
            tx.commit();
        } catch (JDOUserException ue) {
            LOG.error(ue);
            fail("Exception thrown while querying object in second datastore : " + ue.getMessage());
        } finally {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
            pm2.close();
        }
    } finally {
        PersistenceManagerFactory[] pmfs = new PersistenceManagerFactory[] { pmf, pmf2 };
        for (int i = 0; i < pmfs.length; ++i) {
            clean(pmf, Product.class);
        }
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) Product(org.datanucleus.samples.store.Product) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOUserException(javax.jdo.JDOUserException)

Example 59 with PersistenceManagerFactory

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

the class BeanValidationTest method testSize.

/**
 * Test for @Size annotation.
 */
public void testSize() {
    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", "Smith");
            p1.setLogin("12345678901");
            LOG.info(">> Calling persist");
            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 60 with PersistenceManagerFactory

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

the class CacheTest method testL2LoadedFields.

/**
 * Test to check the retrieval of an object from the L2 cache and the observance of its loaded fields.
 */
public void testL2LoadedFields() {
    Properties userProps = new Properties();
    userProps.setProperty(PropertyNames.PROPERTY_CACHE_L1_TYPE, "soft");
    userProps.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "weak");
    PersistenceManagerFactory cachePMF = getPMF(1, userProps);
    try {
        // Create a PM and add an object
        Object id = null;
        PersistenceManager pm = cachePMF.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p1 = new Person(102, "George", "Bush", "george.bush@whitehouse.gov");
            pm.makePersistent(p1);
            id = pm.getObjectId(p1);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Clear the L2 cache so we dont have this object
        Level2Cache l2Cache = ((JDODataStoreCache) cachePMF.getDataStoreCache()).getLevel2Cache();
        l2Cache.evictAll();
        // Pin all Person objects
        l2Cache.pinAll(Person.class, false);
        // Retrieve the object with just some fields
        PersistenceManager pm1 = cachePMF.getPersistenceManager();
        tx = pm1.currentTransaction();
        pm1.getFetchPlan().setGroup("groupA");
        try {
            // Load the Person object - will only have firstName, lastName loaded.
            // Will be added to L2 cache, so pin it
            tx.begin();
            pm1.getObjectById(id);
            // George Bush will now be pinned since all Person objects are pinned
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while retrieving object to store in L2 cache with only few fields : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        // Object should now be in L2 cache
        // Retrieve the object with all fields and check an unretrieved field
        PersistenceManager pm2 = cachePMF.getPersistenceManager();
        tx = pm2.currentTransaction();
        try {
            tx.begin();
            Person p1 = (Person) pm2.getObjectById(id);
            assertTrue("Additional field of L2 cached object hasn't been retrieved correctly (wasnt in original FetchPlan)", p1.getEmailAddress() != null);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while retrieving object from L2 cache : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        pm1.close();
        pm2.close();
    } finally {
        // Clean out created data
        clean(cachePMF, Person.class);
        cachePMF.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Level2Cache(org.datanucleus.cache.Level2Cache) JDODataStoreCache(org.datanucleus.api.jdo.JDODataStoreCache) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) Properties(java.util.Properties) Person(org.jpox.samples.models.company.Person) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

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