Search in sources :

Example 21 with CobiGen

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

the class TemplateScanTest method testCorrectDestinationResoution_emptyPathElements.

/**
 * Tests the correct destination resolution for resources obtained by template-scans in the case of multiple empty
 * path elements
 *
 * @throws Exception test fails
 */
@Test
public void testCorrectDestinationResoution_emptyPathElements() throws Exception {
    Object input = PluginMockFactory.createSimpleJavaConfigurationMock();
    File generationRootFolder = this.tmpFolder.newFolder("generationRootFolder");
    // Useful to see generates if necessary, comment the generationRootFolder above then
    // File generationRootFolder = new File(testFileRootPath + "generates");
    // pre-processing
    File templatesFolder = new File(testFileRootPath);
    CobiGen target = CobiGenFactory.create(templatesFolder.toURI());
    List<TemplateTo> templates = target.getMatchingTemplates(input);
    assertThat(templates).isNotNull();
    TemplateTo targetTemplate = getTemplateById(templates, "prefix_MultiEmpty.java");
    assertThat(targetTemplate).isNotNull();
    // Execution
    GenerationReportTo report = target.generate(input, targetTemplate, Paths.get(generationRootFolder.toURI()), false);
    assertThat(report).isSuccessful();
    // Validation
    assertThat(new File(generationRootFolder.getAbsolutePath() + SystemUtils.FILE_SEPARATOR + "src" + SystemUtils.FILE_SEPARATOR + "main" + SystemUtils.FILE_SEPARATOR + "java" + SystemUtils.FILE_SEPARATOR + "base" + SystemUtils.FILE_SEPARATOR + "MultiEmpty.java")).exists();
}
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) Test(org.junit.Test) AbstractApiTest(com.devonfw.cobigen.systemtest.common.AbstractApiTest)

Example 22 with CobiGen

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

the class TemplateScanTest method testScanTemplatesFromArchivFile.

/**
 * Test template scan within an archive file.
 *
 * @throws Exception test fails
 */
@Test
public void testScanTemplatesFromArchivFile() throws Exception {
    // pre-processing: mocking
    Object input = PluginMockFactory.createSimpleJavaConfigurationMock();
    // test processing
    CobiGen cobigen = CobiGenFactory.create(new File(testFileRootPath + "valid.zip").toURI());
    List<TemplateTo> templates = cobigen.getMatchingTemplates(input);
    // checking
    assertThat(templates, notNullValue());
    assertThat(templates.size(), equalTo(6));
}
Also used : CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) Test(org.junit.Test) AbstractApiTest(com.devonfw.cobigen.systemtest.common.AbstractApiTest)

Example 23 with CobiGen

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

the class CobiGenFactory method create.

/**
 * Creates a new {@link CobiGen} while searching a valid configuration at the given path
 *
 * @param configFileOrFolder the root folder containing the context.xml and all templates, configurations etc.
 * @return a new instance of {@link CobiGen}
 * @throws InvalidConfigurationException if the context configuration could not be read properly.
 */
public static CobiGen create(URI configFileOrFolder) throws InvalidConfigurationException {
    Objects.requireNonNull(configFileOrFolder, "The URI pointing to the configuration could not be null.");
    ConfigurationHolder configurationHolder = new ConfigurationHolder(configFileOrFolder);
    BeanFactory beanFactory = new BeanFactory();
    beanFactory.addManuallyInitializedBean(configurationHolder);
    CobiGen createBean = beanFactory.createBean(CobiGen.class);
    // Notifies all plugins of new template root path
    PluginRegistry.notifyPlugins(configurationHolder.getConfigurationPath());
    return createBean;
}
Also used : BeanFactory(com.devonfw.cobigen.impl.aop.BeanFactory) ConfigurationHolder(com.devonfw.cobigen.impl.config.ConfigurationHolder) CobiGen(com.devonfw.cobigen.api.CobiGen)

Example 24 with CobiGen

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

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

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