Search in sources :

Example 6 with PackageMetaData

use of org.datanucleus.metadata.PackageMetaData 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 7 with PackageMetaData

use of org.datanucleus.metadata.PackageMetaData in project tests by datanucleus.

the class AnnotationPlusXMLTest method testTableGenerator.

/**
 * Test of JPA <table-generator>
 */
public void testTableGenerator() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
    metaDataMgr.loadPersistenceUnit(pumd, null);
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Employee.class.getName(), clr);
    PackageMetaData pmd = cmd1.getPackageMetaData();
    assertEquals("Number of TableGenerators registered for Employee class is wrong", pmd.getNoOfTableGenerators(), 1);
    TableGeneratorMetaData tgmd = pmd.getTableGenerators()[0];
    assertEquals("TableGenerator has incorrect name", tgmd.getName(), "EmployeeGenerator");
    assertEquals("TableGenerator has incorrect table", tgmd.getTableName(), "ID_TABLE");
    assertEquals("TableGenerator has incorrect pk column name", tgmd.getPKColumnName(), "TYPE");
    assertEquals("TableGenerator has incorrect value column name", tgmd.getValueColumnName(), "LATEST_VALUE");
    assertEquals("TableGenerator has incorrect pk column value", tgmd.getPKColumnValue(), "EMPLOYEE");
    assertEquals("TableGenerator has incorrect initial value", tgmd.getInitialValue(), 0);
    assertEquals("TableGenerator has incorrect allocation size", tgmd.getAllocationSize(), 50);
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) PackageMetaData(org.datanucleus.metadata.PackageMetaData) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) TableGeneratorMetaData(org.datanucleus.metadata.TableGeneratorMetaData) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 8 with PackageMetaData

use of org.datanucleus.metadata.PackageMetaData in project tests by datanucleus.

the class AnnotationTest method testSequenceGenerator.

/**
 * Test of basic JPA @SequenceGenerator
 */
public void testSequenceGenerator() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
    metaDataMgr.loadPersistenceUnit(pumd, null);
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Department.class.getName(), clr);
    PackageMetaData pmd = cmd1.getPackageMetaData();
    assertEquals("Number of Sequences registered for Department class is wrong", pmd.getNoOfSequences(), 1);
    SequenceMetaData seqmd = pmd.getSequences()[0];
    assertEquals("SequenceGenerator has incorrect name", seqmd.getName(), "DepartmentGenerator");
    assertEquals("SequenceGenerator has incorrect sequence name", seqmd.getDatastoreSequence(), "DEPT_SEQ");
    assertEquals("SequenceGenerator has incorrect initial value", seqmd.getInitialValue(), 1);
    assertEquals("SequenceGenerator has incorrect allocation size", seqmd.getAllocationSize(), 50);
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) PackageMetaData(org.datanucleus.metadata.PackageMetaData) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) SequenceMetaData(org.datanucleus.metadata.SequenceMetaData)

Example 9 with PackageMetaData

use of org.datanucleus.metadata.PackageMetaData in project datanucleus-api-jdo by datanucleus.

the class JDOMetaDataHelper method getXMLForMetaData.

public String getXMLForMetaData(FileMetaData filemd, String prefix, String indent) {
    StringBuilder str = new StringBuilder();
    str.append(prefix).append("<jdo");
    if (filemd.getCatalog() != null) {
        str.append(" catalog=\"" + filemd.getCatalog() + "\"");
    }
    if (filemd.getSchema() != null) {
        str.append(" schema=\"" + filemd.getSchema() + "\"");
    }
    str.append(">\n");
    // Add packages
    if (filemd.getNoOfPackages() > 0) {
        for (int i = 0; i < filemd.getNoOfPackages(); i++) {
            PackageMetaData pmd = filemd.getPackage(i);
            str.append(getXMLForMetaData(pmd, prefix + indent, indent));
        }
    }
    // Add queries
    QueryMetaData[] queries = filemd.getQueries();
    if (queries != null) {
        for (QueryMetaData qmd : queries) {
            str.append(getXMLForMetaData(qmd, prefix + indent, indent));
        }
    }
    // Add fetch plans
    FetchPlanMetaData[] fetchPlans = filemd.getFetchPlans();
    if (fetchPlans != null) {
        for (FetchPlanMetaData fpmd : fetchPlans) {
            str.append(getXMLForMetaData(fpmd, prefix + indent, indent));
        }
    }
    // Add extensions
    processExtensions(filemd.getExtensions(), str, prefix, indent);
    str.append("</jdo>");
    return str.toString();
}
Also used : QueryMetaData(org.datanucleus.metadata.QueryMetaData) PackageMetaData(org.datanucleus.metadata.PackageMetaData) FetchPlanMetaData(org.datanucleus.metadata.FetchPlanMetaData)

Example 10 with PackageMetaData

use of org.datanucleus.metadata.PackageMetaData in project datanucleus-api-jdo by datanucleus.

the class JDOMetadataImpl method newPackageMetadata.

/* (non-Javadoc)
     * @see javax.jdo.metadata.JDOMetadata#newPackageMetadata(java.lang.Package)
     */
public PackageMetadata newPackageMetadata(Package pkg) {
    PackageMetaData internalPmd = getInternal().newPackageMetadata(pkg.getName());
    PackageMetadataImpl pmd = new PackageMetadataImpl(internalPmd);
    pmd.parent = this;
    return pmd;
}
Also used : PackageMetaData(org.datanucleus.metadata.PackageMetaData)

Aggregations

PackageMetaData (org.datanucleus.metadata.PackageMetaData)18 ClassMetaData (org.datanucleus.metadata.ClassMetaData)14 MetaDataManager (org.datanucleus.metadata.MetaDataManager)8 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)7 NucleusContext (org.datanucleus.NucleusContext)7 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)6 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)6 FileMetaData (org.datanucleus.metadata.FileMetaData)6 PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)6 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)5 InterfaceMetaData (org.datanucleus.metadata.InterfaceMetaData)4 SequenceMetaData (org.datanucleus.metadata.SequenceMetaData)4 TableGeneratorMetaData (org.datanucleus.metadata.TableGeneratorMetaData)3 HashSet (java.util.HashSet)2 DiscriminatorMetaData (org.datanucleus.metadata.DiscriminatorMetaData)2 FetchPlanMetaData (org.datanucleus.metadata.FetchPlanMetaData)2 QueryMetaData (org.datanucleus.metadata.QueryMetaData)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1