Search in sources :

Example 6 with ContainerMatcher

use of com.devonfw.cobigen.impl.config.entity.ContainerMatcher 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");
}
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 7 with ContainerMatcher

use of com.devonfw.cobigen.impl.config.entity.ContainerMatcher 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();
}
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 8 with ContainerMatcher

use of com.devonfw.cobigen.impl.config.entity.ContainerMatcher 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");
}
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 9 with ContainerMatcher

use of com.devonfw.cobigen.impl.config.entity.ContainerMatcher in project cobigen by devonfw.

the class TemplatesConfigurationReaderTest method testIncrementComposition_combiningAllPossibleReferences.

/**
 * Tests the correct resolution of references of templates / templateScans / increments.
 */
@Test
public void testIncrementComposition_combiningAllPossibleReferences() {
    // given
    TemplatesConfigurationReader target = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "valid_increment_composition");
    Trigger trigger = new Trigger("", "asdf", "", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
    // when
    Map<String, Template> templates = target.loadTemplates(trigger);
    Map<String, Increment> increments = target.loadIncrements(templates, trigger);
    // validation
    assertThat(templates).containsOnlyKeys("templateDecl", "prefix_scanned", "scanned", "prefix_scanned2", "scanned2");
    assertThat(increments).containsOnlyKeys("0", "1", "2");
    assertThat(increments.values()).hasSize(3);
    assertThat(increments.get("0").getTemplates()).extracting("name").containsOnly("templateDecl");
    assertThat(increments.get("1").getTemplates()).extracting("name").containsOnly("templateDecl", "prefix_scanned", "scanned", "scanned2");
    assertThat(increments.get("2").getTemplates()).extracting("name").containsOnly("templateDecl", "prefix_scanned", "scanned", "prefix_scanned2");
}
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) Increment(com.devonfw.cobigen.impl.config.entity.Increment) 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 10 with ContainerMatcher

use of com.devonfw.cobigen.impl.config.entity.ContainerMatcher in project cobigen by devonfw.

the class TemplatesConfigurationReaderTest method testRelocate_overlappingExplicitTemplateDestinationPathAndRelocatedScanPath.

/**
 * Tests an overlapping configuration according to the destination paths of a relocated folder within a template scan
 * and a explicitly defined destination path of a template configuration XML node. The destination path of a template
 * configuration should not be affected by any relocation of any template scan.
 */
@Test
public void testRelocate_overlappingExplicitTemplateDestinationPathAndRelocatedScanPath() {
    // given
    String templateScanDestinationPath = "src/main/java/";
    String templatesConfigurationRoot = testFileRootPath + "valid_relocate_template_vs_scan/";
    TemplatesConfigurationReader target = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "valid_relocate_template_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);
    template = templates.get("ExplicitlyDefined");
    assertThat(template).isNotNull();
    assertThat(template.getRelativeTemplatePath()).isEqualTo("OuterTemplate.java");
    assertThat(template.getAbsoluteTemplatePath().toString().replace('\\', '/')).isEqualTo(templatesConfigurationRoot + "OuterTemplate.java");
    // the destination path has designed to match a relocated path during the scan by intention
    String destinationPath = "src/main/java/$_rootpackage_$/$_component_$/common/api/ExplicitlyDefined.java";
    assertThat(template.getUnresolvedTemplatePath()).isEqualTo(destinationPath);
    assertThat(template.getUnresolvedTargetPath()).isEqualTo(destinationPath);
    assertThat(template.getVariables().asMap()).hasSize(0);
}
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

ContainerMatcher (com.devonfw.cobigen.impl.config.entity.ContainerMatcher)19 Trigger (com.devonfw.cobigen.impl.config.entity.Trigger)18 Matcher (com.devonfw.cobigen.impl.config.entity.Matcher)17 AbstractUnitTest (com.devonfw.cobigen.unittest.config.common.AbstractUnitTest)17 File (java.io.File)17 Test (org.junit.Test)17 Template (com.devonfw.cobigen.impl.config.entity.Template)16 TemplatesConfigurationReader (com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader)15 ConfigurationHolder (com.devonfw.cobigen.impl.config.ConfigurationHolder)4 Increment (com.devonfw.cobigen.impl.config.entity.Increment)4 Path (java.nio.file.Path)4 Cached (com.devonfw.cobigen.api.annotation.Cached)2 TriggerInterpreter (com.devonfw.cobigen.api.extension.TriggerInterpreter)2 MatcherTo (com.devonfw.cobigen.api.to.MatcherTo)2 TemplatesConfiguration (com.devonfw.cobigen.impl.config.TemplatesConfiguration)2 ContextConfigurationReader (com.devonfw.cobigen.impl.config.reader.ContextConfigurationReader)2 InputReader (com.devonfw.cobigen.api.extension.InputReader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1