Search in sources :

Example 21 with FetchPlan

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

the class FetchPlanTest method testFetchPlanInheritance.

/**
 * Test the use of fetch plans with inherited objects.
 */
public void testFetchPlanInheritance() {
    FetchPlan fp = getFetchPlan();
    PersistenceNucleusContextImpl nucleusCtx = new PersistenceNucleusContextImpl("JDO", null);
    MetaDataManager metaMgr = new JDOMetaDataManager(nucleusCtx);
    // test parent with fetch-group = group1
    AbstractClassMetaData cmd = metaMgr.getMetaDataForClass(DetachParent.class, new ClassLoaderResolverImpl());
    fp.addGroup("group1");
    fp.removeGroup(FetchPlan.DEFAULT);
    FetchPlanForClass fpc = fp.getFetchPlanForClass(cmd);
    int[] fieldsInFP = fpc.getMemberNumbers();
    assertEquals("should have 2 fields in fetchplan", 2, fieldsInFP.length);
    BitSet fieldsInFPBitSet = fpc.getMemberNumbersByBitSet();
    assertTrue("fieldA should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("fieldA")));
    assertTrue("fieldB should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("fieldB")));
    // test child with fetch-group = group1
    cmd = metaMgr.getMetaDataForClass(DetachChildA.class, new ClassLoaderResolverImpl());
    fp.addGroup("group1");
    fp.removeGroup(FetchPlan.DEFAULT);
    fpc = fp.getFetchPlanForClass(cmd);
    fieldsInFP = fpc.getMemberNumbers();
    assertEquals("should have 3 fields in fetchplan", 3, fieldsInFP.length);
    fieldsInFPBitSet = fpc.getMemberNumbersByBitSet();
    assertTrue("fieldA should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("fieldA")));
    assertTrue("fieldB should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("fieldB")));
    assertFalse("fieldC should not be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("fieldC")));
    assertFalse("fieldD should not be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("fieldD")));
    assertTrue("fieldE should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("fieldE")));
    // test child with no fetch-groups
    cmd = metaMgr.getMetaDataForClass(DetachChildB.class, new ClassLoaderResolverImpl());
    fp.addGroup("group1");
    fp.removeGroup(FetchPlan.DEFAULT);
    fpc = fp.getFetchPlanForClass(cmd);
    fieldsInFP = fpc.getMemberNumbers();
    assertEquals("should have 2 fields in fetchplan", 2, fieldsInFP.length);
    fieldsInFPBitSet = fpc.getMemberNumbersByBitSet();
    assertTrue("fieldA should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("fieldA")));
    assertTrue("fieldB should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("fieldB")));
    cmd = metaMgr.getMetaDataForClass(FP2Sub.class, new ClassLoaderResolverImpl());
    // --------------------------------------
    // use FetchPlan.ALL
    // --------------------------------------
    fp.clearGroups();
    fp.addGroup(FetchPlan.ALL);
    fp.removeGroup(FetchPlan.DEFAULT);
    fpc = fp.getFetchPlanForClass(cmd);
    fieldsInFP = fpc.getMemberNumbers();
    assertEquals("should have 17 fields in fetchplan", 17, fieldsInFP.length);
    // check if fields are in the FP
    fieldsInFPBitSet = fpc.getMemberNumbersByBitSet();
    assertTrue("piece1 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece1")));
    assertTrue("piece2 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece2")));
    assertTrue("piece3 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece3")));
    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("piece6 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece6")));
    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("piece9 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece9")));
    assertTrue("piece10 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece10")));
    assertTrue("piece11 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece11")));
    assertTrue("piece12 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece12")));
    assertTrue("piece14 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece14")));
    assertTrue("piece20 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece20")));
    assertTrue("piece21 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece21")));
    assertTrue("piece22 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece22")));
    assertTrue("piece23 should be in the fetchplan", fieldsInFPBitSet.get(cmd.getAbsolutePositionOfMember("piece23")));
}
Also used : DetachChildB(org.datanucleus.samples.detach.DetachChildB) DetachChildA(org.datanucleus.samples.detach.DetachChildA) FetchPlanForClass(org.datanucleus.FetchPlanForClass) BitSet(java.util.BitSet) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) FP2Sub(org.datanucleus.samples.fetchplan.FP2Sub) FetchPlan(org.datanucleus.FetchPlan) JDOFetchPlan(org.datanucleus.api.jdo.JDOFetchPlan) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 22 with FetchPlan

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

the class FetchPlanTest method testDetachmentRoots.

/**
 * Test the detachment roots
 */
public void testDetachmentRoots() {
    FetchPlan fp = getFetchPlan();
    FP2Base baseObj = new FP2Base();
    assertEquals(0, fp.getDetachmentRoots().size());
    // verify immutable
    try {
        fp.getDetachmentRoots().add(baseObj);
        fail("exception UnsupportedOperationException");
    } catch (UnsupportedOperationException ex) {
    // expected
    }
    List<Object> elms = new ArrayList<>();
    elms.add(baseObj);
    fp.setDetachmentRoots(elms);
    assertEquals(1, fp.getDetachmentRoots().size());
    // verify if immutable by changing original collection
    FP2Base baseObj2 = new FP2Base();
    elms.add(baseObj2);
    assertEquals(1, fp.getDetachmentRoots().size());
    // verify immutable after setting roots
    try {
        fp.getDetachmentRoots().add(baseObj);
        fail("exception UnsupportedOperationException");
    } catch (UnsupportedOperationException ex) {
    // expected
    }
}
Also used : ArrayList(java.util.ArrayList) FetchPlan(org.datanucleus.FetchPlan) JDOFetchPlan(org.datanucleus.api.jdo.JDOFetchPlan) FP2Base(org.datanucleus.samples.fetchplan.FP2Base)

Example 23 with FetchPlan

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

the class FetchPlanTest method testDetachmentRootsClasses.

/**
 * Test the detachment root classes
 */
public void testDetachmentRootsClasses() {
    FetchPlan fp = getFetchPlan();
    assertEquals(0, fp.getDetachmentRootClasses().length);
    assertEquals(Class.class, fp.getDetachmentRootClasses().getClass().getComponentType());
    List<Class> elms = new ArrayList<>();
    elms.add(FP2Base.class);
    fp.setDetachmentRootClasses((Class[]) elms.toArray(new Class[elms.size()]));
    assertEquals(1, fp.getDetachmentRootClasses().length);
}
Also used : ArrayList(java.util.ArrayList) FetchPlanForClass(org.datanucleus.FetchPlanForClass) FetchPlan(org.datanucleus.FetchPlan) JDOFetchPlan(org.datanucleus.api.jdo.JDOFetchPlan)

Aggregations

FetchPlan (org.datanucleus.FetchPlan)23 JDOFetchPlan (org.datanucleus.api.jdo.JDOFetchPlan)15 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)10 FetchPlanForClass (org.datanucleus.FetchPlanForClass)9 ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)7 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)7 BitSet (java.util.BitSet)6 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)6 ExecutionContext (org.datanucleus.ExecutionContext)5 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)5 JDOMetaDataManager (org.datanucleus.api.jdo.metadata.JDOMetaDataManager)5 MetaDataManager (org.datanucleus.metadata.MetaDataManager)5 ObjectProvider (org.datanucleus.state.ObjectProvider)5 FieldValues (org.datanucleus.store.FieldValues)5 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)3 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)3 FP2Base (org.datanucleus.samples.fetchplan.FP2Base)3