Search in sources :

Example 1 with ClassLoaderResolverImpl

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

the class PersistenceTestCase method addClassesToSchema.

/**
 * Method to add the specified classes to the schema.
 * @param classes The classes
 */
protected void addClassesToSchema(Class<?>[] classes) {
    String[] classNames = new String[classes.length];
    for (int i = 0; i < classes.length; i++) {
        classNames[i] = classes[i].getName();
    }
    if (storeMgr == null) {
        throw new IllegalArgumentException("storeMgr is null");
    }
    storeMgr.manageClasses(new ClassLoaderResolverImpl(), classNames);
}
Also used : ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 2 with ClassLoaderResolverImpl

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

the class JDOTestBase method getEnhancedClassesFromFile.

/**
 * Method to return a set of enhanced classes for all of those found in the specified JDO MetaData file.
 * @param resourceName Name of the MetaData file (relative to the CLASSPATH).
 * @return Set of enhanced classes
 * @throws IllegalArgumentException if an error occurs reading the file resource
 */
public Class[] getEnhancedClassesFromFile(String resourceName) {
    InputStream in = JDOTestBase.class.getClassLoader().getResourceAsStream(resourceName);
    if (in == null) {
        throw new IllegalArgumentException("Cannot load resource :" + resourceName);
    }
    StringBuffer sb = new StringBuffer();
    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(in));
        String buf = null;
        while ((buf = br.readLine()) != null) {
            sb.append(buf);
        }
    } catch (IOException e) {
        throw new IllegalArgumentException("Error reading MetaData file " + resourceName + ": " + e.getMessage(), e);
    } finally {
        try {
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    String jdoXmlContents = sb.toString();
    if (jdoXmlContents == null) {
        throw new IllegalArgumentException("Contents of file is null");
    }
    NucleusContext context = new EnhancementNucleusContextImpl("JDO", null);
    MetaDataManager mgr = new JDOMetaDataManager(context);
    MetaDataParser parser = new MetaDataParser(mgr, context.getPluginManager(), true, true);
    ClassLoaderResolverImpl clr = new ClassLoaderResolverImpl();
    // Parse the MetaData
    FileMetaData filemd = (FileMetaData) parser.parseMetaDataStream(new ByteArrayInputStream(jdoXmlContents.getBytes()), null, "JDO");
    if (filemd == null) {
        return null;
    }
    mgr.registerFile("EnhancerTestXMLFile", filemd, clr);
    // Populate/Initialise the MetaData for the actual classes.
    for (int i = 0; i < filemd.getNoOfPackages(); i++) {
        PackageMetaData pmd = filemd.getPackage(i);
        for (int j = 0; j < pmd.getNoOfClasses(); j++) {
            ClassMetaData cmd = pmd.getClass(j);
            cmd.populate(clr, null, mgr);
            cmd.initialise(clr);
        }
    }
    // Enhance the classes
    TestClassLoader cl = new TestClassLoader();
    Class[] result;
    ArrayList<Class> resultList = new ArrayList<Class>();
    for (int i = 0; i < filemd.getNoOfPackages(); i++) {
        PackageMetaData pmd = filemd.getPackage(i);
        for (int j = 0; j < pmd.getNoOfClasses(); j++) {
            ClassMetaData cmd = (ClassMetaData) pmd.getClass(j);
            // Enhance the class using the MetaData
            ClassEnhancer enhancer = getClassEnhancer(cmd, mgr);
            enhancer.enhance();
            // Save the enhanced class
            resultList.add(cl.getClass(cmd.getFullClassName(), enhancer.getClassBytes()));
        }
    }
    result = (Class[]) resultList.toArray(new Class[resultList.size()]);
    return result;
}
Also used : NucleusContext(org.datanucleus.NucleusContext) ArrayList(java.util.ArrayList) FileMetaData(org.datanucleus.metadata.FileMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PackageMetaData(org.datanucleus.metadata.PackageMetaData) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) IOException(java.io.IOException) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) MetaDataParser(org.datanucleus.metadata.xml.MetaDataParser) ClassEnhancer(org.datanucleus.enhancer.ClassEnhancer) EnhancementNucleusContextImpl(org.datanucleus.enhancer.EnhancementNucleusContextImpl) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 3 with ClassLoaderResolverImpl

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

the class AnnotationTest method testSecondaryTable.

/**
 * Test for use of annotations for secondary tables, in particular @SecondaryTable.
 * Uses Printer class, storing some fields in table "PRINTER" and some in "PRINTER_TONER".
 */
public void testSecondaryTable() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    ClassLoaderResolver clr = new ClassLoaderResolverImpl();
    ClassMetaData cmd = (ClassMetaData) metaDataMgr.getMetaDataForClass(Printer.class.getName(), clr);
    assertEquals("detachable is wrong", cmd.isDetachable(), true);
    assertEquals("identity-type is wrong", cmd.getIdentityType(), IdentityType.APPLICATION);
    assertEquals("embedded-only is wrong", cmd.isEmbeddedOnly(), false);
    assertEquals("requires-extent is wrong", cmd.isRequiresExtent(), true);
    assertNull("catalog is wrong", cmd.getCatalog());
    assertNull("schema is wrong", cmd.getSchema());
    assertEquals("table is wrong", cmd.getTable(), "JPA_AN_PRINTER");
    assertEquals("has incorrect number of persistent fields", cmd.getNoOfManagedMembers(), 5);
    // Check JoinMetaData at class-level
    List<JoinMetaData> joinmds = cmd.getJoinMetaData();
    assertNotNull("JoinMetaData at class-level is null!", joinmds);
    assertEquals("Number of JoinMetaData at class-level is wrong!", joinmds.size(), 1);
    assertEquals("Table of JoinMetaData at class-level is wrong", "JPA_AN_PRINTER_TONER", joinmds.get(0).getTable());
    ColumnMetaData[] joinColmds = joinmds.get(0).getColumnMetaData();
    assertEquals("Number of columns with MetaData in secondary table is incorrect", 1, joinColmds.length);
    assertEquals("Column of JoinMetaData at class-level is wrong", joinColmds[0].getName(), "PRINTER_ID");
    // "model" (stored in primary-table)
    AbstractMemberMetaData fmd = cmd.getMetaDataForMember("model");
    assertNotNull("Doesnt have required field", fmd);
    assertNull("Field 'model' has non-null table!", fmd.getTable());
    // "tonerModel" (stored in secondary-table)
    fmd = cmd.getMetaDataForMember("tonerModel");
    assertNotNull("Doesnt have required field", fmd);
    assertEquals("Field 'tonerModel' has non-null table!", fmd.getTable(), "JPA_AN_PRINTER_TONER");
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) JoinMetaData(org.datanucleus.metadata.JoinMetaData) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 4 with ClassLoaderResolverImpl

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

the class AnnotationTest method testCharDefaultTo1Length.

/**
 * Test of char length default to 1 with JPA.
 */
public void testCharDefaultTo1Length() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    ClassLoaderResolver clr = new ClassLoaderResolverImpl();
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(TypeHolder.class.getName(), clr);
    assertEquals(1, cmd1.getMetaDataForMember("char1").getColumnMetaData()[0].getLength().intValue());
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) ClassMetaData(org.datanucleus.metadata.ClassMetaData) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 5 with ClassLoaderResolverImpl

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

the class AnnotationTest method testStringLength.

/**
 * Test of string length default to JPA default 255.
 */
public void testStringLength() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    ClassLoaderResolver clr = new ClassLoaderResolverImpl();
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Account.class.getName(), clr);
    AbstractMemberMetaData mmd1 = cmd1.getMetaDataForMember("username");
    assertEquals(255, mmd1.getColumnMetaData()[0].getLength().intValue());
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Aggregations

ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)53 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)35 MetaDataManager (org.datanucleus.metadata.MetaDataManager)31 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)27 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)23 JDOMetaDataManager (org.datanucleus.api.jdo.metadata.JDOMetaDataManager)22 NucleusContext (org.datanucleus.NucleusContext)14 ClassMetaData (org.datanucleus.metadata.ClassMetaData)14 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)12 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)10 FetchPlan (org.datanucleus.FetchPlan)7 FetchPlanForClass (org.datanucleus.FetchPlanForClass)7 JDOFetchPlan (org.datanucleus.api.jdo.JDOFetchPlan)7 BitSet (java.util.BitSet)6 NonManagedPluginRegistry (org.datanucleus.plugin.NonManagedPluginRegistry)6 HashSet (java.util.HashSet)3 Properties (java.util.Properties)3 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)3 Bundle (org.datanucleus.plugin.Bundle)3 ArrayList (java.util.ArrayList)2