Search in sources :

Example 11 with ContainerMatcher

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

the class TemplatesConfigurationReaderTest method testInvalidIncrementRefOutsideCurrentFile.

/**
 * Tests the correct detection of invalid external increment reference.
 *
 * @throws InvalidConfigurationException expected
 */
@Test(expected = InvalidConfigurationException.class)
public void testInvalidIncrementRefOutsideCurrentFile() {
    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_incrementref", 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 12 with ContainerMatcher

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

the class TriggerMatchingEvaluatorImpl method getMatchingTriggers.

@Cached
@Override
public List<Trigger> getMatchingTriggers(Object matcherInput) {
    LOG.debug("Retrieve matching trigger. input {}, hash: {}", matcherInput, matcherInput.hashCode());
    List<Trigger> matchingTrigger = Lists.newLinkedList();
    for (Trigger trigger : this.configurationHolder.readContextConfiguration().getTriggers()) {
        TriggerInterpreter triggerInterpreter = PluginRegistry.getTriggerInterpreter(trigger.getType());
        if (triggerInterpreter == null) {
            continue;
        // trigger interpreter not yet activated as the plug-in was not yet used.
        // unfortunately the invariant here is, that the CobiGen user has once called CobigenImpl#read
        // to get the matcher input
        }
        InputValidator.validateTriggerInterpreter(triggerInterpreter, trigger);
        LOG.debug("Check {} to match the input.", trigger);
        if (triggerInterpreter.getInputReader().isValidInput(matcherInput)) {
            LOG.debug("Matcher input is marked as valid.");
            boolean triggerMatches = this.matcherEvaluator.matches(matcherInput, trigger.getMatcher(), triggerInterpreter);
            if (triggerMatches) {
                matchingTrigger.add(trigger);
            }
            // recognized as a container.
            if (!triggerMatches) {
                LOG.debug("Check container matchers ...");
                FOR_CONTAINERMATCHER: for (ContainerMatcher containerMatcher : trigger.getContainerMatchers()) {
                    MatcherTo containerMatcherTo = new MatcherTo(containerMatcher.getType(), containerMatcher.getValue(), matcherInput);
                    LOG.debug("Check {} ...", containerMatcherTo);
                    if (triggerInterpreter.getMatcher().matches(containerMatcherTo)) {
                        LOG.debug("Match! Retrieve objects from container ...", containerMatcherTo);
                        // keep backward-compatibility
                        List<Object> containerResources;
                        if (containerMatcher.isRetrieveObjectsRecursively()) {
                            containerResources = triggerInterpreter.getInputReader().getInputObjectsRecursively(matcherInput, Charsets.UTF_8);
                        } else {
                            // the charset does not matter as we just want to see whether there is one
                            // matcher for one of the container resources
                            containerResources = triggerInterpreter.getInputReader().getInputObjects(matcherInput, Charsets.UTF_8);
                        }
                        LOG.debug("{} objects retrieved.", containerResources.size());
                        // check if at least one container element matches the matcher declarations
                        for (Object resource : containerResources) {
                            if (this.matcherEvaluator.matches(resource, trigger.getMatcher(), triggerInterpreter)) {
                                LOG.debug("At least one object from container matches.");
                                triggerMatches = true;
                                break FOR_CONTAINERMATCHER;
                            }
                        }
                        LOG.debug("No element of the container is matched.");
                    }
                }
                if (triggerMatches) {
                    matchingTrigger.add(new Trigger(trigger, true));
                }
            }
            LOG.debug("{} {}", trigger, triggerMatches ? "matches." : "does not match.");
        }
    }
    return matchingTrigger;
}
Also used : TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) List(java.util.List) MatcherTo(com.devonfw.cobigen.api.to.MatcherTo) Cached(com.devonfw.cobigen.api.annotation.Cached)

Example 13 with ContainerMatcher

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

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

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

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