Search in sources :

Example 1 with Vote

use of org.jpox.samples.models.voting.Vote in project tests by datanucleus.

the class CacheTest method testL2CacheAfterReadApplicationIdentity.

/**
 * Test for storage of an object in the L2 cache from a query or from getObjectById.
 */
public void testL2CacheAfterReadApplicationIdentity() {
    Properties userProps = new Properties();
    userProps.setProperty(PropertyNames.PROPERTY_CACHE_L1_TYPE, "weak");
    userProps.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "weak");
    PersistenceManagerFactory cachePMF = getPMF(1, userProps);
    try {
        // Create some data we can use for access
        PersistenceManager pm = cachePMF.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            DataStoreCache l2Cache = cachePMF.getDataStoreCache();
            l2Cache.pinAll(true, Vote.class);
            tx.begin();
            Vote vote1 = new Vote(1, "Vote 1");
            pm.makePersistent(vote1);
            Vote vote2 = new Vote(2, "Vote 2");
            pm.makePersistent(vote2);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Error persisting basic data necessary to run optimistic L2 test");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        Level2Cache l2Cache = ((JDODataStoreCache) cachePMF.getDataStoreCache()).getLevel2Cache();
        assertEquals("Incorrect number of pinned objects", 2, l2Cache.getNumberOfPinnedObjects());
        assertEquals("Incorrect number of unpinned objects", 0, l2Cache.getNumberOfUnpinnedObjects());
        l2Cache.evictAll();
        assertEquals("Incorrect number of pinned objects after evict", 0, l2Cache.getNumberOfPinnedObjects());
        assertEquals("Incorrect number of unpinned objects after evict", 0, l2Cache.getNumberOfUnpinnedObjects());
        // Try getObjectById
        pm = cachePMF.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Vote vote1 = pm.getObjectById(Vote.class, 1);
            Vote vote2 = pm.getObjectById(Vote.class, 2);
            assertNotNull(vote1);
            assertNotNull(vote2);
            assertEquals("Incorrect number of pinned objects after getObjectById", 2, l2Cache.getNumberOfPinnedObjects());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Error updating object that was retrieved from the L2 cache : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        assertEquals("Incorrect number of pinned objects", 2, l2Cache.getNumberOfPinnedObjects());
        assertEquals("Incorrect number of unpinned objects", 0, l2Cache.getNumberOfUnpinnedObjects());
        l2Cache.evictAll();
        assertEquals("Incorrect number of pinned objects after evict", 0, l2Cache.getNumberOfPinnedObjects());
        assertEquals("Incorrect number of unpinned objects after evict", 0, l2Cache.getNumberOfUnpinnedObjects());
        // Try Query
        pm = cachePMF.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query query = pm.newQuery(Vote.class);
            Collection votes = (Collection) query.execute();
            Iterator iter = votes.iterator();
            while (iter.hasNext()) {
                iter.next();
            }
            assertEquals("Incorrect number of pinned objects after Query", 2, l2Cache.getNumberOfPinnedObjects());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Error retrieving object from the L2 cache : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(cachePMF, Vote.class);
        cachePMF.close();
    }
}
Also used : Vote(org.jpox.samples.models.voting.Vote) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Level2Cache(org.datanucleus.cache.Level2Cache) JDODataStoreCache(org.datanucleus.api.jdo.JDODataStoreCache) Iterator(java.util.Iterator) Collection(java.util.Collection) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) Properties(java.util.Properties) JDODataStoreCache(org.datanucleus.api.jdo.JDODataStoreCache) DataStoreCache(javax.jdo.datastore.DataStoreCache) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 2 with Vote

use of org.jpox.samples.models.voting.Vote in project tests by datanucleus.

the class PersistenceModelsTest method testPersistenceOfFKListFKListStructures.

/**
 * Test the persistence of 2 1-N FK Lists.
 * <pre>
 * 1 Topic - N Votes, 1 Meeting - N Votes
 * votes[0] <-> meetings[0], votes[0] <-> topics[0]
 * votes[1] <-> meetings[0], votes[1] <-> topics[0]
 * votes[2] <-> meetings[1], votes[2] <-> topics[1]
 * votes[3] <-> meetings[1], votes[3] <-> topics[1]
 * votes[4] <-> meetings[2], votes[4] <-> topics[2]
 * votes[5] <-> meetings[2], votes[5] <-> topics[2]
 * votes[6] <-> meetings[3], votes[6] <-> topics[3]
 * votes[7] <-> meetings[3], votes[7] <-> topics[3]
 * votes[8] <-> meetings[4], votes[8] <-> topics[4]
 * votes[9] <-> meetings[5], votes[9] <-> topics[4]
 * </pre>
 */
public void testPersistenceOfFKListFKListStructures() {
    try {
        Topic[] topics = new Topic[5];
        Object[] topicsIds = new Object[5];
        for (int i = 0; i < topics.length; i++) {
            topics[i] = new Topic(i, "Topic " + i);
        }
        Vote[] votes = new Vote[10];
        Object[] votesIds = new Object[10];
        for (int i = 0; i < votes.length; i++) {
            votes[i] = new Vote(i, "Vote " + i);
        }
        Meeting[] meetings = new Meeting[5];
        Object[] meetingsIds = new Object[5];
        for (int i = 0; i < meetings.length; i++) {
            meetings[i] = new Meeting(i, "Meeting " + i);
        }
        for (int i = 0, j = 0; i < votes.length; i++) {
            // Relate votes[i] to meetings[j]
            votes[i].setMeeting(meetings[j]);
            meetings[j].getVotes().add(votes[i]);
            // Relate votes[i] to topics[j]
            votes[i].setTopic(topics[j]);
            topics[j].getVoteHistory().add(votes[i]);
            if (((i - 1) % 2) == 0) {
                j++;
            }
        }
        // Persist and get the identities
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistentAll(topics);
            tx.commit();
            for (int i = 0; i < topics.length; i++) {
                topicsIds[i] = pm.getObjectId(topics[i]);
            }
            for (int i = 0; i < votes.length; i++) {
                votesIds[i] = pm.getObjectId(votes[i]);
            }
            for (int i = 0; i < meetings.length; i++) {
                meetingsIds[i] = pm.getObjectId(meetings[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check the data
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            for (int i = 0, j = 0, p = 0; i < topics.length; i++) {
                topics[i] = (Topic) pm.getObjectById(topicsIds[i], true);
                assertEquals(2, topics[i].getVoteHistory().size());
                assertEquals(votesIds[j++], JDOHelper.getObjectId(topics[i].getVoteHistory().get(0)));
                assertEquals(meetingsIds[i], JDOHelper.getObjectId(((Vote) topics[i].getVoteHistory().get(0)).getMeeting()));
                assertEquals(votesIds[j++], JDOHelper.getObjectId(topics[i].getVoteHistory().get(1)));
                assertEquals(meetingsIds[i], JDOHelper.getObjectId(((Vote) topics[i].getVoteHistory().get(1)).getMeeting()));
                assertEquals(votesIds[p], JDOHelper.getObjectId(((Vote) topics[i].getVoteHistory().get(0)).getMeeting().getVotes().get(0)));
                assertEquals(votesIds[p++], JDOHelper.getObjectId(((Vote) topics[i].getVoteHistory().get(1)).getMeeting().getVotes().get(0)));
                assertEquals(votesIds[p], JDOHelper.getObjectId(((Vote) topics[i].getVoteHistory().get(0)).getMeeting().getVotes().get(1)));
                assertEquals(votesIds[p++], JDOHelper.getObjectId(((Vote) topics[i].getVoteHistory().get(1)).getMeeting().getVotes().get(1)));
            }
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Topic.class);
        clean(Vote.class);
        clean(Meeting.class);
        clean(Category.class);
    }
}
Also used : Vote(org.jpox.samples.models.voting.Vote) Transaction(javax.jdo.Transaction) Meeting(org.jpox.samples.models.voting.Meeting) PersistenceManager(javax.jdo.PersistenceManager) Topic(org.jpox.samples.models.voting.Topic) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 3 with Vote

use of org.jpox.samples.models.voting.Vote in project tests by datanucleus.

the class PersistenceModelsTest method testPersistenceOfFKSetFKListStructures.

/**
 * Test the persistence of a 1-N FK Set, and 1-N FK List.
 * <pre>
 * 1 Topic - N Votes FK List
 * 1 Category - N Votes FK Set
 * </pre>
 */
public void testPersistenceOfFKSetFKListStructures() {
    try {
        Topic[] topics = new Topic[5];
        Object[] topicsIds = new Object[5];
        for (int i = 0; i < topics.length; i++) {
            topics[i] = new Topic(i, "Topic " + i);
        }
        Category[] categories = new Category[5];
        Object[] categoriesIds = new Object[5];
        for (int i = 0; i < categories.length; i++) {
            categories[i] = new Category(i, "Category " + i);
        }
        Vote[] votes = new Vote[10];
        Object[] votesIds = new Object[10];
        for (int i = 0; i < votes.length; i++) {
            votes[i] = new Vote(i, "Vote " + i);
        }
        Meeting[] meetings = new Meeting[5];
        Object[] meetingsIds = new Object[5];
        for (int i = 0; i < meetings.length; i++) {
            meetings[i] = new Meeting(i, "Meeting " + i);
        }
        for (int i = 0, j = 0; i < votes.length; i++) {
            votes[i].setCategory(categories[j]);
            categories[j].getVotes().add(votes[i]);
            votes[i].setTopic(topics[j]);
            topics[j].getVoteHistory().add(votes[i]);
            meetings[j].getVotes().add(votes[i]);
            if (((i - 1) % 2) == 0) {
                j++;
            }
        }
        // Persist the data and get the object identities
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistentAll(topics);
            tx.commit();
            for (int i = 0; i < topics.length; i++) {
                topicsIds[i] = pm.getObjectId(topics[i]);
            }
            for (int i = 0; i < votes.length; i++) {
                votesIds[i] = pm.getObjectId(votes[i]);
            }
            for (int i = 0; i < meetings.length; i++) {
                meetingsIds[i] = pm.getObjectId(meetings[i]);
            }
            for (int i = 0; i < categories.length; i++) {
                categoriesIds[i] = pm.getObjectId(categories[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            for (int i = 0, j = 0; i < topics.length; i++) {
                topics[i] = (Topic) pm.getObjectById(topicsIds[i], true);
                assertEquals(2, topics[i].getVoteHistory().size());
                assertEquals(votesIds[j++], JDOHelper.getObjectId(topics[i].getVoteHistory().get(0)));
                assertEquals(meetingsIds[i], JDOHelper.getObjectId(((Vote) topics[i].getVoteHistory().get(0)).getMeeting()));
                assertEquals(votesIds[j++], JDOHelper.getObjectId(topics[i].getVoteHistory().get(1)));
                assertEquals(meetingsIds[i], JDOHelper.getObjectId(((Vote) topics[i].getVoteHistory().get(1)).getMeeting()));
                assertEquals(2, ((Vote) topics[i].getVoteHistory().get(0)).getCategory().getVotes().size());
                assertEquals(2, ((Vote) topics[i].getVoteHistory().get(1)).getCategory().getVotes().size());
            }
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Topic.class);
        clean(Vote.class);
        clean(Meeting.class);
        clean(Category.class);
    }
}
Also used : Vote(org.jpox.samples.models.voting.Vote) Category(org.jpox.samples.models.voting.Category) Meeting(org.jpox.samples.models.voting.Meeting) PersistenceManager(javax.jdo.PersistenceManager) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) Transaction(javax.jdo.Transaction) Topic(org.jpox.samples.models.voting.Topic)

Example 4 with Vote

use of org.jpox.samples.models.voting.Vote in project tests by datanucleus.

the class PersistenceManagerTest method testMakeTransactionalSingleFieldId.

/**
 * Simple test of makeTransactional method with single-field-id.
 * JDO TCK only tests for user-provided application id.
 */
public void testMakeTransactionalSingleFieldId() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Vote v = new Vote(101, "Liberal");
            pm.makeTransactional(v);
            tx.commit();
        } catch (JDOException jdoe) {
            LOG.error("Exception in test", jdoe);
            fail("Exception thrown during makeTransactional : " + jdoe.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Vote.class);
    }
}
Also used : Vote(org.jpox.samples.models.voting.Vote) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDOException(javax.jdo.JDOException)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)4 Transaction (javax.jdo.Transaction)4 Vote (org.jpox.samples.models.voting.Vote)4 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)3 Meeting (org.jpox.samples.models.voting.Meeting)2 Topic (org.jpox.samples.models.voting.Topic)2 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 Properties (java.util.Properties)1 JDOException (javax.jdo.JDOException)1 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)1 Query (javax.jdo.Query)1 DataStoreCache (javax.jdo.datastore.DataStoreCache)1 JDODataStoreCache (org.datanucleus.api.jdo.JDODataStoreCache)1 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)1 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)1 Level2Cache (org.datanucleus.cache.Level2Cache)1 Category (org.jpox.samples.models.voting.Category)1