Search in sources :

Example 21 with JDOMetaDataManager

use of org.datanucleus.api.jdo.metadata.JDOMetaDataManager 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 JDOMetaDataManager

use of org.datanucleus.api.jdo.metadata.JDOMetaDataManager in project tests by datanucleus.

the class BasicTest method testMetaDataManager.

/**
 * Test for the MetaDataManager.
 */
public void testMetaDataManager() {
    MetaDataManager mgr = new JDOMetaDataManager(new PersistenceNucleusContextImpl("JDO", null));
    mgr.setValidate(false);
    org.datanucleus.metadata.AbstractClassMetaData cmd = mgr.getMetaDataForClass(Inventory.class, new ClassLoaderResolverImpl());
    if (cmd == null) {
        fail("Failed to load MetaData for org.jpox.samples.store.Inventory class");
    }
    org.datanucleus.metadata.AbstractClassMetaData cmd2 = mgr.getMetaDataForClass(Book.class, new ClassLoaderResolverImpl());
    if (cmd2 == null) {
        fail("Failed to load MetaData for org.jpox.samples.store.Book class");
    }
}
Also used : JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 23 with JDOMetaDataManager

use of org.datanucleus.api.jdo.metadata.JDOMetaDataManager in project tests by datanucleus.

the class BasicTest method testPersistentPropertiesFieldPropertyClash.

public void testPersistentPropertiesFieldPropertyClash() {
    MetaDataManager mgr = new JDOMetaDataManager(new PersistenceNucleusContextImpl("JDO", null));
    mgr.setValidate(false);
    AbstractClassMetaData cmd = mgr.getMetaDataForClass(Phantom.class, new ClassLoaderResolverImpl());
    assertNull(cmd.getMetaDataForMember("_name"));
    assertEquals(FieldPersistenceModifier.PERSISTENT, cmd.getMetaDataForMember("name").getPersistenceModifier());
}
Also used : JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 24 with JDOMetaDataManager

use of org.datanucleus.api.jdo.metadata.JDOMetaDataManager in project tests by datanucleus.

the class BasicTest method testMetaDataMultithreaded.

/**
 * Test for the MetaDataManager.
 */
public void testMetaDataMultithreaded() {
    final MetaDataManager mgr = new JDOMetaDataManager(new PersistenceNucleusContextImpl("JDO", null));
    mgr.setValidate(false);
    ThreadGroup group = new ThreadGroup("tgroup");
    Thread[] threads = new Thread[500];
    final List<String> errors = new ArrayList<>();
    final Object lock = new Object();
    for (int i = 0; i < 500; i++) {
        threads[i] = new Thread(group, new Runnable() {

            public void run() {
                try {
                    synchronized (lock) {
                        lock.wait();
                    }
                } catch (InterruptedException e) {
                }
                try {
                    // this populate and initilize the class
                    mgr.getMetaDataForClass(Manager.class, new ClassLoaderResolverImpl()).isPopulated();
                } catch (Throwable e) {
                    e.printStackTrace();
                    errors.add(e.getMessage());
                }
            }
        });
    }
    for (int i = 0; i < 500; i++) {
        threads[i].start();
    }
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e1) {
    }
    synchronized (lock) {
        lock.notifyAll();
    }
    for (int i = 0; i < 500; i++) {
        try {
            threads[i].join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    assertEquals("Errors " + errors, 0, errors.size());
    org.datanucleus.metadata.AbstractClassMetaData cmd2 = mgr.getMetaDataForClass(Book.class, new ClassLoaderResolverImpl());
    if (cmd2 == null) {
        fail("Failed to load MetaData for org.jpox.samples.store.Book class");
    }
}
Also used : ArrayList(java.util.ArrayList) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) Manager(org.jpox.samples.models.company.Manager) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceManager(javax.jdo.PersistenceManager) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 25 with JDOMetaDataManager

use of org.datanucleus.api.jdo.metadata.JDOMetaDataManager in project tests by datanucleus.

the class BasicTest method testDatastoreIdentityMetadata.

/**
 * Tests datastore identity declared in many forms
 */
public void testDatastoreIdentityMetadata() {
    MetaDataManager mgr = new JDOMetaDataManager(new PersistenceNucleusContextImpl("JDO", null));
    mgr.setValidate(false);
    AbstractClassMetaData cmdD0 = mgr.getMetaDataForClass(D0.class, new ClassLoaderResolverImpl());
    AbstractClassMetaData cmdD1 = mgr.getMetaDataForClass(D1.class, new ClassLoaderResolverImpl());
    AbstractClassMetaData cmdD2 = mgr.getMetaDataForClass(D2.class, new ClassLoaderResolverImpl());
    assertEquals("identity", cmdD0.getIdentityMetaData().getValueStrategy().toString());
    assertEquals(cmdD0.getIdentityMetaData().getValueStrategy(), cmdD1.getIdentityMetaData().getValueStrategy());
    assertEquals(cmdD0.getIdentityMetaData().getValueStrategy(), cmdD2.getIdentityMetaData().getValueStrategy());
    assertEquals("D_ID", cmdD0.getIdentityMetaData().getColumnMetaData().getName());
    assertEquals(cmdD0.getIdentityMetaData().getColumnMetaData().getName(), cmdD1.getIdentityMetaData().getColumnMetaData().getName());
    assertEquals(cmdD0.getIdentityMetaData().getColumnMetaData().getName(), cmdD2.getIdentityMetaData().getColumnMetaData().getName());
}
Also used : JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Aggregations

JDOMetaDataManager (org.datanucleus.api.jdo.metadata.JDOMetaDataManager)28 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)27 ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)20 MetaDataManager (org.datanucleus.metadata.MetaDataManager)17 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)16 MetaDataParser (org.datanucleus.metadata.xml.MetaDataParser)8 NucleusContext (org.datanucleus.NucleusContext)7 FetchPlan (org.datanucleus.FetchPlan)5 FetchPlanForClass (org.datanucleus.FetchPlanForClass)5 JDOFetchPlan (org.datanucleus.api.jdo.JDOFetchPlan)5 ArrayList (java.util.ArrayList)4 BitSet (java.util.BitSet)4 MetaData (org.datanucleus.metadata.MetaData)4 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 NucleusException (org.datanucleus.exceptions.NucleusException)2 FileMetaData (org.datanucleus.metadata.FileMetaData)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1