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);
}
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;
}
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);
}
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);
}
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);
}
Aggregations