use of com.devonfw.cobigen.impl.config.entity.Template in project cobigen by devonfw.
the class TemplatesConfigurationReaderTest method testTemplatesSourceFolder.
@Test
public void testTemplatesSourceFolder() {
// given
TemplatesConfigurationReader target = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "valid_source_folder");
Trigger trigger = new Trigger("", "asdf", "", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
// when
Map<String, Template> templates = target.loadTemplates(trigger);
// then
assertThat(templates).isNotNull().hasSize(6);
String templateIdFooClass = "prefix_FooClass.java";
Template templateFooClass = templates.get(templateIdFooClass);
assertThat(templateFooClass).isNotNull();
assertThat(templateFooClass.getName()).isEqualTo(templateIdFooClass);
assertThat(templateFooClass.getRelativeTemplatePath()).isEqualTo("foo/FooClass.java.ftl");
assertThat(templateFooClass.getUnresolvedTargetPath()).isEqualTo("src/main/java/foo/FooClass.java");
assertThat(templateFooClass.getMergeStrategy()).isNull();
}
use of com.devonfw.cobigen.impl.config.entity.Template in project cobigen by devonfw.
the class TemplatesConfigurationReaderTest method testRelocate_withTemplateFilenameEnding.
/**
* Test relocate while the template is defined with the template file ending, which should be removed on destination
* path resolution.
*/
@Test
public void testRelocate_withTemplateFilenameEnding() {
// given
String templatesConfigurationRoot = testFileRootPath + "valid_relocate_template_fileending/";
TemplatesConfigurationReader target = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "valid_relocate_template_fileending/");
Trigger trigger = new Trigger("id", "type", "valid_relocate", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
// when
Map<String, Template> templates = target.loadTemplates(trigger);
// validation
assertThat(templates).hasSize(1);
String staticRelocationPrefix = "../server/";
String templateName = "$_Component_$Impl.java";
Template template = templates.get(templateName);
assertThat(template).isNotNull();
String pathWithName = "$_rootpackage_$/$_component_$/logic/impl/" + templateName;
assertThat(template.getRelativeTemplatePath()).isEqualTo("templates/" + pathWithName + ".ftl");
assertThat(template.getAbsoluteTemplatePath().toString().replace('\\', '/')).isEqualTo(templatesConfigurationRoot + "templates/" + pathWithName + ".ftl");
assertThat(template.getUnresolvedTemplatePath()).isEqualTo("src/main/java/" + pathWithName);
assertThat(template.getUnresolvedTargetPath()).isEqualTo(staticRelocationPrefix + pathWithName);
}
use of com.devonfw.cobigen.impl.config.entity.Template in project cobigen by devonfw.
the class TemplatesConfigurationReaderTest method testRelocate_propertiesResolution.
/**
* Tests the correct property inheritance and resolution of cobigen.properties within a template set read by a
* template scan.
*/
@Test
public void testRelocate_propertiesResolution() {
// arrange
TemplatesConfigurationReader target = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "valid_relocate_propertiesresolution/");
Trigger trigger = new Trigger("id", "type", "valid_relocate", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
// act
Map<String, Template> templates = target.loadTemplates(trigger);
assertThat(templates).hasSize(2);
// assert
Template template = templates.get("$_Component_$.java");
assertThat(template).isNotNull();
assertThat(template.getVariables().asMap()).isNotNull().containsEntry("foo", "root").containsEntry("bar", "barValue");
template = templates.get("$_EntityName_$Eto.java");
assertThat(template).isNotNull();
assertThat(template.getVariables().asMap()).isNotNull().containsEntry("relocate", "../api2/${cwd}").containsEntry("foo", "logic.api.to").containsEntry("bar", "barValue").containsEntry("local", "localValue");
}
use of com.devonfw.cobigen.impl.config.entity.Template in project cobigen by devonfw.
the class TemplatesConfigurationReaderTest method testTemplateScan.
/**
* Tests that templates will be correctly resolved by the template-scan mechanism.
*
* @throws Exception test fails
*/
@Test
public void testTemplateScan() throws Exception {
// given
TemplatesConfigurationReader target = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "valid");
Trigger trigger = new Trigger("", "asdf", "", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
// when
Map<String, Template> templates = target.loadTemplates(trigger);
// then
assertThat(templates).isNotNull().hasSize(6);
String templateIdFooClass = "prefix_FooClass.java";
Template templateFooClass = templates.get(templateIdFooClass);
assertThat(templateFooClass).isNotNull();
assertThat(templateFooClass.getName()).isEqualTo(templateIdFooClass);
assertThat(templateFooClass.getRelativeTemplatePath()).isEqualTo("foo/FooClass.java.ftl");
assertThat(templateFooClass.getUnresolvedTargetPath()).isEqualTo("src/main/java/foo/FooClass.java");
assertThat(templateFooClass.getMergeStrategy()).isNull();
}
use of com.devonfw.cobigen.impl.config.entity.Template in project cobigen by devonfw.
the class TemplatesConfigurationReaderTest method testTemplateExtensionDeclarationOverridesTemplateScanDefaults.
/**
* Tests the overriding of all possible attributes by templateExtensions
*
* @throws Exception test fails
*/
@Test
public void testTemplateExtensionDeclarationOverridesTemplateScanDefaults() throws Exception {
// given
TemplatesConfigurationReader target = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "valid");
Trigger trigger = new Trigger("", "asdf", "", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
// when
Map<String, Template> templates = target.loadTemplates(trigger);
// validation
// check scan default as precondition for this test. If they change, this test might be worth to be
// adapted
String templateIdBarClass = "prefix2_BarClass.java";
Template templateBarClass = templates.get(templateIdBarClass);
assertThat(templateBarClass).isNotNull();
// template-scan defaults
assertThat(templateBarClass.getName()).isEqualTo(templateIdBarClass);
assertThat(templateBarClass.getRelativeTemplatePath()).isEqualTo("bar/BarClass.java.ftl");
assertThat(templateBarClass.getUnresolvedTargetPath()).isEqualTo("src/main/java/bar/BarClass.java");
assertThat(templateBarClass.getMergeStrategy()).isNull();
assertThat(templateBarClass.getTargetCharset()).isEqualTo("UTF-8");
// check defaults overwriting by templateExtensions
String templateIdFooClass = "prefix2_FooClass.java";
Template templateFooClass = templates.get(templateIdFooClass);
assertThat(templateFooClass).isNotNull();
// template-scan defaults
assertThat(templateFooClass.getName()).isEqualTo(templateIdFooClass);
assertThat(templateFooClass.getRelativeTemplatePath()).isEqualTo("bar/FooClass.java.ftl");
// overwritten by templateExtension
assertThat(templateFooClass.getUnresolvedTargetPath()).isEqualTo("adapted/path/FooClass.java");
assertThat(templateFooClass.getMergeStrategy()).isEqualTo("javamerge");
assertThat(templateFooClass.getTargetCharset()).isEqualTo("ISO-8859-1");
}
Aggregations