Search in sources :

Example 16 with CobiGen

use of com.devonfw.cobigen.api.CobiGen 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 17 with CobiGen

use of com.devonfw.cobigen.api.CobiGen in project cobigen by devonfw.

the class ContainerMatcherTest method testContainerChildrenWillIndividuallyBeMatched.

/**
 * Create a new {@link ContainerMatcher}, which contains two children which do not match the same trigger.
 *
 * @throws Exception test fails
 */
@Test
@SuppressWarnings("unchecked")
public void testContainerChildrenWillIndividuallyBeMatched() throws Exception {
    Object container = new Object() {

        @Override
        public String toString() {
            return "container";
        }
    };
    Object child1 = new Object() {

        @Override
        public String toString() {
            return "child1";
        }
    };
    Object child2 = new Object() {

        @Override
        public String toString() {
            return "child2";
        }
    };
    // 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("test");
    when(triggerInterpreter.getMatcher()).thenReturn(matcher);
    when(triggerInterpreter.getInputReader()).thenReturn(inputReader);
    when(inputReader.isValidInput(any())).thenReturn(true);
    // Simulate container children resolution of any plug-in
    when(matcher.resolveVariables(argThat(new MatcherToMatcher(equalTo("or"), ANY, sameInstance(child1))), anyList(), any())).thenReturn(ImmutableMap.<String, String>builder().put("variable", "child1").build());
    when(matcher.resolveVariables(argThat(new MatcherToMatcher(equalTo("or"), ANY, sameInstance(child2))), anyList(), any())).thenReturn(ImmutableMap.<String, String>builder().put("variable", "child2").build());
    when(inputReader.getInputObjects(any(), any(Charset.class))).thenReturn(Lists.newArrayList(child1, child2));
    // match container
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("container"), ANY, sameInstance(container))))).thenReturn(true);
    // do not match first child
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("or"), ANY, sameInstance(child1))))).thenReturn(true);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("not"), ANY, sameInstance(child1))))).thenReturn(true);
    // match second child
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("or"), ANY, sameInstance(child2))))).thenReturn(true);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("not"), ANY, sameInstance(child2))))).thenReturn(false);
    PluginRegistry.registerTriggerInterpreter(triggerInterpreter, activator);
    // create CobiGen instance
    File templatesFolder = new File(testFileRootPath + "selectiveContainerGeneration");
    CobiGen target = CobiGenFactory.create(templatesFolder.toURI());
    File folder = this.tmpFolder.newFolder();
    // Execution
    GenerationReportTo report = target.generate(container, target.getMatchingTemplates(container), Paths.get(folder.toURI()), false);
    assertThat(report).isSuccessful();
    // Verification
    assertNotNull(folder.list());
    assertEquals(1, folder.list().length);
    assertEquals("child2.txt", folder.list()[0]);
}
Also used : TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) 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) Charset(java.nio.charset.Charset) CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) GeneratorPluginActivator(com.devonfw.cobigen.api.extension.GeneratorPluginActivator) AbstractApiTest(com.devonfw.cobigen.systemtest.common.AbstractApiTest) Test(org.junit.Test)

Example 18 with CobiGen

use of com.devonfw.cobigen.api.CobiGen 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)

Example 19 with CobiGen

use of com.devonfw.cobigen.api.CobiGen in project cobigen by devonfw.

the class ContainerMatcherTest method testGetAllIncrements.

/**
 * Tests whether the increments can be correctly retrieved for container matchers
 *
 * @throws Exception test fails
 */
@Test
public void testGetAllIncrements() throws Exception {
    // Mocking
    Object containerInput = createTestDataAndConfigureMock(true, true);
    // pre-processing
    File templatesFolder = new File(testFileRootPath + "templates");
    CobiGen target = CobiGenFactory.create(templatesFolder.toURI());
    // Execution
    List<IncrementTo> increments = target.getMatchingIncrements(containerInput);
    // Verification
    Assert.assertNotNull(increments);
    Assert.assertTrue(increments.size() > 0);
}
Also used : IncrementTo(com.devonfw.cobigen.api.to.IncrementTo) CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) AbstractApiTest(com.devonfw.cobigen.systemtest.common.AbstractApiTest) Test(org.junit.Test)

Example 20 with CobiGen

use of com.devonfw.cobigen.api.CobiGen in project cobigen by devonfw.

the class ContainerMatcherTest method testMultipleTriggerWithDifferentContainerMatchersAccumulationType.

/**
 * Tests whether a single trigger will be activated if two triggers with different container matchers (different
 * accumulationTypes) were provided <br/>
 * <a href="https://github.com/devonfw/cobigen/issues/1299">(Bug #1299)</a>
 *
 * @throws Exception test fails
 */
@Test
public void testMultipleTriggerWithDifferentContainerMatchersAccumulationType() throws Exception {
    // Mocking
    Object containerInput = createTestDataAndConfigureMock(true, false);
    // pre-processing
    File templatesFolder = new File(testFileRootPath + "accumulationType");
    CobiGen target = CobiGenFactory.create(templatesFolder.toURI());
    // Execution
    List<String> triggerIds = target.getMatchingTriggerIds(containerInput);
    // Verification
    Assert.assertNotNull(triggerIds);
    Assert.assertEquals(1, triggerIds.size());
}
Also used : 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