use of javax.jdo.metadata.JDOMetadata in project motech by motech.
the class MetadataHolderTest method shouldReturnAndReloadMetadata.
@Test
public void shouldReturnAndReloadMetadata() {
JDOMetadata jdoMetadata = mock(JDOMetadata.class);
when(pmf.newMetadata()).thenReturn(jdoMetadata);
// constructs initial metadata, then reloads
assertEquals(jdoMetadata, metadataHolder.getJdoMetadata());
assertEquals(jdoMetadata, metadataHolder.reloadMetadata());
verify(pmf, times(2)).newMetadata();
// retrieves existing metadata
assertEquals(jdoMetadata, metadataHolder.getJdoMetadata());
verifyNoMoreInteractions(pmf);
}
use of javax.jdo.metadata.JDOMetadata in project tests by datanucleus.
the class JDOMetadataAPITest method testClassInPackageAndJDO.
/**
* Simple test that checks that classes retrieved have a parent package etc
*/
public void testClassInPackageAndJDO() {
TypeMetadata typemd = pmf.getMetadata(Computer.class.getName());
PackageMetadata pmd = (PackageMetadata) typemd.getParent();
assertNotNull("Package of class is null!", pmd);
assertEquals("Package name is different to expected", Computer.class.getName().substring(0, Computer.class.getName().lastIndexOf('.')), pmd.getName());
JDOMetadata jdomd = (JDOMetadata) pmd.getParent();
assertNotNull("JDOMetadata of package is null!", jdomd);
}
use of javax.jdo.metadata.JDOMetadata in project tests by datanucleus.
the class DynamicEnhanceSchemaToolTest method schemaCreate.
public void schemaCreate(DynamicEnhanceSchemaToolClassLoader runtimeCL) throws Exception {
Map props = getPropertiesForDatastore(runtimeCL);
JDOPersistenceManagerFactory pmf = (JDOPersistenceManagerFactory) JDOHelper.getPersistenceManagerFactory(props);
try {
JDOMetadata filemd = pmf.newMetadata();
createMetadata(filemd);
pmf.registerMetadata(filemd);
Set<String> classNames = new HashSet<>();
classNames.add("test.Client");
PersistenceNucleusContext nucCtx = pmf.getNucleusContext();
StoreManager storeMgr = nucCtx.getStoreManager();
if (!(storeMgr instanceof SchemaAwareStoreManager)) {
// Can't create schema with this datastore
return;
}
try {
SchemaTool schematool = new SchemaTool();
schematool.setDdlFile("target/schema.ddl");
schematool.setCompleteDdl(true);
SchemaAwareStoreManager schemaStoreMgr = (SchemaAwareStoreManager) nucCtx.getStoreManager();
schematool.createSchemaForClasses(schemaStoreMgr, classNames);
} catch (Exception e) {
e.printStackTrace();
}
} finally {
pmf.close();
}
}
use of javax.jdo.metadata.JDOMetadata in project tests by datanucleus.
the class DynamicEnhanceSchemaToolTest method testEnhance.
public void testEnhance() throws Exception {
// Create an in-memory class
String className = "test.Client";
NucleusLogger.PERSISTENCE.info(">> Creating class in-memory");
byte[] classBytes = createClass(className);
// Add it to a CustomClassLoader
DynamicEnhanceSchemaToolClassLoader workCL = new DynamicEnhanceSchemaToolClassLoader(Thread.currentThread().getContextClassLoader());
workCL.defineClass("test.Client", classBytes);
// Write the class to disk (debugging)
NucleusLogger.PERSISTENCE.info(">> Writing in-memory class to target/generated/Client.class");
File file = new File("target/generated");
if (!file.exists()) {
file.mkdirs();
}
FileOutputStream fos = new FileOutputStream("target/generated/Client.class");
fos.write(classBytes);
fos.close();
// Create an enhancer (JDO, ASM)
JDOEnhancer enhancer = JDOHelper.getEnhancer();
enhancer.setClassLoader(workCL);
// Create MetaData for the in-memory class and register it with the enhancer
JDOMetadata jdomd = enhancer.newMetadata();
createMetadata(jdomd);
enhancer.registerMetadata(jdomd);
enhancer.addClass(className, classBytes);
// Enhance the in-memory bytes and obtain the enhanced bytes
NucleusLogger.PERSISTENCE.info(">> Enhancing test.Client inmemory bytes");
enhancer.enhance();
byte[] enhancedBytes = enhancer.getEnhancedBytes(className);
// Write the enhanced class to disk (debugging)
NucleusLogger.PERSISTENCE.info(">> Writing enhanced in-memory class to target/enhancedClient.class");
file = new File("target/enhanced");
if (!file.exists()) {
file.mkdirs();
}
fos = new FileOutputStream("target/enhanced/Client.class");
fos.write(enhancedBytes);
fos.close();
// Create our runtime class loader, and load the enhanced class into it
DynamicEnhanceSchemaToolClassLoader runtimeCL = new DynamicEnhanceSchemaToolClassLoader(Thread.currentThread().getContextClassLoader());
runtimeCL.defineClass(className, enhancedBytes);
// SchemaTool
NucleusLogger.PERSISTENCE.info(">> Schema creation for dynamic type");
schemaCreate(runtimeCL);
// Persist an object of the new type
NucleusLogger.PERSISTENCE.info(">> Persisting an object of dynamic type");
persist(runtimeCL);
}
use of javax.jdo.metadata.JDOMetadata in project motech by motech.
the class MDSConstructorImpl method constructEntities.
@Override
public synchronized boolean constructEntities(SchemaHolder schemaHolder) {
// To be able to register updated class, we need to reload class loader
// and therefore add all the classes again
MotechClassPool.clearEnhancedData();
MDSClassLoader.reloadClassLoader();
// we need an jdo enhancer and a temporary classLoader
// to define classes in before enhancement
MDSClassLoader tmpClassLoader = MDSClassLoader.getStandaloneInstance();
MdsJDOEnhancer enhancer = createEnhancer(tmpClassLoader);
JavassistLoader loader = new JavassistLoader(tmpClassLoader);
// process only entities that are not drafts
List<EntityDto> entities = schemaHolder.getAllEntities();
filterEntities(entities);
sortEntities(entities, schemaHolder);
// create enum for appropriate combobox fields
for (EntityDto entity : entities) {
buildEnum(loader, enhancer, entity, schemaHolder);
}
// load entities interfaces
for (EntityDto entity : entities) {
buildInterfaces(loader, enhancer, entity);
}
// generate jdo metadata from scratch for our entities
JDOMetadata jdoMetadata = metadataHolder.reloadMetadata();
// since we just fetch fields from existing definition
for (EntityDto entity : entities) {
if (entity.isRecordHistory()) {
entityBuilder.prepareHistoryClass(entity);
}
entityBuilder.prepareTrashClass(entity);
}
// Build classes
Map<String, ClassData> classDataMap = buildClasses(entities, schemaHolder);
List<Class> classes = new ArrayList<>();
// the temporary ClassLoader and enhancer
for (EntityDto entity : entities) {
String className = entity.getClassName();
Class<?> definition = addClassData(loader, enhancer, classDataMap.get(className));
if (entity.isRecordHistory()) {
addClassData(loader, enhancer, classDataMap.get(ClassName.getHistoryClassName(className)));
}
addClassData(loader, enhancer, classDataMap.get(ClassName.getTrashClassName(className)));
classes.add(definition);
LOGGER.debug("Generated classes for {}", entity.getClassName());
}
for (Class<?> definition : classes) {
loader.loadFieldsAndMethodsOfClass(definition);
}
// Prepare metadata
buildMetadata(entities, jdoMetadata, classDataMap, classes, schemaHolder);
// after the classes are defined, we register their metadata
enhancer.registerMetadata(jdoMetadata);
// then, we commence with enhancement
enhancer.enhance();
// we register the enhanced class bytes
// and build the infrastructure classes
registerEnhancedClassBytes(entities, enhancer, schemaHolder);
metadataBuilder.fixEnhancerIssuesInMetadata(jdoMetadata, schemaHolder);
return CollectionUtils.isNotEmpty(entities);
}
Aggregations