use of org.motechproject.mds.domain.ClassData in project motech by motech.
the class EnumBuilderImplTest method testBuild.
@Test
public void testBuild() throws Exception {
String[] expectedValues = { "A", "B", "C", "D", "E" };
TypeSetting typeSetting = new TypeSetting();
typeSetting.setName(Constants.Settings.COMBOBOX_VALUES);
FieldSetting fieldSetting = new FieldSetting(null, typeSetting, StringUtils.join(expectedValues, "\n"));
Type type = new Type("mds.field.combobox", null, null);
Field field = new Field(null, "swag", "swag", false, false, false, false, false, null, null, null, null);
field.setType(type);
field.addSetting(fieldSetting);
Entity entity = new Entity(CLASS_NAME);
entity.addField(field);
ComboboxHolder holder = new ComboboxHolder(entity, field);
ClassData data = new EnumBuilderImpl().build(holder);
MDSClassLoader.getInstance().safeDefineClass(data.getClassName(), data.getBytecode());
Class<?> enumClass = MDSClassLoader.getInstance().loadClass(data.getClassName());
assertTrue("The class definition should be enum", enumClass.isEnum());
Object[] enumConstants = enumClass.getEnumConstants();
String[] actualValues = new String[enumConstants.length];
for (int i = 0; i < enumConstants.length; i++) {
actualValues[i] = enumConstants[i].toString();
}
Arrays.sort(expectedValues, String.CASE_INSENSITIVE_ORDER);
Arrays.sort(actualValues, String.CASE_INSENSITIVE_ORDER);
assertArrayEquals(expectedValues, actualValues);
}
use of org.motechproject.mds.domain.ClassData in project motech by motech.
the class EntityBuilderTest method buildClass.
private Class<?> buildClass() {
ClassData classData = entityBuilder.build(entity, fields);
assertEquals(ENTITY_NAME, classData.getClassName());
return mdsClassLoader.safeDefineClass(classData.getClassName(), classData.getBytecode());
}
use of org.motechproject.mds.domain.ClassData in project motech by motech.
the class EntityBuilderTest method shouldBuildHistoryClass.
@Test
public void shouldBuildHistoryClass() throws Exception {
fields.addAll(asList(fieldDto("id", Long.class), fieldDto("count", Integer.class), fieldDto("time", Time.class), fieldDto("str", String.class), fieldDto("dec", Double.class), fieldDto("bool", Boolean.class), fieldDto("date", Date.class), fieldDto("dt", DateTime.class), fieldDto("list", List.class), fieldDto("jd", java.time.LocalDate.class), fieldDto("jld", LocalDateTime.class)));
ClassData classData = entityBuilder.buildHistory(entity, fields);
assertEquals("xx.yy.history.BuilderTest__History", classData.getClassName());
Class<?> clazz = mdsClassLoader.safeDefineClass(classData.getClassName(), classData.getBytecode());
assertNotNull(clazz);
assertField(clazz, StringUtils.uncapitalize(clazz.getSimpleName()) + Constants.Util.CURRENT_VERSION, Long.class);
assertField(clazz, "id", Long.class);
assertField(clazz, "count", Integer.class);
assertField(clazz, "time", Time.class);
assertField(clazz, "str", String.class);
assertField(clazz, "dec", Double.class);
assertField(clazz, "bool", Boolean.class);
assertField(clazz, "date", Date.class);
assertField(clazz, "dt", DateTime.class);
assertField(clazz, "jd", java.time.LocalDate.class);
assertField(clazz, "jld", LocalDateTime.class);
assertField(clazz, "list", List.class);
}
use of org.motechproject.mds.domain.ClassData in project motech by motech.
the class EntityInfrastructureBuilderTest method shouldCreateCodeForClassWithLookups.
@Test
public void shouldCreateCodeForClassWithLookups() throws Exception {
MDSClassLoader mdsClassLoaderImpl = MDSClassLoader.getStandaloneInstance(getClass().getClassLoader());
EntityDto entity = new EntityDto(SampleWithLookups.class.getName());
entity.setMaxFetchDepth(-1);
LookupDto lookup = new LookupDto();
lookup.setLookupName("testLookup");
lookup.setMethodName("testLookupMethod");
FieldDto testField = fieldDto("TestField", "testDispName", String.class);
FieldDto testField2 = fieldDto("TestField2", "DisplayName with space", String.class);
FieldDto dateField = fieldDto("dateField", "Display names should not affect methods", DateTime.class);
FieldDto timeField = fieldDto("timeField", Time.class);
when(schemaHolder.getFieldByName(SampleWithLookups.class.getName(), "TestField")).thenReturn(testField);
when(schemaHolder.getFieldByName(SampleWithLookups.class.getName(), "TestField2")).thenReturn(testField2);
when(schemaHolder.getFieldByName(SampleWithLookups.class.getName(), "dateField")).thenReturn(dateField);
when(schemaHolder.getFieldByName(SampleWithLookups.class.getName(), "timeField")).thenReturn(timeField);
lookup.setFieldsOrder(asList("TestField", "TestField2", "dateField", "timeField"));
lookup.setSingleObjectReturn(true);
when(schemaHolder.getLookups(entity)).thenReturn(singletonList(lookup));
List<LookupFieldDto> lookupFields = new ArrayList<>();
lookupFields.add(new LookupFieldDto("TestField", LookupFieldType.VALUE));
lookupFields.add(new LookupFieldDto("TestField2", LookupFieldType.VALUE));
lookupFields.add(new LookupFieldDto("dateField", LookupFieldType.RANGE));
lookupFields.add(new LookupFieldDto("timeField", LookupFieldType.SET));
lookup.setLookupFields(lookupFields);
List<ClassData> data = entityInfrastructureBuilder.buildInfrastructure(entity, schemaHolder);
for (ClassData classData : data) {
mdsClassLoaderImpl.safeDefineClass(classData.getClassName(), classData.getBytecode());
}
verifySingleLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_SERVICE));
verifySingleLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_INTERFACE));
verifyCountLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_SERVICE));
verifyCountLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_INTERFACE));
// lookup with multiple return
lookup.setSingleObjectReturn(false);
mdsClassLoaderImpl = MDSClassLoader.getStandaloneInstance(getClass().getClassLoader());
data = entityInfrastructureBuilder.buildInfrastructure(entity, schemaHolder);
for (ClassData classData : data) {
mdsClassLoaderImpl.safeDefineClass(classData.getClassName(), classData.getBytecode());
}
verifyMultiReturnLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_SERVICE));
verifyMultiReturnLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_INTERFACE));
verifyCountLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_SERVICE));
verifyCountLookup(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_INTERFACE));
verifyFetchDepthInRepository(mdsClassLoaderImpl.loadClass(SAMPLE_WITH_LOOKUPS_REPOSITORY), -1);
}
use of org.motechproject.mds.domain.ClassData in project motech by motech.
the class MdsWeavingHook method weave.
@Override
public void weave(WovenClass wovenClass) {
String className = wovenClass.getClassName();
// definition of org.slf4j.helpers.MessageFormatter (it isn't exported by slf4j bundle)
if (className.startsWith("org.slf4j")) {
return;
}
LOGGER.trace("Weaving called for: {}", className);
ClassData enhancedClassData = MotechClassPool.getEnhancedClassData(className);
if (enhancedClassData == null) {
LOGGER.trace("The class doesn't have enhanced metadata: {}", className);
} else {
LOGGER.info("Weaving {}", className);
// these imports will be required by the provider
addCommonImports(wovenClass);
// add dynamic imports to enums and mds entities
addDynamicImports(wovenClass);
// change the bytecode
wovenClass.setBytes(enhancedClassData.getBytecode());
}
}
Aggregations