Search in sources :

Example 1 with SchemaClass2

use of org.jpox.samples.rdbms.schema.SchemaClass2 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 2 with SchemaClass2

use of org.jpox.samples.rdbms.schema.SchemaClass2 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 3 with SchemaClass2

use of org.jpox.samples.rdbms.schema.SchemaClass2 in project tests by datanucleus.

the class SchemaHandlerTest method testIndexRetrieval.

/**
 * Test of the retrieval of indices.
 */
public void testIndexRetrieval() {
    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 indices using the schema handler
    StoreSchemaHandler handler = databaseMgr.getSchemaHandler();
    Connection con = (Connection) databaseMgr.getConnectionManager().getConnection(((JDOPersistenceManager) pm).getExecutionContext()).getConnection();
    RDBMSTableIndexInfo indexInfo = (RDBMSTableIndexInfo) handler.getSchemaData(con, "indices", new Object[] { table1 });
    int numIndices = 3;
    if (vendorID.equals("hsql")) {
        // HSQL will create an index for the FK without asking, and we can't replace it with our own so end up with two
        numIndices = 4;
    }
    assertEquals("Number of Indices for table " + table1 + " is wrong", numIndices, indexInfo.getNumberOfChildren());
    Iterator indexIter = indexInfo.getChildren().iterator();
    while (indexIter.hasNext()) {
        IndexInfo index = (IndexInfo) indexIter.next();
        String columnName = ((String) index.getProperty("column_name")).toUpperCase();
        boolean unique = !((Boolean) index.getProperty("non_unique")).booleanValue();
        if (columnName.equals("OTHER_ID")) {
            assertFalse("Index for column " + columnName + " is unique!", unique);
        } else if (columnName.equals("TABLE1_ID1")) {
            assertTrue("Index for column " + columnName + " is not unique!", unique);
        } else if (columnName.equals("TABLE1_ID2")) {
            assertTrue("Index for column " + columnName + " is not unique!", unique);
        } else {
            fail("Unexpected index " + columnName + " for table " + table1);
        }
    }
    indexInfo = (RDBMSTableIndexInfo) handler.getSchemaData(con, "indices", new Object[] { table2 });
    assertEquals("Number of Indices for table " + table2 + " is wrong", 2, indexInfo.getNumberOfChildren());
    indexIter = indexInfo.getChildren().iterator();
    while (indexIter.hasNext()) {
        IndexInfo index = (IndexInfo) indexIter.next();
        String columnName = ((String) index.getProperty("column_name")).toUpperCase();
        String indexName = ((String) index.getProperty("index_name")).toUpperCase();
        boolean unique = !((Boolean) index.getProperty("non_unique")).booleanValue();
        if (columnName.equals("VALUE")) {
            assertFalse("Index for column " + columnName + " is unique!", unique);
            assertEquals("Index name for column " + columnName + " is wrong!", "VALUE_IDX", indexName);
        } else if (columnName.equals("TABLE2_ID")) {
            assertTrue("Index for column " + columnName + " is not unique!", unique);
        } else {
            fail("Unexpected index " + columnName + " for table " + table1);
        }
    }
}
Also used : JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) RDBMSTableIndexInfo(org.datanucleus.store.rdbms.schema.RDBMSTableIndexInfo) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) Connection(java.sql.Connection) SchemaClass1(org.jpox.samples.rdbms.schema.SchemaClass1) SchemaClass2(org.jpox.samples.rdbms.schema.SchemaClass2) StoreSchemaHandler(org.datanucleus.store.schema.StoreSchemaHandler) RDBMSTableIndexInfo(org.datanucleus.store.rdbms.schema.RDBMSTableIndexInfo) IndexInfo(org.datanucleus.store.rdbms.schema.IndexInfo) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) Iterator(java.util.Iterator) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass)

Aggregations

Connection (java.sql.Connection)3 PersistenceManager (javax.jdo.PersistenceManager)3 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)3 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)3 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)3 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)3 StoreSchemaHandler (org.datanucleus.store.schema.StoreSchemaHandler)3 SchemaClass1 (org.jpox.samples.rdbms.schema.SchemaClass1)3 SchemaClass2 (org.jpox.samples.rdbms.schema.SchemaClass2)3 Iterator (java.util.Iterator)2 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 IndexInfo (org.datanucleus.store.rdbms.schema.IndexInfo)1 PrimaryKeyInfo (org.datanucleus.store.rdbms.schema.PrimaryKeyInfo)1 RDBMSColumnInfo (org.datanucleus.store.rdbms.schema.RDBMSColumnInfo)1 RDBMSTableIndexInfo (org.datanucleus.store.rdbms.schema.RDBMSTableIndexInfo)1 RDBMSTableInfo (org.datanucleus.store.rdbms.schema.RDBMSTableInfo)1 RDBMSTablePKInfo (org.datanucleus.store.rdbms.schema.RDBMSTablePKInfo)1