use of org.datanucleus.samples.annotations.embedded.Computer in project tests by datanucleus.
the class AnnotationsEmbeddedTest method testEmbeddedPCObjectNullValue.
/**
* Test for an embedded PC object, and use of null-value.
* @throws Exception
*/
public void testEmbeddedPCObjectNullValue() throws Exception {
if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_PC)) {
return;
}
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
// ------------------ Check the persistence of an object with embedded objects -----------------
Object comp_id = null;
try {
tx.begin();
Computer comp = new Computer("MacOS", new ComputerCard("NVidia", ComputerCard.AGP_CARD), null);
pm.makePersistent(comp);
tx.commit();
comp_id = pm.getObjectId(comp);
} catch (Exception e) {
LOG.error("Exception during test", e);
fail("Exception thrown while creating objects with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// -------------- Check the retrieval of objects with embedded subobjects -----------------
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Retrieve the object with both embedded subobjects
Computer comp = (Computer) pm.getObjectById(comp_id);
assertTrue("Unable to retrieve object with embedded object", comp != null);
assertTrue("Retrieved object with embedded object(s) has incorrect operating system field", comp.getOperatingSystem().equals("MacOS"));
assertTrue("Retrieved object with embedded object(s) has no graphics card", comp.getGraphicsCard() != null);
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : graphics card manufacturer is wrong", comp.getGraphicsCard().getManufacturer().equals("NVidia"));
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : graphics card type is wrong", comp.getGraphicsCard().getType() == ComputerCard.AGP_CARD);
assertTrue("Retrieved object with embedded object(s) has sound card, but shouldn't have", comp.getSoundCard() == null);
tx.commit();
} catch (Exception e) {
LOG.error("Exception during test", e);
fail("Exception thrown while fetching objects with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// ------------------- Check update of an embedded object ------------------------
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Retrieve the object with 1 embedded subobject, and one unset, and set its sound card
Computer comp = (Computer) pm.getObjectById(comp_id);
comp.setSoundCard(new ComputerCard("Turtle Beach", ComputerCard.PCI_CARD));
tx.commit();
} catch (Exception e) {
LOG.error("Exception during test", e);
fail("Exception thrown while updating objects with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Retrieve the object that has just been updated
Computer comp = (Computer) pm.getObjectById(comp_id);
assertTrue("Unable to retrieve object with embedded object(s)", comp != null);
assertTrue("Updated object with embedded object(s) has incorrect operating system field", comp.getOperatingSystem().equals("MacOS"));
assertTrue("Updated object with embedded object(s) has no graphics card", comp.getGraphicsCard() != null);
assertTrue("Updated object with embedded object(s) has incorrect embedded object : graphics card manufacturer is wrong", comp.getGraphicsCard().getManufacturer().equals("NVidia"));
assertTrue("Updated object with embedded object(s) has incorrect embedded object : graphics card type is wrong", comp.getGraphicsCard().getType() == ComputerCard.AGP_CARD);
assertTrue("Updated object with embedded object(s) has no sound card", comp.getSoundCard() != null);
assertTrue("Updated object with embedded object(s) has incorrect embedded object : sound card manufacturer is wrong", comp.getSoundCard().getManufacturer().equals("Turtle Beach"));
assertTrue("Updated object with embedded object(s) has incorrect embedded object : sound card type is wrong", comp.getSoundCard().getType() == ComputerCard.PCI_CARD);
tx.commit();
} catch (Exception e) {
LOG.error("Exception during test", e);
fail("Exception thrown while updating objects with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out created data
clean(ComputerCard.class);
clean(Computer.class);
}
}
use of org.datanucleus.samples.annotations.embedded.Computer in project tests by datanucleus.
the class AnnotationsEmbeddedTest method testEmbeddedPCObject.
/**
* Test for an embedded PC object.
* @throws Exception
*/
public void testEmbeddedPCObject() throws Exception {
if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_PC)) {
return;
}
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
// ------------------ Check the persistence of an object with embedded objects -----------------
Object comp_id = null;
try {
tx.begin();
Computer comp = new Computer("Linux", new ComputerCard("ATI", ComputerCard.AGP_CARD), new ComputerCard("Creative Labs", ComputerCard.PCI_CARD));
pm.makePersistent(comp);
// Access the object containing the embedded object before commit
// This used to try to go to the datastore at this point
comp.getGraphicsCard().toString();
tx.commit();
comp_id = pm.getObjectId(comp);
} catch (Exception e) {
LOG.error("Exception during test", e);
fail("Exception thrown while creating objects with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// -------------- Check the retrieval of objects with embedded subobjects -----------------
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Retrieve the object with both embedded subobjects
Computer comp = (Computer) pm.getObjectById(comp_id);
assertTrue("Unable to retrieve object with embedded object(s)", comp != null);
assertTrue("Retrieved object with embedded object(s) has incorrect operating system field", comp.getOperatingSystem().equals("Linux"));
assertTrue("Retrieved object with embedded object(s) has no graphics card", comp.getGraphicsCard() != null);
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : graphics card manufacturer is wrong", comp.getGraphicsCard().getManufacturer().equals("ATI"));
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : graphics card type is wrong", comp.getGraphicsCard().getType() == ComputerCard.AGP_CARD);
assertTrue("Embedded graphics card doesn't have its owner field set", comp.getGraphicsCard().getComputer() != null);
assertTrue("Embedded graphics card has its owner field set incorrectly", comp.getGraphicsCard().getComputer() == comp);
assertTrue("Retrieved object with embedded object(s) has no sound card", comp.getSoundCard() != null);
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : sound card manufacturer is wrong", comp.getSoundCard().getManufacturer().equals("Creative Labs"));
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : sound card type is wrong", comp.getSoundCard().getType() == ComputerCard.PCI_CARD);
assertTrue("Embedded sound card doesn't have its owner field set", comp.getSoundCard().getComputer() != null);
assertTrue("Embedded sound card has its owner field set incorrectly", comp.getSoundCard().getComputer() == comp);
tx.commit();
} catch (Exception e) {
LOG.error("Exception during test", e);
fail("Exception thrown while fetching objects with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// ------------------- Check update of an embedded object ------------------------
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Retrieve the object and change its sound card
Computer comp = (Computer) pm.getObjectById(comp_id);
comp.setSoundCard(new ComputerCard("Turtle Beach", ComputerCard.PCI_CARD));
tx.commit();
} catch (Exception e) {
LOG.error("Exception during test", e);
fail("Exception thrown while updating objects with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Retrieve the object that has just been updated
Computer comp = (Computer) pm.getObjectById(comp_id);
assertTrue("Unable to retrieve object with embedded object(s)", comp != null);
assertTrue("Updated object with embedded object(s) has incorrect operating system field", comp.getOperatingSystem().equals("Linux"));
assertTrue("Updated object with embedded object(s) has no graphics card", comp.getGraphicsCard() != null);
assertTrue("Updated object with embedded object(s) has incorrect embedded object : graphics card manufacturer is wrong", comp.getGraphicsCard().getManufacturer().equals("ATI"));
assertTrue("Updated object with embedded object(s) has incorrect embedded object : graphics card type is wrong", comp.getGraphicsCard().getType() == ComputerCard.AGP_CARD);
assertTrue("Updated object with embedded object(s) has no sound card", comp.getSoundCard() != null);
assertTrue("Updated object with embedded object(s) has incorrect embedded object : sound card manufacturer is wrong", comp.getSoundCard().getManufacturer().equals("Turtle Beach"));
assertTrue("Updated object with embedded object(s) has incorrect embedded object : sound card type is wrong", comp.getSoundCard().getType() == ComputerCard.PCI_CARD);
tx.commit();
} catch (Exception e) {
LOG.error("Exception during test", e);
fail("Exception thrown while updating objects with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// ------------- Check for updates in the embedded object ------------------
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Retrieve the object and update its sound card details
Computer comp = (Computer) pm.getObjectById(comp_id);
ComputerCard sound_card = comp.getSoundCard();
sound_card.setType(ComputerCard.ISA_CARD);
tx.commit();
} catch (Exception e) {
LOG.error("Exception during test", e);
fail("Exception thrown while updating embedded objects : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Retrieve the object that has just been updated
Computer comp = (Computer) pm.getObjectById(comp_id);
assertTrue("Unable to retrieve object with embedded object(s)", comp != null);
assertTrue("Updated object with embedded object(s) has incorrect operating system field", comp.getOperatingSystem().equals("Linux"));
assertTrue("Updated object with embedded object(s) has no graphics card", comp.getGraphicsCard() != null);
assertTrue("Updated object with embedded object(s) has incorrect embedded object : graphics card manufacturer is wrong", comp.getGraphicsCard().getManufacturer().equals("ATI"));
assertTrue("Updated object with embedded object(s) has incorrect embedded object : graphics card type is wrong", comp.getGraphicsCard().getType() == ComputerCard.AGP_CARD);
assertTrue("Updated object with embedded object(s) has no sound card", comp.getSoundCard() != null);
assertTrue("Updated object with embedded object(s) has incorrect embedded object : sound card manufacturer is wrong", comp.getSoundCard().getManufacturer().equals("Turtle Beach"));
assertTrue("Updated object with embedded object(s) has incorrect embedded object : sound card type is wrong", comp.getSoundCard().getType() == ComputerCard.ISA_CARD);
tx.commit();
} catch (Exception e) {
LOG.error("Exception during test", e);
fail("Exception thrown while updating embedded objects : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out created data
clean(ComputerCard.class);
clean(Computer.class);
}
}
use of org.datanucleus.samples.annotations.embedded.Computer in project tests by datanucleus.
the class AnnotationsEmbeddedTest method testEmbeddedObjectPersist.
/**
* Test the ability to persist (in its own right) an embedded object.
* @throws Exception
*/
public void testEmbeddedObjectPersist() throws Exception {
if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_PC)) {
return;
}
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
// ------------------ Persist an object with an embedded PC -----------------
Object comp_id = null;
try {
tx.begin();
Computer comp = new Computer("Windows", new ComputerCard("ATI", ComputerCard.AGP_CARD), new ComputerCard("Intel", ComputerCard.PCI_CARD));
pm.makePersistent(comp);
tx.commit();
comp_id = pm.getObjectId(comp);
} catch (Exception e) {
LOG.error("Exception during test", e);
fail("Exception thrown while creating an object with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
Object card_id = null;
try {
tx.begin();
// Retrieve the Computer
Computer comp = (Computer) pm.getObjectById(comp_id);
assertTrue("Unable to retrieve object with embedded object(s)", comp != null);
assertTrue("Retrieved object with embedded object(s) has incorrect operating system field", comp.getOperatingSystem().equals("Windows"));
assertTrue("Retrieved object with embedded object(s) has no graphics card", comp.getGraphicsCard() != null);
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : graphics card manufacturer is wrong", comp.getGraphicsCard().getManufacturer().equals("ATI"));
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : graphics card type is wrong", comp.getGraphicsCard().getType() == ComputerCard.AGP_CARD);
assertTrue("Retrieved object with embedded object(s) has no sound card", comp.getSoundCard() != null);
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : sound card manufacturer is wrong", comp.getSoundCard().getManufacturer().equals("Intel"));
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : sound card type is wrong", comp.getSoundCard().getType() == ComputerCard.PCI_CARD);
// Persist a copy of the embedded graphics card (can't persist the actual object since it is marked as embedded)
ComputerCard graphics = new ComputerCard(comp.getGraphicsCard().getManufacturer(), comp.getGraphicsCard().getType());
pm.makePersistent(graphics);
tx.commit();
card_id = pm.getObjectId(graphics);
assertTrue("Persisted ComputerCard has no identity !", card_id != null);
} catch (Exception e) {
LOG.error("Exception during test", e);
fail("Exception thrown while fetching objects with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Retrieve the ComputerCard
ComputerCard card = (ComputerCard) pm.getObjectById(card_id);
assertTrue("Unable to retrieve object that was previously persisted embedded", card != null);
assertTrue("Retrieved object has incorrect type", card.getType() == ComputerCard.AGP_CARD);
assertTrue("Retrieved object has unset manufacturer", card.getManufacturer() != null);
assertTrue("Retrieved object has incorrect manufacturer", card.getManufacturer().equals("ATI"));
tx.commit();
} catch (Exception e) {
LOG.error("Exception during test", e);
fail("Exception thrown while checking the persistence of previously embedded objects : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out created data
clean(ComputerCard.class);
clean(Computer.class);
}
}
use of org.datanucleus.samples.annotations.embedded.Computer in project tests by datanucleus.
the class AnnotationsEmbeddedTest method testEmbeddedPCObjectDetachAttach.
/**
* Test for detaching/attaching an embedded PC object.
* @throws Exception
*/
public void testEmbeddedPCObjectDetachAttach() throws Exception {
if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_PC)) {
return;
}
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
// Persist some objects
Object comp_id = null;
try {
tx.begin();
Computer comp = new Computer("Linux", new ComputerCard("ATI", ComputerCard.AGP_CARD), new ComputerCard("Creative", ComputerCard.PCI_CARD));
pm.makePersistent(comp);
// Access the object containing the embedded object before commit
// This used to try to go to the datastore at this point
comp.getGraphicsCard().toString();
comp.getSoundCard().toString();
tx.commit();
comp_id = pm.getObjectId(comp);
} catch (Exception e) {
LOG.error("Exception during test", e);
fail("Exception thrown while creating objects with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the object(s) and detach them
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
pm.getFetchPlan().setGroup(FetchPlan.ALL);
pm.getFetchPlan().setMaxFetchDepth(2);
Computer detachedComputer = null;
try {
tx.begin();
Computer comp = (Computer) pm.getObjectById(comp_id);
assertTrue("Unable to retrieve object with embedded object(s)", comp != null);
assertTrue("Retrieved object with embedded object(s) has incorrect operating system field", comp.getOperatingSystem().equals("Linux"));
assertTrue("Retrieved object with embedded object(s) has no graphics card", comp.getGraphicsCard() != null);
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : graphics card manufacturer is wrong", comp.getGraphicsCard().getManufacturer().equals("ATI"));
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : graphics card type is wrong", comp.getGraphicsCard().getType() == ComputerCard.AGP_CARD);
assertTrue("Embedded graphics card doesn't have its owner field set", comp.getGraphicsCard().getComputer() != null);
assertTrue("Embedded graphics card has its owner field set incorrectly", comp.getGraphicsCard().getComputer() == comp);
assertTrue("Retrieved object with embedded object(s) has no sound card", comp.getSoundCard() != null);
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : sound card manufacturer is wrong", comp.getSoundCard().getManufacturer().equals("Creative"));
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : sound card type is wrong", comp.getSoundCard().getType() == ComputerCard.PCI_CARD);
assertTrue("Embedded sound card doesn't have its owner field set", comp.getSoundCard().getComputer() != null);
assertTrue("Embedded sound card has its owner field set incorrectly", comp.getSoundCard().getComputer() == comp);
detachedComputer = pm.detachCopy(comp);
tx.commit();
} catch (Exception e) {
LOG.error("Exception during test", e);
fail("Exception thrown while retrieving/detaching objects with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check the detached object(s) and check them
assertNotNull("Detached Computer is null!", detachedComputer);
assertTrue("Updated object with embedded object(s) has incorrect operating system field", detachedComputer.getOperatingSystem().equals("Linux"));
assertTrue("Updated object with embedded object(s) has no graphics card", detachedComputer.getGraphicsCard() != null);
assertTrue("Updated object with embedded object(s) has incorrect embedded object : graphics card manufacturer is wrong", detachedComputer.getGraphicsCard().getManufacturer().equals("ATI"));
assertTrue("Updated object with embedded object(s) has incorrect embedded object : graphics card type is wrong", detachedComputer.getGraphicsCard().getType() == ComputerCard.AGP_CARD);
assertTrue("Updated object with embedded object(s) has no sound card", detachedComputer.getSoundCard() != null);
assertTrue("Updated object with embedded object(s) has incorrect embedded object : sound card manufacturer is wrong", detachedComputer.getSoundCard().getManufacturer().equals("Creative"));
assertTrue("Updated object with embedded object(s) has incorrect embedded object : sound card type is wrong", detachedComputer.getSoundCard().getType() == ComputerCard.PCI_CARD);
// Update some objects
detachedComputer.setOperatingSystem("Windows XP");
detachedComputer.getSoundCard().setManufacturer("Intel");
// Attach the objects
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
pm.getFetchPlan().setGroup(FetchPlan.ALL);
pm.getFetchPlan().setMaxFetchDepth(2);
try {
tx.begin();
Computer comp = pm.makePersistent(detachedComputer);
assertTrue("Attached object with embedded object(s) is null!!", comp != null);
assertTrue("Attached object with embedded object(s) has incorrect operating system field", comp.getOperatingSystem().equals("Windows XP"));
assertTrue("Attached object with embedded object(s) has no graphics card", comp.getGraphicsCard() != null);
assertTrue("Attached object with embedded object(s) has incorrect embedded object : graphics card manufacturer is wrong", comp.getGraphicsCard().getManufacturer().equals("ATI"));
assertTrue("Attached object with embedded object(s) has incorrect embedded object : graphics card type is wrong", comp.getGraphicsCard().getType() == ComputerCard.AGP_CARD);
assertTrue("Embedded graphics card doesn't have its owner field set", comp.getGraphicsCard().getComputer() != null);
assertTrue("Embedded graphics card has its owner field set incorrectly", comp.getGraphicsCard().getComputer() == comp);
assertTrue("Retrieved object with embedded object(s) has no sound card", comp.getSoundCard() != null);
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : sound card manufacturer is wrong", comp.getSoundCard().getManufacturer().equals("Intel"));
assertTrue("Retrieved object with embedded object(s) has incorrect embedded object : sound card type is wrong", comp.getSoundCard().getType() == ComputerCard.PCI_CARD);
tx.commit();
} catch (Exception e) {
LOG.error("Exception during test", e);
fail("Exception thrown while attaching objects with embedded field(s) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out created data
clean(ComputerCard.class);
clean(Computer.class);
}
}
Aggregations