Search in sources :

Example 1 with JDOMetaDataManager

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

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

the class InheritanceTest method testSubclassInformation.

/**
 * Some test for subclass information.
 */
public void testSubclassInformation() {
    JDOMetaDataManager mgr = new JDOMetaDataManager(new PersistenceNucleusContextImpl("JDO", null));
    // initialize metadata
    mgr.getMetaDataForClass(JBase.class, new ClassLoaderResolverImpl());
    mgr.getMetaDataForClass(JBowl.class, new ClassLoaderResolverImpl());
    mgr.getMetaDataForClass(JSpottedMarble.class, new ClassLoaderResolverImpl());
    mgr.getMetaDataForClass(JTransparentMarble.class, new ClassLoaderResolverImpl());
    mgr.getSubclassesForClass(JBase.class.getName(), false);
    Set test = getSet(mgr.getSubclassesForClass(JBase.class.getName(), false));
    assertTrue(test.contains(JBowl.class.getName()));
    assertTrue(test.contains(JMarble.class.getName()));
    test = getSet(mgr.getSubclassesForClass(JMarble.class.getName(), false));
    assertTrue(test.contains(JSpottedMarble.class.getName()));
    assertTrue(test.contains(JTransparentMarble.class.getName()));
    test = getSet(mgr.getSubclassesForClass(JBase.class.getName(), true));
    assertTrue(test.contains(JBowl.class.getName()));
    assertTrue(test.contains(JMarble.class.getName()));
    assertTrue(test.contains(JSpottedMarble.class.getName()));
    assertTrue(test.contains(JTransparentMarble.class.getName()));
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) JBase(org.jpox.samples.inheritance.JBase) JMarble(org.jpox.samples.inheritance.JMarble) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 3 with JDOMetaDataManager

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

the class BasicTest method testDependent.

/**
 * Test for defaulting of "dependent" attribute on a field and on a collection.
 */
public void testDependent() {
    MetaDataManager mgr = new JDOMetaDataManager(new PersistenceNucleusContextImpl("JDO", null));
    mgr.setValidate(false);
    AbstractClassMetaData cmdInh2 = mgr.getMetaDataForClass(Container.class, new ClassLoaderResolverImpl());
    AbstractMemberMetaData fmd = cmdInh2.getMetaDataForMember("members");
    // mmd doesn't have dependent attribute, so should default to false
    assertFalse(fmd.isDependent());
    assertFalse(fmd.getCollection().isDependentElement());
}
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) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 4 with JDOMetaDataManager

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

the class BasicTest method testMetaDataManagerI18N.

/**
 * Test for the MetaDataManager.
 */
public void testMetaDataManagerI18N() {
    JDOMetaDataHelper mdProc = new JDOMetaDataHelper();
    String filename = "/org/jpox/samples/i18n/UTF8.jdo";
    MetaDataManager mmgr1 = new JDOMetaDataManager(new PersistenceNucleusContextImpl("JDO", null));
    MetaDataParser parser1 = new MetaDataParser(mmgr1, mmgr1.getNucleusContext().getPluginManager(), true, true);
    mmgr1.setValidate(false);
    FileMetaData fmd = (FileMetaData) parser1.parseMetaDataStream(MetaDataParser.class.getResourceAsStream(filename), filename, "jdo");
    fmd.getPackage("org.jpox.samples.i18n").getClass("UTF8");
    org.datanucleus.metadata.AbstractClassMetaData cmd1 = fmd.getPackage("org.jpox.samples.i18n").getClass("UTF8");
    cmd1.populate(new ClassLoaderResolverImpl(), null, mmgr1);
    cmd1.initialise(new ClassLoaderResolverImpl());
    MetaDataManager mmgr2 = new JDOMetaDataManager(new PersistenceNucleusContextImpl("JDO", null));
    mmgr2.setValidate(false);
    org.datanucleus.metadata.AbstractClassMetaData cmd = mmgr2.getMetaDataForClass(UTF8.class, new ClassLoaderResolverImpl());
    if (!mdProc.getXMLForMetaData(cmd, "", "").equals(mdProc.getXMLForMetaData(cmd1, "", ""))) {
        fail("i18n UTF-8 issues in parser");
    }
    filename = "/org/jpox/samples/i18n/ISO8859_2.jdo";
    MetaDataManager mmgr3 = new JDOMetaDataManager(new PersistenceNucleusContextImpl("JDO", null));
    MetaDataParser parser3 = new MetaDataParser(mmgr3, mmgr3.getNucleusContext().getPluginManager(), true, true);
    mmgr3.setValidate(false);
    fmd = (FileMetaData) parser3.parseMetaDataStream(MetaDataParser.class.getResourceAsStream(filename), filename, "jdo");
    fmd.getPackage("org.jpox.samples.i18n").getClass("ISO8859_2");
    MetaDataManager mmgr4 = new JDOMetaDataManager(new PersistenceNucleusContextImpl("JDO", null));
    mmgr4.setValidate(false);
    cmd1 = fmd.getPackage("org.jpox.samples.i18n").getClass("ISO8859_2");
    cmd1.populate(new ClassLoaderResolverImpl(), null, mmgr4);
    cmd1.initialise(null);
    cmd = mmgr4.getMetaDataForClass(ISO8859_2.class, new ClassLoaderResolverImpl());
    if (!mdProc.getXMLForMetaData(cmd, "", "").equals(mdProc.getXMLForMetaData(cmd1, "", ""))) {
        fail("i18n ISO8859_2 issues in parser");
    }
}
Also used : JDOMetaDataHelper(org.datanucleus.api.jdo.metadata.JDOMetaDataHelper) ISO8859_2(org.jpox.samples.i18n.ISO8859_2) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) MetaDataParser(org.datanucleus.metadata.xml.MetaDataParser) FileMetaData(org.datanucleus.metadata.FileMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Example 5 with JDOMetaDataManager

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

the class BasicTest method testPersistentProperties.

/**
 * Test that MetaData classes can be serialised.
 * Why we would ever want to serialise a MetaData definition is not known to me.
 * @throws Throwable
 */
/*public void testSerialization() throws Throwable
    {
        MetaDataManager mgr=new JDOMetaDataManager(new NucleusContext(new PersistenceConfiguration(){}));
        mgr.setValidate(false);
        AbstractClassMetaData cmdInh2 = mgr.getMetaDataForClass(Container.class, new ClassLoaderResolverImpl());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(cmdInh2);
        byte[] bytes = baos.toByteArray();

        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        ObjectInputStream ois;
        ois = new ObjectInputStream(bais);
        Object obj = ois.readObject();
        assertTrue(obj.toString().equals(cmdInh2.toString()));
    }*/
public void testPersistentProperties() {
    MetaDataManager mgr = new JDOMetaDataManager(new PersistenceNucleusContextImpl("JDO", null));
    mgr.setValidate(false);
    AbstractClassMetaData cmd = mgr.getMetaDataForClass(Vampire.class, new ClassLoaderResolverImpl());
    assertEquals(FieldPersistenceModifier.NONE, cmd.getMetaDataForMember("_age").getPersistenceModifier());
    assertEquals(FieldPersistenceModifier.NONE, cmd.getMetaDataForMember("_name").getPersistenceModifier());
    assertEquals(FieldPersistenceModifier.PERSISTENT, cmd.getMetaDataForMember("name").getPersistenceModifier());
    assertEquals(FieldPersistenceModifier.PERSISTENT, cmd.getMetaDataForMember("age").getPersistenceModifier());
    cmd = mgr.getMetaDataForClass(Phantom.class, new ClassLoaderResolverImpl());
    assertEquals(2, cmd.getNoOfManagedMembers());
    assertEquals(FieldPersistenceModifier.PERSISTENT, cmd.getMetaDataForMember("name").getPersistenceModifier());
    assertEquals(FieldPersistenceModifier.PERSISTENT, cmd.getMetaDataForMember("age").getPersistenceModifier());
}
Also used : Phantom(org.datanucleus.samples.haunted.Phantom) 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