From 7b6bf797a9265bfbe2420467e741f98169dd1055 Mon Sep 17 00:00:00 2001
From: jan-vcapgemini <59438728+jan-vcapgemini@users.noreply.github.com>
Date: Tue, 5 Jul 2022 12:54:10 +0200
Subject: [PATCH] 1551 merge master into template_set_deployables (#1552)
* 1526 fixed infinite loop while generating from source code (#1547)
* #1526 added new Eclipse text input test
added a test which tests a simple generate from text input
added a new processCobiGenWithTextInput method to EclipseCobiGenUtils
refactored processCobiGen method
added new generateWithSelectedIncrements method to EclipseCobiGenUtils
* #1526 fixed infinite loop
wrapped getActiveWorkbenchWindow into asynchronous runnables
added workbenchWindow class variable
added extra check for FileEditorInput
* #1526 added missing files
added .classpath and .project files
* #1526 fixed Eclipse crash
changed asynchronous runnable to synchronized
* #1551 disabled GenerateFromTextInput test
---
.../test/GenerateFromTextInputTest.java | 93 +++++++++++++++++++
.../common/utils/EclipseCobiGenUtils.java | 46 +++++++++
.../input/PlainInput.java | 5 +
.../templates/.classpath | 26 ++++++
.../templates/.project | 23 +++++
.../templates/logback.xml | 17 ++++
.../templates/pom.xml | 24 +++++
.../templates/template1/TestOutput.txt.ftl | 1 +
.../src/main/templates/template1/context.xml | 7 ++
.../main/templates/template1/templates.xml | 11 +++
.../eclipse/common/tools/PlatformUIUtil.java | 39 ++++++--
.../generator/GeneratorWrapperFactory.java | 22 +++--
12 files changed, 299 insertions(+), 15 deletions(-)
create mode 100644 cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/GenerateFromTextInputTest.java
create mode 100644 cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/input/PlainInput.java
create mode 100644 cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/.classpath
create mode 100644 cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/.project
create mode 100644 cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/logback.xml
create mode 100644 cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/pom.xml
create mode 100644 cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/src/main/templates/template1/TestOutput.txt.ftl
create mode 100644 cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/src/main/templates/template1/context.xml
create mode 100644 cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/src/main/templates/template1/templates.xml
diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/GenerateFromTextInputTest.java b/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/GenerateFromTextInputTest.java
new file mode 100644
index 0000000000..1819daa929
--- /dev/null
+++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/GenerateFromTextInputTest.java
@@ -0,0 +1,93 @@
+package com.devonfw.cobigen.eclipse.test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.File;
+
+import org.apache.commons.io.FileUtils;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.ui.JavaUI;
+import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEclipseEditor;
+import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
+import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
+import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import com.devonfw.cobigen.eclipse.common.constants.external.ResourceConstants;
+import com.devonfw.cobigen.eclipse.test.common.SystemTest;
+import com.devonfw.cobigen.eclipse.test.common.swtbot.AllJobsAreFinished;
+import com.devonfw.cobigen.eclipse.test.common.utils.EclipseCobiGenUtils;
+import com.devonfw.cobigen.eclipse.test.common.utils.EclipseUtils;
+
+/**
+ * Test suite for issues with generation from text selection
+ */
+@RunWith(SWTBotJunit4ClassRunner.class)
+public class GenerateFromTextInputTest extends SystemTest {
+
+ /** Root path of the Test Resources */
+ private static final String resourcesRootPath = "src/main/resources/GenerateFromTextInputTest/";
+
+ /**
+ * Setup workbench appropriately for tests
+ *
+ * @throws Exception test fails
+ */
+ @BeforeClass
+ public static void setupClass() throws Exception {
+
+ EclipseUtils.cleanWorkspace(bot, true);
+ // import the configuration project for this test
+ EclipseUtils.importExistingGeneralProject(bot, new File(resourcesRootPath + "templates").getAbsolutePath());
+ EclipseUtils.updateMavenProject(bot, ResourceConstants.CONFIG_PROJECT_NAME);
+ }
+
+ /**
+ * Tests if a generate from text selection works properly
+ *
+ * Reported in: https://github.com/devonfw/cobigen/issues/1526
+ *
+ * @throws Exception Test fails
+ */
+ @Ignore
+ @Test
+ public void testGenerateFromTextDoesNotLoop() throws Exception {
+
+ // create a new temporary java project and copy java class used as an input for CobiGen
+ String testProjectName = "TestInputProj";
+ IJavaProject project = this.tmpMavenProjectRule.createProject(testProjectName);
+ FileUtils.copyFile(new File(resourcesRootPath + "input/PlainInput.java"),
+ project.getUnderlyingResource().getLocation().append("src/main/java/main/PlainInput.java").toFile());
+ project.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
+ this.tmpMavenProjectRule.updateProject();
+
+ // expand the new file in the package explorer
+ SWTBotView view = bot.viewById(JavaUI.ID_PACKAGES);
+ SWTBotTreeItem javaClassItem = view.bot().tree().expandNode(testProjectName, "src/main/java", "main",
+ "PlainInput.java");
+
+ // double click on the file to open the Java editor
+ javaClassItem.select().doubleClick();
+
+ SWTBotEclipseEditor textEditor = bot.editorByTitle("PlainInput.java").toTextEditor();
+
+ // select 1st line of code as input
+ textEditor.selectLine(0);
+
+ EclipseCobiGenUtils.processCobiGenWithTextInput(bot, textEditor, "increment1");
+ EclipseCobiGenUtils.confirmSuccessfullGeneration(bot);
+
+ // check assertions
+ bot.waitUntil(new AllJobsAreFinished(), 10000);
+ IFile generationResult = project.getProject().getFile("TestOutput.txt");
+ assertThat(generationResult.exists()).isTrue();
+
+ }
+
+}
diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/common/utils/EclipseCobiGenUtils.java b/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/common/utils/EclipseCobiGenUtils.java
index d448183d18..fe255638ec 100644
--- a/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/common/utils/EclipseCobiGenUtils.java
+++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/common/utils/EclipseCobiGenUtils.java
@@ -10,6 +10,7 @@
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
+import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEclipseEditor;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
@@ -75,6 +76,20 @@ public static void processCobiGenWithExpectedError(SWTWorkbenchBot bot, SWTBotTr
processCobiGenWithExpectedError(bot, input, DEFAULT_TIMEOUT, expectedErrorTitle);
}
+ /**
+ * Tries a Generate process from a selected text as input
+ *
+ * @param bot the {@link SWTWorkbenchBot} of the test
+ * @param input input of CobiGen to be selected
+ * @param increments increments to be generated.
+ * @throws Exception test fails
+ */
+ public static void processCobiGenWithTextInput(SWTWorkbenchBot bot, SWTBotEclipseEditor input, String... increments)
+ throws Exception {
+
+ processCobiGenWithTextInput(bot, input, DEFAULT_TIMEOUT, increments);
+ }
+
/**
* Expands multi layer nodes of following format: node1>node2>finalNode
*
@@ -116,6 +131,37 @@ public static void processCobiGen(SWTWorkbenchBot bot, SWTBotTreeItem input, int
ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor());
bot.waitUntil(new AllJobsAreFinished(), defaultTimeout); // build might take some time
input.contextMenu("CobiGen").menu("Generate...").click();
+ generateWithSelectedIncrements(bot, defaultTimeout, increments);
+ }
+
+ /**
+ * Tries a Generate process from a selected text as input
+ *
+ * @param bot the {@link SWTWorkbenchBot} of the test
+ * @param input {@link SWTBotEclipseEditor} input of CobiGen to be selected
+ * @param defaultTimeout timeout to be overwritten
+ * @param increments increments to be generated.
+ * @throws Exception test fails
+ */
+ public static void processCobiGenWithTextInput(SWTWorkbenchBot bot, SWTBotEclipseEditor input, int defaultTimeout,
+ String... increments) throws Exception {
+
+ // Open generation wizard with new file as Input
+ ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor());
+ bot.waitUntil(new AllJobsAreFinished(), defaultTimeout); // build might take some time
+ input.contextMenu("CobiGen").menu("Generate...").click();
+ generateWithSelectedIncrements(bot, defaultTimeout, increments);
+ }
+
+ /**
+ * Selects provided increments and clicks on finish button afterwards
+ *
+ * @param bot the {@link SWTWorkbenchBot} of the test
+ * @param defaultTimeout timeout to be overwritten
+ * @param increments increments to be generated.
+ */
+ private static void generateWithSelectedIncrements(SWTWorkbenchBot bot, int defaultTimeout, String... increments) {
+
bot.waitUntil(new AnyShellIsActive(CobiGenDialogConstants.GenerateWizard.DIALOG_TITLE,
CobiGenDialogConstants.GenerateWizard.DIALOG_TITLE_BATCH), defaultTimeout);
diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/input/PlainInput.java b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/input/PlainInput.java
new file mode 100644
index 0000000000..fc3a6a09ec
--- /dev/null
+++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/input/PlainInput.java
@@ -0,0 +1,5 @@
+package main;
+
+public class PlainInput {
+
+}
diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/.classpath b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/.classpath
new file mode 100644
index 0000000000..0a1daddd3e
--- /dev/null
+++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/.classpath
@@ -0,0 +1,26 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/.project b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/.project
new file mode 100644
index 0000000000..29f3bb6869
--- /dev/null
+++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/.project
@@ -0,0 +1,23 @@
+
+
+ CobiGen_Templates
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+ org.eclipse.m2e.core.maven2Builder
+
+
+
+
+
+ org.eclipse.m2e.core.maven2Nature
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/logback.xml b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/logback.xml
new file mode 100644
index 0000000000..a7c9f7855e
--- /dev/null
+++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/logback.xml
@@ -0,0 +1,17 @@
+
+
+
+
+
+
+
+ %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/pom.xml b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/pom.xml
new file mode 100644
index 0000000000..1246bdb412
--- /dev/null
+++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/pom.xml
@@ -0,0 +1,24 @@
+
+
+ 4.0.0
+ com.devonfw.cobigen
+ templates-oasp
+ 1.0.0
+ jar
+ CobiGen OASP Templates
+
+
+ 1.8
+ 1.8
+
+
+
+
+ javax.ws.rs
+ javax.ws.rs-api
+ 2.0
+
+
+
+
\ No newline at end of file
diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/src/main/templates/template1/TestOutput.txt.ftl b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/src/main/templates/template1/TestOutput.txt.ftl
new file mode 100644
index 0000000000..793aa682b0
--- /dev/null
+++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/src/main/templates/template1/TestOutput.txt.ftl
@@ -0,0 +1 @@
+This is a test
\ No newline at end of file
diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/src/main/templates/template1/context.xml b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/src/main/templates/template1/context.xml
new file mode 100644
index 0000000000..b5774d7a5d
--- /dev/null
+++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/src/main/templates/template1/context.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/src/main/templates/template1/templates.xml b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/src/main/templates/template1/templates.xml
new file mode 100644
index 0000000000..ab128701e7
--- /dev/null
+++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/GenerateFromTextInputTest/templates/src/main/templates/template1/templates.xml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/PlatformUIUtil.java b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/PlatformUIUtil.java
index 41ea49d1b8..1106de09ce 100644
--- a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/PlatformUIUtil.java
+++ b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/PlatformUIUtil.java
@@ -26,6 +26,31 @@
*/
public class PlatformUIUtil {
+ /**
+ * The active {@link IWorkbenchWindow}
+ */
+ private static IWorkbenchWindow workbenchWindow;
+
+ /**
+ * Returns the active {@link IWorkbenchWindow}
+ *
+ * @return {@link IWorkbenchWindow}
+ */
+ private static IWorkbenchWindow getWorkbenchWindow() {
+
+ return workbenchWindow;
+ }
+
+ /**
+ * Sets the active {@link IWorkbenchWindow}
+ *
+ * @param workbenchWindow new value of {@link #getworkbenchWindow}.
+ */
+ private static void setWorkbenchWindow(IWorkbenchWindow workbenchWindow) {
+
+ PlatformUIUtil.workbenchWindow = workbenchWindow;
+ }
+
/**
* Waits for the {@link IWorkbench} to appear and returns it
*
@@ -46,11 +71,13 @@ public static IWorkbench getWorkbench() {
*/
public static IWorkbenchWindow getActiveWorkbenchWindow() {
- IWorkbench workbench = getWorkbench();
- IWorkbenchWindow workbenchWindow;
- while ((workbenchWindow = workbench.getActiveWorkbenchWindow()) == null) {
+ getWorkbench().getDisplay().syncExec(() -> {
+ setWorkbenchWindow(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
+ });
+
+ while (getWorkbenchWindow() == null) {
}
- return workbenchWindow;
+ return getWorkbenchWindow();
}
/**
@@ -60,9 +87,9 @@ public static IWorkbenchWindow getActiveWorkbenchWindow() {
*/
public static IWorkbenchPage getActiveWorkbenchPage() {
- IWorkbenchWindow workbenchWindow = getActiveWorkbenchWindow();
+ IWorkbenchWindow activeWorkbenchWindow = getActiveWorkbenchWindow();
IWorkbenchPage page;
- while ((page = workbenchWindow.getActivePage()) == null) {
+ while ((page = activeWorkbenchWindow.getActivePage()) == null) {
}
return page;
}
diff --git a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/generator/GeneratorWrapperFactory.java b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/generator/GeneratorWrapperFactory.java
index 5b20488e86..16a09f1086 100644
--- a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/generator/GeneratorWrapperFactory.java
+++ b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/generator/GeneratorWrapperFactory.java
@@ -20,7 +20,8 @@
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Display;
-import org.eclipse.ui.IFileEditorInput;
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.part.FileEditorInput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -114,15 +115,18 @@ private static List