use of org.jpox.samples.one_one.unidir_2.Newspaper in project tests by datanucleus.
the class ExtentTest method testExtentOfSubclassTable.
/**
* Test the use of extents with a class using "subclass-table" inheritance strategy.
*/
public void testExtentOfSubclassTable() throws Exception {
try {
// Create some objects.
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Newspaper paper = new Newspaper("Daily Vermin", 2, "George Bush", "Sleaze");
Magazine mag = new Magazine("Buenos Dias", 4, "Cachorro sa");
pm.makePersistent(paper);
pm.makePersistent(mag);
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during create of objects using subclass-table inheritance", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Try to get extent of ExtentSub (superclass of both objects, using subclass-table)
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Extent<MediaWork> e = pm.getExtent(MediaWork.class, true);
int number = 0;
Iterator<MediaWork> iter = e.iterator();
while (iter.hasNext()) {
iter.next();
number++;
}
assertEquals("Extent for classes with subclass-table inheritance strategy returned incorrect number of objects", number, 2);
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown for Extent of class using subclass-table", e);
e.printStackTrace();
fail("Exception was thrown when requesting Extent of class using subclass-table inheritance strategy");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out created data
clean(Newspaper.class);
clean(Magazine.class);
}
}
use of org.jpox.samples.one_one.unidir_2.Newspaper in project tests by datanucleus.
the class RelationshipTest method test1to1UnidirInheritanceSubclassTable.
/**
* Test case for 1-1 bidirectional relationships using 2 FK's.
* This is really tested as BasicQuery and DatastoreId test.
*/
/*public void test1to1BidirectionalMultiFK()
throws Exception
{
try
{
Object idUser = null;
Object idUserDetail = null;
PersistenceManager pm=pmf.getPersistenceManager();
Transaction tx=pm.currentTransaction();
// Create sample data
try
{
tx.begin();
User user=new User("andy","password");
UserDetails details=new UserDetails("Andy","Jefferson");
user.setDetails(details);
details.setUser(user);
pm.makePersistent(user);
tx.commit();
idUser = pm.getObjectId(user);
idUserDetail = pm.getObjectId(details);
}
catch (Exception e)
{
LOG.error(e);
fail("Exception thrown while creating 1-1 bidirectional relationship data : " + e.getMessage());
}
finally
{
if (tx.isActive())
{
tx.rollback();
}
pm.close();
}
pm=pmf.getPersistenceManager();
tx=pm.currentTransaction();
// Create sample data
try
{
tx.begin();
User user=(User)pm.getObjectById(idUser,true);
UserDetails details=(UserDetails)pm.getObjectById(idUserDetail,true);
assertEquals(details,user.getUserDetails());
assertEquals(user,details.getUser());
tx.commit();
}
catch (Exception e)
{
LOG.error(e);
fail("Exception thrown while creating 1-1 bidirectional relationship data : " + e.getMessage());
}
finally
{
if (tx.isActive())
{
tx.rollback();
}
pm.close();
}
}
finally
{
clean(User.class);
clean(UserDetails.class);
}
}*/
/**
* Test case for 1-1 uni relationship to a class using "subclass-table" inheritance strategy. See JIRA "NUCRDBMS-18"
*/
public void test1to1UnidirInheritanceSubclassTable() throws Exception {
try {
// Create the necessary schema
try {
addClassesToSchema(new Class[] { Newspaper.class, Magazine.class, MediaWork.class, Reader.class });
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while adding classes for 1-1 relation using subclass-table : " + e.getMessage());
}
// Check the persistence of data
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object fredId = null;
Object pamId = null;
try {
tx.begin();
Magazine hello = new Magazine("Hello", MediaWork.FREQ_WEEKLY, "Trash Publishers");
Newspaper mail = new Newspaper("Daily Mail", MediaWork.FREQ_DAILY, "Piers Morgan", "Tabloid");
Reader fred = new Reader("Fred Smith", mail);
Reader pam = new Reader("Pam Green", hello);
pm.makePersistent(fred);
pm.makePersistent(pam);
tx.commit();
fredId = pm.getObjectId(fred);
pamId = pm.getObjectId(pam);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check the retrieval of the data
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Reader fred = (Reader) pm.getObjectById(fredId, true);
assertTrue("Fred has the wrong name!", fred.getName().equals("Fred Smith"));
assertTrue("Fred has the wrong type of material", fred.getMaterial() instanceof Newspaper);
assertTrue("Fred has the wrong material", fred.getMaterial().getName().equals("Daily Mail"));
Reader pam = (Reader) pm.getObjectById(pamId, true);
assertTrue("Pam has the wrong name", pam.getName().equals("Pam Green"));
assertTrue("Pam has the wrong type of material", pam.getMaterial() instanceof Magazine);
assertTrue("Pam has the wrong material", pam.getMaterial().getName().equals("Hello"));
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check a query
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query q1 = pm.newQuery(Reader.class, "((org.jpox.samples.one_one.unidir_2.Magazine)material).name == \"Hello\"");
List results1 = (List) q1.execute();
assertEquals("Number of readers who read \"Hello\" magazine was incorrect", results1.size(), 1);
Query q2 = pm.newQuery(Reader.class, "((org.jpox.samples.one_one.unidir_2.Newspaper)material).name == \"Daily Mail\"");
List results2 = (List) q2.execute();
assertEquals("Number of readers who read \"Daily Mail\" newspaper was incorrect", results2.size(), 1);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out all data
clean(Reader.class);
clean(Newspaper.class);
clean(Magazine.class);
}
}
Aggregations