Search in sources :

Example 6 with JDOMetadata

use of javax.jdo.metadata.JDOMetadata in project tests by datanucleus.

the class DynamicEnhanceSchemaToolTest method persist.

public void persist(DynamicEnhanceSchemaToolClassLoader runtimeCL) throws Exception {
    // Persist
    Map props = getPropertiesForDatastore(runtimeCL);
    PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(props);
    try {
        ClassLoaderResolver clr = ((JDOPersistenceManagerFactory) pmf).getNucleusContext().getClassLoaderResolver(runtimeCL);
        JDOMetadata filemd = pmf.newMetadata();
        createMetadata(filemd);
        NucleusLogger.PERSISTENCE.info(">> registering metadata");
        pmf.registerMetadata(filemd);
        NucleusLogger.PERSISTENCE.info(filemd);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object id = null;
        try {
            tx.begin();
            Class clazz = clr.classForName("test.Client");
            Object o = clazz.newInstance();
            pm.makePersistent(o);
            tx.commit();
            id = pm.getObjectId(o);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Object obj = pm.getObjectById(id);
            pm.deletePersistent(obj);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        pmf.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) JDOMetadata(javax.jdo.metadata.JDOMetadata) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with JDOMetadata

use of javax.jdo.metadata.JDOMetadata in project tests by datanucleus.

the class JDOMetadataAPITest method testPackageClassInterface.

/**
 * Simple test that checks the numbers of packages, classes, interfaces contained.
 */
public void testPackageClassInterface() {
    JDOMetadata jdomd = pmf.newMetadata();
    PackageMetadata pmd1 = jdomd.newPackageMetadata("org.datanucleus.test");
    pmd1.newClassMetadata("MyClass1");
    pmd1.newClassMetadata("MyClass2");
    PackageMetadata pmd2 = jdomd.newPackageMetadata("org.datanucleus.test2");
    pmd2.newInterfaceMetadata("org.datanucleus.test3");
    // Check the JDO metadata
    assertEquals("Number of packages in JDO metadata is incorrect", 2, jdomd.getNumberOfPackages());
    assertEquals("Number of classes in JDO package metadata 1 is incorrect", 2, pmd1.getNumberOfClasses());
    assertEquals("Number of interfaces in JDO package metadata 1 is incorrect", 0, pmd1.getNumberOfInterfaces());
    assertEquals("Number of classes in JDO package metadata 2 is incorrect", 0, pmd2.getNumberOfClasses());
    assertEquals("Number of interfaces in JDO package metadata 2 is incorrect", 1, pmd2.getNumberOfInterfaces());
    // Check the internal metadata
    FileMetaData filemd = ((JDOMetadataImpl) jdomd).getInternal();
    assertEquals("Number of packages is incorrect", 2, filemd.getNoOfPackages());
}
Also used : JDOMetadataImpl(org.datanucleus.api.jdo.metadata.JDOMetadataImpl) JDOMetadata(javax.jdo.metadata.JDOMetadata) PackageMetadata(javax.jdo.metadata.PackageMetadata) FileMetaData(org.datanucleus.metadata.FileMetaData)

Aggregations

JDOMetadata (javax.jdo.metadata.JDOMetadata)7 HashMap (java.util.HashMap)2 Map (java.util.Map)2 PackageMetadata (javax.jdo.metadata.PackageMetadata)2 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 CtClass (javassist.CtClass)1 JDOEnhancer (javax.jdo.JDOEnhancer)1 PersistenceManager (javax.jdo.PersistenceManager)1 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)1 Transaction (javax.jdo.Transaction)1 TypeMetadata (javax.jdo.metadata.TypeMetadata)1 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)1 PersistenceNucleusContext (org.datanucleus.PersistenceNucleusContext)1 JDOMetadataImpl (org.datanucleus.api.jdo.metadata.JDOMetadataImpl)1 FileMetaData (org.datanucleus.metadata.FileMetaData)1 Computer (org.datanucleus.samples.annotations.embedded.Computer)1