Search in sources :

Example 51 with CobiGen

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

the class TemplateScanTest method testCorrectDestinationResoution.

/**
 * Tests the correct destination resolution for resources obtained by template-scans
 *
 * @throws Exception test fails
 */
@Test
public void testCorrectDestinationResoution() 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_${variables.component#cap_first#replace('1','ONE')}.java");
    assertThat(targetTemplate).isNotNull();
    // Execution
    target.generate(input, targetTemplate, Paths.get(generationRootFolder.toURI()), false);
    // Validation
    assertThat(new File(generationRootFolder.getAbsolutePath() + SystemUtils.FILE_SEPARATOR + "src" + SystemUtils.FILE_SEPARATOR + "main" + SystemUtils.FILE_SEPARATOR + "java" + SystemUtils.FILE_SEPARATOR + "TestCOMP1" + SystemUtils.FILE_SEPARATOR + "CompONE.java")).exists();
}
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 52 with CobiGen

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

the class TemplateScanTest method testCorrectDestinationResoution_emptyPathElement.

/**
 * Tests the correct destination resolution for resources obtained by template-scans in the case of an empty path
 * element
 *
 * @throws Exception test fails
 */
@Test
public void testCorrectDestinationResoution_emptyPathElement() 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_Test.java");
    assertThat(targetTemplate).isNotNull();
    // Execution
    target.generate(input, targetTemplate, Paths.get(generationRootFolder.toURI()), false);
    // 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 + "Test.java")).exists();
}
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 53 with CobiGen

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

the class ClassLoadingTest method callClassLoadingTest.

/**
 * Tests the usage of sample logic classes to be used in a template.
 *
 * @throws Exception test fails
 */
@Test
public void callClassLoadingTest() throws Exception {
    // Mocking
    Object containerInput = createTestDataAndConfigureMock();
    // Useful to see generates if necessary, comment the generationRootFolder above then
    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);
    // Verification
    File expectedResult = new File(testFileRootPath, "expected/Test.java");
    File generatedFile = new File(generationRootFolder, "com/devonfw/Test.java");
    assertThat(report).isSuccessful();
    assertThat(generatedFile).exists();
    assertThat(generatedFile).isFile().hasSameContentAs(expectedResult);
}
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 54 with CobiGen

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

the class ClassLoadingTest method testLoadEnumClass.

@Test
public void testLoadEnumClass() throws Exception {
    // Mocking
    Object containerInput = createTestDataAndConfigureMock();
    // Useful to see generates if necessary, comment the generationRootFolder above then
    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(1), Paths.get(generationRootFolder.toURI()), false);
    // Verification
    File expectedResult = new File(testFileRootPath, "expected/Test2.java");
    File generatedFile = new File(generationRootFolder, "com/devonfw/Test2.java");
    assertThat(report).isSuccessful();
    assertThat(generatedFile).exists();
    assertThat(generatedFile).isFile().hasSameContentAs(expectedResult);
}
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 55 with CobiGen

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

the class GenerationTest method testCobiGenVariableAvailabilityInTemplates.

/**
 * Tests whether context properties as well as cobigen properties are correctly resolved to be served in the template
 * in the {@link ModelBuilderImpl#NS_VARIABLES} namespace.
 *
 * @throws Exception test fails
 */
@Test
public void testCobiGenVariableAvailabilityInTemplates() 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();
    CobiGen cobigen = CobiGenFactory.create(new File(testFileRootPath + "variableAvailability").toURI());
    List<TemplateTo> templates = cobigen.getMatchingTemplates(input);
    TemplateTo targetTemplate = getTemplate(templates, "t1");
    // execute
    GenerationReportTo report = cobigen.generate(input, targetTemplate, Paths.get(folder.toURI()));
    // assert
    assertThat(report).isSuccessful();
    File target = new File(folder, "generated.txt");
    assertThat(target).hasContent("contextValue,cobigenPropValue");
}
Also used : TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) HashMap(java.util.HashMap) 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)

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