Skip to content

Commit

Permalink
1551 merge master into template_set_deployables (#1552)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jan-vcapgemini authored Jul 5, 2022
1 parent b5a2577 commit 7b6bf79
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main;

public class PlainInput {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>CobiGen_Templates</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- This file is for logback classic. The file contains the configuration
for sl4j logging -->
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- defines the level of logging -->
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>

<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>templates-oasp</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>CobiGen OASP Templates</name>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<contextConfiguration xmlns="http://capgemini.com/devonfw/cobigen/ContextConfiguration" version="2.2">
<trigger id="test" type="java">
<matcher type="fqn" value=".*">
</matcher>
</trigger>
</contextConfiguration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<templatesConfiguration xmlns="http://capgemini.com/devonfw/cobigen/TemplatesConfiguration" version="2.1">
<templates>
<template name="t1" destinationPath="TestOutput.txt" templateFile="TestOutput.txt.ftl"/>
</templates>
<increments>
<increment name="i1" description="increment1">
<templateRef ref="t1"/>
</increment>
</increments>
</templatesConfiguration>
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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();
}

/**
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -114,15 +115,18 @@ private static List<Object> extractValidEclipseInputs(ISelection selection) thro

// When the user is selecting text from the text editor
if (selection instanceof ITextSelection) {
IFileEditorInput iEditorInput = (IFileEditorInput) PlatformUIUtil.getActiveWorkbenchPage().getActiveEditor()
.getEditorInput();

IFile iFile = iEditorInput.getFile();
iJavaElem = JavaCore.create(iFile);
if (iJavaElem instanceof ICompilationUnit) {
inputObjects.add(iJavaElem);
} else {
inputObjects.add(iFile);
IEditorInput iEditorInput = PlatformUIUtil.getActiveWorkbenchPage().getActiveEditor().getEditorInput();

if (iEditorInput instanceof FileEditorInput) {

IFile iFile = ((FileEditorInput) iEditorInput).getFile();
iJavaElem = JavaCore.create(iFile);
if (iJavaElem instanceof ICompilationUnit) {
inputObjects.add(iJavaElem);
} else {
inputObjects.add(iFile);
}
}
}

Expand Down

0 comments on commit 7b6bf79

Please sign in to comment.