Search in sources :

Example 11 with ClassData

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);
}
Also used : Entity(org.motechproject.mds.domain.Entity) ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) FieldSetting(org.motechproject.mds.domain.FieldSetting) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) ClassData(org.motechproject.mds.domain.ClassData) TypeSetting(org.motechproject.mds.domain.TypeSetting) Test(org.junit.Test)

Example 12 with ClassData

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());
}
Also used : ClassData(org.motechproject.mds.domain.ClassData)

Example 13 with ClassData

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);
}
Also used : ClassData(org.motechproject.mds.domain.ClassData) Test(org.junit.Test)

Example 14 with ClassData

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);
}
Also used : MDSClassLoader(org.motechproject.mds.util.MDSClassLoader) EntityDto(org.motechproject.mds.dto.EntityDto) ClassData(org.motechproject.mds.domain.ClassData) LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) SampleWithLookups(org.motechproject.mds.builder.SampleWithLookups) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 15 with ClassData

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());
    }
}
Also used : ClassData(org.motechproject.mds.domain.ClassData)

Aggregations

ClassData (org.motechproject.mds.domain.ClassData)28 Test (org.junit.Test)9 FieldDto (org.motechproject.mds.dto.FieldDto)7 CtClass (javassist.CtClass)6 ArrayList (java.util.ArrayList)5 EntityDto (org.motechproject.mds.dto.EntityDto)5 IOException (java.io.IOException)4 CannotCompileException (javassist.CannotCompileException)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 HashSet (java.util.HashSet)2 ByteArrayClassPath (javassist.ByteArrayClassPath)2 ComboboxHolder (org.motechproject.mds.domain.ComboboxHolder)2 MetadataDto (org.motechproject.mds.dto.MetadataDto)2 TypeDto (org.motechproject.mds.dto.TypeDto)2 EntityCreationException (org.motechproject.mds.exception.entity.EntityCreationException)2 EntBuilderTestClass (org.motechproject.mds.testutil.EntBuilderTestClass)2 MDSClassLoader (org.motechproject.mds.util.MDSClassLoader)2 Bundle (org.osgi.framework.Bundle)2 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1