Search in sources :

Example 1 with LogEntry

use of org.datanucleus.samples.nondurable.LogEntry in project tests by datanucleus.

the class NondurableIdTest method testDelete.

/**
 * Method to test the delete of nondurable objects.
 */
public void testDelete() {
    if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_NONDURABLE_ID)) {
        return;
    }
    try {
        // Persist some "nondurable" objects
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            List<LogEntry> entries = new ArrayList<>();
            LogEntry entry = new LogEntry(LogEntry.WARNING, "Datastore adapter not found. Falling back to default");
            entries.add(entry);
            entries.add(new LogEntry(LogEntry.ERROR, "No datastore specified"));
            entries.add(new LogEntry(LogEntry.INFO, "Object X1 persisted"));
            entries.add(new LogEntry(LogEntry.WARNING, "Object X2 persisted"));
            entries.add(new LogEntry(LogEntry.WARNING, "Object Y1 persisted"));
            entries.add(new LogEntry(LogEntry.ERROR, "Error persisting object Y2"));
            pm.makePersistentAll(entries);
            tx.commit();
        } catch (Exception e) {
            NucleusLogger.GENERAL.error(">> Exception during persist", e);
            fail("Exception during persist : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve some objects and update one
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q = pm.newQuery("SELECT FROM " + LogEntry.class.getName() + " WHERE level == 2");
            List results = (List) q.execute();
            assertEquals("Number of LogEntry objects retrieved via Query (ERROR) is incorrect", 2, results.size());
            // Delete the first one
            LogEntry entry = (LogEntry) results.iterator().next();
            Object id = JDOHelper.getObjectId(entry);
            assertNotNull("Identity of nondurable object retrieved from Query is null!", id);
            pm.deletePersistent(entry);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown while deleting nondurable object", e);
            fail("Exception thrown while deleting nondurable object : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clear out our data
        clean(LogEntry.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LogEntry(org.datanucleus.samples.nondurable.LogEntry) JDOUserException(javax.jdo.JDOUserException)

Example 2 with LogEntry

use of org.datanucleus.samples.nondurable.LogEntry in project tests by datanucleus.

the class NondurableIdTest method testRetrieveNonTransactional.

/**
 * Method to test the retrieval of nondurable objects outside of a transaction.
 */
public void testRetrieveNonTransactional() {
    if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_NONDURABLE_ID)) {
        return;
    }
    try {
        // Persist some "nondurable" objects
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            List<LogEntry> entries = new ArrayList<>();
            LogEntry entry = new LogEntry(LogEntry.WARNING, "Datastore adapter not found. Falling back to default");
            entries.add(entry);
            entries.add(new LogEntry(LogEntry.ERROR, "No datastore specified"));
            entries.add(new LogEntry(LogEntry.INFO, "Object X1 persisted"));
            entries.add(new LogEntry(LogEntry.WARNING, "Object X2 persisted"));
            entries.add(new LogEntry(LogEntry.WARNING, "Object Y1 persisted"));
            entries.add(new LogEntry(LogEntry.ERROR, "Error persisting object Y2"));
            pm.makePersistentAll(entries);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        // Retrieve some objects
        pm = pmf.getPersistenceManager();
        try {
            Query<LogEntry> q = pm.newQuery(LogEntry.class);
            List<LogEntry> entries = q.executeList();
            for (LogEntry entry : entries) {
                assertNotNull(entry);
                assertEquals(ObjectState.HOLLOW_PERSISTENT_NONTRANSACTIONAL, JDOHelper.getObjectState(entry));
                // Access a field to make sure it allows transition-read, since the query will have loaded this field into the object
                entry.getMessage();
            }
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clear out our data
        clean(LogEntry.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) ArrayList(java.util.ArrayList) LogEntry(org.datanucleus.samples.nondurable.LogEntry)

Example 3 with LogEntry

use of org.datanucleus.samples.nondurable.LogEntry in project tests by datanucleus.

the class NondurableIdTest method testPersist.

/**
 * Method to test the persistence of nondurable objects.
 */
public void testPersist() {
    if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_NONDURABLE_ID)) {
        return;
    }
    try {
        // Persist some "nondurable" objects
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            List<LogEntry> entries = new ArrayList<>();
            LogEntry entry = new LogEntry(LogEntry.WARNING, "Datastore adapter not found. Falling back to default");
            entries.add(entry);
            entries.add(new LogEntry(LogEntry.ERROR, "No datastore specified"));
            entries.add(new LogEntry(LogEntry.INFO, "Object X1 persisted"));
            entries.add(new LogEntry(LogEntry.WARNING, "Object X2 persisted"));
            entries.add(new LogEntry(LogEntry.WARNING, "Object Y1 persisted"));
            entries.add(new LogEntry(LogEntry.ERROR, "Error persisting object Y2"));
            pm.makePersistentAll(entries);
            tx.commit();
            Object id = pm.getObjectId(entry);
            // Try to access LogEntry in HOLLOW state
            try {
                entry.getLevel();
                fail("Attempt to access field of HOLLOW nondurable instance succeeded!");
            } catch (JDOUserException e) {
            // Expected JDO2 [5.4.4] Access of field of HOLLOW nondurable throws JDOUserException
            }
            // Try to access the (HOLLOW) object using its id
            try {
                tx.begin();
                pm.getObjectById(id);
                fail("Attempt to access hollow nondurable instance succeeded!");
                tx.commit();
            } catch (JDOUserException e) {
            // Expected : JDO2 [5.4.4] Attempt to access nondurable object with id throws JDOUserException
            }
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve some objects
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            // Try an Extent
            Extent ex = pm.getExtent(LogEntry.class);
            Iterator iter = ex.iterator();
            int noEntries = 0;
            while (iter.hasNext()) {
                Object obj = iter.next();
                Object id = JDOHelper.getObjectId(obj);
                assertNotNull("Identity of nondurable object retrieved from Extent is null!", id);
                noEntries++;
            }
            assertEquals("Number of LogEntry objects retrieved via Extent is incorrect", 6, noEntries);
            // Try a query for particular objects
            Query q = pm.newQuery("SELECT FROM " + LogEntry.class.getName() + " WHERE level == 1");
            List results = (List) q.execute();
            assertEquals("Number of LogEntry objects retrieved via Query (WARNING) is incorrect", 3, results.size());
            Iterator queryIter = results.iterator();
            while (queryIter.hasNext()) {
                Object obj = queryIter.next();
                Object id = JDOHelper.getObjectId(obj);
                assertNotNull("Identity of nondurable object retrieved from Query is null!", id);
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clear out our data
        clean(LogEntry.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) JDOUserException(javax.jdo.JDOUserException) LogEntry(org.datanucleus.samples.nondurable.LogEntry)

Example 4 with LogEntry

use of org.datanucleus.samples.nondurable.LogEntry in project tests by datanucleus.

the class NondurableIdTest method testUpdate.

/**
 * Method to test the update of nondurable objects.
 */
public void testUpdate() {
    if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_NONDURABLE_ID)) {
        return;
    }
    try {
        // Persist some "nondurable" objects
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            List<LogEntry> entries = new ArrayList<>();
            LogEntry entry = new LogEntry(LogEntry.WARNING, "Datastore adapter not found. Falling back to default");
            entries.add(entry);
            entries.add(new LogEntry(LogEntry.ERROR, "No datastore specified"));
            entries.add(new LogEntry(LogEntry.INFO, "Object X1 persisted"));
            entries.add(new LogEntry(LogEntry.WARNING, "Object X2 persisted"));
            entries.add(new LogEntry(LogEntry.WARNING, "Object Y1 persisted"));
            entries.add(new LogEntry(LogEntry.ERROR, "Error persisting object Y2"));
            pm.makePersistentAll(entries);
            tx.commit();
        } catch (Exception e) {
            NucleusLogger.GENERAL.error(">> Exception during persist", e);
            fail("Exception during persist : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve some objects and update one
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        String msgOfUpdated = null;
        try {
            tx.begin();
            Query q = pm.newQuery("SELECT FROM " + LogEntry.class.getName() + " WHERE level == 2");
            List results = (List) q.execute();
            assertEquals("Number of LogEntry objects retrieved via Query (ERROR) is incorrect", 2, results.size());
            // Update the first one
            LogEntry entry = (LogEntry) results.iterator().next();
            Object id = JDOHelper.getObjectId(entry);
            assertNotNull("Identity of nondurable object retrieved from Query is null!", id);
            msgOfUpdated = entry.getMessage();
            // Downgrade to warning
            entry.setLevel(LogEntry.WARNING);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown while updating field of nondurable object", e);
            fail("Exception thrown while updating field of nondurable object : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check result
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q = pm.newQuery("SELECT FROM " + LogEntry.class.getName() + " WHERE message == :val");
            List results = (List) q.execute(msgOfUpdated);
            assertEquals("Number of LogEntry objects retrieved via Query is incorrect", 1, results.size());
            LogEntry entry = (LogEntry) results.iterator().next();
            assertEquals("Level of updated object is incorrect", LogEntry.WARNING, entry.getLevel());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown while checking field of nondurable object", e);
            fail("Exception thrown while checking field of nondurable object : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clear out our data
        clean(LogEntry.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LogEntry(org.datanucleus.samples.nondurable.LogEntry) JDOUserException(javax.jdo.JDOUserException)

Aggregations

ArrayList (java.util.ArrayList)4 PersistenceManager (javax.jdo.PersistenceManager)4 Transaction (javax.jdo.Transaction)4 LogEntry (org.datanucleus.samples.nondurable.LogEntry)4 List (java.util.List)3 JDOUserException (javax.jdo.JDOUserException)3 Query (javax.jdo.Query)3 Iterator (java.util.Iterator)1 Extent (javax.jdo.Extent)1