Search in sources :

Example 11 with TemplatesConfigurationReader

use of com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader in project cobigen by devonfw.

the class TemplatesConfigurationReaderTest method testErrorOnDuplicateScannedIds.

/**
 * Tests whether a two equally named files will result in an {@link InvalidConfigurationException} if they are scanned
 * with the same prefix
 *
 * @throws InvalidConfigurationException expected
 */
@Test(expected = InvalidConfigurationException.class)
public void testErrorOnDuplicateScannedIds() throws InvalidConfigurationException {
    TemplatesConfigurationReader reader = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "faulty_duplicate_scanned_id");
    reader.loadTemplates(null);
}
Also used : TemplatesConfigurationReader(com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader) File(java.io.File) AbstractUnitTest(com.devonfw.cobigen.unittest.config.common.AbstractUnitTest) Test(org.junit.Test)

Example 12 with TemplatesConfigurationReader

use of com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader in project cobigen by devonfw.

the class TemplatesConfigurationReaderTest method testRelocate_overlappingTemplateExtensionAndScan.

/**
 * Tests the rewriting of the destination path of a scanned template by using the {@link TemplateExtension}
 * configuration element. The explicitly configured destination path from the configuration should have precedence
 * over the relocated path of the template scan.
 */
@Test
public void testRelocate_overlappingTemplateExtensionAndScan() {
    // given
    String templateScanDestinationPath = "src/main/java/";
    String templatesConfigurationRoot = testFileRootPath + "valid_relocate_templateExt_vs_scan/";
    TemplatesConfigurationReader target = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "valid_relocate_templateExt_vs_scan/");
    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);
    assertThat(templates).hasSize(2);
    // validation
    String staticRelocationPrefix = "../api/";
    String scanRelTemplatePath = "$_rootpackage_$/$_component_$/common/api/";
    Template template = verifyScannedTemplate(templates, "$_EntityName_$.java", scanRelTemplatePath, templatesConfigurationRoot, staticRelocationPrefix, templateScanDestinationPath);
    String templateName = "$_EntityName_$2.java";
    template = templates.get(templateName);
    assertThat(template).isNotNull();
    String pathWithName = scanRelTemplatePath + templateName;
    assertThat(template.getRelativeTemplatePath()).isEqualTo("templates/" + pathWithName);
    assertThat(template.getAbsoluteTemplatePath().toString().replace('\\', '/')).isEqualTo(templatesConfigurationRoot + "templates/" + pathWithName);
    assertThat(template.getUnresolvedTemplatePath()).isEqualTo(templateScanDestinationPath + scanRelTemplatePath + templateName);
    assertThat(template.getUnresolvedTargetPath()).isEqualTo(templateName);
}
Also used : Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) Matcher(com.devonfw.cobigen.impl.config.entity.Matcher) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) TemplatesConfigurationReader(com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader) File(java.io.File) Template(com.devonfw.cobigen.impl.config.entity.Template) AbstractUnitTest(com.devonfw.cobigen.unittest.config.common.AbstractUnitTest) Test(org.junit.Test)

Example 13 with TemplatesConfigurationReader

use of com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader 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);
}
Also used : Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) Matcher(com.devonfw.cobigen.impl.config.entity.Matcher) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) TemplatesConfigurationReader(com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader) File(java.io.File) Template(com.devonfw.cobigen.impl.config.entity.Template) AbstractUnitTest(com.devonfw.cobigen.unittest.config.common.AbstractUnitTest) Test(org.junit.Test)

Example 14 with TemplatesConfigurationReader

use of com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader 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();
}
Also used : Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) Matcher(com.devonfw.cobigen.impl.config.entity.Matcher) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) TemplatesConfigurationReader(com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader) File(java.io.File) Template(com.devonfw.cobigen.impl.config.entity.Template) AbstractUnitTest(com.devonfw.cobigen.unittest.config.common.AbstractUnitTest) Test(org.junit.Test)

Example 15 with TemplatesConfigurationReader

use of com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader 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");
}
Also used : Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) Matcher(com.devonfw.cobigen.impl.config.entity.Matcher) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) TemplatesConfigurationReader(com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader) File(java.io.File) Template(com.devonfw.cobigen.impl.config.entity.Template) AbstractUnitTest(com.devonfw.cobigen.unittest.config.common.AbstractUnitTest) Test(org.junit.Test)

Aggregations

TemplatesConfigurationReader (com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader)20 AbstractUnitTest (com.devonfw.cobigen.unittest.config.common.AbstractUnitTest)20 File (java.io.File)20 Test (org.junit.Test)20 ContainerMatcher (com.devonfw.cobigen.impl.config.entity.ContainerMatcher)15 Matcher (com.devonfw.cobigen.impl.config.entity.Matcher)15 Template (com.devonfw.cobigen.impl.config.entity.Template)15 Trigger (com.devonfw.cobigen.impl.config.entity.Trigger)15 ConfigurationHolder (com.devonfw.cobigen.impl.config.ConfigurationHolder)2 Increment (com.devonfw.cobigen.impl.config.entity.Increment)2 ContextConfigurationReader (com.devonfw.cobigen.impl.config.reader.ContextConfigurationReader)2 Path (java.nio.file.Path)2 HashMap (java.util.HashMap)1