Search in sources :

Example 66 with Extent

use of javax.jdo.Extent 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 67 with Extent

use of javax.jdo.Extent 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 68 with Extent

use of javax.jdo.Extent 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 69 with Extent

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

the class ConcurrencyTest method testBasicConcurrency.

public void testBasicConcurrency() {
    // Persist Accounts and Transfers
    PersistenceManager pm = pmf.getPersistenceManager();
    pm.setProperty(PropertyNames.PROPERTY_SERIALIZE_READ, "true");
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        pm.makePersistent(new Account("alice", 1000));
        pm.makePersistent(new Account("berta", 0));
        pm.makePersistent(new Account("charly", 0));
        pm.makePersistent(new Transfer("alice", "berta", 100));
        pm.makePersistent(new Transfer("alice", "charly", 200));
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Process the Transfers, one per Thread, and one PM per Thread
    pm = pmf.getPersistenceManager();
    pm.setProperty(PropertyNames.PROPERTY_SERIALIZE_READ, "true");
    tx = pm.currentTransaction();
    try {
        tx.begin();
        LinkedList<Thread> threads = new LinkedList<>();
        Extent ext = pm.getExtent(Transfer.class, true);
        Iterator iter = ext.iterator();
        while (iter.hasNext()) {
            Transfer t = (Transfer) iter.next();
            threads.add(startConcurrentTransfer(JDOHelper.getObjectId(t)));
        }
        tx.commit();
        while (!threads.isEmpty()) {
            Thread td = (Thread) threads.removeFirst();
            try {
                td.join();
            } catch (InterruptedException e) {
            }
        }
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // Check the results
    pm = pmf.getPersistenceManager();
    pm.setProperty(PropertyNames.PROPERTY_SERIALIZE_READ, "true");
    tx = pm.currentTransaction();
    try {
        tx.begin();
        Extent ext = pm.getExtent(Transfer.class, true);
        Iterator iter = ext.iterator();
        while (iter.hasNext()) {
            Transfer transfer = (Transfer) iter.next();
            assertTrue(transfer.isBooked());
        }
        ext = pm.getExtent(Account.class, true);
        iter = ext.iterator();
        while (iter.hasNext()) {
            Account acct = (Account) iter.next();
            String name = acct.getName();
            if ("alice".equals(name)) {
                assertEquals(700, acct.getSaldo());
            } else if ("berta".equals(name)) {
                assertEquals(100, acct.getSaldo());
            } else if ("charly".equals(name)) {
                assertEquals(200, acct.getSaldo());
            } else {
                assertFalse("unexpected account name: " + name, true);
            }
        }
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Account(org.datanucleus.samples.concurrency.Account) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Transfer(org.datanucleus.samples.concurrency.Transfer) Iterator(java.util.Iterator) LinkedList(java.util.LinkedList)

Example 70 with Extent

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

the class ViewTest method testViewOfWidgets.

public void testViewOfWidgets() throws Exception {
    if ("sqlserver".equals(vendorID)) {
        // Can't run this test on SQL Server because it doesn't allow you to GROUP BY a bit column.
        return;
    }
    try {
        LOG.info("Testing view derived from of " + StorageTester.TEST_OBJECT_COUNT + " " + Widget.class.getName() + " objects");
        tester.insertObjects(Widget.class);
        MinMaxWidgetValues trueValues = new MinMaxWidgetValues(true);
        MinMaxWidgetValues falseValues = new MinMaxWidgetValues(false);
        TestObject[] objs = tester.getObjects();
        for (int i = 0; i < objs.length; ++i) {
            Widget w = (Widget) objs[i];
            MinMaxWidgetValues tfv = w.getBooleanField() ? trueValues : falseValues;
            if (tfv.getMinByteValue() > w.getByteField()) {
                tfv.setMinByteValue(w.getByteField());
            }
            if (tfv.getMinShortValue() > w.getShortField()) {
                tfv.setMinShortValue(w.getShortField());
            }
            if (tfv.getMaxIntValue() < w.getIntField()) {
                tfv.setMaxIntValue(w.getIntField());
            }
            if (tfv.getMaxLongValue() < w.getLongField()) {
                tfv.setMaxLongValue(w.getLongField());
            }
        }
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Extent ext = pm.getExtent(MinMaxWidgetValues.class, false);
            Iterator exti = ext.iterator();
            int count = 0;
            while (exti.hasNext()) {
                MinMaxWidgetValues wv = (MinMaxWidgetValues) exti.next();
                MinMaxWidgetValues tfv;
                if (wv.getBooleanValue()) {
                    tfv = trueValues;
                    trueValues = null;
                } else {
                    tfv = falseValues;
                    falseValues = null;
                }
                StorageTester.assertFieldsEqual(tfv, wv);
                ++count;
            }
            assertEquals("Iteration over view extent returned wrong number of rows", 2, count);
            tx.commit();
            /*
                 * Negative test #1.  Ensure that an attempt to write a field
                 * throws the proper exception.
                 */
            try {
                tx.begin();
                MinMaxWidgetValues wv = (MinMaxWidgetValues) ext.iterator().next();
                wv.fillRandom();
                tx.commit();
                fail("Writing to a persistent view object succeeded");
            } catch (Exception e) {
                if (tx.isActive()) {
                    tx.rollback();
                }
            }
            /*
                 * Negative test #2.  Ensure that an attempt to make a view object
                 * persistent throws the proper exception.
                 */
            try {
                tx.begin();
                MinMaxWidgetValues wv = new MinMaxWidgetValues(true);
                pm.makePersistent(wv);
                tx.commit();
                fail("Making a view object persistent succeeded");
            } catch (Exception e) {
                if (tx.isActive()) {
                    tx.rollback();
                }
            }
            /*
                 * Negative test #3.  Ensure that an attempt to delete a view object
                 * throws the proper exception.
                 */
            try {
                tx.begin();
                MinMaxWidgetValues wv = (MinMaxWidgetValues) ext.iterator().next();
                pm.deletePersistent(wv);
                tx.commit();
                fail("Deleting a persistent view object succeeded");
            } catch (Exception e) {
                if (tx.isActive()) {
                    tx.rollback();
                }
            }
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        tester.removeObjects();
        clean(Widget.class);
        clean(MinMaxWidgetValues.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) DateWidget(org.jpox.samples.widget.DateWidget) Widget(org.jpox.samples.widget.Widget) DecimalWidget(org.jpox.samples.widget.DecimalWidget) StringWidget(org.jpox.samples.widget.StringWidget) ElementWidget(org.jpox.samples.widget.ElementWidget) FloatWidget(org.jpox.samples.widget.FloatWidget) SetWidget(org.jpox.samples.widget.SetWidget) HashSetWidget(org.jpox.samples.widget.HashSetWidget) Iterator(java.util.Iterator) JDOException(javax.jdo.JDOException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) MinMaxWidgetValues(org.jpox.samples.rdbms.views.MinMaxWidgetValues)

Aggregations

Extent (javax.jdo.Extent)72 PersistenceManager (javax.jdo.PersistenceManager)72 Transaction (javax.jdo.Transaction)70 Iterator (java.util.Iterator)62 JDOUserException (javax.jdo.JDOUserException)35 Collection (java.util.Collection)22 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)22 Query (javax.jdo.Query)15 Manager (org.jpox.samples.models.company.Manager)13 StoreManager (org.datanucleus.store.StoreManager)11 Method (java.lang.reflect.Method)10 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)10 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)10 Employee (org.jpox.samples.models.company.Employee)10 JDOException (javax.jdo.JDOException)9 SQLException (java.sql.SQLException)8 Department (org.jpox.samples.models.company.Department)8 InversePrimitive (org.datanucleus.samples.widget.InversePrimitive)7 Primitive (org.datanucleus.samples.widget.Primitive)6 List (java.util.List)5