Search in sources :

Example 1 with FreeMarkerTemplateEngine

use of com.devonfw.cobigen.tempeng.freemarker.FreeMarkerTemplateEngine in project cobigen by devonfw.

the class FreeMarkerTemplateEngineTest method testBasicGeneration.

/**
 * Tests a basic FreeMarker generation
 */
@Test
public void testBasicGeneration() {
    // arrange
    final File templateFolder = new File(testFileRootPath + "basicGeneration/").getAbsoluteFile();
    TextTemplate template = new TextTemplate() {

        @Override
        public String getRelativeTemplatePath() {
            return "template.ftl";
        }

        @Override
        public Path getAbsoluteTemplatePath() {
            return templateFolder.toPath().resolve("template.ftl");
        }
    };
    HashMap<String, Object> model = new HashMap<>();
    List<Object> fields = new ArrayList<>();
    HashMap<Object, Object> fieldAttr = new HashMap<>();
    fieldAttr.put("type", "A");
    fields.add(fieldAttr);
    fieldAttr = new HashMap<>();
    fieldAttr.put("type", "B");
    fields.add(fieldAttr);
    fieldAttr = new HashMap<>();
    fieldAttr.put("type", "C");
    fields.add(fieldAttr);
    HashMap<String, Object> fieldsAccessor = new HashMap<>();
    fieldsAccessor.put("fields", fields);
    model.put("pojo", fieldsAccessor);
    // act
    StringWriter out = new StringWriter();
    FreeMarkerTemplateEngine templateEngine = new FreeMarkerTemplateEngine();
    templateEngine.setTemplateFolder(templateFolder.toPath());
    templateEngine.process(template, model, out, "UTF-8");
    // assert
    assertThat(out).hasToString("A,B,C,");
}
Also used : FreeMarkerTemplateEngine(com.devonfw.cobigen.tempeng.freemarker.FreeMarkerTemplateEngine) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) File(java.io.File) TextTemplate(com.devonfw.cobigen.api.extension.TextTemplate) Test(org.junit.Test)

Aggregations

TextTemplate (com.devonfw.cobigen.api.extension.TextTemplate)1 FreeMarkerTemplateEngine (com.devonfw.cobigen.tempeng.freemarker.FreeMarkerTemplateEngine)1 File (java.io.File)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1