Search in sources :

Example 16 with JDOPersistenceManagerFactory

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

the class PersistenceManagerFactoryTest method testJDOHelperInstantiation.

/**
 * Test instantiating a <code>PersistenceManagerFactory</code> via
 * <code>JDOHelper.getPersistenceManagerFactory(Properties)</code>.
 */
public void testJDOHelperInstantiation() {
    /*
         * Things to consider:
         *  - Unknown properties should do nothing.
         *  - Setting Optimistic or RetainValues to true sets NontransactionalRead
         *     to true.
         *  - TransactionIsolation has valid values of Connection.TRANSACTION_*;
         *     anything else will throw an Exception.
         *  - A PersistenceManagerFactory obtained via JDOHelper should be
         *     nonconfigurable.
         */
    /*
         * 1) Test setting all propers to valid values.
         */
    boolean optimistic = true;
    boolean retainValues = true;
    boolean restoreValues = true;
    boolean ignoreCache = true;
    boolean nontransactionalRead = true;
    boolean nontransactionalWrite = false;
    boolean multithreaded = true;
    Properties dbProps = TestHelper.getPropertiesForDatastore(1);
    String driverName = dbProps.getProperty("datanucleus.ConnectionDriverName");
    String url = dbProps.getProperty("datanucleus.ConnectionURL");
    String userName = dbProps.getProperty("datanucleus.ConnectionUserName");
    String password = dbProps.getProperty("datanucleus.ConnectionPassword");
    boolean validateTables = true;
    boolean validateConstraints = true;
    boolean autoCreateTables = true;
    boolean autoCreateConstraints = true;
    int transactionIsolation = Connection.TRANSACTION_READ_COMMITTED;
    PMFProperties props = new PMFProperties();
    props.setOptimistic(optimistic);
    props.setRetainValues(retainValues);
    // This throws an exception.  Test later.
    props.setRestoreValues(restoreValues);
    props.setIgnoreCache(ignoreCache);
    props.setNontransactionalRead(nontransactionalRead);
    props.setNontransactionalWrite(nontransactionalWrite);
    props.setMultithreaded(multithreaded);
    if (driverName != null) {
        props.setDriverName(driverName);
    }
    if (userName != null) {
        props.setUserName(userName);
    }
    if (password != null) {
        props.setPassword(password);
    }
    props.setURL(url);
    // props.setFactoryName(factory1);  // Setting this uses jndi which blows up w/out
    // props.setFactory2Name(factory2); // a managed environment (ie - no JNDI).
    props.setValidateTables(validateTables);
    props.setValidateConstraints(validateConstraints);
    props.setAutoCreateTables(autoCreateTables);
    props.setAutoCreateConstraints(autoCreateConstraints);
    props.setTransactionIsolation(transactionIsolation);
    PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertTrue("PMF should be org.datanucleus.api.jdo.JDOPersistenceManagerFactory.", (pmf instanceof JDOPersistenceManagerFactory));
    assertTrue("PMF from JDOHelper doesn't match expected properties (" + props.toString() + ").", props.matchesPMF(pmf));
    pmf.close();
    // Test whether NonTransactionalWrite was correctly handled
    props.setNontransactionalWrite(true);
    try {
        pmf = JDOHelper.getPersistenceManagerFactory(props);
    } catch (Exception e) {
        fail("Setting of nontransactionalWrite shouldnt have caused an Exception but did : " + e.getMessage());
    }
    props.setNontransactionalWrite(false);
    /*
         * Test whether NonTransactionalRead was correctly set 
         */
    props.setNontransactionalRead(false);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("NontransactionalRead should be false", pmf.getNontransactionalRead());
    pmf.close();
    props.setNontransactionalRead(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertTrue("NontransactionalRead should be true", pmf.getNontransactionalRead());
    pmf.close();
    props.setNontransactionalRead(false);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("NontransactionalRead should be false", pmf.getNontransactionalRead());
    pmf.close();
    props.setNontransactionalRead(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertTrue("NontransactionalRead should be true", pmf.getNontransactionalRead());
    pmf.close();
    /*
         * Test whether RetainValues was correctly set 
         */
    props.setRetainValues(false);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("RetainValues should be false", pmf.getRetainValues());
    pmf.close();
    props.setRetainValues(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertTrue("RetainValues should be true", pmf.getRetainValues());
    pmf.close();
    props.setRetainValues(false);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("RetainValues should be false", pmf.getRetainValues());
    pmf.close();
    props.setRetainValues(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertTrue("RetainValues should be true", pmf.getRetainValues());
    pmf.close();
    /*
         * Test whether RestoreValues was correctly set 
         */
    props.setRestoreValues(false);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("RestoreValues should be false", pmf.getRestoreValues());
    pmf.close();
    props.setRestoreValues(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertTrue("RestoreValues should be true", pmf.getRestoreValues());
    pmf.close();
    props.setRestoreValues(false);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("RestoreValues should be false", pmf.getRestoreValues());
    pmf.close();
    props.setRestoreValues(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertTrue("RestoreValues should be true", pmf.getRestoreValues());
    pmf.close();
    /*
         * Test whether Optimistic was correctly set 
         */
    props.setOptimistic(false);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("Optimistic should be false", pmf.getOptimistic());
    pmf.close();
    props.setOptimistic(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertTrue("Optimistic should be true", pmf.getOptimistic());
    pmf.close();
    props.setOptimistic(false);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("Optimistic should be false", pmf.getOptimistic());
    pmf.close();
    props.setOptimistic(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertTrue("Optimistic should be true", pmf.getOptimistic());
    pmf.close();
    /*
         * Test changing one property does not change other property JDO 2 ED
         * spec 5.6 NontransactionalRead, NontransactionalWrite, Optimistic, and
         * RetainValues are independent options. A JDO implementation must not
         * automatically change the values of these properties as a side effect
         * of the user changing other properties.
         */
    props.setNontransactionalRead(false);
    props.setRetainValues(false);
    props.setOptimistic(false);
    props.setRestoreValues(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("NontransactionalRead should be false", pmf.getNontransactionalRead());
    assertFalse("RetainValues should be false", pmf.getRetainValues());
    assertFalse("Optimistic should be false", pmf.getOptimistic());
    assertTrue("RestoreValues should be true", pmf.getRestoreValues());
    pmf.close();
    props.setRestoreValues(false);
    props.setNontransactionalRead(false);
    props.setRetainValues(false);
    props.setOptimistic(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("RestoreValues should be false", pmf.getRestoreValues());
    assertFalse("NontransactionalRead should be false", pmf.getNontransactionalRead());
    assertFalse("RetainValues should be false", pmf.getRetainValues());
    assertTrue("Optimistic should be true", pmf.getOptimistic());
    pmf.close();
    props.setOptimistic(false);
    props.setRestoreValues(false);
    props.setNontransactionalRead(false);
    props.setRetainValues(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("Optimistic should be false", pmf.getOptimistic());
    assertFalse("RestoreValues should be false", pmf.getRestoreValues());
    assertFalse("NontransactionalRead should be false", pmf.getNontransactionalRead());
    assertTrue("RetainValues should be true", pmf.getRetainValues());
    pmf.close();
    props.setRetainValues(false);
    props.setOptimistic(false);
    props.setRestoreValues(false);
    props.setNontransactionalRead(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("RetainValues should be false", pmf.getRetainValues());
    assertFalse("Optimistic should be false", pmf.getOptimistic());
    assertFalse("RestoreValues should be false", pmf.getRestoreValues());
    assertTrue("NontransactionalRead should be true", pmf.getNontransactionalRead());
    pmf.close();
    /*
         * 2) Test that an invalid driver name throws an exception
         */
    if (vendorID != null) {
        props.setDriverName("my.test.driver.should.not.exist");
        try {
            pmf = JDOHelper.getPersistenceManagerFactory(props);
            assertTrue("Invalid driver name should have thrown exception.", false);
            pmf.close();
        } catch (Exception e) {
        /* Ignore */
        }
    }
    /*
         * 3) Test that setting an invalid transaction isolation throws an exception.
         */
    if (vendorID != null) {
        props.setDriverName(driverName);
        props.setTransactionIsolation(8738);
        try {
            pmf = JDOHelper.getPersistenceManagerFactory(props);
            assertTrue("Setting invalid transaction isolation should have thrown exception.", false);
            pmf.close();
        } catch (Exception e) {
        /* Ignore */
        }
    }
    /*
         * 4) Test that PMF is not configurable after getting an instance.
         */
    try {
        if (vendorID != null) {
            props.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
        }
        pmf = JDOHelper.getPersistenceManagerFactory(props);
        assertTrue("PMF from JDOHelper doesn't match expected properties (" + props.toString() + ").", props.matchesPMF(pmf));
        pmf.setConnectionUserName("bobjones");
        assertTrue("Setting properties after frozen should throw exception.", false);
        pmf.close();
    } catch (JDOUserException e) {
    /* Ignore */
    }
    /*
         * 5) Test return data from getProperties
         */
    Properties pmf_props = pmf.getProperties();
    String vendor = pmf_props.getProperty("VendorName");
    if (vendor == null) {
        assertTrue("PMF doesnt return VendorName property", false);
    }
    String version = pmf_props.getProperty("VersionNumber");
    if (version == null) {
        assertTrue("PMF doesnt return VersionNumber property", false);
    }
    /*
         * 6) Test that setting an unknown property does nothing weird.
         */
    props.setProperty("datanucleus.MyTestUnknownProperty", "unknown");
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertTrue("PMF from JDOHelper doesn't match expected properties (" + props.toString() + ").", props.matchesPMF(pmf));
    pmf.close();
}
Also used : PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) Properties(java.util.Properties) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) JDOUserException(javax.jdo.JDOUserException) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) JDOFatalUserException(javax.jdo.JDOFatalUserException)

Example 17 with JDOPersistenceManagerFactory

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

the class DynamicEnhanceSchemaToolTest method schemaCreate.

public void schemaCreate(DynamicEnhanceSchemaToolClassLoader runtimeCL) throws Exception {
    Map props = getPropertiesForDatastore(runtimeCL);
    JDOPersistenceManagerFactory pmf = (JDOPersistenceManagerFactory) JDOHelper.getPersistenceManagerFactory(props);
    try {
        JDOMetadata filemd = pmf.newMetadata();
        createMetadata(filemd);
        pmf.registerMetadata(filemd);
        Set<String> classNames = new HashSet<>();
        classNames.add("test.Client");
        PersistenceNucleusContext nucCtx = pmf.getNucleusContext();
        StoreManager storeMgr = nucCtx.getStoreManager();
        if (!(storeMgr instanceof SchemaAwareStoreManager)) {
            // Can't create schema with this datastore
            return;
        }
        try {
            SchemaTool schematool = new SchemaTool();
            schematool.setDdlFile("target/schema.ddl");
            schematool.setCompleteDdl(true);
            SchemaAwareStoreManager schemaStoreMgr = (SchemaAwareStoreManager) nucCtx.getStoreManager();
            schematool.createSchemaForClasses(schemaStoreMgr, classNames);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } finally {
        pmf.close();
    }
}
Also used : JDOMetadata(javax.jdo.metadata.JDOMetadata) PersistenceNucleusContext(org.datanucleus.PersistenceNucleusContext) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) SchemaTool(org.datanucleus.store.schema.SchemaTool) HashMap(java.util.HashMap) Map(java.util.Map) SchemaAwareStoreManager(org.datanucleus.store.schema.SchemaAwareStoreManager) HashSet(java.util.HashSet) StoreManager(org.datanucleus.store.StoreManager) SchemaAwareStoreManager(org.datanucleus.store.schema.SchemaAwareStoreManager)

Example 18 with JDOPersistenceManagerFactory

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

the class FetchPlanTest method testDynamicFetchPlanViaPMF.

/**
 * Test the use of the dynamic fetch plans via PMF.
 */
public void testDynamicFetchPlanViaPMF() {
    JDOPersistenceManagerFactory myPMF = (JDOPersistenceManagerFactory) pmf;
    AbstractClassMetaData cmd = myPMF.getNucleusContext().getMetaDataManager().getMetaDataForClass(FP2Base.class, new ClassLoaderResolverImpl());
    FetchPlan fp = getFetchPlan();
    // Test initial without DFG - should only have PK in fetch plan for PlaneA
    fp.removeGroup(FetchPlan.DEFAULT);
    FetchPlanForClass fpc = fp.getFetchPlanForClass(cmd);
    int[] fieldsInFP = fpc.getMemberNumbers();
    assertEquals("should have 1 fields in fetchplan (PK)", 1, fieldsInFP.length);
    BitSet fieldsInFPBitSet = fpc.getMemberNumbersByBitSet();
    assertTrue("piece1 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece1")));
    // Test empty group - should only have PK in fetch plan
    javax.jdo.FetchGroup grp1 = myPMF.getFetchGroup(FP2Base.class, "MyGroupPMF");
    myPMF.addFetchGroups(grp1);
    fp.addGroup("MyGroupPMF");
    fpc = fp.getFetchPlanForClass(cmd);
    fieldsInFP = fpc.getMemberNumbers();
    assertEquals("Incorrect number of fields in FetchPlan", 1, fieldsInFP.length);
    fieldsInFPBitSet = fpc.getMemberNumbersByBitSet();
    assertTrue("piece1 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece1")));
    // Test group for other class
    javax.jdo.FetchGroup grp2 = myPMF.getFetchGroup(FP3Base.class, "MyGroupPMF2");
    grp2.addMember("room2");
    fp.clearGroups();
    fp.removeGroup(FetchPlan.DEFAULT);
    fp.addGroup("MyGroupPMF2");
    fpc = fp.getFetchPlanForClass(cmd);
    fieldsInFP = fpc.getMemberNumbers();
    assertEquals("Incorrect number of fields in FetchPlan", 1, fieldsInFP.length);
    fieldsInFPBitSet = fpc.getMemberNumbersByBitSet();
    assertTrue("piece1 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece1")));
    // Test one group
    grp1 = myPMF.getFetchGroup(FP2Base.class, "MyGroupPMF");
    grp1.addMember("piece4").addMember("piece7").addMember("piece10");
    fp.setGroup("MyGroupPMF");
    fpc = fp.getFetchPlanForClass(cmd);
    fieldsInFP = fpc.getMemberNumbers();
    assertEquals("Incorrect number of fields in FetchPlan", 4, fieldsInFP.length);
    fieldsInFPBitSet = fpc.getMemberNumbersByBitSet();
    assertTrue("piece1 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece1")));
    assertTrue("piece4 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece4")));
    assertTrue("piece7 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece7")));
    assertTrue("piece10 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece10")));
    // Test dynamic update to existing group after use
    myPMF.getFetchGroup(FP2Base.class, "MyGroupPMF").removeMember("piece7");
    fieldsInFP = fpc.getMemberNumbers();
    assertEquals("Incorrect number of fields in FetchPlan", 3, fieldsInFP.length);
    fieldsInFPBitSet = fpc.getMemberNumbersByBitSet();
    assertTrue("piece1 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece1")));
    assertTrue("piece4 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece4")));
    assertTrue("piece10 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece10")));
    // Test multiple groups for same class
    grp1 = myPMF.getFetchGroup(FP2Base.class, "MyGroupPMF1");
    grp1.addMember("piece4").addMember("piece5");
    grp2 = myPMF.getFetchGroup(FP2Base.class, "MyGroupPMF2");
    grp2.addMember("piece7").addMember("piece8");
    javax.jdo.FetchGroup grp3 = myPMF.getFetchGroup(FP2Base.class, "MyGroupPMF3");
    grp3.addMember("piece10");
    myPMF.removeAllFetchGroups();
    myPMF.addFetchGroups(new javax.jdo.FetchGroup[] { grp1, grp2, grp3 });
    fp.setGroups(new String[] { "MyGroupPMF1", "MyGroupPMF2", "MyGroupPMF3" });
    fpc = fp.getFetchPlanForClass(cmd);
    fieldsInFP = fpc.getMemberNumbers();
    assertEquals("Incorrect number of fields in FetchPlan", 6, fieldsInFP.length);
    fieldsInFPBitSet = fpc.getMemberNumbersByBitSet();
    assertTrue("piece1 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece1")));
    assertTrue("piece4 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece4")));
    assertTrue("piece5 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece5")));
    assertTrue("piece7 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece7")));
    assertTrue("piece8 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece8")));
    assertTrue("piece10 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece10")));
}
Also used : FetchPlanForClass(org.datanucleus.FetchPlanForClass) BitSet(java.util.BitSet) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) FetchPlan(org.datanucleus.FetchPlan) JDOFetchPlan(org.datanucleus.api.jdo.JDOFetchPlan) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) FP2Base(org.datanucleus.samples.fetchplan.FP2Base) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 19 with JDOPersistenceManagerFactory

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

the class FetchPlanTest method testDynamicFetchPlanViaPM.

/**
 * Test the use of the dynamic fetch plans via PM.
 */
public void testDynamicFetchPlanViaPM() {
    JDOPersistenceManagerFactory myPMF = (JDOPersistenceManagerFactory) pmf;
    AbstractClassMetaData cmd = myPMF.getNucleusContext().getMetaDataManager().getMetaDataForClass(FP2Base.class, new ClassLoaderResolverImpl());
    JDOPersistenceManager myPM = (JDOPersistenceManager) myPMF.getPersistenceManager();
    FetchPlan fp = ((JDOFetchPlan) myPM.getFetchPlan()).getInternalFetchPlan();
    // Test initial without DFG - should only have PK in fetch plan for PlaneA
    fp.removeGroup(FetchPlan.DEFAULT);
    FetchPlanForClass fpc = fp.getFetchPlanForClass(cmd);
    int[] fieldsInFP = fpc.getMemberNumbers();
    assertEquals("should have 1 fields in fetchplan (PK)", 1, fieldsInFP.length);
    BitSet fieldsInFPBitSet = fpc.getMemberNumbersByBitSet();
    assertTrue("piece1 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece1")));
    // Test empty group - should only have PK in fetch plan for PlaneA
    javax.jdo.FetchGroup grp1 = myPM.getFetchGroup(FP2Base.class, "MyGroupPM");
    fp.addGroup("MyGroupPM");
    fpc = fp.getFetchPlanForClass(cmd);
    fieldsInFP = fpc.getMemberNumbers();
    assertEquals("Incorrect number of fields in FetchPlan", 1, fieldsInFP.length);
    fieldsInFPBitSet = fpc.getMemberNumbersByBitSet();
    assertTrue("piece1 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece1")));
    // Test group for other class
    javax.jdo.FetchGroup grp2 = myPM.getFetchGroup(FP3Base.class, "MyGroupPM2");
    grp2.addMember("room2");
    fp.clearGroups();
    fp.removeGroup(FetchPlan.DEFAULT);
    fp.addGroup("MyGroupPM2");
    fpc = fp.getFetchPlanForClass(cmd);
    fieldsInFP = fpc.getMemberNumbers();
    assertEquals("Incorrect number of fields in FetchPlan", 1, fieldsInFP.length);
    fieldsInFPBitSet = fpc.getMemberNumbersByBitSet();
    assertTrue("piece1 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece1")));
    // Test one group
    grp1 = myPM.getFetchGroup(FP2Base.class, "MyGroupPM");
    grp1.addMember("piece4").addMember("piece7").addMember("piece10");
    fp.setGroup("MyGroupPM");
    fpc = fp.getFetchPlanForClass(cmd);
    fieldsInFP = fpc.getMemberNumbers();
    assertEquals("Incorrect number of fields in FetchPlan", 4, fieldsInFP.length);
    fieldsInFPBitSet = fpc.getMemberNumbersByBitSet();
    assertTrue("piece1 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece1")));
    assertTrue("piece4 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece4")));
    assertTrue("piece7 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece7")));
    assertTrue("piece10 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece10")));
    // Test dynamic update to existing group after use
    myPM.getFetchGroup(FP2Base.class, "MyGroupPM").removeMember("piece7");
    fieldsInFP = fpc.getMemberNumbers();
    assertEquals("Incorrect number of fields in FetchPlan", 3, fieldsInFP.length);
    fieldsInFPBitSet = fpc.getMemberNumbersByBitSet();
    assertTrue("piece1 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece1")));
    assertTrue("piece4 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece4")));
    assertTrue("piece10 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece10")));
    // Test multiple groups for same class
    grp1 = myPM.getFetchGroup(FP2Base.class, "MyGroupPM1");
    grp1.addMember("piece4").addMember("piece5");
    grp2 = myPM.getFetchGroup(FP2Base.class, "MyGroupPM2");
    grp2.addMember("piece7").addMember("piece8");
    javax.jdo.FetchGroup grp3 = myPM.getFetchGroup(FP2Base.class, "MyGroupPM3");
    grp3.addMember("piece10");
    myPMF.removeAllFetchGroups();
    myPMF.addFetchGroups(new javax.jdo.FetchGroup[] { grp1, grp2, grp3 });
    fp.setGroups(new String[] { "MyGroupPM1", "MyGroupPM2", "MyGroupPM3" });
    fpc = fp.getFetchPlanForClass(cmd);
    fieldsInFP = fpc.getMemberNumbers();
    assertEquals("Incorrect number of fields in FetchPlan", 6, fieldsInFP.length);
    fieldsInFPBitSet = fpc.getMemberNumbersByBitSet();
    assertTrue("piece1 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece1")));
    assertTrue("piece4 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece4")));
    assertTrue("piece5 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece5")));
    assertTrue("piece7 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece7")));
    assertTrue("piece8 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece8")));
    assertTrue("piece10 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece10")));
}
Also used : JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) FetchPlanForClass(org.datanucleus.FetchPlanForClass) BitSet(java.util.BitSet) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) FetchPlan(org.datanucleus.FetchPlan) JDOFetchPlan(org.datanucleus.api.jdo.JDOFetchPlan) JDOFetchPlan(org.datanucleus.api.jdo.JDOFetchPlan) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) FP2Base(org.datanucleus.samples.fetchplan.FP2Base) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 20 with JDOPersistenceManagerFactory

use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project hive by apache.

the class TestJdbcWithMiniHS2 method getNucleusClassLoaderResolverMapSize.

@SuppressWarnings("unchecked")
private int getNucleusClassLoaderResolverMapSize() {
    Field classLoaderResolverMap;
    Field pmf;
    JDOPersistenceManagerFactory jdoPmf = null;
    NucleusContext nc = null;
    Map<String, ClassLoaderResolver> cMap;
    try {
        pmf = ObjectStore.class.getDeclaredField("pmf");
        if (pmf != null) {
            pmf.setAccessible(true);
            jdoPmf = (JDOPersistenceManagerFactory) pmf.get(null);
            if (jdoPmf != null) {
                nc = jdoPmf.getNucleusContext();
            }
        }
    } catch (Exception e) {
        System.out.println(e);
    }
    if (nc != null) {
        try {
            classLoaderResolverMap = AbstractNucleusContext.class.getDeclaredField("classLoaderResolverMap");
            if (classLoaderResolverMap != null) {
                classLoaderResolverMap.setAccessible(true);
                cMap = (Map<String, ClassLoaderResolver>) classLoaderResolverMap.get(nc);
                if (cMap != null) {
                    return cMap.size();
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
    return -1;
}
Also used : Field(java.lang.reflect.Field) ObjectStore(org.apache.hadoop.hive.metastore.ObjectStore) AbstractNucleusContext(org.datanucleus.AbstractNucleusContext) NucleusContext(org.datanucleus.NucleusContext) AbstractNucleusContext(org.datanucleus.AbstractNucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) TimeoutException(java.util.concurrent.TimeoutException) CancellationException(java.util.concurrent.CancellationException) SQLException(java.sql.SQLException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

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