use of org.jpox.samples.embedded.Network in project tests by datanucleus.
the class BasicTest method testPersistEmbeddedCollection.
/**
* Example using an embedded collection field, with persist then get then delete
* @throws IOException
*/
public void testPersistEmbeddedCollection() throws IOException {
HttpClient client = new HttpClient();
try {
ContentExchange post = new ContentExchange();
post.setURL("http://localhost:" + PORT + "/dn/" + Network.class.getName());
post.setMethod("POST");
JSONObject obj = new JSONObject();
obj.put("id", 1);
obj.put("name", "Home Network");
Collection<JSONObject> devs = new HashSet<JSONObject>();
JSONObject dev1 = new JSONObject();
dev1.put("name", "Toaster");
dev1.put("description", "Kitchen Toaster");
devs.add(dev1);
JSONArray jsonarr = new JSONArray(devs);
obj.put("devices", jsonarr);
post.setRequestContent(new ByteArrayBuffer(obj.toString().getBytes()));
// persist
client.start();
client.send(post);
post.waitForDone();
// validate
assertEquals(201, post.getResponseStatus());
assertNotNull(post.getResponseContent());
obj = new JSONObject(post.getResponseContent());
assertEquals(1, obj.getLong("id"));
try {
ContentExchange get = new ContentExchange();
get.setURL("http://localhost:" + PORT + "/dn/" + Network.class.getName() + "/1?fetch=all");
get.setMethod("GET");
client.send(get);
get.waitForDone();
assertEquals(200, get.getResponseStatus());
assertNotNull(get.getResponseContent());
obj = new JSONObject(get.getResponseContent());
assertEquals("Home Network", obj.getString("name"));
Object devObjs = obj.get("devices");
assertNotNull(devObjs);
assertTrue(devObjs instanceof JSONArray);
JSONArray devArr = (JSONArray) devObjs;
assertEquals(1, devArr.length());
Object devObj = devArr.get(0);
assertTrue(devObj instanceof JSONObject);
JSONObject dev = (JSONObject) devObj;
assertEquals("Toaster", dev.getString("name"));
assertEquals("Kitchen Toaster", dev.getString("description"));
} catch (Exception e) {
LOG.error("Exception in validate", e);
fail(e.getMessage());
}
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
try {
ContentExchange delete = new ContentExchange();
delete.setURL("http://localhost:" + PORT + "/dn/" + Network.class.getName() + "/1");
delete.setMethod("DELETE");
client.send(delete);
delete.waitForDone();
assertEquals(204, delete.getResponseStatus());
assertNull(delete.getResponseContent());
} catch (Exception e) {
fail(e.getMessage());
}
}
}
use of org.jpox.samples.embedded.Network 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);
}
}
use of org.jpox.samples.embedded.Network in project tests by datanucleus.
the class EmbeddedContainerTest method testEmbeddedCollection.
/**
* Test for an embedded PC object.
* @throws Exception
*/
public void testEmbeddedCollection() throws Exception {
if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_COLLECTION)) {
return;
}
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();
}
// 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);
}
}
use of org.jpox.samples.embedded.Network in project tests by datanucleus.
the class EmbeddedTest method testEmbeddedCollectionQuery.
/**
* Test for query of an embedded PC collection object.
* @throws Exception
*/
public void testEmbeddedCollectionQuery() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
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();
} 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();
// Basic query of all Networks
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery(Network.class);
List results = (List) q.execute();
assertTrue("No networks retrieved from query of all Networks!", results != null);
assertEquals("Number of networks retrieved from Query is incorrect", 1, results.size());
Iterator resultsIter = results.iterator();
while (resultsIter.hasNext()) {
Network net = (Network) resultsIter.next();
Device[] devices = net.getDevices();
assertTrue("No devices retrieved from retrieved network)!", devices != null);
assertEquals("Number of devices retrieved from retrieved network 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());
}
tx.commit();
} catch (Exception e) {
LOG.error(">> Exception in test", e);
fail("Exception thrown while querying objects with embedded container : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Query of Collection.contains() with embedded elements
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery(Network.class, "devices.contains(elem) && elem.name == \"Audio Server\"");
q.declareVariables("org.jpox.samples.embedded.Device elem");
List results = (List) q.execute();
assertTrue("No networks retrieved from query of Networks containing audio server!", results != null);
assertEquals("Number of networks retrieved from Query is incorrect", 1, results.size());
tx.commit();
} catch (Exception e) {
LOG.error(">> Exception in test", e);
fail("Exception thrown while querying objects with embedded container : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out created data
clean(Network.class);
}
}
use of org.jpox.samples.embedded.Network in project tests by datanucleus.
the class EmbeddedContainerTest method testEmbeddedCollectionQuery.
/**
* Test for query of an embedded PC collection object.
* @throws Exception
*/
public void testEmbeddedCollectionQuery() throws Exception {
if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_COLLECTION)) {
return;
}
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
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();
} 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();
}
// Basic query of all Networks
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery(Network.class);
List results = (List) q.execute();
assertTrue("No networks retrieved from query of all Networks!", results != null);
assertEquals("Number of networks retrieved from Query is incorrect", 1, results.size());
Iterator resultsIter = results.iterator();
while (resultsIter.hasNext()) {
Network net = (Network) resultsIter.next();
Device[] devices = net.getDevices();
assertTrue("No devices retrieved from retrieved network)!", devices != null);
assertEquals("Number of devices retrieved from retrieved network 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());
}
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while querying objects with embedded container : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Query of Collection.contains() with embedded elements
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery(Network.class, "devices.contains(elem) && elem.name == \"Audio Server\"");
q.declareVariables("org.jpox.samples.embedded.Device elem");
List results = (List) q.execute();
assertTrue("No networks retrieved from query of Networks containing audio server!", results != null);
assertEquals("Number of networks retrieved from Query is incorrect", 1, results.size());
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while querying objects with embedded container : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out created data
clean(Network.class);
}
}
Aggregations