use of org.jpox.samples.models.company.Person in project tests by datanucleus.
the class SQLQueryTest method testNumberedParameters.
/**
* Test use of an SQL query using numbered parameters ("?1", "?2", etc).
* @throws Exception
*/
public void testNumberedParameters() throws Exception {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// insert new elements for table person
tx.begin();
Person p = new Person(1, "John", "Smith", "a@jpox.org");
pm.makePersistent(p);
p = new Person(2, "Paul", "Smith", "b@jpox.org");
pm.makePersistent(p);
p = new Person(3, "Paul", "Green", "c@jpox.org");
pm.makePersistent(p);
p = new Person(4, "John", "Brown", "d@jpox.org");
pm.makePersistent(p);
p = new Person(5, "Jason", "White", "e@jpox.org");
pm.makePersistent(p);
p = new Person(6, "John", "White", "f@jpox.org");
pm.makePersistent(p);
tx.commit();
tx.begin();
// Use numbered params and reuse param 1
String sqlText = "SELECT count(*) FROM PERSON WHERE (FIRSTNAME = ?1 AND LASTNAME = ?2) OR " + "(FIRSTNAME = ?1 AND LASTNAME = ?3)";
Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
List list = (List) query.execute("John", "Smith", "Brown");
Object obj = list.iterator().next();
if (obj instanceof Integer) {
assertEquals(2, ((Integer) obj).intValue());
} else if (obj instanceof Long) {
assertEquals(2, ((Long) obj).intValue());
} else if (obj instanceof BigInteger) {
assertEquals(2, ((BigInteger) obj).intValue());
} else if (obj instanceof BigDecimal) {
assertEquals(2, ((BigDecimal) obj).intValue());
} else {
fail("Test doesnt support count(*) being returned as " + obj.getClass().getName());
}
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);
}
}
use of org.jpox.samples.models.company.Person in project tests by datanucleus.
the class JPQLQueryTest method testFilterUPPER.
/**
* Test using UPPER(str) function.
*/
public void testFilterUPPER() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery("JPQL", "SELECT p FROM " + Person.class.getName() + " p WHERE UPPER(firstName) = 'ANA'");
Collection c = (Collection) q.execute();
assertEquals(1, c.size());
Iterator it = c.iterator();
assertEquals("Ana", ((Person) it.next()).getFirstName());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.jpox.samples.models.company.Person in project tests by datanucleus.
the class JPQLQueryTest method testFilterLike.
/**
* result test
*/
public void testFilterLike() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery("JPQL", "SELECT p FROM " + Person.class.getName() + " p WHERE firstName LIKE 'A.*a'");
Collection c = (Collection) q.execute();
assertEquals(1, c.size());
Iterator it = c.iterator();
assertEquals("Ana", ((Person) it.next()).getFirstName());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.jpox.samples.models.company.Person in project tests by datanucleus.
the class JPQLQueryTest method testFilterInLiteral.
/**
* Test using "IN (literal, literal)" and "NOT IN (literal, literal)".
*/
public void testFilterInLiteral() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery("JPQL", "SELECT p FROM " + Person.class.getName() + " p WHERE firstName IN ('Ana', 'Alan')");
Collection c = (Collection) q.execute();
assertEquals(1, c.size());
Iterator it = c.iterator();
assertEquals("Ana", ((Person) it.next()).getFirstName());
q = pm.newQuery("JPQL", "SELECT p FROM " + Person.class.getName() + " p WHERE firstName NOT IN ('Ana', 'Bugs')");
c = (Collection) q.execute();
assertEquals(1, c.size());
it = c.iterator();
assertEquals("Lami", ((Person) it.next()).getFirstName());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.jpox.samples.models.company.Person in project tests by datanucleus.
the class PersistenceTest method testFetch.
/**
* Fetch objects and assert basic values
*/
public void testFetch() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Person p1 = (Person) pm.getObjectById(id1);
assertEquals("Bugs", p1.getFirstName());
assertEquals("Bunny", p1.getLastName());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
Aggregations