Search in sources :

Example 16 with InputReader

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

the class TriggerActivationTest method testActivation_1OR_MatcherMatches.

/**
 * Tests that a trigger will be activated in case of one OR matcher matches.
 *
 * @throws Exception test fails
 * @author mbrunnli (22.02.2015)
 */
@Test
@SuppressWarnings("unchecked")
public void testActivation_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("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, hasItem("triggerId2"));
}
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 17 with InputReader

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

the class TriggerActivationTest method testNoActivation_1OR_0AND_MatcherMatches.

/**
 * Tests that a trigger will not be activated in case of one OR matcher but no AND matcher matches.
 *
 * @throws Exception test fails
 * @author mbrunnli (22.02.2015)
 */
@Test
@SuppressWarnings("unchecked")
public void testNoActivation_1OR_0AND_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(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 18 with InputReader

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

the class TriggerActivationTest method testNoActivation_1Of2AND_MatcherMatches.

/**
 * Tests that a trigger will not be activated in case of one of two AND Matchers matches.
 *
 * @throws Exception test fails
 * @author mbrunnli (22.02.2015)
 */
@Test
@SuppressWarnings("unchecked")
public void testNoActivation_1Of2AND_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(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, 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 19 with InputReader

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

the class PluginMockFactory method createSimpleJavaConfigurationMock.

/**
 * Creates simple to debug test data, which includes only one object as input. A {@link TriggerInterpreter
 * TriggerInterpreter} will be mocked with all necessary supplier classes to mock a simple java trigger interpreter.
 * Furthermore, the mocked trigger interpreter will be directly registered in the {@link PluginRegistry}.
 *
 * @return the input for generation
 */
@SuppressWarnings("unchecked")
public static Object createSimpleJavaConfigurationMock() {
    // we only need any objects for inputs to have a unique object reference to affect the mocked method
    // calls as intended
    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(false);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("package"), ANY, sameInstance(input))))).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<>(3);
    variables.put("rootPackage", "com.devonfw");
    variables.put("component", "comp1");
    variables.put("detail", "");
    when(matcher.resolveVariables(argThat(new MatcherToMatcher(equalTo("fqn"), ANY, sameInstance(input))), argThat(hasItemsInList(// 
    new VariableAssignmentToMatcher(equalTo("regex"), equalTo("rootPackage"), equalTo("1"), equalTo(false)), new VariableAssignmentToMatcher(equalTo("regex"), equalTo("entityName"), equalTo("3"), equalTo(false)))), any())).thenReturn(variables);
    PluginRegistry.registerTriggerInterpreter(triggerInterpreter, activator);
    return input;
}
Also used : TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) MatcherToMatcher(com.devonfw.cobigen.api.matchers.MatcherToMatcher) InputReader(com.devonfw.cobigen.api.extension.InputReader) HashMap(java.util.HashMap) MatcherInterpreter(com.devonfw.cobigen.api.extension.MatcherInterpreter) VariableAssignmentToMatcher(com.devonfw.cobigen.api.matchers.VariableAssignmentToMatcher) GeneratorPluginActivator(com.devonfw.cobigen.api.extension.GeneratorPluginActivator)

Example 20 with InputReader

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

the class ContainerMatcherTest method createTestDataAndConfigureMock.

/**
 * Creates simple to debug test data, which includes on container object and one child of the container object. A
 * {@link TriggerInterpreter TriggerInterpreter} will be mocked with all necessary supplier classes to mock a simple
 * java trigger interpreter. Furthermore, the mocked trigger interpreter will be directly registered in the
 * {@link PluginRegistry}.
 *
 * @param containerChildMatchesTrigger defines whether the child of the container input should match any non-container
 *        matcher
 * @param multipleContainerChildren defines whether the container should contain multiple children
 * @return the container as input for generation interpreter for
 * @author mbrunnli (16.10.2014)
 */
@SuppressWarnings("unchecked")
private Object createTestDataAndConfigureMock(boolean containerChildMatchesTrigger, boolean multipleContainerChildren) {
    // we only need any objects for inputs to have a unique object reference to affect the mocked method
    // calls as intended
    Object container = new Object() {

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

        @Override
        public String toString() {
            return "child";
        }
    };
    // 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(container))))).thenReturn(false);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("package"), ANY, sameInstance(container))))).thenReturn(true);
    // Simulate container children resolution of any plug-in
    if (multipleContainerChildren) {
        Object secondChildResource = new Object() {

            @Override
            public String toString() {
                return "child2";
            }
        };
        when(inputReader.getInputObjects(any(), any(Charset.class))).thenReturn(Lists.newArrayList(firstChildResource, secondChildResource));
    } else {
        when(inputReader.getInputObjects(any(), any(Charset.class))).thenReturn(Lists.newArrayList(firstChildResource));
    }
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("fqn"), ANY, sameInstance(firstChildResource))))).thenReturn(containerChildMatchesTrigger);
    // Simulate variable resolving of any plug-in
    when(matcher.resolveVariables(argThat(new MatcherToMatcher(equalTo("fqn"), ANY, sameInstance(firstChildResource))), argThat(hasItemsInList(// 
    new VariableAssignmentToMatcher(equalTo("regex"), equalTo("rootPackage"), equalTo("1"), equalTo(false)), new VariableAssignmentToMatcher(equalTo("regex"), equalTo("entityName"), equalTo("3"), equalTo(false)))), any())).thenReturn(ImmutableMap.<String, String>builder().put("rootPackage", "com.devonfw").put("entityName", "Test").build());
    PluginRegistry.registerTriggerInterpreter(triggerInterpreter, activator);
    return container;
}
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) Charset(java.nio.charset.Charset) VariableAssignmentToMatcher(com.devonfw.cobigen.api.matchers.VariableAssignmentToMatcher) GeneratorPluginActivator(com.devonfw.cobigen.api.extension.GeneratorPluginActivator)

Aggregations

InputReader (com.devonfw.cobigen.api.extension.InputReader)22 File (java.io.File)17 Test (org.junit.Test)16 TriggerInterpreter (com.devonfw.cobigen.api.extension.TriggerInterpreter)14 GeneratorPluginActivator (com.devonfw.cobigen.api.extension.GeneratorPluginActivator)13 MatcherInterpreter (com.devonfw.cobigen.api.extension.MatcherInterpreter)13 MatcherToMatcher (com.devonfw.cobigen.api.matchers.MatcherToMatcher)13 CobiGen (com.devonfw.cobigen.api.CobiGen)10 AbstractApiTest (com.devonfw.cobigen.systemtest.common.AbstractApiTest)10 VariableAssignmentToMatcher (com.devonfw.cobigen.api.matchers.VariableAssignmentToMatcher)3 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)3 MatcherTo (com.devonfw.cobigen.api.to.MatcherTo)3 Charset (java.nio.charset.Charset)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 TemplateTo (com.devonfw.cobigen.api.to.TemplateTo)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Cached (com.devonfw.cobigen.api.annotation.Cached)1