Search in sources :

Example 1 with CobiGen

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

the class AnnotationQueryingTest method testAnnotationWithObjectArraysAsValues.

/**
 * Tests whether annotations with object array values are correctly accessible within the templates
 *
 * @throws Exception test fails
 */
@Test
public void testAnnotationWithObjectArraysAsValues() throws Exception {
    CobiGen cobiGen = CobiGenFactory.create(this.cobigenConfigFolder.toURI());
    File tmpFolderCobiGen = this.tmpFolder.newFolder("cobigen_output");
    Object input = cobiGen.read(new File("src/test/resources/testdata/unittest/inputreader/TestClassWithAnnotationsContainingObjectArrays.java").toPath(), Charset.forName("UTF-8"), getClass().getClassLoader());
    List<TemplateTo> templates = cobiGen.getMatchingTemplates(input);
    boolean methodTemplateFound = false;
    for (TemplateTo template : templates) {
        if (template.getId().equals("annotationQuerying.txt")) {
            GenerationReportTo report = cobiGen.generate(input, template, Paths.get(tmpFolderCobiGen.getAbsolutePath()), false);
            File expectedFile = new File(tmpFolderCobiGen.getAbsoluteFile() + SystemUtils.FILE_SEPARATOR + "annotationQuerying.txt");
            assertThat(report).isSuccessful();
            assertThat(expectedFile).exists();
            assertThat(expectedFile).hasContent("TestClassWithAnnotationsContainingObjectArrays.class,TestClassWithAnnotations.class,");
            methodTemplateFound = true;
            break;
        }
    }
    if (!methodTemplateFound) {
        throw new AssertionFailedError("Test template not found");
    }
}
Also used : GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) CobiGen(com.devonfw.cobigen.api.CobiGen) AssertionFailedError(junit.framework.AssertionFailedError) File(java.io.File) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) Test(org.junit.Test) AbstractIntegrationTest(com.devonfw.cobigen.javaplugin.integrationtest.common.AbstractIntegrationTest)

Example 2 with CobiGen

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

the class ModelCreationTest method testResolveDependencyManagementVersionFromParent.

/**
 * Tests if a valid child pom artifact dependency version can be resolved from its parent using dependencyManagement
 * (mvn help:effective-pom still works)
 *
 * https://github.com/m-m-m/code/issues/35
 *
 * @throws Exception test fails
 */
@Test
public void testResolveDependencyManagementVersionFromParent() throws Exception {
    // given
    CobiGen cobiGen = CobiGenFactory.create(this.cobigenConfigFolder.toURI());
    // when
    Object input = cobiGen.read(new File("src/test/resources/testdata/integrationtest/localmavenproject/maven.project/core/src/main/java/com/example/CustomerEntity.java").toPath().toAbsolutePath(), Charset.forName("UTF-8"));
    // then
    assertThat(input).isNotNull();
}
Also used : CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) Test(org.junit.Test) AbstractIntegrationTest(com.devonfw.cobigen.javaplugin.integrationtest.common.AbstractIntegrationTest)

Example 3 with CobiGen

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

the class VariablesResolutionTest method testSuccessfulPathResolution_variableEqNull.

/**
 * Tests that the path resolution is performed successfully in case of including path variables derived from variable
 * assignments retrieved by regex groups, which have been resolved to null. This bug has been introduced by changing
 * the model building from DOM to Bean model. The latter required to explicitly not to set <code>null</code> as a
 * value for variable resolution. Basically, this is odd, but we have to comply with backward compatibility and the
 * issue that we cannot encode unary-operators like ?? in a file path sufficiently.
 *
 * @throws Exception test fails
 */
@Test
public void testSuccessfulPathResolution_variableEqNull() throws Exception {
    CobiGen cobiGen = CobiGenFactory.create(this.cobigenConfigFolder.toURI());
    File tmpFolderCobiGen = this.tmpFolder.newFolder("cobigen_output");
    Object input = cobiGen.read(new File("src/test/resources/testdata/integrationtest/javaSources/SampleEntity.java").toPath(), Charset.forName("UTF-8"));
    List<TemplateTo> templates = cobiGen.getMatchingTemplates(input);
    boolean methodTemplateFound = false;
    for (TemplateTo template : templates) {
        if (template.getId().equals("${variables.entityName}.java")) {
            GenerationReportTo report = cobiGen.generate(input, template, Paths.get(tmpFolderCobiGen.getAbsolutePath()), false);
            assertThat(report).isSuccessful();
            methodTemplateFound = true;
            break;
        }
    }
    if (!methodTemplateFound) {
        throw new AssertionFailedError("Test template not found");
    }
}
Also used : GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) CobiGen(com.devonfw.cobigen.api.CobiGen) AssertionFailedError(junit.framework.AssertionFailedError) File(java.io.File) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) AbstractIntegrationTest(com.devonfw.cobigen.javaplugin.integrationtest.common.AbstractIntegrationTest) Test(org.junit.Test)

Example 4 with CobiGen

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

the class GenerateMojo method createCobiGenInstance.

/**
 * Creates an instance of {@link CobiGen} based on a given configuration project or configuration jar.
 *
 * @return the initialized {@link CobiGen} instance
 */
private CobiGen createCobiGenInstance() {
    CobiGen cobiGen;
    if (this.configurationFolder != null) {
        getLog().debug("ConfigurationFolder configured as " + this.configurationFolder.toURI().toString());
        cobiGen = CobiGenFactory.create(this.configurationFolder.toURI());
    } else {
        final ClassRealm classRealm = this.pluginDescriptor.getClassRealm();
        URL contextConfigurationLocation = ConfigurationClassLoaderUtil.getContextConfiguration(classRealm);
        URI configFile = URI.create(contextConfigurationLocation.getFile().toString().split("!")[0]);
        getLog().debug("Reading configuration from file " + configFile.toString());
        cobiGen = CobiGenFactory.create(configFile);
    }
    return cobiGen;
}
Also used : ClassRealm(org.codehaus.plexus.classworlds.realm.ClassRealm) CobiGen(com.devonfw.cobigen.api.CobiGen) URI(java.net.URI) URL(java.net.URL)

Example 5 with CobiGen

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

the class GenerateMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    CobiGen cobiGen = createCobiGenInstance();
    List<Object> inputs = collectInputs(cobiGen);
    if (inputs.isEmpty()) {
        getLog().info("No inputs specified for generation!");
        getLog().info("");
        return;
    }
    if ((this.templates == null || this.templates.isEmpty()) && (this.increments == null || this.increments.isEmpty())) {
        getLog().info("No templates/increments specified for generation!");
        getLog().info("");
        return;
    }
    List<GenerableArtifact> generableArtifacts = collectIncrements(cobiGen, inputs);
    generableArtifacts.addAll(collectTemplates(cobiGen, inputs));
    try {
        for (Object input : inputs) {
            getLog().debug("Invoke CobiGen for input of class " + input.getClass().getCanonicalName());
            GenerationReportTo report = cobiGen.generate(input, generableArtifacts, Paths.get(this.destinationRoot.toURI()), this.forceOverride, (task, progress) -> {
            });
            if (!report.isSuccessful()) {
                for (Throwable e : report.getErrors()) {
                    getLog().error(e.getMessage(), e);
                }
                throw new MojoFailureException("Generation not successfull", report.getErrors().get(0));
            }
            if (report.getGeneratedFiles().isEmpty() && this.failOnNothingGenerated) {
                throw new MojoFailureException("The execution '" + this.execution.getExecutionId() + "' of cobigen-maven-plugin resulted in no file to be generated!");
            }
        }
    } catch (CobiGenRuntimeException e) {
        getLog().error(e.getMessage(), e);
        throw new MojoFailureException(e.getMessage(), e);
    } catch (MojoFailureException e) {
        throw e;
    } catch (Throwable e) {
        getLog().error("An error occured while executing CobiGen: " + e.getMessage(), e);
        throw new MojoFailureException("An error occured while executing CobiGen: " + e.getMessage(), e);
    }
}
Also used : GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) GenerableArtifact(com.devonfw.cobigen.api.to.GenerableArtifact) MojoFailureException(org.apache.maven.plugin.MojoFailureException) CobiGen(com.devonfw.cobigen.api.CobiGen)

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