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");
}
}
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();
}
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");
}
}
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;
}
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);
}
}
Aggregations