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