Search in sources :

Example 1 with TemplateTo

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

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

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

the class CheckStateListener method checkStateChanged.

@Override
public void checkStateChanged(CheckStateChangedEvent event) {
    MDC.put(InfrastructureConstants.CORRELATION_ID, UUID.randomUUID().toString());
    LOG.info("Increment selection changed. Calculating generation preview file tree...");
    try {
        CheckboxTreeViewer resourcesTree = this.page.getResourcesTree();
        CheckboxTreeViewer incrementSelector = this.page.getPackageSelector();
        if (event.getSource().equals(resourcesTree)) {
            resourcesTree.setSubtreeChecked(event.getElement(), event.getChecked());
            ((SelectFileLabelProvider) resourcesTree.getLabelProvider()).setSelectedResources(resourcesTree.getCheckedElements());
            refreshNodes(event);
        } else if (event.getSource().equals(incrementSelector)) {
            performCheckLogic(event, incrementSelector);
            Set<Object> checkedElements = new HashSet<>(Arrays.asList(incrementSelector.getCheckedElements()));
            performCheckLogicForALLIncrement(incrementSelector, checkedElements);
            Map<String, Set<TemplateTo>> paths = this.cobigenWrapper.getTemplateDestinationPaths(this.selectedIncrements);
            Set<String> workspaceExternalPaths = Sets.newHashSet();
            for (String path : paths.keySet()) {
                if (this.cobigenWrapper.isWorkspaceExternalPath(path)) {
                    workspaceExternalPaths.add(path);
                }
            }
            List<OffWorkspaceResourceTreeNode> offScopeResourceTree = buildOffScopeResourceTree(workspaceExternalPaths);
            ((SelectFileContentProvider) resourcesTree.getContentProvider()).filter(paths.keySet(), offScopeResourceTree);
            this.page.setDisplayedfilePathToTemplateMapping(paths);
            resourcesTree.setCheckedElements(new Object[0]);
            resourcesTree.refresh();
            resourcesTree.expandAll();
            if (!this.batch) {
                selectNewResources();
                selectMergeableResources();
                selectOverridingResources();
            } else {
                selectAllResources(paths.keySet());
            }
        }
        checkPageComplete();
    } catch (Throwable e) {
        ExceptionHandler.handle(e, null);
    }
    LOG.info("Calculating of changed preview file tree finished.");
    MDC.remove(InfrastructureConstants.CORRELATION_ID);
}
Also used : CheckboxTreeViewer(org.eclipse.jface.viewers.CheckboxTreeViewer) HashSet(java.util.HashSet) Set(java.util.Set) SelectFileLabelProvider(com.devonfw.cobigen.eclipse.wizard.common.model.SelectFileLabelProvider) LinkedList(java.util.LinkedList) List(java.util.List) Map(java.util.Map) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo)

Example 4 with TemplateTo

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

the class XmlPluginIntegrationTest method testSimpleUmlEntityExtraction.

/**
 * Tests simple extraction of entities out of XMI UML.
 *
 * @throws Exception test fails
 */
@Test
public void testSimpleUmlEntityExtraction() throws Exception {
    // arrange
    Path configFolder = new File(testFileRootPath + "uml-classdiag").toPath();
    File xmlFile = configFolder.resolve("completeUmlXmi.xml").toFile();
    CobiGen cobigen = CobiGenFactory.create(configFolder.toUri());
    Object doc = cobigen.read(xmlFile.toPath(), UTF_8);
    File targetFolder = this.tmpFolder.newFolder("testSimpleUmlEntityExtraction");
    // act
    List<TemplateTo> matchingTemplates = cobigen.getMatchingTemplates(doc);
    List<TemplateTo> templateOfInterest = matchingTemplates.stream().filter(e -> e.getId().equals("${className}.txt")).collect(Collectors.toList());
    assertThat(templateOfInterest).hasSize(1);
    GenerationReportTo generate = cobigen.generate(doc, templateOfInterest, targetFolder.toPath());
    // assert
    assertThat(generate).isSuccessful();
    File[] files = targetFolder.listFiles();
    assertThat(files).extracting(e -> e.getName()).containsExactlyInAnyOrder("Student.txt", "User.txt", "Marks.txt", "Teacher.txt");
    assertThat(targetFolder.toPath().resolve("Student.txt")).hasContent("public Student EAID_4509184A_D724_495f_AAEB_1ACE1AD90879");
    assertThat(targetFolder.toPath().resolve("User.txt")).hasContent("public User EAID_C2E366C0_510F_4145_B650_110537B98360");
    assertThat(targetFolder.toPath().resolve("Marks.txt")).hasContent("public Marks EAID_1D7DCE81_651D_40f2_A6E5_A522CF6E0C64");
    assertThat(targetFolder.toPath().resolve("Teacher.txt")).hasContent("public Teacher EAID_6EA6FC61_FB9B_4e8e_98A1_30BD386AEA9A");
}
Also used : Path(java.nio.file.Path) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) AssertionFailedError(junit.framework.AssertionFailedError) CobiGen(com.devonfw.cobigen.api.CobiGen) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) List(java.util.List) PluginNotAvailableException(com.devonfw.cobigen.api.exception.PluginNotAvailableException) CobiGenFactory(com.devonfw.cobigen.impl.CobiGenFactory) CobiGenAsserts.assertThat(com.devonfw.cobigen.api.assertj.CobiGenAsserts.assertThat) Rule(org.junit.Rule) Charset(java.nio.charset.Charset) Paths(java.nio.file.Paths) Assert(org.junit.Assert) GenerationReportToAssert(com.devonfw.cobigen.api.assertj.GenerationReportToAssert) Path(java.nio.file.Path) TemporaryFolder(org.junit.rules.TemporaryFolder) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) Test(org.junit.Test)

Example 5 with TemplateTo

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

the class XPathGenerationTest method testXpathAccess.

/**
 * Testing basic Xpath Access
 *
 * @throws Exception test fails
 */
@Test
public void testXpathAccess() throws Exception {
    Path cobigenConfigFolder = new File("src/test/resources/testdata/integrationtest/uml-basic-test").toPath();
    Path input = cobigenConfigFolder.resolve("uml.xml");
    CobiGen cobigen = CobiGenFactory.create(cobigenConfigFolder.toUri());
    Object compliantInput = cobigen.read(input, Charset.forName("UTF-8"));
    List<TemplateTo> matchingTemplates = cobigen.getMatchingTemplates(compliantInput);
    assertThat(matchingTemplates).isNotNull().hasSize(1);
    File targetFolder = this.tmpFolder.newFolder("testXpathAccess");
    GenerationReportTo report = cobigen.generate(compliantInput, matchingTemplates.get(0), targetFolder.toPath());
    assertThat(report).isSuccessful();
    assertThat(targetFolder.toPath().resolve("DocXPath.txt")).hasContent("Bill");
}
Also used : Path(java.nio.file.Path) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) Test(org.junit.Test)

Aggregations

TemplateTo (com.devonfw.cobigen.api.to.TemplateTo)41 CobiGen (com.devonfw.cobigen.api.CobiGen)30 File (java.io.File)28 Test (org.junit.Test)27 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)25 AbstractApiTest (com.devonfw.cobigen.systemtest.common.AbstractApiTest)12 IncrementTo (com.devonfw.cobigen.api.to.IncrementTo)9 Path (java.nio.file.Path)9 List (java.util.List)9 AssertionFailedError (junit.framework.AssertionFailedError)9 Paths (java.nio.file.Paths)6 Set (java.util.Set)6 HashSet (java.util.HashSet)5 Collectors (java.util.stream.Collectors)5 CobiGenAsserts.assertThat (com.devonfw.cobigen.api.assertj.CobiGenAsserts.assertThat)4 GenerationReportToAssert (com.devonfw.cobigen.api.assertj.GenerationReportToAssert)4 ComparableIncrement (com.devonfw.cobigen.eclipse.generator.entity.ComparableIncrement)4 CobiGenFactory (com.devonfw.cobigen.impl.CobiGenFactory)4 AbstractIntegrationTest (com.devonfw.cobigen.javaplugin.integrationtest.common.AbstractIntegrationTest)4 StandardCharsets (java.nio.charset.StandardCharsets)4