use of org.datanucleus.metadata.xml.MetaDataParser 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;
}
use of org.datanucleus.metadata.xml.MetaDataParser 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");
}
}
use of org.datanucleus.metadata.xml.MetaDataParser in project datanucleus-api-jdo by datanucleus.
the class JDOMetaDataHandlerTest method testParseDefaultNamespace.
public void testParseDefaultNamespace() {
NucleusContext nucCtx = new PersistenceNucleusContextImpl("JDO", null);
MetaDataParser parser = new MetaDataParser(new JDOMetaDataManager(nucCtx), nucCtx.getPluginManager(), true, true);
MetaData md = parser.parseMetaDataURL(getClass().getResource("/org/datanucleus/api/jdo/metadata/xml/package1.jdo"), "jdo");
assertNotNull(md);
}
use of org.datanucleus.metadata.xml.MetaDataParser in project datanucleus-api-jdo by datanucleus.
the class PersistenceFileMetaDataHandlerTest method testParseNamespace.
public void testParseNamespace() {
NucleusContext nucCtx = new PersistenceNucleusContextImpl("JDO", null);
MetaDataParser parser = new MetaDataParser(new JDOMetaDataManager(nucCtx), nucCtx.getPluginManager(), true, true);
MetaData md = parser.parseMetaDataURL(getClass().getResource("/org/datanucleus/api/jdo/metadata/xml/persistence2.xml"), "persistence");
assertNotNull(md);
}
use of org.datanucleus.metadata.xml.MetaDataParser in project datanucleus-api-jdo by datanucleus.
the class MetaDataParserTest method testParseMetaDataURLnullURL.
public void testParseMetaDataURLnullURL() {
NucleusContext nucCtx = new PersistenceNucleusContextImpl("JDO", null);
MetaDataParser parser = new MetaDataParser(new JDOMetaDataManager(nucCtx), nucCtx.getPluginManager(), true, true);
try {
parser.parseMetaDataURL(null, "jdo");
fail("expected JPOXException");
} catch (NucleusException ex) {
// expected
}
}
Aggregations