Search in sources :

Example 1 with LibraryUser

use of org.datanucleus.tests.embedded.LibraryUser in project tests by datanucleus.

the class EmbeddedTest method testEmbeddedCollectionWithReferenceToUnembedded.

public void testEmbeddedCollectionWithReferenceToUnembedded() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Video vid1 = new Video();
            vid1.setIdVideo(456L);
            vid1.setVideoTitle("Inception");
            Video vid2 = new Video();
            vid2.setIdVideo(789L);
            vid2.setVideoTitle("The Matrix");
            Video vid3 = new Video();
            vid3.setIdVideo(123L);
            vid3.setVideoTitle("Saving Private Ryan");
            LibraryUser user1 = new LibraryUser();
            user1.setName("Andy");
            user1.getViewedVideos().add(new ViewedVideo(vid1));
            user1.getViewedVideos().add(new ViewedVideo(vid2));
            LibraryUser user2 = new LibraryUser();
            user2.setName("Chris");
            user2.getViewedVideos().add(new ViewedVideo(vid1));
            user2.getViewedVideos().add(new ViewedVideo(vid2));
            user2.getViewedVideos().add(new ViewedVideo(vid3));
            // Persist all elements :
            pm.makePersistentAll(user1, user2);
            tx.commit();
        } catch (Throwable thr) {
            LOG.error(">> Exception in persist", thr);
            fail("Exception in persist of data : " + thr.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Query the data
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q = pm.newQuery("SELECT FROM " + LibraryUser.class.getName() + " ORDER BY name");
            List<LibraryUser> users = (List<LibraryUser>) q.execute();
            assertEquals("Number of users is wrong", 2, users.size());
            LibraryUser user1 = users.get(0);
            assertNotNull(user1);
            assertEquals("Andy", user1.getName());
            Set<ViewedVideo> vids1 = user1.getViewedVideos();
            assertNotNull(vids1);
            assertEquals(2, vids1.size());
            boolean vid1Present = false;
            boolean vid2Present = false;
            boolean vid3Present = false;
            for (ViewedVideo vid : vids1) {
                if (vid.getVideo().getIdVideo() == 456) {
                    vid1Present = true;
                } else if (vid.getVideo().getIdVideo() == 789) {
                    vid2Present = true;
                } else if (vid.getVideo().getIdVideo() == 123) {
                    vid3Present = true;
                }
            }
            assertTrue(vid1Present);
            assertTrue(vid2Present);
            assertFalse(vid3Present);
            LibraryUser user2 = users.get(1);
            assertNotNull(user2);
            assertEquals("Chris", user2.getName());
            Set<ViewedVideo> vids2 = user2.getViewedVideos();
            assertNotNull(vids2);
            assertEquals(3, vids2.size());
            vid1Present = false;
            vid2Present = false;
            vid3Present = false;
            for (ViewedVideo vid : vids2) {
                if (vid.getVideo().getIdVideo() == 456) {
                    vid1Present = true;
                } else if (vid.getVideo().getIdVideo() == 789) {
                    vid2Present = true;
                } else if (vid.getVideo().getIdVideo() == 123) {
                    vid3Present = true;
                }
            }
            assertTrue(vid1Present);
            assertTrue(vid2Present);
            assertTrue(vid3Present);
            tx.commit();
        } catch (Throwable thr) {
            LOG.error(">> Exception in query", thr);
            fail("Exception in query of data : " + thr.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        PersistenceManager pm = pmf.getPersistenceManager();
        Collection<Object> toDelete;
        Query qVideo = pm.newQuery(Video.class);
        Query qUser = pm.newQuery(LibraryUser.class);
        toDelete = new ArrayList<Object>((Collection<Object>) qVideo.execute());
        toDelete.addAll(new ArrayList<Object>((Collection<Object>) qUser.execute()));
        pm.deletePersistentAll(toDelete);
        pm.close();
    }
}
Also used : Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) ViewedVideo(org.datanucleus.tests.embedded.ViewedVideo) Transaction(javax.jdo.Transaction) LibraryUser(org.datanucleus.tests.embedded.LibraryUser) Video(org.datanucleus.tests.embedded.Video) ViewedVideo(org.datanucleus.tests.embedded.ViewedVideo) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 PersistenceManager (javax.jdo.PersistenceManager)1 Query (javax.jdo.Query)1 Transaction (javax.jdo.Transaction)1 LibraryUser (org.datanucleus.tests.embedded.LibraryUser)1 Video (org.datanucleus.tests.embedded.Video)1 ViewedVideo (org.datanucleus.tests.embedded.ViewedVideo)1