Search in sources :

Example 26 with Trigger

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

the class TemplatesConfigurationReaderTest method testIncrementRefOutsideCurrentFile.

/**
 * Tests the correct resolution of incrementsRef from outside the current templates file. (Issue #678)
 */
@Test
public void testIncrementRefOutsideCurrentFile() {
    // given
    Trigger trigger = new Trigger("testingTrigger", "asdf", "valid_external_incrementref", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
    Path config = Paths.get(new File(testFileRootPath).toURI());
    ConfigurationHolder configurationHolder = new ConfigurationHolder(config.toUri());
    TemplatesConfiguration templatesConfiguration = configurationHolder.readTemplatesConfiguration(trigger);
    Map<String, Increment> increments = templatesConfiguration.getIncrements();
    // validation
    assertThat(templatesConfiguration.getTrigger().getId()).isEqualTo("testingTrigger");
    assertThat(increments).containsOnlyKeys("3", "4", "5");
    Increment incrementThree = increments.get("3").getDependentIncrements().get(0);
    assertThat(incrementThree.getName()).isEqualTo("0");
    assertThat(incrementThree.getTemplates().size()).isEqualTo(1);
    Increment incrementFour = increments.get("4").getDependentIncrements().get(0);
    assertThat(incrementFour.getName()).isEqualTo("1");
    assertThat(incrementFour.getTemplates().size()).isEqualTo(4);
    Increment incrementFive = increments.get("5").getDependentIncrements().get(0);
    assertThat(incrementFive.getName()).isEqualTo("2");
    assertThat(incrementFive.getTemplates().size()).isEqualTo(4);
}
Also used : Path(java.nio.file.Path) 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) ConfigurationHolder(com.devonfw.cobigen.impl.config.ConfigurationHolder) File(java.io.File) TemplatesConfiguration(com.devonfw.cobigen.impl.config.TemplatesConfiguration) AbstractUnitTest(com.devonfw.cobigen.unittest.config.common.AbstractUnitTest) Test(org.junit.Test)

Example 27 with Trigger

use of com.devonfw.cobigen.impl.config.entity.Trigger 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 28 with Trigger

use of com.devonfw.cobigen.impl.config.entity.Trigger 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)

Example 29 with Trigger

use of com.devonfw.cobigen.impl.config.entity.Trigger 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);
}
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) HashMap(java.util.HashMap) 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 30 with Trigger

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

the class TemplatesConfigurationReaderTest method testCorrectResolutionOfTemplateScanReferences.

/**
 * Tests the correct resolution of template scan references in increments.
 *
 * @throws InvalidConfigurationException test fails
 */
@Test
public void testCorrectResolutionOfTemplateScanReferences() throws InvalidConfigurationException {
    // given
    TemplatesConfigurationReader reader = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "valid_template_scan_references");
    Trigger trigger = new Trigger("", "asdf", "", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
    // when
    Map<String, Template> templates = reader.loadTemplates(trigger);
    Map<String, Increment> increments = reader.loadIncrements(templates, trigger);
    // validation
    assertThat(templates).containsOnlyKeys("prefix_foo_BarClass.java", "prefix_bar_Foo2Class.java", "prefix_foo_FooClass.java");
    assertThat(increments).containsOnlyKeys("test");
    assertThat(increments.get("test").getTemplates()).extracting("name").containsOnly("prefix_foo_BarClass.java", "prefix_foo_FooClass.java");
}
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)

Aggregations

Trigger (com.devonfw.cobigen.impl.config.entity.Trigger)30 File (java.io.File)20 Template (com.devonfw.cobigen.impl.config.entity.Template)19 ContainerMatcher (com.devonfw.cobigen.impl.config.entity.ContainerMatcher)18 Matcher (com.devonfw.cobigen.impl.config.entity.Matcher)17 AbstractUnitTest (com.devonfw.cobigen.unittest.config.common.AbstractUnitTest)17 Test (org.junit.Test)17 TemplatesConfigurationReader (com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader)15 Path (java.nio.file.Path)9 ConfigurationHolder (com.devonfw.cobigen.impl.config.ConfigurationHolder)5 Increment (com.devonfw.cobigen.impl.config.entity.Increment)5 CobiGenRuntimeException (com.devonfw.cobigen.api.exception.CobiGenRuntimeException)4 TemplatesConfiguration (com.devonfw.cobigen.impl.config.TemplatesConfiguration)4 Cached (com.devonfw.cobigen.api.annotation.Cached)3 TriggerInterpreter (com.devonfw.cobigen.api.extension.TriggerInterpreter)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 InvalidConfigurationException (com.devonfw.cobigen.api.exception.InvalidConfigurationException)2 TextTemplateEngine (com.devonfw.cobigen.api.extension.TextTemplateEngine)2 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)2