Search in sources :

Example 11 with TemplateTo

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);
}
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 TemplateTo

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

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

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();
}
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 15 with TemplateTo

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);
}
Also used : 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)

Aggregations

TemplateTo (com.devonfw.cobigen.api.to.TemplateTo)41 CobiGen (com.devonfw.cobigen.api.CobiGen)30 File (java.io.File)28 Test (org.junit.Test)27 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)25 AbstractApiTest (com.devonfw.cobigen.systemtest.common.AbstractApiTest)12 IncrementTo (com.devonfw.cobigen.api.to.IncrementTo)9 Path (java.nio.file.Path)9 List (java.util.List)9 AssertionFailedError (junit.framework.AssertionFailedError)9 Paths (java.nio.file.Paths)6 Set (java.util.Set)6 HashSet (java.util.HashSet)5 Collectors (java.util.stream.Collectors)5 CobiGenAsserts.assertThat (com.devonfw.cobigen.api.assertj.CobiGenAsserts.assertThat)4 GenerationReportToAssert (com.devonfw.cobigen.api.assertj.GenerationReportToAssert)4 ComparableIncrement (com.devonfw.cobigen.eclipse.generator.entity.ComparableIncrement)4 CobiGenFactory (com.devonfw.cobigen.impl.CobiGenFactory)4 AbstractIntegrationTest (com.devonfw.cobigen.javaplugin.integrationtest.common.AbstractIntegrationTest)4 StandardCharsets (java.nio.charset.StandardCharsets)4