use of com.devonfw.cobigen.api.to.TemplateTo in project cobigen by devonfw.
the class ClassLoadingTest method callClassLoadingTest.
/**
* Tests the usage of sample logic classes to be used in a template.
*
* @throws Exception test fails
*/
@Test
public void callClassLoadingTest() throws Exception {
// Mocking
Object containerInput = createTestDataAndConfigureMock();
// Useful to see generates if necessary, comment the generationRootFolder above then
File generationRootFolder = this.tmpFolder.newFolder("generationRootFolder");
// pre-processing
File templatesFolder = new File(testFileRootPath + "templates");
CobiGen target = CobiGenFactory.create(templatesFolder.toURI());
List<TemplateTo> templates = target.getMatchingTemplates(containerInput);
// Execution
GenerationReportTo report = target.generate(containerInput, templates.get(0), Paths.get(generationRootFolder.toURI()), false);
// Verification
File expectedResult = new File(testFileRootPath, "expected/Test.java");
File generatedFile = new File(generationRootFolder, "com/devonfw/Test.java");
assertThat(report).isSuccessful();
assertThat(generatedFile).exists();
assertThat(generatedFile).isFile().hasSameContentAs(expectedResult);
}
use of com.devonfw.cobigen.api.to.TemplateTo in project cobigen by devonfw.
the class ClassLoadingTest method testLoadEnumClass.
@Test
public void testLoadEnumClass() throws Exception {
// Mocking
Object containerInput = createTestDataAndConfigureMock();
// Useful to see generates if necessary, comment the generationRootFolder above then
File generationRootFolder = this.tmpFolder.newFolder("generationRootFolder");
// pre-processing
File templatesFolder = new File(testFileRootPath + "templates");
CobiGen target = CobiGenFactory.create(templatesFolder.toURI());
List<TemplateTo> templates = target.getMatchingTemplates(containerInput);
// Execution
GenerationReportTo report = target.generate(containerInput, templates.get(1), Paths.get(generationRootFolder.toURI()), false);
// Verification
File expectedResult = new File(testFileRootPath, "expected/Test2.java");
File generatedFile = new File(generationRootFolder, "com/devonfw/Test2.java");
assertThat(report).isSuccessful();
assertThat(generatedFile).exists();
assertThat(generatedFile).isFile().hasSameContentAs(expectedResult);
}
use of com.devonfw.cobigen.api.to.TemplateTo in project cobigen by devonfw.
the class GenerationTest method testCobiGenVariableAvailabilityInTemplates.
/**
* Tests whether context properties as well as cobigen properties are correctly resolved to be served in the template
* in the {@link ModelBuilderImpl#NS_VARIABLES} namespace.
*
* @throws Exception test fails
*/
@Test
public void testCobiGenVariableAvailabilityInTemplates() throws Exception {
Object input = new Object() {
@Override
public String toString() {
return "input object";
}
};
// Pre-processing: Mocking
GeneratorPluginActivator activator = mock(GeneratorPluginActivator.class);
TriggerInterpreter triggerInterpreter = mock(TriggerInterpreter.class);
MatcherInterpreter matcher = mock(MatcherInterpreter.class);
InputReader inputReader = mock(InputReader.class);
when(triggerInterpreter.getType()).thenReturn("mockplugin");
when(triggerInterpreter.getMatcher()).thenReturn(matcher);
when(triggerInterpreter.getInputReader()).thenReturn(inputReader);
when(inputReader.isValidInput(any())).thenReturn(true);
when(matcher.matches(argThat(new MatcherToMatcher(equalTo("fqn"), ANY, sameInstance(input))))).thenReturn(true);
// Simulate variable resolving of any plug-in
HashMap<String, String> variables = new HashMap<>(1);
variables.put("contextVar", "contextValue");
when(matcher.resolveVariables(any(MatcherTo.class), any(List.class), any())).thenReturn(variables);
PluginRegistry.registerTriggerInterpreter(triggerInterpreter, activator);
// further setup
File folder = this.tmpFolder.newFolder();
CobiGen cobigen = CobiGenFactory.create(new File(testFileRootPath + "variableAvailability").toURI());
List<TemplateTo> templates = cobigen.getMatchingTemplates(input);
TemplateTo targetTemplate = getTemplate(templates, "t1");
// execute
GenerationReportTo report = cobigen.generate(input, targetTemplate, Paths.get(folder.toURI()));
// assert
assertThat(report).isSuccessful();
File target = new File(folder, "generated.txt");
assertThat(target).hasContent("contextValue,cobigenPropValue");
}
use of com.devonfw.cobigen.api.to.TemplateTo in project cobigen by devonfw.
the class GenerationTest method getTemplate.
/**
* Finds the template with the given id in the list of templates and assures it to be found.
*
* @param templates list of templates to search in
* @param id to search for
* @return non null found template
*/
private TemplateTo getTemplate(List<TemplateTo> templates, String id) {
TemplateTo targetTemplate = null;
for (TemplateTo t : templates) {
if (t.getId().equals(id)) {
targetTemplate = t;
break;
}
}
assertThat(targetTemplate).as("Template to be generated").isNotNull();
return targetTemplate;
}
use of com.devonfw.cobigen.api.to.TemplateTo in project cobigen by devonfw.
the class ConfigurationInterpreterImpl method getMatchingTemplates.
@Cached
@Override
public List<TemplateTo> getMatchingTemplates(Object matcherInput) throws InvalidConfigurationException {
LOG.debug("Matching templates requested.");
List<TemplateTo> templates = Lists.newLinkedList();
for (TemplatesConfiguration templatesConfiguration : getMatchingTemplatesConfigurations(matcherInput)) {
for (Template template : templatesConfiguration.getAllTemplates()) {
templates.add(new TemplateTo(template.getName(), template.getMergeStrategy(), templatesConfiguration.getTrigger().getId()));
}
}
LOG.debug("{} matching templates found.", templates.size());
return templates;
}
Aggregations