use of javax.jdo.JDOUserException in project tests by datanucleus.
the class InterfacesTest method testReadAllShapeHolders.
/**
* Check that ShapeHolder objects can be read back again using an Extent.
*/
public void testReadAllShapeHolders() throws Exception {
try {
addClassesToSchema(new Class[] { ShapeHolder.class, Rectangle.class, Circle.class, Square.class, Triangle.class });
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Rectangle rectangle = new Rectangle(r.nextInt(), 1.0, 2.0);
ShapeHolder rectangleHolder = new ShapeHolder(r.nextInt());
rectangleHolder.setShape1(rectangle);
pm.makePersistent(rectangleHolder);
ShapeHolder circleHolder = new ShapeHolder(r.nextInt());
Circle circle = (Circle) circleHolder.getShape1();
pm.makePersistent(circleHolder);
ShapeHolder squareHolder = new ShapeHolder(r.nextInt());
Square square = new Square(r.nextInt(), 2.0, 3.5);
squareHolder.setShape1(square);
pm.makePersistent(squareHolder);
ShapeHolder triangleHolder = new ShapeHolder(r.nextInt());
Triangle triangle = new Triangle(r.nextInt(), "tri " + r.nextInt(), 2.1, 3.2);
triangleHolder.setShape1(triangle);
pm.makePersistent(triangleHolder);
tx.commit();
tx.begin();
Extent extent = pm.getExtent(ShapeHolder.class, false);
for (Iterator i = extent.iterator(); i.hasNext(); ) {
ShapeHolder holder = (ShapeHolder) (i.next());
Shape shape = holder.getShape1();
if (holder.getShape1() instanceof Circle) {
// found the circle
assertNotNull("Two ShapeHolders with circles found in database, only one inserted.", circle);
assertEquals("Circle read back in ShapeHolder does not match the one inserted.", circle, shape);
circle = null;
} else if (holder.getShape1() instanceof Rectangle) {
// found the rectangle
assertNotNull("Two ShapeHolders with rectangles found in database, only one inserted.", rectangle);
assertEquals("Rectangle read back in ShapeHolder does not match the one inserted.", rectangle, shape);
rectangle = null;
} else if (holder.getShape1() instanceof Square) {
// found the square
assertNotNull("Two ShapeHolders with squares found in database, only one inserted.", square);
assertEquals("Square read back in ShapeHolder does not match the one inserted.", square, shape);
square = null;
} else if (holder.getShape1() instanceof Triangle) {
// found the triangle
assertNotNull("Two ShapeHolders with triangles found in database, only one inserted.", triangle);
assertEquals("Triangle read back in ShapeHolder does not match the one inserted.", triangle, shape);
triangle = null;
} else {
fail("Unknown shape [" + shape + "] found in ShapeHolder");
}
}
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during test.", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(ShapeHolder.class);
clean(Circle.class);
clean(Triangle.class);
clean(Rectangle.class);
clean(Square.class);
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class InterfacesTest method testCreation.
/**
* Check that we can create a persistent object with an interface FCO.
*/
public void testCreation() throws Exception {
try {
addClassesToSchema(new Class[] { ShapeHolder.class });
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Create some objects.
tx.begin();
ShapeHolder holder = new ShapeHolder(r.nextInt());
pm.makePersistent(holder);
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during create of interface objects.", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(ShapeHolder.class);
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class InterfacesTest method testChangeImplementation.
/**
* Persist a ShapeHolder with one implementation of Shape, then change to
* a different implementation and check that the saved version also has
* its implementation updated.
*/
public void testChangeImplementation() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Create the ShapeHolder record
tx.begin();
ShapeHolder holder = new ShapeHolder(r.nextInt());
Shape shape = new Rectangle(r.nextInt(), 5.0, 3.4);
holder.setShape1(shape);
pm.makePersistent(holder);
tx.commit();
// Load it back and change the Shape to a Circle
tx.begin();
Extent extent = pm.getExtent(ShapeHolder.class, false);
holder = (ShapeHolder) (extent.iterator().next());
assertEquals("The inserted rectangle", shape, holder.getShape1());
shape = new Circle(r.nextInt(), 1.75);
holder.setShape1(shape);
extent.closeAll();
tx.commit();
// Load back the holder and check that its Shape is a Circle, then change
// it back to a Rectangle
tx.begin();
extent = pm.getExtent(ShapeHolder.class, false);
holder = (ShapeHolder) (extent.iterator().next());
assertEquals("The inserted circle", shape, holder.getShape1());
shape = new Rectangle(r.nextInt(), 3.897, 1.18);
holder.setShape1(shape);
extent.closeAll();
tx.commit();
// check that the holder's shape is a Rectangle again
tx.begin();
extent = pm.getExtent(ShapeHolder.class, false);
holder = (ShapeHolder) (extent.iterator().next());
assertEquals("The re-inserted rectangle", shape, holder.getShape1());
extent.closeAll();
tx.commit();
// Do it again with application identity classes
tx.begin();
extent = pm.getExtent(ShapeHolder.class, false);
holder = (ShapeHolder) (extent.iterator().next());
shape = new Triangle(r.nextInt(), "tri" + r.nextInt(), 4.97, 7.29);
holder.setShape1(shape);
extent.closeAll();
tx.commit();
// check that the holder's shape is a Triangle again
tx.begin();
extent = pm.getExtent(ShapeHolder.class, false);
holder = (ShapeHolder) (extent.iterator().next());
assertEquals("The re-inserted triangle", shape, holder.getShape1());
extent.closeAll();
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during test.", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(ShapeHolder.class);
clean(Circle.class);
clean(Triangle.class);
clean(Rectangle.class);
clean(Square.class);
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class InterfacesTest method testMappingStrategyIdentity1To1.
/**
* Test for use of mapping-strategy="identity" for 1-1 relation
*/
public void testMappingStrategyIdentity1To1() throws Exception {
try {
addClassesToSchema(new Class[] { Diet.class });
Object id = null;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Create some objects
tx.begin();
Diet diet = new Diet(1);
Food fave = new Steak();
diet.setFavouriteFood(fave);
pm.makePersistent(diet);
tx.commit();
id = JDOHelper.getObjectId(diet);
} catch (JDOUserException ue) {
fail("Exception thrown during create of interface objects.");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Diet diet = (Diet) pm.getObjectById(id);
Food fave = diet.getFavouriteFood();
assertNotNull("Favourite food is null!!", fave);
assertTrue("Favourite should be Steak!", fave instanceof Steak);
// Set to null
diet.setFavouriteFood(null);
tx.commit();
} catch (JDOUserException ue) {
fail("Exception thrown during retrieval of objects");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Diet diet = (Diet) pm.getObjectById(id);
Food fave = diet.getFavouriteFood();
assertNull("Favourite food is not null!!", fave);
tx.commit();
} catch (JDOUserException ue) {
fail("Exception thrown during retrieval of objects");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Diet.class);
clean(Steak.class);
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class JDOQLBasicTest method testStringIndexOf.
/**
* Test for the use of the String.indexOf() method.
*/
public void testStringIndexOf() {
Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1");
Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
// Eh, what's up, doc?
Employee bunny = new Employee(3, "Bugs", "Bunny", "bugs.bunny@warnerbros.com", 12, "serial 3");
// Beep! Beep!
Employee roadrunner = new Employee(4, "Road", "Runner", "road.runner@warnerbros.com", 11, "serial 4");
// I hate the gravity
Employee coyote = new Employee(5, "Wile", "E. Coyote", "wile.coyote@acme.com", 9, "serial 5");
// paranoid, and neurotic
Employee duck = new Employee(6, "Daffy", "Duck", "daffy.duck@warnerbros.com", 7, "serial 6");
// You are my peanut.
Employee pepe = new Employee(7, "Pepe", "le Pew", "pepe.lepew@warnerbros.com", 8, "serial 7");
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(woody);
pm.makePersistent(bart);
pm.makePersistent(bunny);
pm.makePersistent(roadrunner);
pm.makePersistent(coyote);
pm.makePersistent(duck);
pm.makePersistent(pepe);
tx.commit();
tx.begin();
try {
Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.setFilter("emailAddress.indexOf(\"@\") >= 0");
HashSet results = new HashSet((Collection) q.execute());
assertEquals("received: " + results, 7, results.size());
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
try {
Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.setFilter("emailAddress.indexOf(\"woodpecker\") == 6");
HashSet results = new HashSet((Collection) q.execute());
assertEquals("received: " + results, 1, results.size());
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
try {
Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.setFilter("emailAddress.indexOf(\"wood\",7) >= 0");
HashSet results = new HashSet((Collection) q.execute());
assertEquals("received: " + results, 0, results.size());
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
// Clean out our data
clean(Employee.class);
}
}
Aggregations