Search in sources :

Example 26 with Person

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

the class PersistenceTest method testPersistDuplicates.

/**
 * Test of persistence of more than 1 app id objects with the same "id".
 */
public void testPersistDuplicates() {
    // Persist an object with same PK as the setUp object
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Person p2 = new Person(2, "Mickey", "Mouse", "micket.mouse@warnerbros.com");
        p2.setGlobalNum("1");
        pm.makePersistent(p2);
        tx.commit();
        fail("Was allowed to persist two application-identity objects with the same identity");
    } catch (Exception e) {
    // Expected
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 27 with Person

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

the class PersistenceTest method testUpdateFieldNoLongerNull.

/**
 * Test that adds a new attribute to spreadsheet
 */
public void testUpdateFieldNoLongerNull() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Person p = (Person) pm.getObjectById(id1);
        p.setEmailAddress("ppp");
        tx.commit();
        pm.close();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        Person p1 = (Person) pm.getObjectById(id1);
        assertEquals("ppp", p1.getEmailAddress());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person)

Example 28 with Person

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

the class ApplicationIdPersistenceTest method testInsert.

public void testInsert() throws Exception {
    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");
            Person p2 = new Person();
            p2.setPersonNum(2);
            p2.setGlobalNum("2");
            p2.setFirstName("My");
            p2.setLastName("Friend");
            p.setBestFriend(p2);
            pm.makePersistent(p);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 29 with Person

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

the class JDOQLBasicTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    // Delete the file so each test starts from nothing
    File file = new File("test.xml");
    if (file.exists()) {
        file.delete();
    }
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Person p = new Person();
        p.setPersonNum(4);
        p.setGlobalNum("4");
        p.setFirstName("Bugs");
        p.setLastName("Bunny");
        pm.makePersistent(p);
        Person bugs = p;
        // id[0] = pm.getObjectId(p);
        p = new Person();
        p.setPersonNum(5);
        p.setGlobalNum("5");
        p.setFirstName("Ana");
        p.setLastName("Hick");
        pm.makePersistent(p);
        // id[1] = pm.getObjectId(p);
        p = new Person();
        p.setPersonNum(3);
        p.setGlobalNum("3");
        p.setFirstName("Lami");
        p.setLastName("Puxa");
        p.setBestFriend(bugs);
        pm.makePersistent(p);
        // id[2] = pm.getObjectId(p);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) File(java.io.File) Person(org.jpox.samples.models.company.Person)

Example 30 with Person

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

the class JDOQLBasicTest method testQueryWithOneToOneParameterInputAsNull.

/**
 * test query with use of parameter for a 1-1 relation, input as null.
 * This tests the SQL generation of the query since, while there is a parameter, we don't want it
 * to appear in the SQL as "COL == ?", since the value of the paramerter is null, hence the SQL should
 * be "COL IS NULL".
 */
public void testQueryWithOneToOneParameterInputAsNull() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p1 = new Person(1, "Bugs", "Bunny", "bugs.bunny@warnerbros.com");
            Person p2 = new Person(2, "Road", "Runner", "road.runner@warnerbros.com");
            Person p3 = new Person(3, "Bart", "Simpson", "bart.simpson@simpsons.com");
            p1.setBestFriend(p2);
            p2.setBestFriend(p3);
            pm.makePersistent(p1);
            pm.makePersistent(p2);
            pm.makePersistent(p3);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q = pm.newQuery("SELECT FROM " + Person.class.getName() + " WHERE bestFriend == :param");
            Collection c = (Collection) q.execute(null);
            assertEquals(1, c.size());
            Person p = (Person) c.iterator().next();
            assertEquals("FirstName is incorrect", "Bart", p.getFirstName());
            assertEquals("LastName is incorrect", "Simpson", p.getLastName());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) 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