Search in sources :

Example 6 with TriggerInterpreter

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

use of com.devonfw.cobigen.api.extension.TriggerInterpreter 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 8 with TriggerInterpreter

use of com.devonfw.cobigen.api.extension.TriggerInterpreter in project cobigen by devonfw.

the class TriggerActivationTest method testNoActivation_1Of2AND_1OR_MatcherMatches.

/**
 * Tests that a trigger will not be activated in case of one of two AND Matchers and one OR matcher matches.
 *
 * @throws Exception test fails
 * @author mbrunnli (22.02.2015)
 */
@Test
@SuppressWarnings("unchecked")
public void testNoActivation_1Of2AND_1OR_MatcherMatches() throws Exception {
    Object input = new 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("test");
    when(triggerInterpreter.getMatcher()).thenReturn(matcher);
    when(triggerInterpreter.getInputReader()).thenReturn(inputReader);
    when(inputReader.isValidInput(any())).thenReturn(true);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("and1"), ANY, sameInstance(input))))).thenReturn(false);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("and2"), ANY, sameInstance(input))))).thenReturn(true);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("or"), ANY, sameInstance(input))))).thenReturn(true);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("not"), ANY, sameInstance(input))))).thenReturn(false);
    PluginRegistry.registerTriggerInterpreter(triggerInterpreter, activator);
    // execution
    CobiGen cobigen = CobiGenFactory.create(new File(testFileRootPath + "templates").toURI());
    List<String> matchingTriggerIds = cobigen.getMatchingTriggerIds(input);
    assertThat(matchingTriggerIds, not(hasItem("triggerId")));
}
Also used : TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) MatcherToMatcher(com.devonfw.cobigen.api.matchers.MatcherToMatcher) InputReader(com.devonfw.cobigen.api.extension.InputReader) MatcherInterpreter(com.devonfw.cobigen.api.extension.MatcherInterpreter) CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) GeneratorPluginActivator(com.devonfw.cobigen.api.extension.GeneratorPluginActivator) Test(org.junit.Test) AbstractApiTest(com.devonfw.cobigen.systemtest.common.AbstractApiTest)

Example 9 with TriggerInterpreter

use of com.devonfw.cobigen.api.extension.TriggerInterpreter in project cobigen by devonfw.

the class TriggerActivationTest method testActivation_2Of2AND_MatcherMatches.

/**
 * Tests that a trigger will be activated in case of two of two AND Matchers matches.
 *
 * @throws Exception test fails
 * @author mbrunnli (22.02.2015)
 */
@Test
@SuppressWarnings("unchecked")
public void testActivation_2Of2AND_MatcherMatches() throws Exception {
    Object input = new 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("test");
    when(triggerInterpreter.getMatcher()).thenReturn(matcher);
    when(triggerInterpreter.getInputReader()).thenReturn(inputReader);
    when(inputReader.isValidInput(any())).thenReturn(true);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("and1"), ANY, sameInstance(input))))).thenReturn(true);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("and2"), ANY, sameInstance(input))))).thenReturn(true);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("or"), ANY, sameInstance(input))))).thenReturn(false);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("not"), ANY, sameInstance(input))))).thenReturn(false);
    PluginRegistry.registerTriggerInterpreter(triggerInterpreter, activator);
    // execution
    CobiGen cobigen = CobiGenFactory.create(new File(testFileRootPath + "templates").toURI());
    List<String> matchingTriggerIds = cobigen.getMatchingTriggerIds(input);
    assertThat(matchingTriggerIds, hasItem("triggerId"));
}
Also used : TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) MatcherToMatcher(com.devonfw.cobigen.api.matchers.MatcherToMatcher) InputReader(com.devonfw.cobigen.api.extension.InputReader) MatcherInterpreter(com.devonfw.cobigen.api.extension.MatcherInterpreter) CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) GeneratorPluginActivator(com.devonfw.cobigen.api.extension.GeneratorPluginActivator) Test(org.junit.Test) AbstractApiTest(com.devonfw.cobigen.systemtest.common.AbstractApiTest)

Example 10 with TriggerInterpreter

use of com.devonfw.cobigen.api.extension.TriggerInterpreter in project cobigen by devonfw.

the class TriggerActivationTest method testNoActivation_1OR_1NOT_MatcherMatches.

/**
 * Tests that a trigger will not be activated in case of one OR matcher and one NOT matcher matches.
 *
 * @throws Exception test fails
 * @author mbrunnli (22.02.2015)
 */
@Test
@SuppressWarnings("unchecked")
public void testNoActivation_1OR_1NOT_MatcherMatches() throws Exception {
    Object input = new 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("test");
    when(triggerInterpreter.getMatcher()).thenReturn(matcher);
    when(triggerInterpreter.getInputReader()).thenReturn(inputReader);
    when(inputReader.isValidInput(any())).thenReturn(true);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("and1"), ANY, sameInstance(input))))).thenReturn(false);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("and2"), ANY, sameInstance(input))))).thenReturn(false);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("or"), ANY, sameInstance(input))))).thenReturn(true);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("not"), ANY, sameInstance(input))))).thenReturn(true);
    PluginRegistry.registerTriggerInterpreter(triggerInterpreter, activator);
    // execution
    CobiGen cobigen = CobiGenFactory.create(new File(testFileRootPath + "templates").toURI());
    List<String> matchingTriggerIds = cobigen.getMatchingTriggerIds(input);
    assertThat(matchingTriggerIds, not(hasItem("triggerId")));
}
Also used : TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) MatcherToMatcher(com.devonfw.cobigen.api.matchers.MatcherToMatcher) InputReader(com.devonfw.cobigen.api.extension.InputReader) MatcherInterpreter(com.devonfw.cobigen.api.extension.MatcherInterpreter) CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) GeneratorPluginActivator(com.devonfw.cobigen.api.extension.GeneratorPluginActivator) Test(org.junit.Test) AbstractApiTest(com.devonfw.cobigen.systemtest.common.AbstractApiTest)

Aggregations

TriggerInterpreter (com.devonfw.cobigen.api.extension.TriggerInterpreter)22 GeneratorPluginActivator (com.devonfw.cobigen.api.extension.GeneratorPluginActivator)15 InputReader (com.devonfw.cobigen.api.extension.InputReader)14 MatcherInterpreter (com.devonfw.cobigen.api.extension.MatcherInterpreter)13 MatcherToMatcher (com.devonfw.cobigen.api.matchers.MatcherToMatcher)13 File (java.io.File)11 CobiGen (com.devonfw.cobigen.api.CobiGen)10 AbstractApiTest (com.devonfw.cobigen.systemtest.common.AbstractApiTest)10 Test (org.junit.Test)10 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)5 HashMap (java.util.HashMap)5 CobiGenRuntimeException (com.devonfw.cobigen.api.exception.CobiGenRuntimeException)4 MatcherTo (com.devonfw.cobigen.api.to.MatcherTo)4 List (java.util.List)4 VariableAssignmentToMatcher (com.devonfw.cobigen.api.matchers.VariableAssignmentToMatcher)3 TemplateTo (com.devonfw.cobigen.api.to.TemplateTo)3 Trigger (com.devonfw.cobigen.impl.config.entity.Trigger)3 Charset (java.nio.charset.Charset)3 Path (java.nio.file.Path)3 Cached (com.devonfw.cobigen.api.annotation.Cached)2