Search in sources :

Example 1 with JDOFetchPlan

use of org.datanucleus.api.jdo.JDOFetchPlan 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)

Aggregations

BitSet (java.util.BitSet)1 ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)1 FetchPlan (org.datanucleus.FetchPlan)1 FetchPlanForClass (org.datanucleus.FetchPlanForClass)1 JDOFetchPlan (org.datanucleus.api.jdo.JDOFetchPlan)1 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)1 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)1 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)1 FP2Base (org.datanucleus.samples.fetchplan.FP2Base)1