Skip to content

Commit

Permalink
#1151 Merge branch 'dev_core'
Browse files Browse the repository at this point in the history
  • Loading branch information
maybeec committed Apr 30, 2020
2 parents b45afbb + a9aad6d commit 927e87a
Show file tree
Hide file tree
Showing 28 changed files with 1,284 additions and 586 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

import com.devonfw.cobigen.api.annotation.ExceptionFacade;
import com.devonfw.cobigen.api.extension.ModelBuilder;
Expand Down Expand Up @@ -65,14 +66,16 @@ public GenerationReportTo generate(Object input, List<? extends GenerableArtifac
* if <code>true</code> and the destination path is already existent, the contents will be
* overwritten by the generated ones iff there is no merge strategy defined by the templates
* configuration. (default: {@code false})
* @param logicClasses
* a {@link List} of java class files, which will be included as accessible beans in the
* template model. Such classes can be used to implement more complex template logic.
* @param classLoader
* a {@link ClassLoader} provided by each plugin, containing the archive to load template
* utility classes from
* @param templateFolderPath
* Path to load template utility classes from (root path of CobiGen templates)
* @return The {@link GenerationReportTo generation report} covering the actual status of success, a list
* of warnings, as well as a list of error messages.
*/
public GenerationReportTo generate(Object input, List<? extends GenerableArtifact> generableArtifacts,
Path targetRootPath, boolean forceOverride, List<Class<?>> logicClasses);
Path targetRootPath, boolean forceOverride, ClassLoader classLoader, Path templateFolderPath);

/**
* Generates code by processing the {@link List} of {@link GenerableArtifact}s for the given input.
Expand All @@ -88,16 +91,49 @@ public GenerationReportTo generate(Object input, List<? extends GenerableArtifac
* if <code>true</code> and the destination path is already existent, the contents will be
* overwritten by the generated ones iff there is no merge strategy defined by the templates
* configuration. (default: {@code false})
* @param logicClasses
* a {@link List} of java class files, which will be included as accessible beans in the
* template model. Such classes can be used to implement more complex template logic.
* @param classLoader
* a {@link ClassLoader} provided by each plugin, containing the archive to load template
* utility classes from
* @param rawModel
* externally adapted model to be used for generation.
* @param progressCallback
* expects the progress in percent as Integer
* @param templateFolderPath
* Path to load template utility classes from (root path of CobiGen templates)
* @return The {@link GenerationReportTo generation report} covering the actual status of success, a list
* of warnings, as well as a list of error messages.
*/
public GenerationReportTo generate(Object input, List<? extends GenerableArtifact> generableArtifacts,
Path targetRootPath, boolean forceOverride, List<Class<?>> logicClasses, Map<String, Object> rawModel);
Path targetRootPath, boolean forceOverride, ClassLoader classLoader, Map<String, Object> rawModel,
BiConsumer<String, Integer> progressCallback, Path templateFolderPath);

/**
* Generates code by processing the {@link List} of {@link GenerableArtifact}s for the given input.
*
* @param input
* generator input object
* @param generableArtifacts
* a {@link List} of artifacts to be generated
* @param targetRootPath
* target root path to generate to (to be used to resolve the dependent template destination
* paths)
* @param forceOverride
* if <code>true</code> and the destination path is already existent, the contents will be
* overwritten by the generated ones iff there is no merge strategy defined by the templates
* configuration. (default: {@code false})
* @param classLoader
* a {@link ClassLoader} provided by each plugin, containing the archive to load template
* utility classes from
* @param rawModel
* externally adapted model to be used for generation.
* @param templateFolderPath
* Path to load template utility classes from (root path of CobiGen templates)
* @return The {@link GenerationReportTo generation report} covering the actual status of success, a list
* of warnings, as well as a list of error messages.
*/
public GenerationReportTo generate(Object input, List<? extends GenerableArtifact> generableArtifacts,
Path targetRootPath, boolean forceOverride, ClassLoader classLoader, Map<String, Object> rawModel,
Path templateFolderPath);

/**
* Generates code by processing the {@link GenerableArtifact} for the given input.
Expand Down Expand Up @@ -148,14 +184,16 @@ public GenerationReportTo generate(Object input, GenerableArtifact generableArti
* if <code>true</code> and the destination path is already existent, the contents will be
* overwritten by the generated ones iff there is no merge strategy defined by the templates
* configuration. (default: {@code false})
* @param logicClasses
* a {@link List} of java class files, which will be included as accessible beans in the
* template model. Such classes can be used to implement more complex template logic.
* @param classLoader
* a {@link ClassLoader} provided by each plugin, containing the archive to load template
* utility classes from
* @param templateFolderPath
* Path to load template utility classes from (root path of CobiGen templates)
* @return The {@link GenerationReportTo generation report} covering the actual status of success, a list
* of warnings, as well as a list of error messages.
*/
public GenerationReportTo generate(Object input, GenerableArtifact generableArtifact, Path targetRootPath,
boolean forceOverride, List<Class<?>> logicClasses);
boolean forceOverride, ClassLoader classLoader, Path templateFolderPath);

/**
* Generates code by processing the {@link GenerableArtifact} for the given input.
Expand All @@ -171,16 +209,18 @@ public GenerationReportTo generate(Object input, GenerableArtifact generableArti
* if <code>true</code> and the destination path is already existent, the contents will be
* overwritten by the generated ones iff there is no merge strategy defined by the templates
* configuration. (default: {@code false})
* @param logicClasses
* a {@link List} of java class files, which will be included as accessible beans in the
* template model. Such classes can be used to implement more complex template logic.
* @param classLoader
* a {@link ClassLoader} provided by each plugin, containing the archive to load template
* utility classes from
* @param rawModel
* externally adapted model to be used for generation.
* @param templateFolderPath
* Path to load template utility classes from (root path of CobiGen templates)
* @return The {@link GenerationReportTo generation report} covering the actual status of success, a list
* of warnings, as well as a list of error messages.
*/
public GenerationReportTo generate(Object input, GenerableArtifact generableArtifact, Path targetRootPath,
boolean forceOverride, List<Class<?>> logicClasses, Map<String, Object> rawModel);
boolean forceOverride, ClassLoader classLoader, Map<String, Object> rawModel, Path templateFolderPath);

/**
* Returns a new {@link ModelBuilder} instance for the given input object. <i>Caution: this method will
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.devonfw.cobigen.api.exception;

import java.util.concurrent.CancellationException;

/**
* Exception is thrown if the generation Process is Cancelled
*/
public class CobiGenCancellationException extends CancellationException {

/**
* Default serial version UID.
*/
private static final long serialVersionUID = 1L;

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public class GenerationReportTo {
/** @see #getGeneratedFiles() */
private Set<Path> generatedFiles = new TreeSet<>();

/**
* @see #isCancelled()
*/
private boolean cancelled = false;

/**
* @see #getGeneratedFiles()
* @param file
Expand Down Expand Up @@ -165,4 +170,22 @@ public boolean hasErrors() {
public boolean isSuccessful() {
return errors.isEmpty();
}

/**
* Return whether the generation got cancelled by the user
* @return <code>true</code> if it got cancelled, <code>false</code> otherwise
*/
public boolean isCancelled() {
return cancelled;
}

/**
* set's whether the generation got cancelled or not
* @param cancelled
* if generation got cancelled
*/
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.internal.matchers.Any.ANY;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
Expand Down Expand Up @@ -67,14 +64,12 @@ public void callClassLoadingTest() throws Exception {
CobiGen target = CobiGenFactory.create(templatesFolder.toURI());
List<TemplateTo> templates = target.getMatchingTemplates(containerInput);

// very manual way to load classes
List<Class<?>> logicClasses = new ArrayList<>();
logicClasses.add(getJarClass("JarredClass"));
logicClasses.add(getJarClass("OtherJarredClass"));
File file = new File(testFileRootPath + "jarredclasses/jarred.jar");
ClassLoader cl = URLClassLoader.newInstance(new URL[] { file.toURI().toURL() }, getClass().getClassLoader());

// Execution
GenerationReportTo report = target.generate(containerInput, templates.get(0),
Paths.get(generationRootFolder.toURI()), false, logicClasses);
GenerationReportTo report =
target.generate(containerInput, templates.get(0), Paths.get(generationRootFolder.toURI()), false, cl, null);

// Verification
File expectedResult = new File(testFileRootPath, "expected/Test.java");
Expand All @@ -99,14 +94,12 @@ public void testLoadEnumClass() throws Exception {
CobiGen target = CobiGenFactory.create(templatesFolder.toURI());
List<TemplateTo> templates = target.getMatchingTemplates(containerInput);

// very manual way to load classes
List<Class<?>> logicClasses = new ArrayList<>();
logicClasses.add(getJarClass("JarredEnum"));
logicClasses.add(getJarClass("EnumTestJarredClass"));
File file = new File(testFileRootPath + "jarredclasses/jarred.jar");
ClassLoader cl = URLClassLoader.newInstance(new URL[] { file.toURI().toURL() }, getClass().getClassLoader());

// Execution
GenerationReportTo report = target.generate(containerInput, templates.get(1),
Paths.get(generationRootFolder.toURI()), false, logicClasses);
GenerationReportTo report =
target.generate(containerInput, templates.get(1), Paths.get(generationRootFolder.toURI()), false, cl, null);

// Verification
File expectedResult = new File(testFileRootPath, "expected/Test2.java");
Expand Down Expand Up @@ -176,25 +169,4 @@ public String toString() {
return container;
}

/**
* An impractical way of loading classes from a jar file
* @param name
* the name of the class to load from the jar file
* @return the requested class
* @author sroeger (Aug 12, 2016)
*/
private Class<?> getJarClass(String name) {

File file = new File(testFileRootPath + "jarredclasses/jarred.jar");

URLClassLoader cl;
Class<?> jarred = null;
try {
cl = URLClassLoader.newInstance(new URL[] { file.toURI().toURL() });
jarred = cl.loadClass(name);
} catch (MalformedURLException | ClassNotFoundException e) {
fail();
}
return jarred;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static com.devonfw.cobigen.test.assertj.CobiGenAsserts.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.mockito.Matchers.any;
Expand All @@ -12,6 +13,7 @@

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -23,6 +25,7 @@
import org.junit.Test;

import com.devonfw.cobigen.api.CobiGen;
import com.devonfw.cobigen.api.exception.InvalidConfigurationException;
import com.devonfw.cobigen.api.extension.InputReader;
import com.devonfw.cobigen.api.extension.MatcherInterpreter;
import com.devonfw.cobigen.api.extension.TriggerInterpreter;
Expand Down Expand Up @@ -107,6 +110,30 @@ public void testGenerationWithExternalIncrements() throws Exception {
assertThat(triggersIds.size()).isEqualTo(2);
}

/**
* Tests generation of external increments where its trigger does not match
* @throws IOException
* test fails
*/
public void testGenerationWithExternalIncrementsFailsWhenExternalTriggerNotMatch() throws IOException {
// given
Object input = PluginMockFactory.createSimpleJavaConfigurationMock();

File folder = tmpFolder.newFolder("GenerationTest");
File target = new File(folder, "generated.txt");
FileUtils.write(target, "base");

// when
CobiGen cobigen =
CobiGenFactory.create(new File(testFileRootPath + "externalIncrementsGenerationException").toURI());

// exception is thrown while getting all increments
assertThatThrownBy(() -> {
cobigen.getMatchingIncrements(input);
}).isInstanceOf(InvalidConfigurationException.class).hasMessageContaining(
"An external incrementRef to valid_increment_composition::0 is referenced from external_incrementref but its trigger does not match");
}

/**
* 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.
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<contextConfiguration xmlns="http://capgemini.com/devonfw/cobigen/ContextConfiguration" version="2.1">
<trigger id="external_incrementref" type="mockplugin" templateFolder="valid_external_incrementref">
<matcher type="fqn" value="Whatever is still matched">
</matcher>
</trigger>
<trigger id="valid_increment_composition" type="mockplugin" templateFolder="valid_increment_composition">
<matcher type="invalid" value="Whatever is still not matched">
</matcher>
</trigger>
</contextConfiguration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<templatesConfiguration xmlns="http://capgemini.com/devonfw/cobigen/TemplatesConfiguration"
version="2.1">

<increments>
<increment name="1" description="1">
<incrementRef ref="valid_increment_composition::0"/>
</increment>
</increments>
</templatesConfiguration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<templatesConfiguration xmlns="http://capgemini.com/devonfw/cobigen/TemplatesConfiguration"
version="2.1">

<templates>
<template name="templateDecl" destinationPath="src/main/java/templateDecl.java" templateFile="templates/templateDecl.ftl" />
</templates>

<increments>
<increment name="0" description="0">
<templateRef ref="templateDecl"/>
</increment>
</increments>
</templatesConfiguration>
Loading

0 comments on commit 927e87a

Please sign in to comment.