use of org.jpox.samples.one_many.unidir.Computer in project tests by datanucleus.
the class ApplicationIdPersistenceTest method testPersistOneToManyUni.
/**
* Test of persist of 1-N UNIDIR relation (PC field).
*/
public void testPersistOneToManyUni() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object officeId = null;
try {
tx.begin();
Office office = new Office("theOffice");
LaptopComputer macBook = new LaptopComputer("192.168.1.1", "MACOSX", 4, 0);
macBook.setId(1);
office.addComputer(macBook);
DesktopComputer dell = new DesktopComputer("192.168.1.2", "Windows", 2);
dell.setId(2);
office.addComputer(dell);
pm.makePersistent(office);
tx.commit();
officeId = pm.getObjectId(office);
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown persisting data " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check data
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Office theOffice = (Office) pm.getObjectById(officeId);
assertEquals("Office name retrieved is incorrect", "theOffice", theOffice.getName());
assertEquals("Office computers size retrieved is incorrect", 2, theOffice.getNumberOfComputers());
Iterator iterator = theOffice.getComputers().iterator();
while (iterator.hasNext()) {
Computer computer = (Computer) iterator.next();
if (computer instanceof LaptopComputer) {
LaptopComputer laptop = (LaptopComputer) computer;
assertEquals("Laptop id retrieved is incorrect", 1, laptop.getId());
assertEquals("Laptop ip retrieved is incorrect", "192.168.1.1", laptop.getIpAddress());
assertEquals("Laptop OperatingSystem retrieved is incorrect", "MACOSX", laptop.getOperatingSystem());
assertEquals("Laptop BatteryLife retrieved is incorrect", 4, laptop.getBatteryLife());
assertEquals("Laptop NumberOfPcmcia retrieved is incorrect", 0, laptop.getNumberOfPcmcia());
} else if (computer instanceof DesktopComputer) {
DesktopComputer desktop = (DesktopComputer) computer;
assertEquals("Desktop id retrieved is incorrect", 2, desktop.getId());
assertEquals("Desktop ip retrieved is incorrect", "192.168.1.2", desktop.getIpAddress());
assertEquals("Desktop Operating System retrieved is incorrect", "Windows", desktop.getOperatingSystem());
assertEquals("Desktop NumberOfProcessors retrieved is incorrect", 2, desktop.getNumberOfProcessors());
}
}
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown retrieving data " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out data
clean(Office.class);
clean(Computer.class);
}
}
Aggregations