use of com.devonfw.cobigen.api.to.TemplateTo in project cobigen by devonfw.
the class GenerationTest method testGenerationWithExternalIncrements.
/**
* Tests whether the generation of external increments works properly.
*
* @throws Exception test fails
*/
@Test
public void testGenerationWithExternalIncrements() throws Exception {
// given
Object input = PluginMockFactory.createSimpleJavaConfigurationMock();
File folder = this.tmpFolder.newFolder("GenerationTest");
File target = new File(folder, "generated.txt");
FileUtils.write(target, "base");
// when
CobiGen cobigen = CobiGenFactory.create(new File(testFileRootPath + "externalIncrementsGeneration").toURI());
List<TemplateTo> templates = cobigen.getMatchingTemplates(input);
List<IncrementTo> increments = cobigen.getMatchingIncrements(input);
List<String> triggersIds = cobigen.getMatchingTriggerIds(input);
// assert
IncrementTo externalIncrement = null;
// we try to get an increment containing external increments
for (IncrementTo inc : increments) {
if (inc.getId().equals("3")) {
externalIncrement = inc;
}
}
assertThat(templates).hasSize(5);
// We expect increment 3 to have an external increment 0 containing one template
assertThat(externalIncrement).isNotNull();
assertThat(externalIncrement.getDependentIncrements().get(0).getTemplates().size()).isEqualTo(1);
// We expect two triggers, the main one and the external one
assertThat(triggersIds.size()).isEqualTo(2);
}
use of com.devonfw.cobigen.api.to.TemplateTo in project cobigen by devonfw.
the class GenerationTest method testOverrideMergeStrategy.
/**
* Tests that sources get overwritten if merge strategy override is configured.
*
* @throws Exception test fails.
*/
@Test
public void testOverrideMergeStrategy() throws Exception {
Object input = PluginMockFactory.createSimpleJavaConfigurationMock();
File folder = this.tmpFolder.newFolder("GenerationTest");
File target = new File(folder, "generated.txt");
FileUtils.write(target, "base");
CobiGen cobigen = CobiGenFactory.create(new File(testFileRootPath + "overrideMergeStrategy").toURI());
List<TemplateTo> templates = cobigen.getMatchingTemplates(input);
assertThat(templates).hasSize(1);
GenerationReportTo report = cobigen.generate(input, templates.get(0), Paths.get(folder.toURI()));
assertThat(report).isSuccessful();
assertThat(target).hasContent("overwritten");
}
use of com.devonfw.cobigen.api.to.TemplateTo in project cobigen by devonfw.
the class GenerationTest method testCobiGenVariableAvailabilityInTemplates_cobigenPropertiesTargetLocation.
/**
* Tests whether the cobigen properties specified in the target folder are correctly resolved to be served in the
* template in the {@link ModelBuilderImpl#NS_VARIABLES} namespace.
*
* @throws Exception test fails
*/
@Test
public void testCobiGenVariableAvailabilityInTemplates_cobigenPropertiesTargetLocation() 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();
Path cobigenPropTarget = folder.toPath().resolve("cobigen.properties");
Files.createFile(cobigenPropTarget);
try (FileWriter writer = new FileWriter(cobigenPropTarget.toFile())) {
IOUtils.write("cobigenPropTarget=extValue", writer);
}
CobiGen cobigen = CobiGenFactory.create(new File(testFileRootPath + "variableAvailability").toURI());
List<TemplateTo> templates = cobigen.getMatchingTemplates(input);
TemplateTo targetTemplate = getTemplate(templates, "t2");
// execute
GenerationReportTo report = cobigen.generate(input, targetTemplate, Paths.get(folder.toURI()));
// assert
assertThat(report).isSuccessful();
File target = new File(folder, "generated2.txt");
assertThat(target).hasContent("contextValue,cobigenPropValue,extValue");
}
use of com.devonfw.cobigen.api.to.TemplateTo in project cobigen by devonfw.
the class ContainerMatcherTest method testContextVariableResolvingOnGeneration.
/**
* Tests whether variable resolving works for a contains's children during generation
*
* @throws Exception test fails
*/
@Test
public void testContextVariableResolvingOnGeneration() throws Exception {
// Mocking
Object containerInput = createTestDataAndConfigureMock(true);
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);
// assertion
assertThat(report).isSuccessful();
}
use of com.devonfw.cobigen.api.to.TemplateTo in project cobigen by devonfw.
the class ContainerMatcherTest method testContextVariableResolving.
/**
* Tests whether variable resolving works for a container's children as the container itself does not include any
* variable resolving
*
* @throws Exception test fails
*/
@Test
public void testContextVariableResolving() throws Exception {
// Mocking
Object containerInput = createTestDataAndConfigureMock(true);
// Execution
File templatesFolder = new File(testFileRootPath + "templates");
CobiGen target = CobiGenFactory.create(templatesFolder.toURI());
List<TemplateTo> matchingTemplates = target.getMatchingTemplates(containerInput);
// Verification
Assert.assertNotNull(matchingTemplates);
}
Aggregations