Search in sources :

Example 26 with PersistenceManager

use of javax.jdo.PersistenceManager in project tests by datanucleus.

the class SchemaHandlerTest method testColumnRetrieval.

/**
 * Test of the retrieval of columns.
 */
public void testColumnRetrieval() {
    addClassesToSchema(new Class[] { SchemaClass1.class, SchemaClass2.class });
    PersistenceManager pm = pmf.getPersistenceManager();
    RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
    StoreSchemaHandler handler = databaseMgr.getSchemaHandler();
    ClassLoaderResolver clr = storeMgr.getNucleusContext().getClassLoaderResolver(null);
    Connection con = (Connection) databaseMgr.getConnectionManager().getConnection(((JDOPersistenceManager) pm).getExecutionContext()).getConnection();
    // Retrieve and check the table for SchemaClass1
    DatastoreClass table1 = databaseMgr.getDatastoreClass(SchemaClass1.class.getName(), clr);
    RDBMSTableInfo tableInfo1 = (RDBMSTableInfo) handler.getSchemaData(con, "columns", new Object[] { table1 });
    assertNotNull("TableInfo from getColumns is NULL!", tableInfo1);
    assertEquals("Number of columns for table " + table1 + " is wrong", 4, tableInfo1.getNumberOfChildren());
    Iterator colsIter = tableInfo1.getChildren().iterator();
    Collection colNamesPresent = new HashSet();
    colNamesPresent.add("TABLE1_ID1");
    colNamesPresent.add("TABLE1_ID2");
    colNamesPresent.add("NAME");
    colNamesPresent.add("OTHER_ID");
    while (colsIter.hasNext()) {
        RDBMSColumnInfo colInfo = (RDBMSColumnInfo) colsIter.next();
        String colInfoName = colInfo.getColumnName().toUpperCase();
        if (colInfoName.equals("TABLE1_ID1") || colInfoName.equals("TABLE1_ID2") || colInfoName.equals("NAME") || colInfoName.equals("OTHER_ID")) {
            colNamesPresent.remove(colInfoName);
        }
    }
    assertTrue("Some columns expected were not present in the datastore table : " + StringUtils.collectionToString(colNamesPresent), colNamesPresent.size() == 0);
    // Retrieve and check the table for SchemaClass2
    DatastoreClass table2 = databaseMgr.getDatastoreClass(SchemaClass2.class.getName(), clr);
    RDBMSTableInfo tableInfo2 = (RDBMSTableInfo) handler.getSchemaData(con, "columns", new Object[] { table2 });
    assertEquals("Number of columns for table " + table2 + " is wrong", 3, tableInfo2.getNumberOfChildren());
    colsIter = tableInfo2.getChildren().iterator();
    colNamesPresent.clear();
    colNamesPresent.add("TABLE2_ID");
    colNamesPresent.add("NAME");
    colNamesPresent.add("VALUE");
    while (colsIter.hasNext()) {
        RDBMSColumnInfo colInfo = (RDBMSColumnInfo) colsIter.next();
        String colInfoName = colInfo.getColumnName().toUpperCase();
        if (colInfoName.equals("TABLE2_ID")) {
            colNamesPresent.remove(colInfoName);
        }
        if (colInfoName.equals("NAME")) {
            colNamesPresent.remove(colInfoName);
            assertEquals("Length of column " + colInfo.getColumnName() + " has incorrect length", 20, colInfo.getColumnSize());
        }
        if (colInfoName.equals("VALUE")) {
            colNamesPresent.remove(colInfoName);
        }
    }
    assertTrue("Some columns expected were not present in the datastore table : " + StringUtils.collectionToString(colNamesPresent), colNamesPresent.size() == 0);
    // Now check retrieval of a column for a table
    RDBMSColumnInfo colInfo = (RDBMSColumnInfo) handler.getSchemaData(con, "column", new Object[] { table2, "VALUE" });
    if (colInfo == null) {
        colInfo = (RDBMSColumnInfo) handler.getSchemaData(con, "column", new Object[] { table2, "value" });
    }
    assertNotNull("Column VALUE for table " + table2 + " was not found", colInfo);
    assertEquals("Column name is wrong", "VALUE", colInfo.getColumnName().toUpperCase());
}
Also used : RDBMSColumnInfo(org.datanucleus.store.rdbms.schema.RDBMSColumnInfo) RDBMSTableInfo(org.datanucleus.store.rdbms.schema.RDBMSTableInfo) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) Connection(java.sql.Connection) SchemaClass1(org.jpox.samples.rdbms.schema.SchemaClass1) StoreSchemaHandler(org.datanucleus.store.schema.StoreSchemaHandler) SchemaClass2(org.jpox.samples.rdbms.schema.SchemaClass2) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) Iterator(java.util.Iterator) Collection(java.util.Collection) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) HashSet(java.util.HashSet)

Example 27 with PersistenceManager

use of javax.jdo.PersistenceManager in project tests by datanucleus.

the class SchemaHandlerTest method testPrimaryKeyRetrieval.

/**
 * Test of the retrieval of PKs.
 */
public void testPrimaryKeyRetrieval() {
    addClassesToSchema(new Class[] { SchemaClass1.class, SchemaClass2.class });
    PersistenceManager pm = pmf.getPersistenceManager();
    RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
    // Retrieve the table for SchemaClass1
    ClassLoaderResolver clr = storeMgr.getNucleusContext().getClassLoaderResolver(null);
    DatastoreClass table1 = databaseMgr.getDatastoreClass(SchemaClass1.class.getName(), clr);
    DatastoreClass table2 = databaseMgr.getDatastoreClass(SchemaClass2.class.getName(), clr);
    // Check for the FK using the schema handler
    StoreSchemaHandler handler = databaseMgr.getSchemaHandler();
    Connection con = (Connection) databaseMgr.getConnectionManager().getConnection(((JDOPersistenceManager) pm).getExecutionContext()).getConnection();
    RDBMSTablePKInfo pkInfo1 = (RDBMSTablePKInfo) handler.getSchemaData(con, "primary-keys", new Object[] { table1 });
    RDBMSTablePKInfo pkInfo2 = (RDBMSTablePKInfo) handler.getSchemaData(con, "primary-keys", new Object[] { table2 });
    // Expecting 2 PK columns for SchemaClass1
    // TODO Enable checks on the PK name (when JDBC drivers return it correctly)
    assertEquals("Number of PKs for table " + table1 + " is wrong", 2, pkInfo1.getNumberOfChildren());
    PrimaryKeyInfo pk = (PrimaryKeyInfo) pkInfo1.getChild(0);
    assertEquals("Column Name is wrong", "TABLE1_ID1", ((String) pk.getProperty("column_name")).toUpperCase());
    // assertEquals("PK Name is wrong", "TABLE1_PK", pk.getProperty("pk_name"));
    pk = (PrimaryKeyInfo) pkInfo1.getChild(1);
    assertEquals("Column Name is wrong", "TABLE1_ID2", ((String) pk.getProperty("column_name")).toUpperCase());
    // assertEquals("PK Name is wrong", "TABLE1_PK", pk.getProperty("pk_name"));
    // Expecting 1 PK column for SchemaClass
    assertEquals("Number of PKs for table " + table1 + " is wrong", 1, pkInfo2.getNumberOfChildren());
    pk = (PrimaryKeyInfo) pkInfo2.getChild(0);
    assertEquals("Column Name is wrong", "TABLE2_ID", ((String) pk.getProperty("column_name")).toUpperCase());
// assertEquals("PK Name is wrong", "TABLE2_PK", pk.getProperty("pk_name"));
}
Also used : PrimaryKeyInfo(org.datanucleus.store.rdbms.schema.PrimaryKeyInfo) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) Connection(java.sql.Connection) SchemaClass1(org.jpox.samples.rdbms.schema.SchemaClass1) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) SchemaClass2(org.jpox.samples.rdbms.schema.SchemaClass2) StoreSchemaHandler(org.datanucleus.store.schema.StoreSchemaHandler) RDBMSTablePKInfo(org.datanucleus.store.rdbms.schema.RDBMSTablePKInfo) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 28 with PersistenceManager

use of javax.jdo.PersistenceManager in project tests by datanucleus.

the class TypeQueryTest method testQueryWithDateJdbcTypeChange.

/**
 * Test for persistence of a java.util.Date with a jdbc-type of "DATE" and queried against
 * an input date parameter.
 */
public void testQueryWithDateJdbcTypeChange() {
    try {
        // Persist some objects
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Calendar cal = Calendar.getInstance();
        cal.set(2005, 5, 20);
        Date cutOffDate = cal.getTime();
        try {
            tx.begin();
            DateHolder holder1 = new DateHolder();
            Calendar cal1 = Calendar.getInstance();
            cal1.set(2003, 6, 1);
            holder1.setDateField(cal1.getTime());
            DateHolder holder2 = new DateHolder();
            Calendar cal2 = Calendar.getInstance();
            cal2.set(2006, 6, 1);
            holder2.setDateField(cal2.getTime());
            pm.makePersistent(holder1);
            pm.makePersistent(holder2);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error("Exception thrown persisting object with date field", e);
            fail("Exception thrown persisting objects : " + e.getMessage());
            return;
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Query using an input parameter
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q = pm.newQuery(DateHolder.class, "dateField < :myDate");
            List results = (List) q.execute(cutOffDate);
            assertEquals("Number of objects returned from Query of date is wrong", 1, results.size());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error("Exception thrown querying object using date parameter", e);
            fail("Exception thrown querying object using date parameter with changed jdbc-type : " + e.getMessage());
            return;
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(DateHolder.class);
    }
}
Also used : DateHolder(org.jpox.samples.types.basic.DateHolder) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Calendar(java.util.Calendar) List(java.util.List) Date(java.util.Date)

Example 29 with PersistenceManager

use of javax.jdo.PersistenceManager in project tests by datanucleus.

the class TypesMappingTest method testMSSQLUniqueIdentifierType.

/**
 * Test for MSSQL datastore "UNIQUEIDENTIFIER" type.
 */
public void testMSSQLUniqueIdentifierType() {
    if (!vendorID.equals("sqlserver")) {
        return;
    }
    MSSQLTypes types;
    Object id = null;
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        types = new MSSQLTypes();
        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 = (MSSQLTypes) pm.getObjectById(id, true);
        assertEquals("UUIDString retrieved is not 36 chars", 36, types.getUuid().length());
        assertEquals("Simple String retrieved is wrong", "some string", types.getSimpleString());
        assertEquals("UUIDString retrieved is not 36 chars", 36, types.getAnotherString().length());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
    }
    tx = pm.currentTransaction();
    try {
        tx.begin();
        types = new MSSQLTypes();
        types.setUuid("6F9619FF-8B86-D011-B42D-00C04FC964FF");
        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 = (MSSQLTypes) pm.getObjectById(id, true);
        assertEquals("UUIDString retrieved is not 36 chars", 36, types.getUuid().length());
        assertEquals("UUIDString retrieved is not 6F9619FF-8B86-D011-B42D-00C04FC964FF", "6F9619FF-8B86-D011-B42D-00C04FC964FF", types.getUuid());
        assertEquals("Simple String retrieved is wrong", "some string", types.getSimpleString());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) MSSQLTypes(org.jpox.samples.rdbms.types.MSSQLTypes)

Example 30 with PersistenceManager

use of javax.jdo.PersistenceManager in project tests by datanucleus.

the class TypesMappingTest method testXMLType.

/**
 * Test for Oracles XMLType.
 * TODO Need to have Oracle XMLType in CLASSPATH for this
 */
public void testXMLType() {
    if (!vendorID.equals("oracle")) {
        return;
    }
    OracleTypes types;
    Object id = null;
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    String xml = "<jdo><package/></jdo>";
    String xml2 = "<jdo><package name=\"org.datanucleus\"/></jdo>";
    try {
        tx.begin();
        types = new OracleTypes();
        types.setXml(xml);
        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 = (OracleTypes) pm.getObjectById(id, true);
        assertEquals(xml, types.getXml());
        tx.commit();
        // test update
        tx.begin();
        types = (OracleTypes) pm.getObjectById(id, true);
        types.setXml(xml2);
        tx.commit();
        tx.begin();
        types = (OracleTypes) pm.getObjectById(id, true);
        pm.refresh(types);
        assertEquals(xml2, types.getXml());
        tx.commit();
        // test update to null
        tx.begin();
        types = (OracleTypes) pm.getObjectById(id, true);
        types.setXml(null);
        tx.commit();
        tx.begin();
        types = (OracleTypes) pm.getObjectById(id, true);
        pm.refresh(types);
        assertNull(types.getXml());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
    }
    // test null
    tx = pm.currentTransaction();
    try {
        tx.begin();
        types = new OracleTypes();
        types.setXml(null);
        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 = (OracleTypes) pm.getObjectById(id, true);
        assertNull(types.getXml());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) OracleTypes(org.datanucleus.samples.dbspecific.OracleTypes)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)1664 Transaction (javax.jdo.Transaction)1542 Query (javax.jdo.Query)671 List (java.util.List)397 JDOUserException (javax.jdo.JDOUserException)352 Collection (java.util.Collection)309 Iterator (java.util.Iterator)239 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)185 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)170 Person (org.jpox.samples.models.company.Person)146 Employee (org.jpox.samples.models.company.Employee)128 Manager (org.jpox.samples.models.company.Manager)107 HashSet (java.util.HashSet)101 ArrayList (java.util.ArrayList)85 SQLException (java.sql.SQLException)82 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)77 JDOException (javax.jdo.JDOException)74 Extent (javax.jdo.Extent)72 JDOFatalUserException (javax.jdo.JDOFatalUserException)68 JDODataStoreException (javax.jdo.JDODataStoreException)65