use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class OneOneTest method testOwnerAtReferencingSide.
/**
* Person-(1)----------------------------(1)-Address
* <ul>
* <li>The Person class has a reference to Address
* <li>In LDAP the relation is stored at the Person side (attribute seeAlso)
* </ul>
*/
public void testOwnerAtReferencingSide() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// persist
tx.begin();
Address address = new Address("D-City", "D-Street");
Person daffyDuck = new Person("Daffy", "Duck", "Daffy Duck", address, null);
pm.makePersistent(daffyDuck);
pm.makePersistent(address);
tx.commit();
pm.close();
// test fetch
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());
assertNotNull(daffyDuck.getAddress());
assertEquals("D-City", daffyDuck.getAddress().getCity());
assertEquals("D-Street", daffyDuck.getAddress().getStreet());
assertNull(daffyDuck.getComputer());
assertNotNull(daffyDuck.getAccounts());
assertTrue(daffyDuck.getAccounts().isEmpty());
// set reference to null
daffyDuck.setAddress(null);
tx.commit();
pm.close();
// test removed reference
// ensure address was deleted as it is dependent
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
address = null;
try {
pm.getObjectById(Address.class, "D-City");
fail("Object 'D-City' should not exist any more!");
} catch (JDOObjectNotFoundException e) {
// expected
}
assertNull(daffyDuck.getAddress());
assertEquals("Daffy Duck", daffyDuck.getFullName());
// set new reference
Address address2 = new Address("X-City", "X-Street");
daffyDuck.setAddress(address2);
tx.commit();
pm.close();
// test new reference
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
assertNotNull(daffyDuck.getAddress());
assertEquals("X-City", daffyDuck.getAddress().getCity());
assertEquals("X-Street", daffyDuck.getAddress().getStreet());
// set old reference again
address = new Address("D-City", "D-Street");
daffyDuck.setAddress(null);
daffyDuck.setAddress(address);
tx.commit();
pm.close();
// test old reference
// ensure second address was deleted as it is dependent
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
address2 = null;
try {
pm.getObjectById(Address.class, "X-City");
fail("Object 'X-City' should not exist any more!");
} catch (JDOObjectNotFoundException e) {
// expected
}
assertNotNull(daffyDuck.getAddress());
assertEquals("D-City", daffyDuck.getAddress().getCity());
assertEquals("D-Street", daffyDuck.getAddress().getStreet());
// delete address
address = pm.getObjectById(Address.class, "D-City");
pm.deletePersistent(address);
tx.commit();
pm.close();
// test object is deleted and reference is removed
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
address = null;
try {
pm.getObjectById(Address.class, "D-City");
fail("Object 'D-City' should not exist any more!");
} catch (JDOObjectNotFoundException e) {
// expected
}
assertNull(daffyDuck.getAddress());
// set new reference
Address address3 = new Address("Z-City", "Z-Street");
daffyDuck.setAddress(address3);
tx.commit();
pm.close();
// test new reference
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
assertNotNull(daffyDuck.getAddress());
assertEquals("Z-City", daffyDuck.getAddress().getCity());
assertEquals("Z-Street", daffyDuck.getAddress().getStreet());
// delete person
pm.deletePersistent(daffyDuck);
tx.commit();
pm.close();
// test person is deleted
// ensure dependent address is also deleted
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
daffyDuck = null;
try {
pm.getObjectById(Person.class, "Daffy Duck");
fail("Object 'Daffy Duck' should not exist any more!");
} catch (JDOObjectNotFoundException e) {
// expected
}
try {
pm.getObjectById(Address.class, "Z-City");
fail("Object 'Z-City' should not exist any more!");
} catch (JDOObjectNotFoundException e) {
// expected
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class OneOneTest method testPersistPersonWithRef.
public void testPersistPersonWithRef() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// persist using persistence-by-reachability
tx.begin();
Address address = new Address("D-City", "D-Street");
Computer computer = new Computer("PC-1234", "Daffy Duck's computer");
Person daffyDuck = new Person("Daffy", "Duck", "Daffy Duck", address, computer);
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());
assertNotNull(daffyDuck.getAddress());
assertNotNull(daffyDuck.getComputer());
assertNotNull(daffyDuck.getAccounts());
assertTrue(daffyDuck.getAccounts().isEmpty());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class EmbeddedAsChildTest method testPersistPersonWithEmbeddedAndNestedEmbedded.
public void testPersistPersonWithEmbeddedAndNestedEmbedded() {
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", "Daffy's notebook", daffyDuck);
ComputerCard notebookCard = new ComputerCard("card1", "Foo bar1");
notebook.getCards().add(notebookCard);
daffyDuck.setNotebook(notebook);
Computer apple = new Computer("PC-0001", "Apple");
Computer sunfire = new Computer("PC-9999", "Sun Fire");
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);
daffyDuck.getComputers().add(apple);
daffyDuck.getComputers().add(sunfire);
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());
notebook = daffyDuck.getNotebook();
assertNotNull(notebook);
assertEquals("M-0001", notebook.getSerialNumber());
assertEquals("Daffy's notebook", notebook.getName());
assertNotNull(notebook.getOwner());
assertNotNull(notebook.getCards());
assertEquals(1, notebook.getCards().size());
assertEquals("card1", notebook.getCards().iterator().next().getName());
assertEquals(daffyDuck, notebook.getOwner());
List<Computer> computers = daffyDuck.getComputers();
assertNotNull(computers);
assertEquals(2, computers.size());
for (Computer computer : computers) {
if (computer.getSerialNumber().equals("PC-0001")) {
assertEquals(3, computer.getCards().size());
} else if (computer.getSerialNumber().equals("PC-9999")) {
assertEquals(0, computer.getCards().size());
} else {
fail("Unexpected computer " + computer.getName());
}
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class EmbeddedAsChildTest method testUpdateEmbeddedObjectOneMany.
public void testUpdateEmbeddedObjectOneMany() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Person daffyDuck = new Person("Daffy", "Duck", "Daffy Duck", null, null);
Computer apple = new Computer("PC-0001", "Apple");
Computer sunfire = new Computer("PC-9999", "Sun Fire");
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);
daffyDuck.getComputers().add(apple);
daffyDuck.getComputers().add(sunfire);
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());
assertNotNull(daffyDuck.getComputers());
assertEquals(2, daffyDuck.getComputers().size());
ComputerCard cardToUpdate = null;
for (Computer computer : daffyDuck.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")) {
cardToUpdate = card;
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());
}
}
} else if (computer.getSerialNumber().equals("PC-9999")) {
assertEquals("Sun Fire", computer.getName());
assertEquals(0, computer.getCards().size());
} else {
fail("Unexpected computer " + computer.getName());
}
}
// update field in embedded nested object
cardToUpdate.setDescription("AMD");
tx.commit();
pm.close();
// verify updated field in embedded object
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());
assertNotNull(daffyDuck.getComputers());
assertEquals(2, daffyDuck.getComputers().size());
Computer appleToModify = null;
ComputerCard soundToDelete = null;
for (Computer computer : daffyDuck.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("AMD", card.getDescription());
} else if (card.getName().equals("Sound")) {
appleToModify = computer;
soundToDelete = card;
assertEquals("Creative", card.getDescription());
} else if (card.getName().equals("Network")) {
assertEquals("Intel", card.getDescription());
} else {
fail("Unexpected card " + card.getName());
}
}
} else if (computer.getSerialNumber().equals("PC-9999")) {
assertEquals("Sun Fire", computer.getName());
assertEquals(0, computer.getCards().size());
} else {
fail("Unexpected computer " + computer.getName());
}
}
// delete one embedded nested object
appleToModify.getCards().remove(soundToDelete);
tx.commit();
pm.close();
// verify deleted object in nested embedded object
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());
assertNotNull(daffyDuck.getComputers());
assertEquals(2, daffyDuck.getComputers().size());
Computer sunfireToModify = null;
for (Computer computer : daffyDuck.getComputers()) {
if (computer.getSerialNumber().equals("PC-0001")) {
assertEquals("Apple", computer.getName());
assertEquals(2, computer.getCards().size());
for (ComputerCard card : computer.getCards()) {
if (card.getName().equals("Graphics")) {
assertEquals("AMD", card.getDescription());
} else if (card.getName().equals("Network")) {
assertEquals("Intel", card.getDescription());
} else {
fail("Unexpected card " + card.getName());
}
}
} else if (computer.getSerialNumber().equals("PC-9999")) {
assertEquals("Sun Fire", computer.getName());
assertEquals(0, computer.getCards().size());
sunfireToModify = computer;
} else {
fail("Unexpected computer " + computer.getName());
}
}
// add one embedded nested object
ComputerCard scsi = new ComputerCard("SCSI", "Adaptec");
sunfireToModify.getCards().add(scsi);
tx.commit();
pm.close();
// verify added object in nested embedded object
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());
assertNotNull(daffyDuck.getComputers());
assertEquals(2, daffyDuck.getComputers().size());
for (Computer computer : daffyDuck.getComputers()) {
if (computer.getSerialNumber().equals("PC-0001")) {
assertEquals("Apple", computer.getName());
assertEquals(2, computer.getCards().size());
for (ComputerCard card : computer.getCards()) {
if (card.getName().equals("Graphics")) {
assertEquals("AMD", card.getDescription());
} else if (card.getName().equals("Network")) {
assertEquals("Intel", card.getDescription());
} else {
fail("Unexpected card " + card.getName());
}
}
} else if (computer.getSerialNumber().equals("PC-9999")) {
assertEquals("Sun Fire", 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());
}
}
} else {
fail("Unexpected computer " + computer.getName());
}
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class EmbeddedAsChildTest method testUpdateEmbeddedObjectOneOne.
public void testUpdateEmbeddedObjectOneOne() {
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 notebookCard = new ComputerCard("card1", "Foo bar1");
notebook.getCards().add(notebookCard);
daffyDuck.setNotebook(notebook);
OperatingSystem os = new OperatingSystem("Windows", "Vista");
notebook.setOperatingSystem(os);
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());
assertNotNull(daffyDuck.getNotebook());
assertEquals("IBM", daffyDuck.getNotebook().getName());
assertNotNull(daffyDuck.getNotebook().getOperatingSystem());
assertEquals("Vista", daffyDuck.getNotebook().getOperatingSystem().getVersion());
// update field in embedded object
daffyDuck.getNotebook().setName("Lenovo");
tx.commit();
pm.close();
// verify updated field in embedded object
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
assertNotNull(daffyDuck.getNotebook());
assertEquals("Lenovo", daffyDuck.getNotebook().getName());
assertNotNull(daffyDuck.getNotebook().getOperatingSystem());
assertEquals("Vista", daffyDuck.getNotebook().getOperatingSystem().getVersion());
// update field in embedded nested object
daffyDuck.getNotebook().getOperatingSystem().setVersion("XP");
tx.commit();
pm.close();
// verify updated field in embedded nested object
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
assertNotNull(daffyDuck.getNotebook());
assertEquals("Lenovo", daffyDuck.getNotebook().getName());
assertNotNull(daffyDuck.getNotebook().getOperatingSystem());
assertEquals("XP", daffyDuck.getNotebook().getOperatingSystem().getVersion());
// set reference to embedded nested object to null, must be deleted
daffyDuck.getNotebook().setOperatingSystem(null);
tx.commit();
pm.close();
// verify deleted
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
assertNotNull(daffyDuck.getNotebook());
assertEquals("Lenovo", daffyDuck.getNotebook().getName());
assertNull(daffyDuck.getNotebook().getOperatingSystem());
// add a new nested embedded object
OperatingSystem newOs = new OperatingSystem("Ubuntu", "9.04");
daffyDuck.getNotebook().setOperatingSystem(newOs);
tx.commit();
pm.close();
// verify added
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
assertNotNull(daffyDuck.getNotebook());
assertEquals("Lenovo", daffyDuck.getNotebook().getName());
assertNotNull(daffyDuck.getNotebook().getOperatingSystem());
assertEquals("9.04", daffyDuck.getNotebook().getOperatingSystem().getVersion());
// set reference to embedded object to null, must be deleted
daffyDuck.setNotebook(null);
tx.commit();
pm.close();
// verify deleted
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
daffyDuck = pm.getObjectById(Person.class, "Daffy Duck");
assertNull(daffyDuck.getNotebook());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
Aggregations