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);
}
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;
}
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");
}
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());
}
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());
}
Aggregations