Search in sources :

Example 31 with GenerationReportTo

use of com.devonfw.cobigen.api.to.GenerationReportTo 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 32 with GenerationReportTo

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

Example 33 with GenerationReportTo

use of com.devonfw.cobigen.api.to.GenerationReportTo in project cobigen by devonfw.

the class ConfigurationInterpreterImpl method resolveTemplateDestinationPath.

@Override
public Path resolveTemplateDestinationPath(Path targetRootPath, TemplateTo template, Object input) {
    Trigger trigger = this.configurationHolder.readContextConfiguration().getTrigger(template.getTriggerId());
    InputValidator.validateTrigger(trigger);
    TriggerInterpreter triggerInterpreter = PluginRegistry.getTriggerInterpreter(trigger.getType());
    // the GenerationReportTo won't be further processed
    Variables variables = new ContextVariableResolver(input, trigger).resolveVariables(triggerInterpreter, new GenerationReportTo());
    Template templateEty = this.configurationHolder.readTemplatesConfiguration(trigger).getTemplate(template.getId());
    try {
        String resolvedDestinationPath = new PathExpressionResolver(variables).evaluateExpressions(templateEty.getUnresolvedTargetPath());
        return targetRootPath.resolve(resolvedDestinationPath).normalize();
    } catch (UnknownContextVariableException e) {
        throw new CobiGenRuntimeException("Could not resolve path '" + templateEty.getUnresolvedTargetPath() + "' for input '" + (input instanceof Object[] ? Arrays.toString((Object[]) input) : input.toString()) + "' and template '" + templateEty.getAbsoluteTemplatePath() + "'. Available variables: " + variables.toString());
    }
}
Also used : TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) Variables(com.devonfw.cobigen.impl.config.entity.Variables) Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) ContextVariableResolver(com.devonfw.cobigen.impl.model.ContextVariableResolver) PathExpressionResolver(com.devonfw.cobigen.impl.config.resolver.PathExpressionResolver) UnknownContextVariableException(com.devonfw.cobigen.impl.exceptions.UnknownContextVariableException) Template(com.devonfw.cobigen.impl.config.entity.Template)

Aggregations

GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)33 CobiGen (com.devonfw.cobigen.api.CobiGen)27 Test (org.junit.Test)26 File (java.io.File)25 TemplateTo (com.devonfw.cobigen.api.to.TemplateTo)24 AbstractApiTest (com.devonfw.cobigen.systemtest.common.AbstractApiTest)9 Path (java.nio.file.Path)9 AssertionFailedError (junit.framework.AssertionFailedError)8 List (java.util.List)7 TriggerInterpreter (com.devonfw.cobigen.api.extension.TriggerInterpreter)5 MatcherTo (com.devonfw.cobigen.api.to.MatcherTo)5 Paths (java.nio.file.Paths)5 GenerationReportToAssert (com.devonfw.cobigen.api.assertj.GenerationReportToAssert)4 CobiGenRuntimeException (com.devonfw.cobigen.api.exception.CobiGenRuntimeException)4 IncrementTo (com.devonfw.cobigen.api.to.IncrementTo)4 AbstractIntegrationTest (com.devonfw.cobigen.javaplugin.integrationtest.common.AbstractIntegrationTest)4 Charset (java.nio.charset.Charset)4 CobiGenAsserts.assertThat (com.devonfw.cobigen.api.assertj.CobiGenAsserts.assertThat)3 PluginNotAvailableException (com.devonfw.cobigen.api.exception.PluginNotAvailableException)3 GeneratorPluginActivator (com.devonfw.cobigen.api.extension.GeneratorPluginActivator)3