Search in sources :

Example 1 with Meeting

use of org.jpox.samples.models.voting.Meeting 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 2 with Meeting

use of org.jpox.samples.models.voting.Meeting 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 3 with Meeting

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

the class DatastoreIdPersistenceTest method testInsert.

public void testInsert() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Meeting m1 = new Meeting(1, "First Meeting");
            Meeting m2 = new Meeting(2, "Annual General Meeting");
            pm.makePersistent(m1);
            pm.makePersistent(m2);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Meeting.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Meeting(org.jpox.samples.models.voting.Meeting)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)3 Transaction (javax.jdo.Transaction)3 Meeting (org.jpox.samples.models.voting.Meeting)3 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)2 Topic (org.jpox.samples.models.voting.Topic)2 Vote (org.jpox.samples.models.voting.Vote)2 Category (org.jpox.samples.models.voting.Category)1