Skip to content

Commit 79296a0

Browse files
author
MansourD
committed
devonfw#1509 New tests for Adaption in Eclipse
1 parent ee7c05f commit 79296a0

File tree

4 files changed

+148
-15
lines changed

4 files changed

+148
-15
lines changed

cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java

+119-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import static org.assertj.core.api.Assertions.assertThat;
55

66
import java.io.File;
7+
import java.nio.file.Files;
78
import java.nio.file.Path;
9+
import java.nio.file.Paths;
810

911
import org.apache.commons.io.FileUtils;
1012
import org.eclipse.core.resources.IFile;
@@ -17,10 +19,12 @@
1719
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
1820
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
1921
import org.junit.BeforeClass;
22+
import org.junit.Ignore;
2023
import org.junit.Rule;
2124
import org.junit.Test;
2225
import org.junit.rules.TemporaryFolder;
2326

27+
import com.devonfw.cobigen.eclipse.common.constants.external.ResourceConstants;
2428
import com.devonfw.cobigen.eclipse.test.common.SystemTest;
2529
import com.devonfw.cobigen.eclipse.test.common.swtbot.AllJobsAreFinished;
2630
import com.devonfw.cobigen.eclipse.test.common.utils.EclipseCobiGenUtils;
@@ -58,7 +62,7 @@ public static void setupClass() throws Exception {
5862
*
5963
* @throws Exception test fails
6064
*/
61-
public void testBasicOpenAPIGenerationWithAdaptTemplates() throws Exception {
65+
public void testBasicOpenAPIGenerationWithAdaptTemplateSets() throws Exception {
6266

6367
// copy sample project to external location and import it into the workspace
6468
String testProjName = "ExtTestProj";
@@ -94,12 +98,123 @@ public void testBasicOpenAPIGenerationWithAdaptTemplates() throws Exception {
9498
}
9599

96100
/**
101+
* Test for external projects (not in workspace) taken as input
102+
*
103+
* @throws Exception test fails
104+
*/
105+
public void testAdaptTemplatesAndImportIntoEclipse() throws Exception {
106+
107+
// copy sample project to external location and import it into the workspace
108+
String testProjName = "ExtTestProj";
109+
IJavaProject project = this.tmpMavenProjectRule.createProject(testProjName);
110+
FileUtils.copyFile(new File(resourcesRootPath + "input/adapt-templates.yml"),
111+
project.getUnderlyingResource().getLocation().append("adapt-templates.yml").toFile());
112+
project.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
113+
this.tmpMavenProjectRule.updateProject();
114+
115+
EclipseCobiGenUtils.runAndCaptureAdaptTemplatesSets(bot);
116+
117+
EclipseUtils.openErrorsTreeInProblemsView(bot);
118+
119+
// expand the new file in the package explorer
120+
SWTBotView view = bot.viewById(JavaUI.ID_PACKAGES);
121+
SWTBotTreeItem javaClassItem = view.bot().tree().expandNode(testProjName, "adapt-templates.yml");
122+
javaClassItem.select();
123+
124+
IProject generatorProjOfTempltesSets = ResourcesPlugin.getWorkspace().getRoot()
125+
.getProject(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME);
126+
bot.waitUntil(new AllJobsAreFinished(), 10000000);
127+
IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject(testProjName);
128+
129+
Path adaptedFolder = Paths.get(generatorProjOfTempltesSets.getLocationURI())
130+
.resolve(ResourceConstants.TEMPLATE_SETS_ADAPTED);
131+
132+
assertThat(generatorProjOfTempltesSets.exists()).isTrue();
133+
assertThat(Files.exists(adaptedFolder)).isTrue();
134+
135+
}
136+
137+
/**
138+
* Test for external projects (not in workspace) taken as input for generation
139+
*
140+
* @throws Exception test fails
141+
*/
142+
public void testBasicOpenAPIGenerationWithAdaptMonolithicTemplates() throws Exception {
143+
144+
// copy sample project to external location and import it into the workspace
145+
String testProjName = "ExtTestProj";
146+
IJavaProject project = this.tmpMavenProjectRule.createProject(testProjName);
147+
FileUtils.copyFile(new File(resourcesRootPath + "input/adapt-templates.yml"),
148+
project.getUnderlyingResource().getLocation().append("adapt-templates.yml").toFile());
149+
project.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
150+
this.tmpMavenProjectRule.updateProject();
151+
152+
EclipseCobiGenUtils.runAndCaptureUpdateTemplates(bot);
153+
EclipseCobiGenUtils.runAndCaptureAdaptTemplates(bot);
154+
EclipseUtils.updateMavenProject(bot, ResourceConstants.CONFIG_PROJECT_NAME);
155+
156+
EclipseUtils.openErrorsTreeInProblemsView(bot);
157+
158+
// expand the new file in the package explorer
159+
SWTBotView view = bot.viewById(JavaUI.ID_PACKAGES);
160+
SWTBotTreeItem javaClassItem = view.bot().tree().expandNode(testProjName, "adapt-templates.yml");
161+
javaClassItem.select();
162+
163+
// execute CobiGen
164+
EclipseCobiGenUtils.processCobiGen(bot, javaClassItem, 25000, "CRUD devon4j Server>CRUD REST services");
165+
bot.waitUntil(new AllJobsAreFinished(), 10000);
166+
// increase timeout as the openAPI parser is slow on initialization
167+
EclipseCobiGenUtils.confirmSuccessfullGeneration(bot, 40000);
168+
169+
bot.waitUntil(new AllJobsAreFinished(), 10000);
170+
IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject(testProjName);
171+
IFile generationResult = proj.getFile(
172+
"src/main/java/com/devonfw/test/sampledatamanagement/service/impl/rest/SampledatamanagementRestServiceImpl.java");
173+
174+
assertThat(generationResult.exists()).isTrue();
175+
}
176+
177+
/*
178+
*
97179
* Test of testBasicOpenAPIGenerationWithAdaptTemplates with custom COBIGEN_HOME environment variable
98180
*
181+
*/
182+
@Test
183+
public void testAdaptMonolithicTemplatesAndGenerate() throws Exception {
184+
185+
File tmpProject = this.tempFolder.newFolder("playground", "project");
186+
withEnvironmentVariable("COBIGEN_HOME", tmpProject.toPath().toString())
187+
.execute(() -> testBasicOpenAPIGenerationWithAdaptMonolithicTemplates());
188+
}
189+
190+
/**
191+
* TODO Test of testBasicOpenAPIGenerationWithAdaptTemplates with custom template-sets after adapt the jar files
192+
*
99193
* @throws Exception test fails
100194
*/
101195
@Test
102-
public void testAdaptTemplatesAndGenerate() throws Exception {
196+
@Ignore
197+
public void testAdaptTemplateSetsAndGenerate() throws Exception {
198+
199+
Path devTemplatesPath = new File(
200+
AdaptTemplatesTest.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile()
201+
.getParentFile().toPath().resolve("cobigen-templates").resolve("crud-openapi-java-server-app")
202+
.resolve("target").resolve("crud-openapi-java-server-app-2021.12.007-SNAPSHOT.jar");
203+
204+
File tmpProject = this.tempFolder.newFolder("playground", "project");
205+
File downloaded = this.tempFolder.newFolder("playground", "project", "template-sets", "downloaded");
206+
FileUtils.copyFileToDirectory(devTemplatesPath.toFile(), downloaded);
207+
withEnvironmentVariable("COBIGEN_HOME", tmpProject.toPath().toString())
208+
.execute(() -> testBasicOpenAPIGenerationWithAdaptTemplateSets());
209+
}
210+
211+
/*
212+
*
213+
* Test adaption of template-sets/downloaded/.jar files and importing the project into Eclipse
214+
*
215+
*/
216+
@Test
217+
public void testAdaptTemplateSetsAndImport() throws Exception {
103218

104219
Path devTemplatesPath = new File(
105220
AdaptTemplatesTest.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile()
@@ -110,6 +225,7 @@ public void testAdaptTemplatesAndGenerate() throws Exception {
110225
File downloaded = this.tempFolder.newFolder("playground", "project", "template-sets", "downloaded");
111226
FileUtils.copyFileToDirectory(devTemplatesPath.toFile(), downloaded);
112227
withEnvironmentVariable("COBIGEN_HOME", tmpProject.toPath().toString())
113-
.execute(() -> testBasicOpenAPIGenerationWithAdaptTemplates());
228+
.execute(() -> testAdaptTemplatesAndImportIntoEclipse());
229+
114230
}
115231
}

cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/constants/external/ResourceConstants.java

+10
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,14 @@ public class ResourceConstants {
1515
*/
1616
public static final String TEMPLATE_SETS_CONFIG_PROJECT_NAME = "template-sets";
1717

18+
/**
19+
* Name of template-sets/downloaded folder
20+
*/
21+
public static final String TEMPLATE_SETS_DOWNLOADED = "downloaded";
22+
23+
/**
24+
* Name of template-sets/adapted folder
25+
*/
26+
public static final String TEMPLATE_SETS_ADAPTED = "adapted";
27+
1828
}

cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,10 @@ public static void adaptTemplateSet(Path templateSetsLocation) {
285285

286286
try {
287287
TemplateAdapter templateAdapter = new TemplateAdapterImpl(templateSetsLocation);
288-
List<Path> jarFilePath = TemplatesJarUtil.getJarFiles(templateSetsLocation.resolve("downloaded"));
289-
templateAdapter.adaptTemplateSets(jarFilePath, templateSetsLocation.resolve("adapted"), false);
288+
List<Path> jarFilePath = TemplatesJarUtil
289+
.getJarFiles(templateSetsLocation.resolve(ResourceConstants.TEMPLATE_SETS_DOWNLOADED));
290+
templateAdapter.adaptTemplateSets(jarFilePath,
291+
templateSetsLocation.resolve(ResourceConstants.TEMPLATE_SETS_ADAPTED), false);
290292

291293
} catch (Exception e) {
292294
LOG.error("An exception occurred while processing Jar files to create CobiGen_Templates folder", e);

cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,15 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
6464

6565
// 2. downloaded exists? first adapt the jar file (files), then import project to eclipse
6666

67-
Path downloadedPath = templateSet.resolve("downloaded");
68-
Path adaptedPath = templateSet.resolve("adapted");
67+
Path downloadedPath = templateSet.resolve(ResourceConstants.TEMPLATE_SETS_DOWNLOADED);
68+
Path adaptedPath = templateSet.resolve(ResourceConstants.TEMPLATE_SETS_ADAPTED);
6969

7070
// A. adapt the jar files if not already adapted
7171
if (Files.exists(downloadedPath) && !Files.exists(adaptedPath))
7272
ResourcesPluginUtil.adaptTemplateSet(templateSet);
7373

74-
// B. TODO Import the project
74+
// B. Import the project
75+
importProjectIntoWorkspace(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME, templateSet);
7576
/*
7677
* 3. TODO downloaded does not exists? update command must be executed. then go to 2. // (step 3 can be ignored
7778
* for now until the new template-sets are deployed online.)
@@ -107,7 +108,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
107108
}
108109
ResourcesPluginUtil.processJar(fileName);
109110

110-
importProjectIntoWorkspace();
111+
importProjectIntoWorkspace(ResourceConstants.CONFIG_PROJECT_NAME, null);
111112
dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Information", null,
112113
"CobiGen_Templates folder is imported sucessfully", MessageDialog.INFORMATION, new String[] { "Ok" },
113114
1);
@@ -133,7 +134,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
133134
/**
134135
* CobiGen_Templates folder created at main folder using source jar will be imported into workspace
135136
*/
136-
private void importProjectIntoWorkspace() {
137+
private void importProjectIntoWorkspace(String projectName, Path projectPath) {
137138

138139
ProgressMonitorDialog progressMonitor = new ProgressMonitorDialog(Display.getDefault().getActiveShell());
139140

@@ -143,26 +144,30 @@ private void importProjectIntoWorkspace() {
143144
progressMonitor.open();
144145
progressMonitor.getProgressMonitor().beginTask("Importing templates...", 0);
145146
try {
146-
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(ResourceConstants.CONFIG_PROJECT_NAME);
147+
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
147148
IProjectDescription description = ResourcesPlugin.getWorkspace().newProjectDescription(project.getName());
148-
description.setLocation(new org.eclipse.core.runtime.Path(this.ws.toPortableString() + "/CobiGen_Templates"));
149+
if (projectName == ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME) {
150+
description.setLocationURI(projectPath.toUri());
151+
} else {
152+
description.setLocation(new org.eclipse.core.runtime.Path(this.ws.toPortableString() + "/" + projectName));
153+
}
149154
project.create(description, null);
150155

151156
// We set the current project to be converted to a Maven project
152157
ISelection sel = new StructuredSelection(project);
153158
mavenConverter.selectionChanged(null, sel);
154159

155160
project.open(null);
156-
157161
// Converts the current project to a Maven project
158-
mavenConverter.run(null);
162+
if (projectName == ResourceConstants.CONFIG_PROJECT_NAME)
163+
mavenConverter.run(null);
159164
progressMonitor.close();
160165

161166
} catch (CoreException e) {
162167
progressMonitor.close();
163168
e.printStackTrace();
164169
MessageDialog.openWarning(Display.getDefault().getActiveShell(), "Warning",
165-
"Some Exception occurred while importing CobiGen_Templates into workspace");
170+
"Some Exception occurred while importing " + projectName + " into workspace");
166171
}
167172
}
168173

0 commit comments

Comments
 (0)