Search in sources :

Example 1 with ContainerMatcher

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

the class InputResolverImpl method resolveContainerElements.

@Cached
@Override
public List<Object> resolveContainerElements(Object input, Trigger trigger) {
    List<Object> inputObjects = new ArrayList<>();
    if (this.inputInterpreter.combinesMultipleInputs(input)) {
        TriggerInterpreter triggerInterpreter = PluginRegistry.getTriggerInterpreter(trigger.getType());
        InputReader inputReader = triggerInterpreter.getInputReader();
        // check whether the inputs should be retrieved recursively
        boolean retrieveInputsRecursively = false;
        for (ContainerMatcher containerMatcher : trigger.getContainerMatchers()) {
            MatcherTo matcherTo = new MatcherTo(containerMatcher.getType(), containerMatcher.getValue(), input);
            if (triggerInterpreter.getMatcher().matches(matcherTo)) {
                if (!retrieveInputsRecursively) {
                    retrieveInputsRecursively = containerMatcher.isRetrieveObjectsRecursively();
                } else {
                    break;
                }
            }
        }
        if (retrieveInputsRecursively) {
            inputObjects = inputReader.getInputObjectsRecursively(input, trigger.getInputCharset());
        } else {
            inputObjects = inputReader.getInputObjects(input, trigger.getInputCharset());
        }
        // Remove non matching inputs
        Iterator<Object> it = inputObjects.iterator();
        while (it.hasNext()) {
            Object next = it.next();
            if (!this.matcherEvaluator.matches(next, trigger.getMatcher(), triggerInterpreter)) {
                it.remove();
            }
        }
    } else {
        inputObjects.add(input);
    }
    return inputObjects;
}
Also used : TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) InputReader(com.devonfw.cobigen.api.extension.InputReader) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) ArrayList(java.util.ArrayList) MatcherTo(com.devonfw.cobigen.api.to.MatcherTo) Cached(com.devonfw.cobigen.api.annotation.Cached)

Example 2 with ContainerMatcher

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

the class TemplatesConfigurationReaderTest method testTemplateRefOutsideCurrentFile.

/**
 * Tests the correct resolution of TemplateRef from outside the current templates file.
 */
@Test
public void testTemplateRefOutsideCurrentFile() {
    // given
    Trigger trigger = new Trigger("testingTrigger", "asdf", "valid_external_templateref", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
    Path configPath = Paths.get(new File(testFileRootPath).toURI());
    ConfigurationHolder configurationHolder = new ConfigurationHolder(configPath.toUri());
    TemplatesConfiguration templatesConfiguration = configurationHolder.readTemplatesConfiguration(trigger);
    Map<String, Increment> increments = templatesConfiguration.getIncrements();
    assertThat(templatesConfiguration.getTrigger().getId()).isEqualTo("testingTrigger");
    // validation
    Increment incrementThree = increments.get("3");
    LinkedList<String> templateNamesThree = new LinkedList<>();
    for (Template tmplate : incrementThree.getTemplates()) {
        templateNamesThree.add(tmplate.getName());
    }
    assertThat(templateNamesThree).containsExactly("templateDecl");
    Increment incrementFour = increments.get("4");
    LinkedList<String> templateNamesFour = new LinkedList<>();
    for (Template tmplate : incrementFour.getTemplates()) {
        templateNamesFour.add(tmplate.getName());
    }
    assertThat(templateNamesFour).containsExactly("ExplicitlyDefined");
}
Also used : Path(java.nio.file.Path) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) Matcher(com.devonfw.cobigen.impl.config.entity.Matcher) ConfigurationHolder(com.devonfw.cobigen.impl.config.ConfigurationHolder) TemplatesConfiguration(com.devonfw.cobigen.impl.config.TemplatesConfiguration) LinkedList(java.util.LinkedList) Template(com.devonfw.cobigen.impl.config.entity.Template) Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) Increment(com.devonfw.cobigen.impl.config.entity.Increment) File(java.io.File) AbstractUnitTest(com.devonfw.cobigen.unittest.config.common.AbstractUnitTest) Test(org.junit.Test)

Example 3 with ContainerMatcher

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

the class TemplatesConfigurationReaderTest method testInvalidTemplateRefOutsideCurrentFile.

/**
 * Tests the correct detection of invalid external increment reference.
 *
 * @throws InvalidConfigurationException expected
 */
@Test(expected = InvalidConfigurationException.class)
public void testInvalidTemplateRefOutsideCurrentFile() {
    Path config = Paths.get(new File(testFileRootPath).toURI());
    new ContextConfigurationReader(config);
    // given
    ConfigurationHolder configurationHolder = new ConfigurationHolder(config.toUri());
    TemplatesConfigurationReader target = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "faulty_invalid_external_templateref", configurationHolder);
    Trigger trigger = new Trigger("", "asdf", "", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
    // when
    Map<String, Template> templates = target.loadTemplates(trigger);
    target.loadIncrements(templates, trigger);
}
Also used : Path(java.nio.file.Path) ContextConfigurationReader(com.devonfw.cobigen.impl.config.reader.ContextConfigurationReader) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) Matcher(com.devonfw.cobigen.impl.config.entity.Matcher) ConfigurationHolder(com.devonfw.cobigen.impl.config.ConfigurationHolder) Template(com.devonfw.cobigen.impl.config.entity.Template) Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) 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 4 with ContainerMatcher

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

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