Search in sources :

Example 86 with PersistenceManager

use of javax.jdo.PersistenceManager in project tests by datanucleus.

the class ApplicationIdPersistenceTest method testAccessJdoConnection.

public void testAccessJdoConnection() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p = new Person();
            p.setPersonNum(1);
            p.setGlobalNum("1");
            p.setFirstName("Bugs");
            p.setLastName("Bunny");
            Person p2 = new Person();
            p2.setPersonNum(2);
            p2.setGlobalNum("2");
            p2.setFirstName("My");
            p2.setLastName("Friend");
            pm.makePersistent(p);
            pm.makePersistent(p2);
            JDOConnection conn = pm.getDataStoreConnection();
            assertTrue("Native connection should be instanceof com.mongodb.DB!", conn.getNativeConnection() instanceof DB);
            conn.close();
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during persist with connection access", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) JDOConnection(javax.jdo.datastore.JDOConnection) Person(org.jpox.samples.models.company.Person) DB(com.mongodb.DB) JDOException(javax.jdo.JDOException) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 87 with PersistenceManager

use of javax.jdo.PersistenceManager in project tests by datanucleus.

the class DatastoreIdPersistenceTest method testInsertOneToOneBidirWithIdentity.

public void testInsertOneToOneBidirWithIdentity() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Boiler boiler = new Boiler("Vaillant", "Superwarm");
            Timer timer = new Timer("Casio", true, boiler);
            boiler.setTimer(timer);
            pm.makePersistent(timer);
            tx.commit();
        } catch (Throwable thr) {
            LOG.error("Exception during persist", thr);
            fail("Exception thrown when running test " + thr.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Timer.class);
        clean(Boiler.class);
    }
}
Also used : Boiler(org.jpox.samples.one_one.bidir.Boiler) Transaction(javax.jdo.Transaction) Timer(org.jpox.samples.one_one.bidir.Timer) PersistenceManager(javax.jdo.PersistenceManager)

Example 88 with PersistenceManager

use of javax.jdo.PersistenceManager 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 89 with PersistenceManager

use of javax.jdo.PersistenceManager 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)

Example 90 with PersistenceManager

use of javax.jdo.PersistenceManager in project tests by datanucleus.

the class EmbeddedTest method testEmbeddedCollection.

/**
 * Test for an embedded PC object.
 * @throws Exception
 */
public void testEmbeddedCollection() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        // ------------------ Check the persistence of an object with embedded collection -----------------
        Object networkId = null;
        try {
            tx.begin();
            Network network = new Network("Local Area Net");
            Device device1 = new Device("Printer", "192.168.0.2", "HP LaserJet", network);
            Device device2 = new Device("Audio Server", "192.168.0.3", "SLiMP3", network);
            Device device3 = new Device("Mail Server", "192.168.0.4", "Office IMAP", network);
            network.addDevice(device1);
            network.addDevice(device2);
            network.addDevice(device3);
            pm.makePersistent(network);
            // 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 devices is not correct", 3, network.getNumberOfDevices());
            tx.commit();
            networkId = pm.getObjectId(network);
        } 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 Network and the devices
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Network net = (Network) pm.getObjectById(networkId);
            // Check "contains"
            assertTrue("Network says that it doesnt contain the Audio Server but it should", net.containsDevice(new Device("Audio Server", "192.168.0.3", "SLiMP3", null)));
            assertFalse("Network says that it contains the Printer on an incorrect IP address but doesnt", net.containsDevice(new Device("Printer", "192.168.0.3", "HP LaserJet", null)));
            // Check retrieval of devices
            Device[] devices = net.getDevices();
            assertTrue("No devices retrieved from calling getDevices()!", devices != null);
            assertEquals("Number of devices retrieved from calling getDevices() is incorrect", 3, devices.length);
            Device printerDevice = null;
            Device audioDevice = null;
            Device mailDevice = null;
            for (int i = 0; i < devices.length; i++) {
                Device dev = devices[i];
                if (dev.getName().equals("Audio Server")) {
                    audioDevice = dev;
                } else if (dev.getName().equals("Printer")) {
                    printerDevice = dev;
                } else if (dev.getName().equals("Mail Server")) {
                    mailDevice = dev;
                }
            }
            assertTrue("Audio device was not returned by getDevices!", audioDevice != null);
            assertTrue("Printer device was not returned by getDevices!", printerDevice != null);
            assertTrue("Mail device was not returned by getDevices!", mailDevice != null);
            assertEquals("Audio Server has incorrect IP address", "192.168.0.3", audioDevice.getIPAddress());
            assertEquals("Printer has incorrect IP address", "192.168.0.2", printerDevice.getIPAddress());
            assertEquals("Mail Server has incorrect IP address", "192.168.0.4", mailDevice.getIPAddress());
            assertTrue("Audio device has no owner network assigned", audioDevice.getNetwork() != null);
            assertEquals("Audio device has incorrect network assigned", "Local Area Net", audioDevice.getNetwork().getName());
            // Remove the "Mail Server"
            net.removeDevice(mailDevice);
            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 mail server is removed from the datastore
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Network net = (Network) pm.getObjectById(networkId);
            // Check "contains"
            assertTrue("Network says that it contains the Mail Server but it shouldnt", !net.containsDevice(new Device("Mail Server", "192.168.0.4", "Office IMAP", null)));
            // Check retrieval of devices
            Device[] devices = net.getDevices();
            assertTrue("No devices retrieved from calling getDevices()!", devices != null);
            assertEquals("Number of devices retrieved from calling getDevices() is incorrect", 2, devices.length);
            Device printerDevice = null;
            Device audioDevice = null;
            for (int i = 0; i < devices.length; i++) {
                Device dev = devices[i];
                if (dev.getName().equals("Audio Server")) {
                    audioDevice = dev;
                } else if (dev.getName().equals("Printer")) {
                    printerDevice = dev;
                }
            }
            assertTrue("Audio device was not returned by getDevices!", audioDevice != null);
            assertTrue("Printer device was not returned by getDevices!", printerDevice != null);
            assertEquals("Audio Server has incorrect IP address", "192.168.0.3", audioDevice.getIPAddress());
            assertEquals("Printer has incorrect IP address", "192.168.0.2", printerDevice.getIPAddress());
            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 device to the retrieved Network
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Network net = (Network) pm.getObjectById(networkId);
            // Add a "DB server"
            Device dbServer = new Device("DB Server", "192.168.0.10", "MySQL 4.0", net);
            net.addDevice(dbServer);
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception in test", e);
            fail("Exception thrown while adding object to embedded container : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check that the DB server is added to the datastore
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Network net = (Network) pm.getObjectById(networkId);
            // Check retrieval of devices
            Device[] devices = net.getDevices();
            assertTrue("No devices retrieved from calling getDevices()!", devices != null);
            assertEquals("Number of devices retrieved from calling getDevices() is incorrect", 3, devices.length);
            Device dbDevice = null;
            for (int i = 0; i < devices.length; i++) {
                Device dev = devices[i];
                if (dev.getName().equals("DB Server")) {
                    dbDevice = dev;
                }
            }
            assertTrue("DB Server was not returned by getDevices!", dbDevice != null);
            assertEquals("DB Server has incorrect IP address", "192.168.0.10", dbDevice.getIPAddress());
            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 an embedded element and check that the update gets to the DB
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Network net = (Network) pm.getObjectById(networkId);
            Device[] devices = net.getDevices();
            Device audioDevice = null;
            for (int i = 0; i < devices.length; i++) {
                Device dev = devices[i];
                if (dev.getName().equals("Audio Server")) {
                    audioDevice = dev;
                    break;
                }
            }
            // Change the IP address of our printer
            audioDevice.setIPAddress("192.168.1.20");
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception in test", e);
            fail("Exception thrown while updating embedded objects stored in container : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check that the audio server has been updated in the datastore
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Network net = (Network) pm.getObjectById(networkId);
            Device[] devices = net.getDevices();
            assertTrue("No devices retrieved from calling getDevices()!", devices != null);
            assertEquals("Number of devices retrieved from calling getDevices() is incorrect", 3, devices.length);
            Device printerDevice = null;
            Device audioDevice = null;
            Device dbDevice = null;
            for (int i = 0; i < devices.length; i++) {
                Device dev = devices[i];
                if (dev.getName().equals("Audio Server")) {
                    audioDevice = dev;
                } else if (dev.getName().equals("Printer")) {
                    printerDevice = dev;
                } else if (dev.getName().equals("DB Server")) {
                    dbDevice = dev;
                }
            }
            assertTrue("Audio device was not returned by getDevices!", audioDevice != null);
            assertTrue("Printer device was not returned by getDevices!", printerDevice != null);
            assertTrue("DB device was not returned by getDevices!", dbDevice != null);
            assertEquals("Audio Server has incorrect IP address", "192.168.1.20", audioDevice.getIPAddress());
            assertEquals("Printer has incorrect IP address", "192.168.0.2", printerDevice.getIPAddress());
            assertEquals("DB Server has incorrect IP address", "192.168.0.10", dbDevice.getIPAddress());
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception in test", e);
            fail("Exception thrown while retrieving updated objects with embedded container : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out created data
        clean(Network.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Device(org.jpox.samples.embedded.Device) Network(org.jpox.samples.embedded.Network)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)1664 Transaction (javax.jdo.Transaction)1542 Query (javax.jdo.Query)671 List (java.util.List)397 JDOUserException (javax.jdo.JDOUserException)352 Collection (java.util.Collection)309 Iterator (java.util.Iterator)239 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)185 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)170 Person (org.jpox.samples.models.company.Person)146 Employee (org.jpox.samples.models.company.Employee)128 Manager (org.jpox.samples.models.company.Manager)107 HashSet (java.util.HashSet)101 ArrayList (java.util.ArrayList)85 SQLException (java.sql.SQLException)82 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)77 JDOException (javax.jdo.JDOException)74 Extent (javax.jdo.Extent)72 JDOFatalUserException (javax.jdo.JDOFatalUserException)68 JDODataStoreException (javax.jdo.JDODataStoreException)65