use of com.devonfw.cobigen.impl.config.entity.Template in project cobigen by devonfw.
the class TemplatesConfigurationReaderTest method testRelocate.
/**
* Test the basic valid configuration of <a href="https://github.com/devonfw/cobigen/issues/157">issue 157</a> for
* relocation of templates to support multi-module generation.
*/
@Test
public void testRelocate() {
// given
String noRelocation = "";
String templateScanDestinationPath = "src/main/java/";
String templatesConfigurationRoot = testFileRootPath + "valid_relocate/";
TemplatesConfigurationReader target = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "valid_relocate/");
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(3);
String staticRelocationPrefix = "../api/";
verifyScannedTemplate(templates, "$_EntityName_$Entity.java", "$_rootpackage_$/$_component_$/dataaccess/api/", templatesConfigurationRoot, staticRelocationPrefix, templateScanDestinationPath);
staticRelocationPrefix = "../api2/";
verifyScannedTemplate(templates, "$_EntityName_$Eto.java", "$_rootpackage_$/$_component_$/logic/api/to/", templatesConfigurationRoot, staticRelocationPrefix, templateScanDestinationPath);
verifyScannedTemplate(templates, "$_Component_$.java", "$_rootpackage_$/$_component_$/logic/api/", templatesConfigurationRoot, noRelocation, templateScanDestinationPath);
}
use of com.devonfw.cobigen.impl.config.entity.Template in project cobigen by devonfw.
the class TemplatesConfigurationReaderTest method testTemplateScanDoesNotOverwriteExplicitTemplateDeclarations.
/**
* Tests that the template-scan mechanism does not overwrite an explicit template declaration with the defaults
*
* @throws Exception test fails
*/
@Test
public void testTemplateScanDoesNotOverwriteExplicitTemplateDeclarations() 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);
// this one is a predefined template and shall not be overwritten by scan...
String templateIdFoo2Class = "prefix_Foo2Class.java";
Template templateFoo2Class = templates.get(templateIdFoo2Class);
assertThat(templateFoo2Class).isNotNull();
assertThat(templateFoo2Class.getName()).isEqualTo(templateIdFoo2Class);
assertThat(templateFoo2Class.getRelativeTemplatePath()).isEqualTo("foo/Foo2Class.java.ftl");
assertThat(templateFoo2Class.getUnresolvedTargetPath()).isEqualTo("src/main/java/foo/Foo2Class${variable}.java");
assertThat(templateFoo2Class.getMergeStrategy()).isEqualTo("javamerge");
String templateIdBarClass = "prefix_BarClass.java";
Template templateBarClass = templates.get(templateIdBarClass);
assertThat(templateBarClass).isNotNull();
assertThat(templateBarClass.getName()).isEqualTo(templateIdBarClass);
assertThat(templateBarClass.getRelativeTemplatePath()).isEqualTo("foo/bar/BarClass.java.ftl");
assertThat(templateBarClass.getUnresolvedTargetPath()).isEqualTo("src/main/java/foo/bar/BarClass.java");
assertThat(templateBarClass.getMergeStrategy()).isNull();
}
use of com.devonfw.cobigen.impl.config.entity.Template in project cobigen by devonfw.
the class TemplatesConfigurationReaderTest method testEmptyTemplateExtensionDeclarationDoesNotOverrideAnyDefaults.
/**
* Tests an empty templateExtensions does not override any defaults
*
* @throws Exception test fails
*/
@Test
public void testEmptyTemplateExtensionDeclarationDoesNotOverrideAnyDefaults() 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
String templateIdFooClass = "prefix2_Foo2Class.java";
Template templateFooClass = templates.get(templateIdFooClass);
assertThat(templateFooClass).isNotNull();
// template-scan defaults
assertThat(templateFooClass.getName()).isEqualTo(templateIdFooClass);
assertThat(templateFooClass.getRelativeTemplatePath()).isEqualTo("bar/Foo2Class.java.ftl");
assertThat(templateFooClass.getUnresolvedTargetPath()).isEqualTo("src/main/java/bar/Foo2Class.java");
assertThat(templateFooClass.getMergeStrategy()).isNull();
assertThat(templateFooClass.getTargetCharset()).isEqualTo("UTF-8");
}
use of com.devonfw.cobigen.impl.config.entity.Template in project cobigen by devonfw.
the class TemplatesConfigurationReaderTest method verifyScannedTemplate.
/**
* Verifies a template's path properties
*
* @param templates list of all templates mapping template name to template
* @param templateName name of the template
* @param templatePath template path
* @param templatesConfigurationRoot configuration root folder of the templates configuration
* @param staticRelocationPrefix static prefix of a relocation value excluding ${cwd}
* @param templateScanDestinationPath destination path of the involved {@link TemplateScan}
* @return the template with the given templateName
*/
private Template verifyScannedTemplate(Map<String, Template> templates, String templateName, String templatePath, String templatesConfigurationRoot, String staticRelocationPrefix, String templateScanDestinationPath) {
Template template = templates.get(templateName);
assertThat(template).isNotNull();
String pathWithName = templatePath + templateName;
assertThat(template.getRelativeTemplatePath()).isEqualTo("templates/" + pathWithName);
assertThat(template.getAbsoluteTemplatePath().toString().replace('\\', '/')).isEqualTo(templatesConfigurationRoot + "templates/" + pathWithName);
assertThat(template.getUnresolvedTemplatePath()).isEqualTo("src/main/java/" + pathWithName);
if (StringUtils.isEmpty(staticRelocationPrefix)) {
assertThat(template.getUnresolvedTargetPath()).isEqualTo(templateScanDestinationPath + pathWithName);
} else {
assertThat(template.getUnresolvedTargetPath()).isEqualTo(staticRelocationPrefix + pathWithName);
}
return template;
}
use of com.devonfw.cobigen.impl.config.entity.Template in project cobigen by devonfw.
the class TemplatesConfigurationReaderTest method testTemplatesOfAPackageRetrieval.
/**
* Tests whether all templates of a template package could be retrieved successfully.
*
* @throws Exception test fails
*/
@Test
public void testTemplatesOfAPackageRetrieval() throws Exception {
TemplatesConfigurationReader target = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "valid");
Trigger trigger = new Trigger("", "asdf", "", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
Template templateMock = mock(Template.class);
HashMap<String, Template> templates = new HashMap<>();
templates.put("resources_resources_spring_common", templateMock);
target.loadIncrements(templates, trigger);
}
Aggregations