Search in sources :

Example 11 with CobiGen

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);
}
Also used : IncrementTo(com.devonfw.cobigen.api.to.IncrementTo) CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) AbstractApiTest(com.devonfw.cobigen.systemtest.common.AbstractApiTest) Test(org.junit.Test)

Example 12 with CobiGen

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");
}
Also used : GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) AbstractApiTest(com.devonfw.cobigen.systemtest.common.AbstractApiTest) Test(org.junit.Test)

Example 13 with CobiGen

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");
}
Also used : TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) Path(java.nio.file.Path) HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) GeneratorPluginActivator(com.devonfw.cobigen.api.extension.GeneratorPluginActivator) MatcherToMatcher(com.devonfw.cobigen.api.matchers.MatcherToMatcher) InputReader(com.devonfw.cobigen.api.extension.InputReader) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) MatcherInterpreter(com.devonfw.cobigen.api.extension.MatcherInterpreter) List(java.util.List) CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) MatcherTo(com.devonfw.cobigen.api.to.MatcherTo) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) AbstractApiTest(com.devonfw.cobigen.systemtest.common.AbstractApiTest) Test(org.junit.Test)

Example 14 with CobiGen

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");
}
Also used : CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) InvalidConfigurationException(com.devonfw.cobigen.api.exception.InvalidConfigurationException)

Example 15 with CobiGen

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();
}
Also used : IncrementTo(com.devonfw.cobigen.api.to.IncrementTo) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) AbstractApiTest(com.devonfw.cobigen.systemtest.common.AbstractApiTest) Test(org.junit.Test)

Aggregations

CobiGen (com.devonfw.cobigen.api.CobiGen)55 File (java.io.File)45 Test (org.junit.Test)45 TemplateTo (com.devonfw.cobigen.api.to.TemplateTo)31 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)28 AbstractApiTest (com.devonfw.cobigen.systemtest.common.AbstractApiTest)26 GeneratorPluginActivator (com.devonfw.cobigen.api.extension.GeneratorPluginActivator)10 InputReader (com.devonfw.cobigen.api.extension.InputReader)10 MatcherInterpreter (com.devonfw.cobigen.api.extension.MatcherInterpreter)10 TriggerInterpreter (com.devonfw.cobigen.api.extension.TriggerInterpreter)10 MatcherToMatcher (com.devonfw.cobigen.api.matchers.MatcherToMatcher)10 List (java.util.List)10 Path (java.nio.file.Path)9 AssertionFailedError (junit.framework.AssertionFailedError)9 IncrementTo (com.devonfw.cobigen.api.to.IncrementTo)7 Paths (java.nio.file.Paths)7 CobiGenFactory (com.devonfw.cobigen.impl.CobiGenFactory)5 AbstractIntegrationTest (com.devonfw.cobigen.javaplugin.integrationtest.common.AbstractIntegrationTest)5 Collectors (java.util.stream.Collectors)5 CobiGenAsserts.assertThat (com.devonfw.cobigen.api.assertj.CobiGenAsserts.assertThat)4