Search in sources :

Example 41 with CobiGen

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

the class XmlPluginIntegrationTest method testUmlMethodAttributeExtraction.

/**
 * Tests simple extraction of methods and attributes out of XMI UML.
 *
 * @throws Exception test fails
 */
@Test
public void testUmlMethodAttributeExtraction() 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}MethodsAttributes.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("StudentMethodsAttributes.txt", "UserMethodsAttributes.txt", "MarksMethodsAttributes.txt", "TeacherMethodsAttributes.txt");
    assertThat(targetFolder.toPath().resolve("StudentMethodsAttributes.txt")).hasContent("public newOperation");
    assertThat(targetFolder.toPath().resolve("UserMethodsAttributes.txt")).hasContent("");
    assertThat(targetFolder.toPath().resolve("MarksMethodsAttributes.txt")).hasContent("private int attributeExample");
    assertThat(targetFolder.toPath().resolve("TeacherMethodsAttributes.txt")).hasContent("");
}
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 42 with CobiGen

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

the class XmlPluginIntegrationTest method testUmlEntityExtraction.

/**
 * Tests the generation of entities out of XMI UML. </br>
 * </br>
 * In marks class there is an attribute from which it has to generate getters and setters and also the associations
 * between marks and the rest of connected classes. </br>
 * </br>
 * The file nullMultiplicity contains a class called TestingNullMultiplicity which is connected to marks but without
 * multiplicity defined.
 *
 * @throws Exception test fails
 */
@Test
public void testUmlEntityExtraction() throws Exception {
    // arrange
    Path configFolder = new File(testFileRootPath + "uml-classdiag").toPath();
    File xmlFile = configFolder.resolve("nullMultiplicity.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}Entity.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("StudentEntity.txt", "UserEntity.txt", "MarksEntity.txt", "TeacherEntity.txt", "TestingNullMultiplicityEntity.txt");
    assertThat(targetFolder.toPath().resolve("MarksEntity.txt")).hasContent("import java.util.List;\n" + "import javax.persistence.Column;\n" + "import javax.persistence.Entity;\n" + "import javax.persistence.Table;\n" + "@Entity\n" + "@Table(name=Marks)\n" + "public class MarksEntity extends ApplicationPersistenceEntity implements Marks {\n" + "private static final long serialVersionUID = 1L;\n" + "private int attributeExample;\n" + "// I want one\n" + "private Student student;\n" + "@Override\n" + "public Student getStudent(){\n" + "return this.student;\n" + "}\n" + "@Override\n" + "public void setStudent(Student student){\n" + "student = this.student;\n" + "}\n" + "@Override\n" + "public Integer getAttributeExample(){\n" + "return this.attributeExample;\n" + "}\n" + "public void setAttributeExample(Integer attributeExample){\n" + "this.attributeExample = attributeExample;\n" + "}\n" + "}");
    assertThat(targetFolder.toPath().resolve("TestingNullMultiplicityEntity.txt")).hasContent("import java.util.List;\n" + "import javax.persistence.Column;\n" + "import javax.persistence.Entity;\n" + "import javax.persistence.Table;\n" + "@Entity\n" + "@Table(name=TestingNullMultiplicity)\n" + "public class TestingNullMultiplicityEntity extends ApplicationPersistenceEntity implements TestingNullMultiplicity {\n" + "private static final long serialVersionUID = 1L;\n" + "}\n");
}
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 43 with CobiGen

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

the class XmlPluginIntegrationTest method generateTemplateAndTestOutput.

/**
 * Generates the template with the given templateId and reads the generated File with the outputFileName. It will be
 * asserted, that this file has the expectedFileContents passed as parameter.
 *
 * @param templateId Template to generate
 * @param outputFileName file name of the generated output File
 * @param expectedFileContents generated contents to be expected (asserted)
 * @return the resulting report
 * @throws Exception if anything fails.
 */
private GenerationReportToAssert generateTemplateAndTestOutput(String templateId, String outputFileName, String expectedFileContents) throws Exception {
    CobiGen cobiGen = CobiGenFactory.create(this.cobigenConfigFolder.toURI());
    // wenn der temporäre Output Ordner breits existiert, dann wird dieser wiederverwendet.
    File tmpFolderCobiGen = new File(this.tmpFolder.getRoot().getAbsolutePath() + File.separator + "cobigen_output");
    if (!tmpFolderCobiGen.exists()) {
        tmpFolderCobiGen = this.tmpFolder.newFolder("cobigen_output");
    }
    // read xml File as Document
    Object inputDocument = cobiGen.read(this.testinput.toPath(), Charset.forName("UTF-8"));
    // find matching templates and use test template for generation
    List<TemplateTo> templates = cobiGen.getMatchingTemplates(inputDocument);
    boolean templateFound = false;
    GenerationReportToAssert asserts = null;
    for (TemplateTo template : templates) {
        if (template.getId().equals(templateId)) {
            GenerationReportTo report = cobiGen.generate(inputDocument, template, Paths.get(tmpFolderCobiGen.getAbsolutePath()), false, (taskname, progress) -> {
            });
            asserts = assertThat(report);
            File expectedFile = new File(tmpFolderCobiGen.getAbsoluteFile() + File.separator + outputFileName);
            Assert.assertTrue(expectedFile.exists());
            // validate results if expected file contents are defined
            if (expectedFileContents != null) {
                Assert.assertEquals(expectedFileContents, FileUtils.readFileToString(expectedFile, StandardCharsets.UTF_8));
            }
            templateFound = true;
            break;
        }
    }
    if (!templateFound) {
        throw new AssertionFailedError("Test template not found");
    }
    return asserts;
}
Also used : GenerationReportToAssert(com.devonfw.cobigen.api.assertj.GenerationReportToAssert) 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)

Example 44 with CobiGen

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

the class TriggerActivationTest method testNoActivation_2Of2AND_1NOT_MatcherMatches.

/**
 * Tests that a trigger will not be activated in case of two of two AND matchers and one NOT matcher matches.
 *
 * @throws Exception test fails
 * @author mbrunnli (22.02.2015)
 */
@Test
@SuppressWarnings("unchecked")
public void testNoActivation_2Of2AND_1NOT_MatcherMatches() throws Exception {
    Object input = new 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("test");
    when(triggerInterpreter.getMatcher()).thenReturn(matcher);
    when(triggerInterpreter.getInputReader()).thenReturn(inputReader);
    when(inputReader.isValidInput(any())).thenReturn(true);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("and1"), ANY, sameInstance(input))))).thenReturn(true);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("and2"), ANY, sameInstance(input))))).thenReturn(true);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("or"), ANY, sameInstance(input))))).thenReturn(false);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("not"), ANY, sameInstance(input))))).thenReturn(true);
    PluginRegistry.registerTriggerInterpreter(triggerInterpreter, activator);
    // execution
    CobiGen cobigen = CobiGenFactory.create(new File(testFileRootPath + "templates").toURI());
    List<String> matchingTriggerIds = cobigen.getMatchingTriggerIds(input);
    assertThat(matchingTriggerIds, not(hasItem("triggerId")));
}
Also used : TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) MatcherToMatcher(com.devonfw.cobigen.api.matchers.MatcherToMatcher) InputReader(com.devonfw.cobigen.api.extension.InputReader) MatcherInterpreter(com.devonfw.cobigen.api.extension.MatcherInterpreter) CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) GeneratorPluginActivator(com.devonfw.cobigen.api.extension.GeneratorPluginActivator) Test(org.junit.Test) AbstractApiTest(com.devonfw.cobigen.systemtest.common.AbstractApiTest)

Example 45 with CobiGen

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

the class TriggerActivationTest method testActivation_1OR_MatcherMatches.

/**
 * Tests that a trigger will be activated in case of one OR matcher matches.
 *
 * @throws Exception test fails
 * @author mbrunnli (22.02.2015)
 */
@Test
@SuppressWarnings("unchecked")
public void testActivation_1OR_MatcherMatches() throws Exception {
    Object input = new 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("test");
    when(triggerInterpreter.getMatcher()).thenReturn(matcher);
    when(triggerInterpreter.getInputReader()).thenReturn(inputReader);
    when(inputReader.isValidInput(any())).thenReturn(true);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("or"), ANY, sameInstance(input))))).thenReturn(true);
    when(matcher.matches(argThat(new MatcherToMatcher(equalTo("not"), ANY, sameInstance(input))))).thenReturn(false);
    PluginRegistry.registerTriggerInterpreter(triggerInterpreter, activator);
    // execution
    CobiGen cobigen = CobiGenFactory.create(new File(testFileRootPath + "templates").toURI());
    List<String> matchingTriggerIds = cobigen.getMatchingTriggerIds(input);
    assertThat(matchingTriggerIds, hasItem("triggerId2"));
}
Also used : TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) MatcherToMatcher(com.devonfw.cobigen.api.matchers.MatcherToMatcher) InputReader(com.devonfw.cobigen.api.extension.InputReader) MatcherInterpreter(com.devonfw.cobigen.api.extension.MatcherInterpreter) CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) GeneratorPluginActivator(com.devonfw.cobigen.api.extension.GeneratorPluginActivator) Test(org.junit.Test) AbstractApiTest(com.devonfw.cobigen.systemtest.common.AbstractApiTest)

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