Search in sources :

Example 1 with Film

use of org.jpox.samples.embedded.Film in project tests by datanucleus.

the class EmbeddedTest method testEmbeddedMap.

/**
 * Test for Map with embedded value object.
 * @throws Exception
 */
public void testEmbeddedMap() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        // ------------------ Check the persistence of an object with an embedded map -----------------
        Object libraryId = null;
        try {
            tx.begin();
            Film film1 = new Film("ET", "Steven Spielberg", "Extra-terrestrial nonsense");
            Film film2 = new Film("Los diarios del motociclista", "Walter Salles", "Journey of Che Guevara");
            FilmLibrary library = new FilmLibrary("Mr Blockbuster");
            library.addFilm("ET", film1);
            library.addFilm("Motorcycle Diaries", film2);
            pm.makePersistent(library);
            // Access the object containing the embedded object before commit
            // This tries to go to the DB if our object isn't marked as embedded.
            assertEquals("Number of films is not correct", 2, library.getNumberOfFilms());
            tx.commit();
            libraryId = pm.getObjectId(library);
        } catch (Exception e) {
            LOG.error(">> Exception in test", e);
            fail("Exception thrown while creating objects with embedded field(s) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        // Retrieve the Library and the films
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            FilmLibrary library = (FilmLibrary) pm.getObjectById(libraryId);
            // Check "contains"
            assertTrue("Library says that it doesnt contain the ET but it should", library.containsFilm("ET"));
            assertFalse("Library says that it contains Independence Day but doesnt", library.containsFilm("Independence Day"));
            // Check retrieval of films
            Collection films = library.getFilms();
            assertTrue("No films retrieved from calling getFilms()!", films != null);
            assertEquals("Number of films retrieved from calling getFilms() is incorrect", 2, films.size());
            Film film1 = library.getFilm("ET");
            assertTrue("ET was not returned by getFilm!", film1 != null);
            assertEquals("ET has wrong name", "ET", film1.getName());
            assertEquals("ET has wrong director", "Steven Spielberg", film1.getDirector());
            Film film2 = library.getFilm("Motorcycle Diaries");
            assertTrue("Motorcycle Diaries was not returned by getFilm!", film2 != null);
            assertEquals("Motorcycle Diaries has wrong name", "Los diarios del motociclista", film2.getName());
            assertEquals("Motorcycle Diaries has wrong director", "Walter Salles", film2.getDirector());
            // Remove "ET" since nobody wants to see it :-)
            library.removeFilm("ET");
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception in test", e);
            fail("Exception thrown while retrieving objects with embedded container : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check that the film was removed
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            FilmLibrary library = (FilmLibrary) pm.getObjectById(libraryId);
            // Check retrieval of films
            Collection films = library.getFilms();
            assertTrue("No films retrieved from calling getFilms()!", films != null);
            assertEquals("Number of films retrieved from calling getFilms() is incorrect", 1, films.size());
            Film film1 = library.getFilm("ET");
            assertTrue("ET was returned by getFilm!", film1 == null);
            Film film2 = library.getFilm("Motorcycle Diaries");
            assertTrue("Motorcycle Diaries was not returned by getFilm!", film2 != null);
            assertEquals("Motorcycle Diaries has wrong name", "Los diarios del motociclista", film2.getName());
            assertEquals("Motorcycle Diaries has wrong director", "Walter Salles", film2.getDirector());
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception in test", e);
            fail("Exception thrown while retrieving objects with embedded container : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Add a new film to the library
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            FilmLibrary library = (FilmLibrary) pm.getObjectById(libraryId);
            Film film3 = new Film("Star Wars Episode IV", "George Lucas", "War between the Jedi and the Sith");
            library.addFilm("Star Wars", film3);
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception in test", e);
            fail("Exception thrown while adding objects to embedded container : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check that the film was added
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            FilmLibrary library = (FilmLibrary) pm.getObjectById(libraryId);
            // Check retrieval of films
            Collection films = library.getFilms();
            assertTrue("No films retrieved from calling getFilms()!", films != null);
            assertEquals("Number of films retrieved from calling getFilms() is incorrect", 2, films.size());
            Film film2 = library.getFilm("Motorcycle Diaries");
            assertTrue("Motorcycle Diaries was not returned by getFilm!", film2 != null);
            assertEquals("Motorcycle Diaries has wrong name", "Los diarios del motociclista", film2.getName());
            assertEquals("Motorcycle Diaries has wrong director", "Walter Salles", film2.getDirector());
            Film film3 = library.getFilm("Star Wars");
            assertTrue("Star Wars was not returned by getFilm!", film3 != null);
            assertEquals("Star Wars has wrong name", "Star Wars Episode IV", film3.getName());
            assertEquals("Star Wars has wrong director", "George Lucas", film3.getDirector());
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception in test", e);
            fail("Exception thrown while retrieving objects with embedded container : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Update a film in the library
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            FilmLibrary library = (FilmLibrary) pm.getObjectById(libraryId);
            Film film = library.getFilm("Star Wars");
            LOG.info(">> Updating name of film StarWars state=" + JDOHelper.getObjectState(film));
            film.setName("Star Wars Episode IV : A New Hope");
            LOG.info(">> Updated name of film StarWars state=" + JDOHelper.getObjectState(film));
            tx.commit();
            LOG.info(">> Updated name of film StarWars ?");
        } catch (Exception e) {
            LOG.error(">> Exception in test", e);
            fail("Exception thrown while updating objects in embedded container : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check that the film was updated
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        FilmLibrary detachedLibrary = null;
        try {
            tx.begin();
            LOG.info(">> Retrieving films");
            pm.getFetchPlan().addGroup("film_all");
            FilmLibrary library = (FilmLibrary) pm.getObjectById(libraryId);
            // Check retrieval of films
            Collection films = library.getFilms();
            assertTrue("No films retrieved from calling getFilms()!", films != null);
            assertEquals("Number of films retrieved from calling getFilms() is incorrect", 2, films.size());
            LOG.info(">> Correctly retrieved 2 films");
            Film film3 = library.getFilm("Star Wars");
            assertTrue("Star Wars was not returned by getFilm!", film3 != null);
            assertEquals("Star Wars has wrong name", "Star Wars Episode IV : A New Hope", film3.getName());
            assertEquals("Star Wars has wrong director", "George Lucas", film3.getDirector());
            detachedLibrary = (FilmLibrary) pm.detachCopy(library);
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception in test", e);
            fail("Exception thrown while retrieving objects with embedded container : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Update the detached object, adding a new film
        detachedLibrary.addFilm("Kamchatka", new Film("Kamchatka", "Marcelo Py�eiro", "Darkest part of Argentinian history told through the eyeys of a child"));
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            FilmLibrary library = (FilmLibrary) pm.makePersistent(detachedLibrary);
            Collection films = library.getFilms();
            assertTrue("No films retrieved from calling getFilms() on attached object!", films != null);
            assertEquals("Number of films retrieved from calling getFilms() on attached object is incorrect", 3, films.size());
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception in test", e);
            fail("Exception thrown while attaching objects with embedded container : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check that the film was attached
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            FilmLibrary library = (FilmLibrary) pm.getObjectById(libraryId);
            // Check retrieval of films
            Collection films = library.getFilms();
            assertTrue("No films retrieved from calling getFilms()!", films != null);
            assertEquals("Number of films retrieved from calling getFilms() is incorrect", 3, films.size());
            Film film3 = library.getFilm("Kamchatka");
            assertTrue("Kamchatka was not returned by getFilm!", film3 != null);
            assertEquals("Kamchatka has wrong name", "Kamchatka", film3.getName());
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception in test", e);
            fail("Exception thrown while retrieving objects with embedded container : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(FilmLibrary.class);
    }
}
Also used : FilmLibrary(org.jpox.samples.embedded.FilmLibrary) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Film(org.jpox.samples.embedded.Film) Collection(java.util.Collection)

Example 2 with Film

use of org.jpox.samples.embedded.Film in project tests by datanucleus.

the class EmbeddedContainerTest method testEmbeddedMap.

/**
 * Test for Map with embedded value object.
 * @throws Exception
 */
public void testEmbeddedMap() throws Exception {
    if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_MAP)) {
        return;
    }
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        // ------------------ Check the persistence of an object with an embedded map -----------------
        Object libraryId = null;
        try {
            tx.begin();
            Film film1 = new Film("ET", "Steven Spielberg", "Extra-terrestrial nonsense");
            Film film2 = new Film("Los diarios del motociclista", "Walter Salles", "Journey of Che Guevara");
            FilmLibrary library = new FilmLibrary("Mr Blockbuster");
            library.addFilm("ET", film1);
            library.addFilm("Motorcycle Diaries", film2);
            pm.makePersistent(library);
            // Access the object containing the embedded object before commit
            // This tries to go to the DB if our object isn't marked as embedded.
            assertEquals("Number of films is not correct", 2, library.getNumberOfFilms());
            tx.commit();
            libraryId = pm.getObjectId(library);
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while creating objects with embedded field(s) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the Library and the films
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            FilmLibrary library = (FilmLibrary) pm.getObjectById(libraryId);
            // Check "contains"
            assertTrue("Library says that it doesnt contain the ET but it should", library.containsFilm("ET"));
            assertFalse("Library says that it contains Independence Day but doesnt", library.containsFilm("Independence Day"));
            // Check retrieval of films
            Collection films = library.getFilms();
            assertTrue("No films retrieved from calling getFilms()!", films != null);
            assertEquals("Number of films retrieved from calling getFilms() is incorrect", 2, films.size());
            Film film1 = library.getFilm("ET");
            assertTrue("ET was not returned by getFilm!", film1 != null);
            assertEquals("ET has wrong name", "ET", film1.getName());
            assertEquals("ET has wrong director", "Steven Spielberg", film1.getDirector());
            Film film2 = library.getFilm("Motorcycle Diaries");
            assertTrue("Motorcycle Diaries was not returned by getFilm!", film2 != null);
            assertEquals("Motorcycle Diaries has wrong name", "Los diarios del motociclista", film2.getName());
            assertEquals("Motorcycle Diaries has wrong director", "Walter Salles", film2.getDirector());
            // Remove "ET" since nobody wants to see it :-)
            library.removeFilm("ET");
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while retrieving objects with embedded container : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check that the film was removed
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            FilmLibrary library = (FilmLibrary) pm.getObjectById(libraryId);
            // Check retrieval of films
            Collection films = library.getFilms();
            assertTrue("No films retrieved from calling getFilms()!", films != null);
            assertEquals("Number of films retrieved from calling getFilms() is incorrect", 1, films.size());
            Film film1 = library.getFilm("ET");
            assertTrue("ET was returned by getFilm!", film1 == null);
            Film film2 = library.getFilm("Motorcycle Diaries");
            assertTrue("Motorcycle Diaries was not returned by getFilm!", film2 != null);
            assertEquals("Motorcycle Diaries has wrong name", "Los diarios del motociclista", film2.getName());
            assertEquals("Motorcycle Diaries has wrong director", "Walter Salles", film2.getDirector());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while retrieving objects with embedded container : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Add a new film to the library
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            FilmLibrary library = (FilmLibrary) pm.getObjectById(libraryId);
            Film film3 = new Film("Star Wars Episode IV", "George Lucas", "War between the Jedi and the Sith");
            library.addFilm("Star Wars", film3);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while adding objects to embedded container : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check that the film was added
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            FilmLibrary library = (FilmLibrary) pm.getObjectById(libraryId);
            // Check retrieval of films
            Collection films = library.getFilms();
            assertTrue("No films retrieved from calling getFilms()!", films != null);
            assertEquals("Number of films retrieved from calling getFilms() is incorrect", 2, films.size());
            Film film2 = library.getFilm("Motorcycle Diaries");
            assertTrue("Motorcycle Diaries was not returned by getFilm!", film2 != null);
            assertEquals("Motorcycle Diaries has wrong name", "Los diarios del motociclista", film2.getName());
            assertEquals("Motorcycle Diaries has wrong director", "Walter Salles", film2.getDirector());
            Film film3 = library.getFilm("Star Wars");
            assertTrue("Star Wars was not returned by getFilm!", film3 != null);
            assertEquals("Star Wars has wrong name", "Star Wars Episode IV", film3.getName());
            assertEquals("Star Wars has wrong director", "George Lucas", film3.getDirector());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while retrieving objects with embedded container : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Update a film in the library
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            FilmLibrary library = (FilmLibrary) pm.getObjectById(libraryId);
            Film film = library.getFilm("Star Wars");
            film.setName("Star Wars Episode IV : A New Hope");
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while updating objects in embedded container : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check that the film was updated
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        FilmLibrary detachedLibrary = null;
        try {
            tx.begin();
            pm.getFetchPlan().addGroup("film_all");
            FilmLibrary library = (FilmLibrary) pm.getObjectById(libraryId);
            // Check retrieval of films
            Collection films = library.getFilms();
            assertTrue("No films retrieved from calling getFilms()!", films != null);
            assertEquals("Number of films retrieved from calling getFilms() is incorrect", 2, films.size());
            Film film3 = library.getFilm("Star Wars");
            assertTrue("Star Wars was not returned by getFilm!", film3 != null);
            assertEquals("Star Wars has wrong name", "Star Wars Episode IV : A New Hope", film3.getName());
            assertEquals("Star Wars has wrong director", "George Lucas", film3.getDirector());
            detachedLibrary = (FilmLibrary) pm.detachCopy(library);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while retrieving objects with embedded container : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Update the detached object, adding a new film
        detachedLibrary.addFilm("Kamchatka", new Film("Kamchatka", "Marcelo Py�eiro", "Darkest part of Argentinian history told through the eyeys of a child"));
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            FilmLibrary library = (FilmLibrary) pm.makePersistent(detachedLibrary);
            Collection films = library.getFilms();
            assertTrue("No films retrieved from calling getFilms() on attached object!", films != null);
            assertEquals("Number of films retrieved from calling getFilms() on attached object is incorrect", 3, films.size());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while attaching objects with embedded container : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check that the film was attached
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            FilmLibrary library = (FilmLibrary) pm.getObjectById(libraryId);
            // Check retrieval of films
            Collection films = library.getFilms();
            assertTrue("No films retrieved from calling getFilms()!", films != null);
            assertEquals("Number of films retrieved from calling getFilms() is incorrect", 3, films.size());
            Film film3 = library.getFilm("Kamchatka");
            assertTrue("Kamchatka was not returned by getFilm!", film3 != null);
            assertEquals("Kamchatka has wrong name", "Kamchatka", film3.getName());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while retrieving objects with embedded container : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(FilmLibrary.class);
    }
}
Also used : FilmLibrary(org.jpox.samples.embedded.FilmLibrary) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Film(org.jpox.samples.embedded.Film) Collection(java.util.Collection)

Aggregations

Collection (java.util.Collection)2 PersistenceManager (javax.jdo.PersistenceManager)2 Transaction (javax.jdo.Transaction)2 Film (org.jpox.samples.embedded.Film)2 FilmLibrary (org.jpox.samples.embedded.FilmLibrary)2