use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class TypesMappingTest method testDB2DataLinkType.
/**
* Test for DB2 datastore "DATALINK" type.
*/
public void testDB2DataLinkType() {
if (!vendorID.equals("db2")) {
return;
}
DB2Types types;
Object id = null;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
types = new DB2Types();
types.setDataLinkString("http://www.jpox.org");
types.setDataLinkString2("http://www.someurl.org/path");
types.setSimpleString("some string");
pm.makePersistent(types);
id = JDOHelper.getObjectId(types);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
types = (DB2Types) pm.getObjectById(id, true);
assertEquals("DataLinkString retrieved is wrong", "http://www.jpox.org".toUpperCase(), types.getDataLinkString().toUpperCase());
assertEquals("DataLinkString2 retrieved is wrong", "/path".toUpperCase(), types.getDataLinkString2().toUpperCase());
assertEquals("Simple String retrieved is wrong", "some string", types.getSimpleString());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
types = (DB2Types) ((Collection) pm.newQuery(DB2Types.class).execute()).iterator().next();
assertEquals("DataLinkString retrieved from Query is wrong", "http://www.jpox.org".toUpperCase(), types.getDataLinkString().toUpperCase());
assertEquals("DataLinkString2 retrieved from Query is wrong", "/path".toUpperCase(), types.getDataLinkString2().toUpperCase());
assertEquals("Simple String retrieved from Query is wrong", "some string", types.getSimpleString());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class ViewTest method testNameViewWithId.
/**
* Use of a simple view for an object, using application identity for the view objects.
*/
public void testNameViewWithId() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(new NameObject(1, "FIRST"));
pm.makePersistent(new NameObject(2, "SECOND"));
pm.makePersistent(new NameObject(3, "THIRD"));
pm.makePersistent(new NameObject(4, "FOURTH"));
tx.commit();
tx.begin();
Query q = pm.newQuery("SELECT FROM " + NameObject.class.getName() + " ORDER BY id");
List<NameObject> persons = (List<NameObject>) q.execute();
assertEquals(4, persons.size());
assertEquals("FIRST", persons.get(0).getName());
Query q1 = pm.newQuery("SELECT FROM " + FNameView2.class.getName() + " ORDER BY id");
List<FNameView2> fNames = ((List<FNameView2>) q1.execute());
assertEquals(2, fNames.size());
assertEquals("FIRST", fNames.get(0).getName());
assertEquals("FOURTH", fNames.get(1).getName());
tx.commit();
} catch (Throwable thr) {
LOG.error(">> Exception thrown persist/view data with FNameView2", thr);
fail("Failed to persist data : " + thr.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(NameObject.class);
}
}
use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class ViewTest method testViewOfSetWidgets.
public void testViewOfSetWidgets() throws Exception {
/*
* We can't run this test on Cloudscape because the view used by
* SetWidgetCounts doesn't execute properly; some counts that should
* be 0 come up 1. This is presumably due to a bug in Cloudscape
* (last tried on both 3.6 and 4.0).
*/
if ("cloudscape".equals(vendorID)) {
return;
}
try {
LOG.info("Testing view derived from of " + StorageTester.TEST_OBJECT_COUNT + " " + SetWidget.class.getName() + " objects");
tester.insertObjects(SetWidget.class);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Extent ext = pm.getExtent(SetWidgetCounts.class, true);
Iterator exti = ext.iterator();
int count = 0;
while (exti.hasNext()) {
SetWidgetCounts actual = (SetWidgetCounts) exti.next();
SetWidgetCounts expected = new SetWidgetCounts(actual.getSetWidget());
StorageTester.assertFieldsEqual(expected, actual);
++count;
}
tx.commit();
assertEquals("Iteration over view extent returned wrong number of rows", StorageTester.TEST_OBJECT_COUNT, count);
tx.begin();
Query query = pm.newQuery(pm.getExtent(SetWidgetCounts.class, true));
query.setFilter("normalSetSize != 0");
query.setOrdering("sw.numElementWidgets descending");
Collection results = (Collection) query.execute();
TestObject[] objs = tester.getObjects();
try {
HashSet expected = new HashSet();
for (int i = 0; i < objs.length; ++i) {
SetWidget sw = (SetWidget) objs[i];
if (sw.getNormalSet().size() != 0) {
expected.add(new SetWidgetCounts(sw));
}
}
assertTrue("Query has no expected results (test is broken)", !expected.isEmpty());
assertTrue("Query returned no rows", !results.isEmpty());
HashSet actual = new HashSet(results);
assertEquals("Query returned duplicate rows", results.size(), actual.size());
assertTrue("Query did not return expected results: expected " + expected + ", but was " + actual, TestObject.compareSet(expected, actual));
} finally {
query.closeAll();
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} catch (JDOException e) {
LOG.error(">> Exception during test", e);
fail("Exception occurred during test : " + e.getMessage());
} finally {
tester.removeObjects();
clean(Widget.class);
}
}
use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class SQLQueryTest method testTimestampQueryOnOracle.
/**
* Verify CORE-2976
*/
public void testTimestampQueryOnOracle() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// run this test only on Oracle
if (vendorID != null && vendorID.equals("oracle")) {
tx.begin();
Query timestampQuery = pm.newQuery(Query.SQL, "SELECT LOCALTIMESTAMP FROM DUAL");
timestampQuery.setResultClass(java.sql.Timestamp.class);
timestampQuery.setUnique(true);
Timestamp result = (Timestamp) timestampQuery.execute();
assertNotNull(result);
}
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class SQLQueryTest method testWithoutCandidatesClassWithParameters.
/**
* Basic test of SQL without a candidate class but with parameters.
*/
public void testWithoutCandidatesClassWithParameters() throws Exception {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// insert a new element for table person
tx.begin();
Person p = new Person(1, "Nobody", "Nobody", "nobody@jpox.org");
pm.makePersistent(p);
tx.commit();
tx.begin();
String sqlText = "SELECT count(*) FROM PERSON WHERE EMAIL_ADDRESS = ?";
Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
List results = (List) query.execute("nobody@jpox.org");
Iterator iter = results.iterator();
assertEquals(1, results.size());
while (iter.hasNext()) {
Object obj = iter.next();
if (obj.getClass().isArray()) {
fail("SQL Query selecting count(*) has returned an Object[] yet should have been Object");
}
assertTrue("SQL Query selecting count(*) has returned an object of the wrong type : was " + obj.getClass().getName() + " but should have been Number or subclass", obj instanceof Number);
Number value = (Number) obj;
assertEquals("SQL Query selecting count(*) returned the wrong value : was " + value.longValue() + " but should have been 1", value.longValue(), 1);
}
// test more than one parameter
sqlText = "SELECT count(*) FROM PERSON WHERE EMAIL_ADDRESS = ? AND FIRSTNAME = ?";
query = pm.newQuery("javax.jdo.query.SQL", sqlText);
results = (List) query.execute("nobody@jpox.org", "Nobody");
iter = results.iterator();
assertEquals(1, results.size());
while (iter.hasNext()) {
Object obj = iter.next();
if (obj.getClass().isArray()) {
fail("SQL Query selecting count(*) has returned an Object[] yet should have been Object");
}
assertTrue("SQL Query selecting count(*) has returned an object of the wrong type : was " + obj.getClass().getName() + " but should have been Number or subclass", obj instanceof Number);
Number value = (Number) obj;
assertEquals("SQL Query selecting count(*) returned the wrong value : was " + value.longValue() + " but should have been 1", value.longValue(), 1);
}
// test more than one parameter
sqlText = "SELECT count(*) FROM PERSON WHERE EMAIL_ADDRESS = ? AND FIRSTNAME = ?";
query = pm.newQuery("javax.jdo.query.SQL", sqlText);
results = (List) query.execute("nobody@jpox.org", "Noboda");
assertEquals(1, results.size());
// test more than one parameter
sqlText = "SELECT * FROM PERSON WHERE EMAIL_ADDRESS = ? AND FIRSTNAME = ?";
query = pm.newQuery("javax.jdo.query.SQL", sqlText);
results = (List) query.execute("nobody@jpox.org", "Nobody");
assertEquals(1, results.size());
// test more than one parameter
sqlText = "SELECT * FROM PERSON WHERE EMAIL_ADDRESS = ? AND FIRSTNAME = ?";
query = pm.newQuery("javax.jdo.query.SQL", sqlText);
results = (List) query.execute("nobody@jpox.org", "Noboda");
assertEquals(0, results.size());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
LOG.error(e);
fail("Exception thrown while running SQL query with parameters : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
clean(Person.class);
}
}
Aggregations