Search in sources :

Example 91 with ClassLoaderResolver

use of org.datanucleus.ClassLoaderResolver 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 92 with ClassLoaderResolver

use of org.datanucleus.ClassLoaderResolver 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 93 with ClassLoaderResolver

use of org.datanucleus.ClassLoaderResolver in project tests by datanucleus.

the class ConvertersTest method testUseOfPersistentConverter.

/**
 * Test the use of "@Persistent(converter="...")" annotation on a field.
 */
public void testUseOfPersistentConverter() {
    try {
        PersistenceNucleusContext nucCtx = ((JDOPersistenceManagerFactory) pmf).getNucleusContext();
        ClassLoaderResolver clr = nucCtx.getClassLoaderResolver(null);
        AbstractClassMetaData cmd = nucCtx.getMetaDataManager().getMetaDataForClass(PersonWithConverters.class, clr);
        // Check the converter is registered with metadata
        AbstractMemberMetaData mmd = cmd.getMetaDataForMember("myBool1");
        assertNotNull(mmd);
        assertTrue(mmd.hasExtension("type-converter-name"));
        String converterName = mmd.getValueForExtension("type-converter-name");
        assertEquals("org.datanucleus.samples.converters.Boolean10Converter", converterName);
        // Check the correct mapping is chosen for this field
        RDBMSStoreManager storeMgr = (RDBMSStoreManager) nucCtx.getStoreManager();
        DatastoreClass tbl = storeMgr.getDatastoreClass(PersonWithConverters.class.getName(), clr);
        JavaTypeMapping mapping = tbl.getMemberMapping(mmd);
        assertTrue(mapping instanceof TypeConverterMapping);
    } catch (Exception e) {
        LOG.error("Exception during test", e);
        fail("Exception was thrown : " + e.getMessage());
    } finally {
    }
}
Also used : TypeConverterMapping(org.datanucleus.store.rdbms.mapping.java.TypeConverterMapping) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) PersonWithConverters(org.datanucleus.samples.converters.PersonWithConverters) PersistenceNucleusContext(org.datanucleus.PersistenceNucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 94 with ClassLoaderResolver

use of org.datanucleus.ClassLoaderResolver in project tests by datanucleus.

the class ConvertersTest method testUseOfConvert.

/**
 * Test the use of "@Convert" annotation on a field.
 */
public void testUseOfConvert() {
    try {
        PersistenceNucleusContext nucCtx = ((JDOPersistenceManagerFactory) pmf).getNucleusContext();
        ClassLoaderResolver clr = nucCtx.getClassLoaderResolver(null);
        AbstractClassMetaData cmd = nucCtx.getMetaDataManager().getMetaDataForClass(PersonWithConverters.class, clr);
        // Check the converter is registered with metadata
        AbstractMemberMetaData mmd = cmd.getMetaDataForMember("myBool2");
        assertNotNull(mmd);
        assertTrue(mmd.hasExtension("type-converter-name"));
        String converterName = mmd.getValueForExtension("type-converter-name");
        assertEquals("org.datanucleus.samples.converters.BooleanYNConverter", converterName);
        // Check the correct mapping is chosen for this field
        RDBMSStoreManager storeMgr = (RDBMSStoreManager) nucCtx.getStoreManager();
        DatastoreClass tbl = storeMgr.getDatastoreClass(PersonWithConverters.class.getName(), clr);
        JavaTypeMapping mapping = tbl.getMemberMapping(mmd);
        assertTrue(mapping instanceof TypeConverterMapping);
    } catch (Exception e) {
        LOG.error("Exception during test", e);
        fail("Exception was thrown : " + e.getMessage());
    } finally {
    }
}
Also used : TypeConverterMapping(org.datanucleus.store.rdbms.mapping.java.TypeConverterMapping) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) PersonWithConverters(org.datanucleus.samples.converters.PersonWithConverters) PersistenceNucleusContext(org.datanucleus.PersistenceNucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 95 with ClassLoaderResolver

use of org.datanucleus.ClassLoaderResolver in project tests by datanucleus.

the class IdentifierFactoryTest method testDataNucleus2.

/**
 * Tests for DNIdentifierFactory
 */
public void testDataNucleus2() {
    RDBMSStoreManager srm = (RDBMSStoreManager) storeMgr;
    IdentifierFactory idFactory = srm.getIdentifierFactory();
    ClassLoaderResolver clr = new ClassLoaderResolverImpl();
    // Table identifiers
    // a). generated name shorter than max length
    DatastoreIdentifier id = idFactory.newIdentifier(IdentifierType.TABLE, "MyClass");
    assertTrue("newIdentifier(TABLE, String) has generated an incorrect name : " + id.getName(), "MYCLASS".equalsIgnoreCase(id.getName()));
    // b). specified name shorter than max length
    id = idFactory.newTableIdentifier("MY_TABLE_NAME");
    assertTrue("newDatastoreContainerIdentifier(String) has returned an incorrect name when should have used the supplied name " + id.getName(), "MY_TABLE_NAME".equalsIgnoreCase(id.getName()));
    // c). name specified via ClassMetaData
    AbstractClassMetaData managerCMD = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass("org.jpox.samples.models.company.Manager", clr);
    id = idFactory.newTableIdentifier(managerCMD);
    assertTrue("newDatastoreContainerIdentifier(clr, ClassMetaData) has returned an incorrect generated name " + id.getName(), "MANAGER".equalsIgnoreCase(id.getName()));
    // d). name specified via ClassMetaData
    AbstractMemberMetaData fmd = managerCMD.getMetaDataForMember("subordinates");
    id = idFactory.newTableIdentifier(fmd);
    assertTrue("newDatastoreContainerIdentifier(clr, AbstractMemberMetaData) has returned an incorrect generated name " + id.getName(), "MANAGER_SUBORDINATES".equalsIgnoreCase(id.getName()));
    // Column identifiers
    // a). generated name shorter than max length
    id = idFactory.newIdentifier(IdentifierType.COLUMN, "myField");
    assertTrue("newIdentifier(COLUMN, String) has generated an incorrect name : " + id.getName(), "MYFIELD".equalsIgnoreCase(id.getName()));
    // b). specified name shorter than max length
    id = idFactory.newColumnIdentifier("MYCOLUMNNAME");
    assertTrue("newColumnIdentifier(String) has returned an incorrect name when should have used the supplied name " + id.getName(), "MYCOLUMNNAME".equalsIgnoreCase(id.getName()));
    // c). Discriminator column identifier
    id = idFactory.newDiscriminatorFieldIdentifier();
    assertTrue("newDiscriminatorFieldIdentifier() has returned an incorrect name : " + id.getName(), "DISCRIMINATOR".equalsIgnoreCase(id.getName()));
    // d). Version column identifier
    id = idFactory.newVersionFieldIdentifier();
    assertTrue("newVersionFieldIdentifier() has returned an incorrect name : " + id.getName(), "VERSION".equalsIgnoreCase(id.getName()));
    // e). Index (ordering) column identifier
    id = idFactory.newIndexFieldIdentifier(fmd);
    assertTrue("newIndexFieldIdentifier() has returned an incorrect name : " + id.getName(), "IDX".equalsIgnoreCase(id.getName()));
    // f). Adapter Index column identifier
    id = idFactory.newAdapterIndexFieldIdentifier();
    assertTrue("newAdapterIndexFieldIdentifier() has returned an incorrect name : " + id.getName(), "IDX".equalsIgnoreCase(id.getName()));
    AbstractMemberMetaData[] relatedMmds = fmd.getRelatedMemberMetaData(clr);
    // g). join table owner column identifier (1-N bi JoinTable)
    DatastoreIdentifier destId = idFactory.newColumnIdentifier("MANAGER_ID");
    id = idFactory.newJoinTableFieldIdentifier(fmd, relatedMmds != null ? relatedMmds[0] : null, destId, false, FieldRole.ROLE_OWNER);
    assertTrue("newJoinTableFieldIdentifier(OWNER) has returned an incorrect generated name " + id.getName(), "MANAGER_ID_OID".equalsIgnoreCase(id.getName()));
    // h). join table element column identifier (1-N bi JoinTable)
    destId = idFactory.newColumnIdentifier("EMPLOYEE_ID");
    id = idFactory.newJoinTableFieldIdentifier(fmd, relatedMmds != null ? relatedMmds[0] : null, destId, false, FieldRole.ROLE_COLLECTION_ELEMENT);
    assertTrue("newJoinTableFieldIdentifier(ELEMENT) has returned an incorrect generated name " + id.getName(), "EMPLOYEE_ID_EID".equalsIgnoreCase(id.getName()));
    // i). FK owner column identifier (1-N bi FK)
    AbstractMemberMetaData deptsFMD = managerCMD.getMetaDataForMember("departments");
    AbstractMemberMetaData[] deptsRelatedMmds = deptsFMD.getRelatedMemberMetaData(clr);
    destId = idFactory.newColumnIdentifier("MANAGER_ID");
    id = idFactory.newForeignKeyFieldIdentifier(deptsFMD, deptsRelatedMmds != null ? deptsRelatedMmds[0] : null, destId, false, FieldRole.ROLE_OWNER);
    assertTrue("newForeignKeyFieldIdentifier(OWNER) has returned an incorrect generated name " + id.getName(), "MANAGER_MANAGER_ID_OID".equalsIgnoreCase(id.getName()));
// Primary key identifiers
// Index identifiers
// Foreign key identifiers
// Candidate key identifiers
// Sequence identifiers
}
Also used : DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPOXIdentifierFactory(org.datanucleus.store.rdbms.identifier.JPOXIdentifierFactory) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Aggregations

ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)242 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)94 MetaDataManager (org.datanucleus.metadata.MetaDataManager)72 NucleusContext (org.datanucleus.NucleusContext)68 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)65 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)65 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)56 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)56 ClassMetaData (org.datanucleus.metadata.ClassMetaData)54 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)51 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)44 NucleusException (org.datanucleus.exceptions.NucleusException)42 PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)40 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)40 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)39 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)37 ArrayList (java.util.ArrayList)36 ExecutionContext (org.datanucleus.ExecutionContext)32 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)30 HashMap (java.util.HashMap)28