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