use of com.devonfw.cobigen.api.CobiGen 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.CobiGen 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.CobiGen 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.CobiGen in project cobigen by devonfw.
the class GenerationTest method testGenerationWithExternalIncrementsFailsWhenExternalTriggerNotMatch.
/**
* Tests generation of external increments where its trigger does not match
*
* @throws IOException test fails
*/
public void testGenerationWithExternalIncrementsFailsWhenExternalTriggerNotMatch() throws IOException {
// 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 + "externalIncrementsGenerationException").toURI());
// exception is thrown while getting all increments
assertThatThrownBy(() -> {
cobigen.getMatchingIncrements(input);
}).isInstanceOf(InvalidConfigurationException.class).hasMessageContaining("An external incrementRef to valid_increment_composition::0 is referenced from external_incrementref but its trigger does not match");
}
use of com.devonfw.cobigen.api.CobiGen in project cobigen by devonfw.
the class TransactionalGenerationTest method testNoPartialApplicationOfGeneration.
/**
* Tests, whether no partial generation will be applied to the target if generation fails in between.
*
* @throws Throwable test fails
*/
@Test
public void testNoPartialApplicationOfGeneration() throws Throwable {
// arrange
Object generationInput = PluginMockFactory.createSimpleJavaConfigurationMock();
File targetRoot = this.tmpFolder.newFolder();
CobiGen cobigen = CobiGenFactory.create(new File(testFileRootPath + "templates").toURI());
List<IncrementTo> matchingIncrements = cobigen.getMatchingIncrements(generationInput);
// act
GenerationReportTo report = cobigen.generate(generationInput, matchingIncrements, targetRoot.toPath());
// assert
assertThat(report.isSuccessful()).isFalse();
assertThat(new File(targetRoot, "valid.txt")).doesNotExist();
assertThat(new File(targetRoot, "invalid.txt")).doesNotExist();
}
Aggregations