Search in sources :

Example 6 with Configuration

use of org.datanucleus.Configuration in project datanucleus-core by datanucleus.

the class FrequentlyAccessedPropertiesTest method testConfiguration.

@Test
public void testConfiguration() {
    Map<String, Object> props = new HashMap<String, Object>();
    props.put(PropertyNames.PROPERTY_DETACH_ON_CLOSE, "true");
    props.put(PropertyNames.PROPERTY_OPTIMISTIC, "true");
    props.put("datanucleus.storeManagerType", StoreManagerStub.class.getName());
    PersistenceNucleusContextImpl ctx = new PersistenceNucleusContextImpl(null, props) {

        private static final long serialVersionUID = 6287389368679465707L;

        @Override
        public synchronized void initialise() {
        }
    };
    Configuration conf = ctx.getConfiguration();
    Assert.assertFalse(conf.getFrequentProperties().getDetachAllOnCommit());
    Assert.assertTrue(conf.getFrequentProperties().getDetachOnClose());
    ExecutionContextImpl ec = new ExecutionContextImpl(ctx, null, new HashMap<String, Object>());
    Assert.assertTrue(ec.getTransaction().getOptimistic());
    ec.setProperty(PropertyNames.PROPERTY_OPTIMISTIC, "false");
    Assert.assertFalse(ec.getTransaction().getOptimistic());
    Assert.assertTrue(conf.getFrequentProperties().getOptimisticTransaction());
}
Also used : Configuration(org.datanucleus.Configuration) HashMap(java.util.HashMap) ExecutionContextImpl(org.datanucleus.ExecutionContextImpl) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) Test(org.junit.Test)

Example 7 with Configuration

use of org.datanucleus.Configuration 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)

Aggregations

Configuration (org.datanucleus.Configuration)7 HashMap (java.util.HashMap)4 Map (java.util.Map)3 Properties (java.util.Properties)2 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)2 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)2 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 Entry (java.util.Map.Entry)1 StringTokenizer (java.util.StringTokenizer)1 ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)1 ExecutionContextImpl (org.datanucleus.ExecutionContextImpl)1 PersistenceNucleusContext (org.datanucleus.PersistenceNucleusContext)1 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)1 NucleusException (org.datanucleus.exceptions.NucleusException)1