Search in sources :

Example 36 with JDOPersistenceManagerFactory

use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tests by datanucleus.

the class DN2NamingFactoryTest method testJoinTableName.

public void testJoinTableName() {
    JDOPersistenceManagerFactory jdoPMF = (JDOPersistenceManagerFactory) pmf;
    NucleusContext nucCtx = jdoPMF.getNucleusContext();
    ClassLoaderResolver clr = nucCtx.getClassLoaderResolver(null);
    MetaDataManager mmgr = nucCtx.getMetaDataManager();
    AbstractClassMetaData cmd1 = mmgr.getMetaDataForClass(UserGroup.class, clr);
    DN2NamingFactory factory = new DN2NamingFactory(nucCtx);
    factory.setNamingCase(NamingCase.LOWER_CASE);
    factory.setMaximumLength(SchemaComponent.TABLE, 128);
    String name = factory.getTableName(cmd1.getMetaDataForMember("members"));
    assertEquals("Join table name is incorrect", "usergroup_members", name);
}
Also used : DN2NamingFactory(org.datanucleus.store.schema.naming.DN2NamingFactory) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) MetaDataManager(org.datanucleus.metadata.MetaDataManager) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 37 with JDOPersistenceManagerFactory

use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tests by datanucleus.

the class DN2NamingFactoryTest method testNestedEmbeddedPCColumnNames.

/**
 * Test where the sample classes have no EmbeddedMetaData overriding the column names, so we just get generated names for embedded class and nested embedded class.
 */
public void testNestedEmbeddedPCColumnNames() {
    JDOPersistenceManagerFactory jdoPMF = (JDOPersistenceManagerFactory) pmf;
    NucleusContext nucCtx = jdoPMF.getNucleusContext();
    ClassLoaderResolver clr = nucCtx.getClassLoaderResolver(null);
    MetaDataManager mmgr = nucCtx.getMetaDataManager();
    DN2NamingFactory factory = new DN2NamingFactory(nucCtx);
    factory.setMaximumLength(SchemaComponent.COLUMN, 128);
    factory.setNamingCase(NamingCase.LOWER_CASE);
    AbstractClassMetaData emb1Cmd = mmgr.getMetaDataForClass(EmbCls1.class, clr);
    AbstractMemberMetaData embCls2aMmd = emb1Cmd.getMetaDataForMember("embCls2a");
    AbstractMemberMetaData embCls2bMmd = emb1Cmd.getMetaDataForMember("embCls2b");
    AbstractClassMetaData emb2Cmd = mmgr.getMetaDataForClass(EmbCls2.class, clr);
    AbstractMemberMetaData embCls3Mmd = emb2Cmd.getMetaDataForMember("embCls3");
    AbstractMemberMetaData cls2NameMmd = emb2Cmd.getMetaDataForMember("cls2Name");
    AbstractClassMetaData emb3Cmd = mmgr.getMetaDataForClass(EmbCls3.class, clr);
    AbstractMemberMetaData cls3NameMmd = emb3Cmd.getMetaDataForMember("cls3Name");
    List<AbstractMemberMetaData> colMmds = new ArrayList<AbstractMemberMetaData>();
    colMmds.add(embCls2aMmd);
    colMmds.add(cls2NameMmd);
    String colName = factory.getColumnName(colMmds, 0);
    assertEquals("embcls2a_cls2name", colName);
    colMmds.clear();
    colMmds.add(embCls2bMmd);
    colMmds.add(cls2NameMmd);
    colName = factory.getColumnName(colMmds, 0);
    assertEquals("embcls2b_cls2name", colName);
    colMmds.clear();
    colMmds.add(embCls2aMmd);
    colMmds.add(embCls3Mmd);
    colMmds.add(cls3NameMmd);
    colName = factory.getColumnName(colMmds, 0);
    assertEquals("embcls2a_embcls3_cls3name", colName);
    colMmds.clear();
    colMmds.add(embCls2bMmd);
    colMmds.add(embCls3Mmd);
    colMmds.add(cls3NameMmd);
    colName = factory.getColumnName(colMmds, 0);
    assertEquals("embcls2b_embcls3_cls3name", colName);
}
Also used : DN2NamingFactory(org.datanucleus.store.schema.naming.DN2NamingFactory) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) ArrayList(java.util.ArrayList) MetaDataManager(org.datanucleus.metadata.MetaDataManager) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 38 with JDOPersistenceManagerFactory

use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tests by datanucleus.

the class IdentifierFactoryTest method testJPA.

/**
 * Tests for JPAIdentifierFactory.
 */
public void testJPA() {
    RDBMSStoreManager srm = (RDBMSStoreManager) storeMgr;
    IdentifierFactory idFactory = null;
    ClassLoaderResolver clr = new ClassLoaderResolverImpl();
    try {
        JDOPersistenceManagerFactory thePMF = (JDOPersistenceManagerFactory) pmf;
        Map props = new HashMap();
        Configuration conf = getConfigurationForPMF(thePMF);
        if (conf.getStringProperty("datanucleus.mapping.Catalog") != null) {
            props.put("DefaultCatalog", conf.getStringProperty("datanucleus.mapping.Catalog"));
        }
        if (conf.getStringProperty("datanucleus.mapping.Schema") != null) {
            props.put("DefaultSchema", conf.getStringProperty("datanucleus.mapping.Schema"));
        }
        if (conf.getStringProperty("datanucleus.identifier.case") != null) {
            props.put("RequiredCase", conf.getStringProperty("datanucleus.identifier.case"));
        } else {
            props.put("RequiredCase", srm.getDefaultIdentifierCase());
        }
        if (conf.getStringProperty("datanucleus.identifier.wordSeparator") != null) {
            props.put("WordSeparator", conf.getStringProperty("datanucleus.identifier.wordSeparator"));
        }
        if (conf.getStringProperty("datanucleus.identifier.tablePrefix") != null) {
            props.put("TablePrefix", conf.getStringProperty("datanucleus.identifier.tablePrefix"));
        }
        if (conf.getStringProperty("datanucleus.identifier.tableSuffix") != null) {
            props.put("TableSuffix", conf.getStringProperty("datanucleus.identifier.tableSuffix"));
        }
        Class cls = Class.forName("org.datanucleus.store.rdbms.identifier.JPAIdentifierFactory");
        Class[] argTypes = new Class[] { DatastoreAdapter.class, ClassLoaderResolver.class, Map.class };
        Object[] args = new Object[] { srm.getDatastoreAdapter(), srm.getNucleusContext().getClassLoaderResolver(null), props };
        idFactory = (IdentifierFactory) ClassUtils.newInstance(cls, argTypes, args);
    } catch (Exception e) {
        fail("Error creating JPAIdentifierFactory : " + e.getMessage());
    }
    // 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_EMPLOYEE".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("MY_COLUMN_NAME");
    assertTrue("newColumnIdentifier(String) has returned an incorrect name when should have used the supplied name " + id.getName(), "MY_COLUMN_NAME".equalsIgnoreCase(id.getName()));
    // c). Discriminator column identifier
    id = idFactory.newDiscriminatorFieldIdentifier();
    assertTrue("newDiscriminatorFieldIdentifier() has returned an incorrect name : " + id.getName(), "DTYPE".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(), "SUBORDINATES_ORDER".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_MANAGER_ID".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(), "SUBORDINATES_EMPLOYEE_ID".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, true, FieldRole.ROLE_OWNER);
    assertTrue("newForeignKeyFieldIdentifier(OWNER) has returned an incorrect generated name " + id.getName(), "MANAGER_MANAGER_ID".equalsIgnoreCase(id.getName()));
// Primary key identifiers
// Index identifiers
// Foreign key identifiers
// Candidate key identifiers
// Sequence identifiers
}
Also used : Configuration(org.datanucleus.Configuration) HashMap(java.util.HashMap) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPOXIdentifierFactory(org.datanucleus.store.rdbms.identifier.JPOXIdentifierFactory) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) HashMap(java.util.HashMap) Map(java.util.Map) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 39 with JDOPersistenceManagerFactory

use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tests by datanucleus.

the class TestRunListener method cleanupDatastore.

private void cleanupDatastore(int number) {
    String datastoreProtocol = TestHelper.getDatastorePluginProtocol(number);
    if (// Avoid creating PMF when clean up may not be supported
    datastoreProtocol != null && !datastoreProtocol.equals("jdbc")) {
        LOG.info("Datastore clean up not supported for datastore=" + datastoreProtocol);
        return;
    }
    JDOPersistenceManagerFactory pmf = (JDOPersistenceManagerFactory) JDOPersistenceTestCase.getPMF(number, null);
    PersistenceNucleusContext ctx = pmf.getNucleusContext();
    if (ctx.getStoreManager() instanceof RDBMSStoreManager) {
        PersistenceManager pm = null;
        JDOConnection jdoConnection = null;
        Connection nativeConnection = null;
        try {
            pm = pmf.getPersistenceManager();
            jdoConnection = pm.getDataStoreConnection();
            // Obtain the connection via PM instead of using the PMF properties so we support DataSource too
            nativeConnection = (Connection) jdoConnection.getNativeConnection();
            cleanupRDBMSdatastore(nativeConnection);
        } catch (SQLException e) {
            LOG.error("Error during datastore clean up", e);
        } finally {
            if (jdoConnection != null) {
                jdoConnection.close();
            }
            if (pm != null) {
                pm.close();
            }
        }
    } else {
        // TODO Support cleanup of mongodb, Cassandra, Excel, ODF etc
        LOG.info("Datastore clean up not supported");
    }
    pmf.close();
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) SQLException(java.sql.SQLException) PersistenceNucleusContext(org.datanucleus.PersistenceNucleusContext) Connection(java.sql.Connection) JDOConnection(javax.jdo.datastore.JDOConnection) JDOConnection(javax.jdo.datastore.JDOConnection) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Aggregations

JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)39 PersistenceManager (javax.jdo.PersistenceManager)15 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)14 Transaction (javax.jdo.Transaction)14 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)14 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)12 NucleusContext (org.datanucleus.NucleusContext)9 Query (javax.jdo.Query)8 MetaDataManager (org.datanucleus.metadata.MetaDataManager)7 DN2NamingFactory (org.datanucleus.store.schema.naming.DN2NamingFactory)7 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)6 SQLException (java.sql.SQLException)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 JDOException (javax.jdo.JDOException)4 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)4 IOException (java.io.IOException)3 Field (java.lang.reflect.Field)3 ArrayList (java.util.ArrayList)3