Search in sources :

Example 76 with Person

use of org.jpox.samples.models.company.Person in project tests by datanucleus.

the class JDOQLTest method testRange.

public void testRange() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p1 = new Person();
            p1.setPersonNum(1);
            p1.setGlobalNum("1");
            p1.setFirstName("Bugs");
            p1.setLastName("Bunny");
            Person p2 = new Person();
            p2.setPersonNum(2);
            p2.setGlobalNum("2");
            p2.setFirstName("Daffy");
            p2.setLastName("Duck");
            Employee e3 = new Employee();
            e3.setFirstName("Barney");
            e3.setLastName("Rubble");
            e3.setPersonNum(103);
            e3.setGlobalNum("103");
            e3.setSalary(124.50f);
            pm.makePersistent(p1);
            pm.makePersistent(p2);
            pm.makePersistent(e3);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during persist", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            // Check number of objects present
            Query q2 = pm.newQuery("SELECT FROM " + Person.class.getName() + " ORDER BY this.personNum RANGE 1,3");
            List<Person> results2 = (List<Person>) q2.execute();
            assertEquals(2, results2.size());
            Iterator<Person> iter = results2.iterator();
            boolean daffyPresent = false;
            boolean barneyPresent = false;
            while (iter.hasNext()) {
                Person p = iter.next();
                if (p.getFirstName().equals("Barney")) {
                    barneyPresent = true;
                } else if (p.getFirstName().equals("Daffy")) {
                    daffyPresent = true;
                }
            }
            assertTrue(daffyPresent);
            assertTrue(barneyPresent);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during query", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Employee.class);
        clean(Person.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) Person(org.jpox.samples.models.company.Person)

Example 77 with Person

use of org.jpox.samples.models.company.Person in project tests by datanucleus.

the class JDOQLTest method testResultAggregate.

public void testResultAggregate() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p1 = new Person();
            p1.setPersonNum(1);
            p1.setGlobalNum("1");
            p1.setFirstName("Bugs");
            p1.setLastName("Bunny");
            Person p2 = new Person();
            p2.setPersonNum(2);
            p2.setGlobalNum("2");
            p2.setFirstName("Daffy");
            p2.setLastName("Duck");
            Employee e3 = new Employee();
            e3.setFirstName("Barney");
            e3.setLastName("Rubble");
            e3.setPersonNum(103);
            e3.setGlobalNum("103");
            e3.setSalary(124.50f);
            pm.makePersistent(p1);
            pm.makePersistent(p2);
            pm.makePersistent(e3);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during persist", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q2 = pm.newQuery("SELECT MAX(this.personNum), MIN(this.personNum) FROM " + Person.class.getName());
            Object[] results2 = (Object[]) q2.execute();
            assertEquals(2, results2.length);
            assertEquals(103l, results2[0]);
            assertEquals(1l, results2[1]);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during query", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Employee.class);
        clean(Person.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person)

Example 78 with Person

use of org.jpox.samples.models.company.Person in project tests by datanucleus.

the class ApplicationIdPersistenceTest method testDate.

/**
 * Test persistence of Date field (using StringConverter).
 */
public void testDate() {
    Object id = null;
    Object id2 = null;
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p = new Person();
            p.setPersonNum(1);
            p.setGlobalNum("1");
            p.setFirstName("Bugs");
            p.setLastName("Bunny");
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, 2011);
            cal.set(Calendar.MONTH, 4);
            cal.set(Calendar.DAY_OF_MONTH, 15);
            p.setBirthDate(cal.getTime());
            pm.makePersistent(p);
            Person p2 = new Person();
            p2.setPersonNum(2);
            p2.setGlobalNum("2");
            p2.setFirstName("My");
            p2.setLastName("Friend");
            pm.makePersistent(p2);
            tx.commit();
            id = pm.getObjectId(p);
            id2 = pm.getObjectId(p2);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p = (Person) pm.getObjectById(id, true);
            assertNotNull("Date is null!", p.getBirthDate());
            Calendar cal = Calendar.getInstance();
            cal.setTime(p.getBirthDate());
            assertEquals("Year is wrong", 2011, cal.get(Calendar.YEAR));
            assertEquals("Month is wrong", 4, cal.get(Calendar.MONTH));
            assertEquals("Day is wrong", 15, cal.get(Calendar.DAY_OF_MONTH));
            Person p2 = (Person) pm.getObjectById(id2, true);
            assertNull("Date is not null!", p2.getBirthDate());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) Person(org.jpox.samples.models.company.Person)

Example 79 with Person

use of org.jpox.samples.models.company.Person in project tests by datanucleus.

the class ApplicationIdPersistenceTest method testOneToManyMapStrings.

public void testOneToManyMapStrings() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object p1Id = null;
        Object p2Id = null;
        try {
            tx.begin();
            Person p1 = new Person();
            p1.setPersonNum(1);
            p1.setGlobalNum("1");
            p1.setFirstName("Bugs");
            p1.setLastName("Bunny");
            Map<String, PhoneNumber> phones = p1.getPhoneNumbers();
            PhoneNumber number1 = new PhoneNumber("Home Number", "98706 123454");
            number1.setId(101);
            PhoneNumber number2 = new PhoneNumber("Mobile", "98605 436578");
            number2.setId(102);
            phones.put("John", number1);
            phones.put("Sally", number2);
            Person p2 = new Person();
            p2.setPersonNum(2);
            p2.setGlobalNum("2");
            p2.setFirstName("My");
            p2.setLastName("Friend");
            pm.makePersistent(p1);
            pm.makePersistent(p2);
            tx.commit();
            p1Id = pm.getObjectId(p1);
            p2Id = pm.getObjectId(p2);
        } catch (Exception e) {
            LOG.error("Exception during 1-1 persist", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p1 = (Person) pm.getObjectById(p1Id);
            assertNotNull("P1 not found", p1);
            Map phones1 = p1.getPhoneNumbers();
            assertNotNull("P2 phone numbers null!", phones1);
            assertEquals("Number of phone numbers is wrong", 2, phones1.size());
            assertTrue("John number not found", phones1.containsKey("John"));
            PhoneNumber number1 = (PhoneNumber) phones1.get("John");
            assertEquals("John number incorrect", "98706 123454", number1.getNumber());
            assertTrue("Sally number not found", phones1.containsKey("Sally"));
            PhoneNumber number2 = (PhoneNumber) phones1.get("Sally");
            assertEquals("Sally number incorrect", "98605 436578", number2.getNumber());
            Person p2 = (Person) pm.getObjectById(p2Id);
            assertNotNull("P2 not found", p2);
            assertNotNull("P2 phone numbers null!", p2.getPhoneNumbers());
            assertEquals("P2 phone numbers incorrect size!", 0, p2.getPhoneNumbers().size());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during 1-1 persist", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Person.class);
        clean(PhoneNumber.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) PhoneNumber(org.jpox.samples.models.company.PhoneNumber) Person(org.jpox.samples.models.company.Person) Map(java.util.Map) JDOException(javax.jdo.JDOException) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 80 with Person

use of org.jpox.samples.models.company.Person in project tests by datanucleus.

the class JDOQLTest method testFilterWithNullParameter.

/**
 * Query with a simple filter with one clause ("field == :param")
 * @throws Exception
 */
public void testFilterWithNullParameter() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p1 = new Person();
            p1.setPersonNum(1);
            p1.setGlobalNum("1");
            p1.setFirstName("Bugs");
            p1.setLastName("Bunny");
            Person p2 = new Person();
            p2.setPersonNum(2);
            p2.setGlobalNum("2");
            p2.setFirstName("Daffy");
            p2.setLastName(null);
            Employee e3 = new Employee();
            e3.setFirstName("Barney");
            e3.setLastName("Rubble");
            e3.setPersonNum(103);
            e3.setGlobalNum("103");
            e3.setSalary(124.50f);
            pm.makePersistent(p1);
            pm.makePersistent(p2);
            pm.makePersistent(e3);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during persist", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q1 = pm.newQuery("SELECT FROM " + Person.class.getName() + " WHERE lastName == :name");
            List<Person> results1 = (List<Person>) q1.execute(null);
            assertEquals(1, results1.size());
            Iterator<Person> iter = results1.iterator();
            Person p = iter.next();
            assertEquals("Daffy", p.getFirstName());
            assertNull(p.getLastName());
            assertEquals(2, p.getPersonNum());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during query", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Employee.class);
        clean(Person.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) Person(org.jpox.samples.models.company.Person)

Aggregations

Person (org.jpox.samples.models.company.Person)132 PersistenceManager (javax.jdo.PersistenceManager)130 Transaction (javax.jdo.Transaction)128 Query (javax.jdo.Query)75 List (java.util.List)60 JDOUserException (javax.jdo.JDOUserException)41 Iterator (java.util.Iterator)40 Collection (java.util.Collection)27 Employee (org.jpox.samples.models.company.Employee)22 HashMap (java.util.HashMap)21 JDODataStoreException (javax.jdo.JDODataStoreException)21 JDOException (javax.jdo.JDOException)16 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)16 ArrayList (java.util.ArrayList)15 BigInteger (java.math.BigInteger)14 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)14 BigDecimal (java.math.BigDecimal)10 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)10 JDOQuery (org.datanucleus.api.jdo.JDOQuery)10 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)10