Search in sources :

Example 56 with PersistenceManager

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

the class EmbeddedAsChildTest method testAttachDetach.

public void testAttachDetach() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Person daffyDuck = new Person("Daffy", "Duck", "Daffy Duck", null, null);
        Account account = new Account("dduck", "secret12");
        daffyDuck.setAccount(account);
        Address address = new Address(12345, "D-City", "D-Street");
        ContactData contactData = new ContactData(address, null);
        contactData.getPhoneNumbers().add("+49-123-456-789");
        contactData.getPhoneNumbers().add("+49-987-654-321");
        contactData.setPerson(daffyDuck);
        daffyDuck.setContactData(contactData);
        Notebook notebook = new Notebook("M-0001", "IBM", daffyDuck);
        ComputerCard isdnCard = new ComputerCard("ISDN", "Fritz");
        notebook.getCards().add(isdnCard);
        OperatingSystem os = new OperatingSystem("Windows", "Vista");
        notebook.setOperatingSystem(os);
        daffyDuck.setNotebook(notebook);
        Computer apple = new Computer("PC-0001", "Apple");
        ComputerCard graphics = new ComputerCard("Graphics", "Matrox");
        ComputerCard sound = new ComputerCard("Sound", "Creative");
        ComputerCard network = new ComputerCard("Network", "Intel");
        apple.getCards().add(graphics);
        apple.getCards().add(sound);
        apple.getCards().add(network);
        OperatingSystem macosx = new OperatingSystem("MacOSX", "10");
        apple.setOperatingSystem(macosx);
        Computer sunfire = new Computer("PC-9999", "Sun Fire");
        OperatingSystem solaris = new OperatingSystem("Solaris", "8");
        sunfire.setOperatingSystem(solaris);
        daffyDuck.getComputers().add(apple);
        daffyDuck.getComputers().add(sunfire);
        pm.makePersistent(daffyDuck);
        tx.commit();
        pm.close();
        // detach
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        Person detachedDaffyDuck = pm.detachCopy(daffyDuck);
        tx.commit();
        pm.close();
        // test values of detached object
        assertEquals("Daffy Duck", detachedDaffyDuck.getFullName());
        assertEquals("Daffy", detachedDaffyDuck.getFirstName());
        assertEquals("Duck", detachedDaffyDuck.getLastName());
        assertNotNull(detachedDaffyDuck.getAccount());
        assertEquals("dduck", detachedDaffyDuck.getAccount().getUid());
        assertEquals("secret12", detachedDaffyDuck.getAccount().getPassword());
        assertNotNull(detachedDaffyDuck.getContactData());
        assertNotNull(detachedDaffyDuck.getContactData().getAddress());
        assertEquals(12345, detachedDaffyDuck.getContactData().getAddress().getZip());
        assertEquals("D-City", detachedDaffyDuck.getContactData().getAddress().getCity());
        assertEquals("D-Street", detachedDaffyDuck.getContactData().getAddress().getStreet());
        assertNotNull(detachedDaffyDuck.getContactData().getPhoneNumbers());
        assertEquals(2, detachedDaffyDuck.getContactData().getPhoneNumbers().size());
        assertTrue(detachedDaffyDuck.getContactData().getPhoneNumbers().contains("+49-123-456-789"));
        assertTrue(detachedDaffyDuck.getContactData().getPhoneNumbers().contains("+49-987-654-321"));
        assertEquals(detachedDaffyDuck, detachedDaffyDuck.getContactData().getPerson());
        assertNotNull(detachedDaffyDuck.getNotebook());
        assertEquals("IBM", detachedDaffyDuck.getNotebook().getName());
        assertEquals("M-0001", detachedDaffyDuck.getNotebook().getSerialNumber());
        assertNotNull(detachedDaffyDuck.getNotebook().getOperatingSystem());
        assertEquals("Windows", detachedDaffyDuck.getNotebook().getOperatingSystem().getName());
        assertEquals("Vista", detachedDaffyDuck.getNotebook().getOperatingSystem().getVersion());
        assertNotNull(detachedDaffyDuck.getNotebook().getCards());
        assertEquals(1, detachedDaffyDuck.getNotebook().getCards().size());
        for (ComputerCard card : detachedDaffyDuck.getNotebook().getCards()) {
            if (card.getName().equals("ISDN")) {
                assertEquals("Fritz", card.getDescription());
            } else {
                fail("Unexpected card " + card.getName());
            }
        }
        assertNotNull(detachedDaffyDuck.getComputers());
        assertEquals(2, detachedDaffyDuck.getComputers().size());
        for (Computer computer : detachedDaffyDuck.getComputers()) {
            if (computer.getSerialNumber().equals("PC-0001")) {
                assertEquals("Apple", computer.getName());
                assertEquals(3, computer.getCards().size());
                for (ComputerCard card : computer.getCards()) {
                    if (card.getName().equals("Graphics")) {
                        assertEquals("Matrox", card.getDescription());
                    } else if (card.getName().equals("Sound")) {
                        assertEquals("Creative", card.getDescription());
                    } else if (card.getName().equals("Network")) {
                        assertEquals("Intel", card.getDescription());
                    } else {
                        fail("Unexpected card " + card.getName());
                    }
                }
                assertNotNull(computer.getOperatingSystem());
                assertEquals("MacOSX", computer.getOperatingSystem().getName());
                assertEquals("10", computer.getOperatingSystem().getVersion());
            } else if (computer.getSerialNumber().equals("PC-9999")) {
                assertEquals("Sun Fire", computer.getName());
                assertEquals(0, computer.getCards().size());
                assertNotNull(computer.getOperatingSystem());
                assertEquals("Solaris", computer.getOperatingSystem().getName());
                assertEquals("8", computer.getOperatingSystem().getVersion());
            } else {
                fail("Unexpected computer " + computer.getName());
            }
        }
        // update the detached object
        detachedDaffyDuck.getContactData().getAddress().setZip(23456);
        detachedDaffyDuck.getContactData().getAddress().setStreet("D-Street2");
        detachedDaffyDuck.getContactData().getPhoneNumbers().remove("+49-123-456-789");
        detachedDaffyDuck.getContactData().getPhoneNumbers().add("+49-000-000-000");
        detachedDaffyDuck.getAccount().setPassword("secret90");
        detachedDaffyDuck.setFirstName("Daffy2");
        detachedDaffyDuck.getNotebook().setName("Lenovo");
        detachedDaffyDuck.getNotebook().setOperatingSystem(new OperatingSystem("Ubuntu", "9.04"));
        detachedDaffyDuck.getNotebook().getCards().iterator().next().setDescription("AVM");
        detachedDaffyDuck.getNotebook().getCards().add(new ComputerCard("Modem", "56k"));
        Iterator<Computer> computerIterator = detachedDaffyDuck.getComputers().iterator();
        while (computerIterator.hasNext()) {
            Computer computer = computerIterator.next();
            if (computer.getSerialNumber().equals("PC-0001")) {
                computer.setName("Pear");
                computer.getCards().clear();
                computer.getOperatingSystem().setVersion("11");
            } else if (computer.getSerialNumber().equals("PC-9999")) {
                computerIterator.remove();
            }
        }
        Computer desktop = new Computer("PC-0002", "self-made");
        desktop.setOperatingSystem(new OperatingSystem("FreeBSD", "7.2"));
        desktop.getCards().add(new ComputerCard("SCSI", "Adaptec"));
        detachedDaffyDuck.getComputers().add(desktop);
        // attach the object
        pm = pmf.getPersistenceManager();
        pm.getFetchPlan().setGroup(FetchPlan.ALL);
        pm.getFetchPlan().setMaxFetchDepth(5);
        tx = pm.currentTransaction();
        tx.begin();
        pm.makePersistent(detachedDaffyDuck);
        tx.commit();
        pm.close();
        // verify that the modifications are persistent
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertEquals("Daffy Duck", daffyDuck.getFullName());
        assertEquals("Daffy2", daffyDuck.getFirstName());
        assertEquals("Duck", daffyDuck.getLastName());
        assertNotNull(daffyDuck.getAccount());
        assertEquals("dduck", daffyDuck.getAccount().getUid());
        assertEquals("secret90", daffyDuck.getAccount().getPassword());
        assertNotNull(daffyDuck.getContactData());
        assertNotNull(daffyDuck.getContactData().getAddress());
        assertEquals(23456, daffyDuck.getContactData().getAddress().getZip());
        assertEquals("D-City", daffyDuck.getContactData().getAddress().getCity());
        assertEquals("D-Street2", daffyDuck.getContactData().getAddress().getStreet());
        assertNotNull(daffyDuck.getContactData().getPhoneNumbers());
        assertEquals(2, daffyDuck.getContactData().getPhoneNumbers().size());
        assertTrue(daffyDuck.getContactData().getPhoneNumbers().contains("+49-000-000-000"));
        assertTrue(daffyDuck.getContactData().getPhoneNumbers().contains("+49-987-654-321"));
        assertEquals(daffyDuck, daffyDuck.getContactData().getPerson());
        assertNotNull(daffyDuck.getNotebook());
        assertEquals("Lenovo", daffyDuck.getNotebook().getName());
        assertEquals("M-0001", daffyDuck.getNotebook().getSerialNumber());
        assertNotNull(daffyDuck.getNotebook().getOperatingSystem());
        assertEquals("Ubuntu", daffyDuck.getNotebook().getOperatingSystem().getName());
        assertEquals("9.04", daffyDuck.getNotebook().getOperatingSystem().getVersion());
        assertNotNull(daffyDuck.getNotebook().getCards());
        assertEquals(2, daffyDuck.getNotebook().getCards().size());
        for (ComputerCard card : daffyDuck.getNotebook().getCards()) {
            if (card.getName().equals("ISDN")) {
                assertEquals("AVM", card.getDescription());
            } else if (card.getName().equals("Modem")) {
                assertEquals("56k", card.getDescription());
            } else {
                fail("Unexpected card " + card.getName());
            }
        }
        assertNotNull(daffyDuck.getComputers());
        assertEquals(2, daffyDuck.getComputers().size());
        for (Computer computer : daffyDuck.getComputers()) {
            if (computer.getSerialNumber().equals("PC-0001")) {
                assertEquals("Pear", computer.getName());
                assertEquals(0, computer.getCards().size());
                assertNotNull(computer.getOperatingSystem());
                assertEquals("MacOSX", computer.getOperatingSystem().getName());
                assertEquals("11", computer.getOperatingSystem().getVersion());
            } else if (computer.getSerialNumber().equals("PC-0002")) {
                assertEquals("self-made", computer.getName());
                assertEquals(1, computer.getCards().size());
                for (ComputerCard card : computer.getCards()) {
                    if (card.getName().equals("SCSI")) {
                        assertEquals("Adaptec", card.getDescription());
                    } else {
                        fail("Unexpected card " + card.getName());
                    }
                }
                assertNotNull(computer.getOperatingSystem());
                assertEquals("FreeBSD", computer.getOperatingSystem().getName());
                assertEquals("7.2", computer.getOperatingSystem().getVersion());
            } else {
                fail("Unexpected computer " + computer.getName());
            }
        }
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
    // pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 57 with PersistenceManager

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

the class EmbeddedAsChildTest method testPersistPersonWithoutRef.

public void testPersistPersonWithoutRef() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Person daffyDuck = new Person("Daffy", "Duck", "Daffy Duck", null, null);
        pm.makePersistent(daffyDuck);
        tx.commit();
        pm.close();
        // test
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertEquals("Daffy Duck", daffyDuck.getFullName());
        assertEquals("Daffy", daffyDuck.getFirstName());
        assertEquals("Duck", daffyDuck.getLastName());
        assertNull(daffyDuck.getNotebook());
        assertNotNull(daffyDuck.getComputers());
        assertEquals(0, daffyDuck.getComputers().size());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 58 with PersistenceManager

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

the class EmbeddedMixedTest method testUpdateOfHierarchicalEmbedded4.

public void testUpdateOfHierarchicalEmbedded4() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Person daffyDuck = new Person("Daffy", "Duck", "Daffy Duck", null, null);
        Notebook notebook = new Notebook("M-0001", "IBM", daffyDuck);
        ComputerCard isdnCard = new ComputerCard("ISDN", "Fritz");
        notebook.getCards().add(isdnCard);
        OperatingSystem os = new OperatingSystem("Windows", "Vista");
        notebook.setOperatingSystem(os);
        daffyDuck.setNotebook(notebook);
        pm.makePersistent(daffyDuck);
        tx.commit();
        pm.close();
        // verify
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertEquals("Daffy Duck", daffyDuck.getFullName());
        assertNotNull(daffyDuck.getNotebook());
        assertEquals("IBM", daffyDuck.getNotebook().getName());
        assertEquals("M-0001", daffyDuck.getNotebook().getSerialNumber());
        assertNotNull(daffyDuck.getNotebook().getOperatingSystem());
        assertEquals("Windows", daffyDuck.getNotebook().getOperatingSystem().getName());
        assertEquals("Vista", daffyDuck.getNotebook().getOperatingSystem().getVersion());
        assertEquals("Fritz", daffyDuck.getNotebook().getCards().iterator().next().getDescription());
        // update
        Set<ComputerCard> cards = daffyDuck.getNotebook().getCards();
        ComputerCard card = cards.iterator().next();
        card.setDescription("Hans");
        tx.commit();
        pm.close();
        // verify updated values
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertEquals(1, daffyDuck.getNotebook().getCards().size());
        assertEquals("Hans", daffyDuck.getNotebook().getCards().iterator().next().getDescription());
        // verify non-upated values
        assertEquals("IBM", daffyDuck.getNotebook().getName());
        assertEquals("M-0001", daffyDuck.getNotebook().getSerialNumber());
        assertNotNull(daffyDuck.getNotebook().getOperatingSystem());
        assertEquals("Windows", daffyDuck.getNotebook().getOperatingSystem().getName());
        assertEquals("Vista", daffyDuck.getNotebook().getOperatingSystem().getVersion());
        assertEquals(0, daffyDuck.getComputers().size());
        assertNull(daffyDuck.getAccount());
        assertNotNull(daffyDuck.getContactData());
        assertNull(daffyDuck.getContactData().getAddress());
        assertEquals(0, daffyDuck.getContactData().getPhoneNumbers().size());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 59 with PersistenceManager

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

the class EmbeddedMixedTest method testUpdateOfHierarchicalEmbedded1.

public void testUpdateOfHierarchicalEmbedded1() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Person daffyDuck = new Person("Daffy", "Duck", "Daffy Duck", null, null);
        Notebook notebook = new Notebook("M-0001", "IBM", daffyDuck);
        ComputerCard isdnCard = new ComputerCard("ISDN", "Fritz");
        notebook.getCards().add(isdnCard);
        OperatingSystem os = new OperatingSystem("Windows", "Vista");
        notebook.setOperatingSystem(os);
        daffyDuck.setNotebook(notebook);
        pm.makePersistent(daffyDuck);
        tx.commit();
        pm.close();
        // verify
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertEquals("Daffy Duck", daffyDuck.getFullName());
        assertNotNull(daffyDuck.getNotebook());
        assertEquals("IBM", daffyDuck.getNotebook().getName());
        assertEquals("M-0001", daffyDuck.getNotebook().getSerialNumber());
        assertNotNull(daffyDuck.getNotebook().getOperatingSystem());
        assertEquals("Windows", daffyDuck.getNotebook().getOperatingSystem().getName());
        assertEquals("Vista", daffyDuck.getNotebook().getOperatingSystem().getVersion());
        assertEquals(0, daffyDuck.getComputers().size());
        assertNull(daffyDuck.getAccount());
        assertNotNull(daffyDuck.getContactData());
        assertNull(daffyDuck.getContactData().getAddress());
        assertEquals(0, daffyDuck.getContactData().getPhoneNumbers().size());
        // update
        daffyDuck.getNotebook().getOperatingSystem().setVersion("XP");
        tx.commit();
        pm.close();
        // verify updated values
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertEquals("XP", daffyDuck.getNotebook().getOperatingSystem().getVersion());
        // verify non-updated values
        assertEquals("Daffy Duck", daffyDuck.getFullName());
        assertEquals("IBM", daffyDuck.getNotebook().getName());
        assertEquals("M-0001", daffyDuck.getNotebook().getSerialNumber());
        assertEquals("Windows", daffyDuck.getNotebook().getOperatingSystem().getName());
        assertEquals(0, daffyDuck.getComputers().size());
        assertNull(daffyDuck.getAccount());
        assertNotNull(daffyDuck.getContactData());
        assertNull(daffyDuck.getContactData().getAddress());
        assertEquals(0, daffyDuck.getContactData().getPhoneNumbers().size());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 60 with PersistenceManager

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

the class EmbeddedMixedTest method testUpdateEmbeddedDetached2.

public void testUpdateEmbeddedDetached2() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Person daffyDuck = new Person("Daffy", "Duck", "Daffy Duck", null, null);
        Account account = new Account("dduck", "secret12");
        daffyDuck.setAccount(account);
        Address address = new Address(12345, "D-City", "D-Street");
        ContactData contactData = new ContactData(address, null);
        contactData.getPhoneNumbers().add("+49-123-456-789");
        contactData.getPhoneNumbers().add("+49-987-654-321");
        contactData.setPerson(daffyDuck);
        daffyDuck.setContactData(contactData);
        Notebook notebook = new Notebook("M-0001", "IBM", daffyDuck);
        ComputerCard isdnCard = new ComputerCard("ISDN", "Fritz");
        notebook.getCards().add(isdnCard);
        OperatingSystem os = new OperatingSystem("Windows", "Vista");
        notebook.setOperatingSystem(os);
        daffyDuck.setNotebook(notebook);
        Computer apple = new Computer("PC-0001", "Apple");
        ComputerCard graphics = new ComputerCard("Graphics", "Matrox");
        ComputerCard sound = new ComputerCard("Sound", "Creative");
        ComputerCard network = new ComputerCard("Network", "Intel");
        apple.getCards().add(graphics);
        apple.getCards().add(sound);
        apple.getCards().add(network);
        OperatingSystem macosx = new OperatingSystem("MacOSX", "10");
        apple.setOperatingSystem(macosx);
        Computer sunfire = new Computer("PC-9999", "Sun Fire");
        OperatingSystem solaris = new OperatingSystem("Solaris", "8");
        sunfire.setOperatingSystem(solaris);
        daffyDuck.getComputers().add(apple);
        daffyDuck.getComputers().add(sunfire);
        pm.makePersistent(daffyDuck);
        tx.commit();
        pm.close();
        // detach
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        Person detachedDaffyDuck = pm.detachCopy(daffyDuck);
        tx.commit();
        pm.close();
        // update
        detachedDaffyDuck.getNotebook().getOperatingSystem().setVersion("XP");
        // attach the object
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        pm.makePersistent(detachedDaffyDuck);
        tx.commit();
        pm.close();
        // verify
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
        assertEquals("Daffy Duck", daffyDuck.getFullName());
        assertNotNull(daffyDuck.getAccount());
        assertNotNull(daffyDuck.getContactData());
        assertNotNull(daffyDuck.getContactData().getAddress());
        assertEquals(2, daffyDuck.getContactData().getPhoneNumbers().size());
        assertNotNull(daffyDuck.getNotebook());
        assertEquals("IBM", daffyDuck.getNotebook().getName());
        assertEquals("M-0001", daffyDuck.getNotebook().getSerialNumber());
        assertNotNull(daffyDuck.getNotebook().getOperatingSystem());
        assertEquals("Windows", daffyDuck.getNotebook().getOperatingSystem().getName());
        assertEquals("XP", daffyDuck.getNotebook().getOperatingSystem().getVersion());
        assertEquals(1, daffyDuck.getNotebook().getCards().size());
        assertEquals(2, daffyDuck.getComputers().size());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

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