Skip to content

Commit 4c5cfab

Browse files
devonfw#1509 implemented testAdaptTemplateSetsAndGenerate and modify testBasicOpenAPIGenerationWithAdaptMonolithicTemplates
1 parent 096f2ef commit 4c5cfab

File tree

7 files changed

+138
-105
lines changed

7 files changed

+138
-105
lines changed

cobigen-eclipse/cobigen-eclipse-test/pom.xml

+50
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<properties>
1414
<tycho.testArgLine.enriched>${surefireArgLineIntegration} -Xms512m -Xmx2048m -XX:+DisableExplicitGC</tycho.testArgLine.enriched>
1515
<enforcer.skip>true</enforcer.skip>
16+
<maven.test.AdaptTemplatesTest1>src\main\resources\AdaptTemplatesTest\template-sets\adapted\crud-java-server-app</maven.test.AdaptTemplatesTest1>
17+
<maven.test.AdaptTemplatesTest2>src\main\resources\AdaptTemplatesTest\template-sets\adapted\crud-java-server-app-complex</maven.test.AdaptTemplatesTest2>
18+
1619
</properties>
1720

1821
<dependencies>
@@ -58,6 +61,53 @@
5861
</execution>
5962
</executions>
6063
</plugin>
64+
65+
<!-- Building a Maven project that is used on the tests -->
66+
<plugin>
67+
<groupId>org.codehaus.mojo</groupId>
68+
<artifactId>exec-maven-plugin</artifactId>
69+
<version>3.0.0</version>
70+
<executions>
71+
<execution>
72+
<id>Install test adaptTemplatesTests1</id>
73+
<phase>test-compile</phase>
74+
<goals>
75+
<goal>exec</goal>
76+
</goals>
77+
<configuration>
78+
<executable>mvn</executable>
79+
<workingDirectory>${maven.test.AdaptTemplatesTest1}</workingDirectory>
80+
<arguments>
81+
<argument>install</argument>
82+
<!-- https://stackoverflow.com/a/66801171 -->
83+
<argument>-Djansi.force=true</argument>
84+
<argument>-Djansi.passthrough=true</argument>
85+
<argument>-B</argument>
86+
<argument>-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn</argument>
87+
</arguments>
88+
</configuration>
89+
</execution>
90+
<execution>
91+
<id>Install test adaptTemplatesTests2</id>
92+
<phase>test-compile</phase>
93+
<goals>
94+
<goal>exec</goal>
95+
</goals>
96+
<configuration>
97+
<executable>mvn</executable>
98+
<workingDirectory>${maven.test.AdaptTemplatesTest2}</workingDirectory>
99+
<arguments>
100+
<argument>install</argument>
101+
<!-- https://stackoverflow.com/a/66801171 -->
102+
<argument>-Djansi.force=true</argument>
103+
<argument>-Djansi.passthrough=true</argument>
104+
<argument>-B</argument>
105+
<argument>-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn</argument>
106+
</arguments>
107+
</configuration>
108+
</execution>
109+
</executions>
110+
</plugin>
61111
<plugin>
62112
<groupId>org.apache.maven.plugins</groupId>
63113
<artifactId>maven-clean-plugin</artifactId>

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

+25-13
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public class AdaptTemplatesTest extends SystemTest {
4848
/** Line separator, e.g. for windows '\r\n' */
4949
public static final String LINE_SEPARATOR = java.lang.System.getProperty("line.separator");
5050

51+
/* Root path for templateSets jar */
52+
private static final String testResource = "src/main/resources/AdaptTemplatesTest/template-sets/downloaded";
53+
5154
/**
5255
* Setup workbench appropriately for tests
5356
*
@@ -116,17 +119,25 @@ public void testAdaptTemplatesAndImportIntoEclipse() throws Exception {
116119

117120
EclipseCobiGenUtils.runAndCaptureAdaptTemplatesSets(bot);
118121

119-
EclipseUtils.openErrorsTreeInProblemsView(bot);
122+
// EclipseUtils.updateMavenProject(bot, ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME);
120123

121124
// expand the new file in the package explorer
122125
SWTBotView view = bot.viewById(JavaUI.ID_PACKAGES);
123126
SWTBotTreeItem javaClassItem = view.bot().tree().expandNode(testProjName, "adapt-templates.yml");
124127
javaClassItem.select();
125128

129+
// execute CobiGen
130+
EclipseCobiGenUtils.processCobiGen(bot, javaClassItem, 25000, "CRUD devon4j Server>CRUD REST services");
131+
bot.waitUntil(new AllJobsAreFinished(), 10000);
132+
// increase timeout as the openAPI parser is slow on initialization
133+
EclipseCobiGenUtils.confirmSuccessfullGeneration(bot, 40000);
134+
bot.waitUntil(new AllJobsAreFinished(), 10000);
135+
136+
// EclipseUtils.openErrorsTreeInProblemsView(bot);
137+
126138
IProject generatorProjOfTempltesSets = ResourcesPlugin.getWorkspace().getRoot()
127139
.getProject(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME);
128-
bot.waitUntil(new AllJobsAreFinished(), 10000000);
129-
IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject(testProjName);
140+
bot.waitUntil(new AllJobsAreFinished(), 10000);
130141

131142
Path adaptedFolder = Paths.get(generatorProjOfTempltesSets.getLocationURI())
132143
.resolve(ResourceConstants.TEMPLATE_SETS_ADAPTED);
@@ -151,7 +162,11 @@ public void testBasicOpenAPIGenerationWithAdaptMonolithicTemplates() throws Exce
151162
project.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
152163
this.tmpMavenProjectRule.updateProject();
153164

154-
EclipseCobiGenUtils.runAndCaptureUpdateTemplates(bot);
165+
// retrieve CobiGen home directory (overwritten through environment variables)
166+
File templatesDirectory = CobiGenPaths.getTemplatesFolderPath().toFile();
167+
// create templates directory as this should be present in this scenario
168+
TemplatesJarUtil.downloadLatestDevon4jTemplates(true, templatesDirectory);
169+
TemplatesJarUtil.downloadLatestDevon4jTemplates(false, templatesDirectory);
155170
EclipseCobiGenUtils.runAndCaptureAdaptTemplates(bot);
156171
EclipseUtils.updateMavenProject(bot, ResourceConstants.CONFIG_PROJECT_NAME);
157172

@@ -184,7 +199,7 @@ public void testBasicOpenAPIGenerationWithAdaptMonolithicTemplates() throws Exce
184199
@Test
185200
public void testAdaptMonolithicTemplatesAndGenerate() throws Exception {
186201

187-
File tmpProject = this.tempFolder.newFolder("playground", "project", "templates");
202+
File tmpProject = this.tempFolder.newFolder("playground", "project");
188203
withEnvironmentVariable("COBIGEN_HOME", tmpProject.toPath().toString())
189204
.execute(() -> testBasicOpenAPIGenerationWithAdaptMonolithicTemplates());
190205
}
@@ -210,22 +225,19 @@ public void testAdaptTemplateSetsAndGenerate() throws Exception {
210225
.execute(() -> testBasicOpenAPIGenerationWithAdaptTemplateSets());
211226
}
212227

213-
/*
214-
*
228+
/**
215229
* Test adaption of template-sets/downloaded/.jar files and importing the project into Eclipse
216230
*
231+
* @throws Exception
217232
*/
218233
@Test
219234
public void testAdaptTemplateSetsAndImport() throws Exception {
220235

221-
Path devTemplatesPath = new File(
222-
AdaptTemplatesTest.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile()
223-
.getParentFile().toPath().resolve("cobigen-templates").resolve("crud-openapi-java-server-app")
224-
.resolve("target").resolve("crud-openapi-java-server-app-2021.12.007-SNAPSHOT.jar");
225-
226236
File tmpProject = this.tempFolder.newFolder("playground", "project");
227237
File downloaded = this.tempFolder.newFolder("playground", "project", "template-sets", "downloaded");
228-
FileUtils.copyFileToDirectory(devTemplatesPath.toFile(), downloaded);
238+
239+
FileUtils.copyDirectory(new File(testResource), downloaded);
240+
229241
withEnvironmentVariable("COBIGEN_HOME", tmpProject.toPath().toString())
230242
.execute(() -> testAdaptTemplatesAndImportIntoEclipse());
231243

cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/common/utils/EclipseCobiGenUtils.java

+5
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,11 @@ public static void runAndCaptureAdaptTemplatesSets(SWTWorkbenchBot bot) throws E
415415

416416
SWTBotView view = bot.viewById(JavaUI.ID_PACKAGES);
417417
view.bot().tree().contextMenu("CobiGen").menu("Adapt Templates...").click();
418+
419+
SWTBotShell informationDialog = bot.shell("Information");
420+
bot.waitUntil(new AnyShellIsActive("Information"), DEFAULT_TIMEOUT);
421+
takeScreenshot(bot, "Adapt Templates Information");
422+
informationDialog.bot().button("Ok").click();
418423
}
419424

420425
/**

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

+8-37
Original file line numberDiff line numberDiff line change
@@ -106,46 +106,17 @@ public static void refreshConfigurationProject() {
106106
*/
107107
public static IProject getGeneratorConfigurationProject() throws GeneratorProjectNotExistentException, CoreException {
108108

109-
Path templateSetDirectory = getTemplateSetDirectory();
110-
111-
if (!Files.exists(templateSetDirectory)) {
112-
File templatesDirectory = getTemplatesDirectory();
109+
generatorProj = ResourcesPlugin.getWorkspace().getRoot()
110+
.getProject(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME);
111+
if (generatorProj.exists()) {
112+
return generatorProj;
113+
} else {
113114
generatorProj = ResourcesPlugin.getWorkspace().getRoot().getProject(ResourceConstants.CONFIG_PROJECT_NAME);
114-
115-
if (!generatorProj.exists()) {
116-
if (!isUpdateDialogShown) {
117-
if (templatesDirectory.exists()) {
118-
Path jarFilePath = TemplatesJarUtil.getJarFile(false, templatesDirectory.toPath());
119-
// If we don't find at least one jar, then we do need to download new templates
120-
if (jarFilePath == null || !Files.exists(jarFilePath)) {
121-
int result = createUpdateTemplatesDialog();
122-
isUpdateDialogShown = true;
123-
if (result == 1) {
124-
// User does not want to download templates.
125-
userWantsToDownloadTemplates = false;
126-
} else {
127-
userWantsToDownloadTemplates = true;
128-
}
129-
}
130-
131-
} else {
132-
int result = createUpdateTemplatesDialog();
133-
isUpdateDialogShown = true;
134-
if (result == 1) {
135-
// User does not want to download templates.
136-
userWantsToDownloadTemplates = false;
137-
} else {
138-
userWantsToDownloadTemplates = true;
139-
}
140-
}
141-
}
142-
}
143-
if (userWantsToDownloadTemplates) {
115+
if (generatorProj.exists()) {
144116
return generatorProj;
145-
} else {
146-
return null;
147117
}
148118
}
119+
149120
return null;
150121
}
151122

@@ -265,7 +236,7 @@ public static void processJar(String fileName) throws MalformedURLException, IOE
265236
}
266237

267238
try {
268-
TemplateAdapter templateAdapter = new TemplateAdapterImpl(templateDirectory);
239+
TemplateAdapter templateAdapter = new TemplateAdapterImpl(null);
269240
templateAdapter.adaptMonolithicTemplates(templateDirectory.resolve(ConfigurationConstants.COBIGEN_TEMPLATES),
270241
false);
271242
} catch (Exception e) {

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

+48-44
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import java.io.File;
44
import java.io.IOException;
5-
import java.net.MalformedURLException;
65
import java.nio.file.Files;
76
import java.nio.file.Path;
87
import java.util.Comparator;
8+
import java.util.List;
99
import java.util.UUID;
1010

1111
import org.eclipse.core.commands.AbstractHandler;
@@ -27,11 +27,15 @@
2727
import org.slf4j.LoggerFactory;
2828
import org.slf4j.MDC;
2929

30+
import com.devonfw.cobigen.api.TemplateAdapter;
31+
import com.devonfw.cobigen.api.constants.ConfigurationConstants;
32+
import com.devonfw.cobigen.api.exception.TemplateSelectionForAdaptionException;
33+
import com.devonfw.cobigen.api.exception.UpgradeTemplatesNotificationException;
3034
import com.devonfw.cobigen.eclipse.common.constants.InfrastructureConstants;
3135
import com.devonfw.cobigen.eclipse.common.constants.external.ResourceConstants;
3236
import com.devonfw.cobigen.eclipse.common.tools.ExceptionHandler;
33-
import com.devonfw.cobigen.eclipse.common.tools.PlatformUIUtil;
3437
import com.devonfw.cobigen.eclipse.common.tools.ResourcesPluginUtil;
38+
import com.devonfw.cobigen.impl.adapter.TemplateAdapterImpl;
3539

3640
/**
3741
* Handler for the Package-Explorer EventfimportProjectIntoWorkspace
@@ -53,31 +57,23 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
5357

5458
MDC.put(InfrastructureConstants.CORRELATION_ID, UUID.randomUUID().toString());
5559
IProject generatorProj = ResourcesPlugin.getWorkspace().getRoot().getProject(ResourceConstants.CONFIG_PROJECT_NAME);
56-
IProject generatorProjOfTempltesSets = ResourcesPlugin.getWorkspace().getRoot()
57-
.getProject(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME);
58-
Path templateSet = ResourcesPluginUtil.getTemplateSetDirectory();
59-
60-
if (generatorProjOfTempltesSets.exists()) {
61-
// 1. the project is imported so, do not adapt(no need to extract the jar files) For now nothing to do.
62-
63-
} else if (Files.exists(templateSet)) {
6460

65-
// 2. downloaded exists? first adapt the jar file (files), then import project to eclipse
66-
67-
Path downloadedPath = templateSet.resolve(ResourceConstants.TEMPLATE_SETS_DOWNLOADED);
68-
Path adaptedPath = templateSet.resolve(ResourceConstants.TEMPLATE_SETS_ADAPTED);
61+
Path templateSet = ResourcesPluginUtil.getTemplateSetDirectory();
62+
File template = ResourcesPluginUtil.getTemplatesDirectory();
6963

70-
// A. adapt the jar files if not already adapted
71-
if (Files.exists(downloadedPath) && !Files.exists(adaptedPath))
72-
ResourcesPluginUtil.adaptTemplateSet(templateSet);
64+
TemplateAdapter templateAdapter;
7365

74-
// B. Import the project
75-
importProjectIntoWorkspace(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME, templateSet);
76-
/*
77-
* 3. TODO downloaded does not exists? update command must be executed. then go to 2. // (step 3 can be ignored
78-
* for now until the new template-sets are deployed online.)
79-
*/
66+
if (Files.exists(templateSet)) {
67+
templateAdapter = new TemplateAdapterImpl(templateSet);
68+
} else if (template.exists()) {
69+
templateAdapter = new TemplateAdapterImpl(template.toPath());
8070
} else {
71+
LOG.debug("No configuration project was found");
72+
return null;
73+
}
74+
try {
75+
templateAdapter.adaptTemplates();
76+
8177
if (generatorProj.exists()) {
8278
MessageDialog dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Info!", null,
8379
"CobiGen_Templates folder is already imported, click on Update templates button to update with latest. ",
@@ -94,40 +90,48 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
9490

9591
if (result == 0) {
9692
try {
97-
String fileName = ResourcesPluginUtil.getJarPath(true);
98-
if (fileName.equals("")) {
99-
result = createUpdateTemplatesDialog();
100-
if (result == 1) {
101-
MessageDialog.openWarning(Display.getDefault().getActiveShell(), "Warning",
102-
"Templates have not been found, please download them!");
103-
throw new NullPointerException("Templates have not been found!");
104-
} else {
105-
fileName = ResourcesPluginUtil.downloadJar(true);
106-
}
107-
108-
}
109-
ResourcesPluginUtil.processJar(fileName);
110-
11193
importProjectIntoWorkspace(ResourceConstants.CONFIG_PROJECT_NAME,
11294
ResourcesPluginUtil.getTemplatesDirectory().toPath());
11395
dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Information", null,
11496
"CobiGen_Templates folder is imported sucessfully", MessageDialog.INFORMATION, new String[] { "Ok" },
11597
1);
11698
dialog.setBlockOnOpen(true);
11799
dialog.open();
118-
} catch (MalformedURLException e) {
119-
LOG.error("An exception with download url of maven central", e);
120-
PlatformUIUtil.openErrorDialog("An exception with download url of maven central", e);
121-
} catch (IOException e) {
122-
LOG.error("An exception occurred while writing Jar files to .metadata folder", e);
123-
PlatformUIUtil.openErrorDialog("An exception occurred while writing Jar files to .metadata folder", e);
124100
} catch (Throwable e) {
125101
ExceptionHandler.handle(e, HandlerUtil.getActiveShell(event));
126102
}
127103
}
128104
MDC.remove(InfrastructureConstants.CORRELATION_ID);
129105
}
106+
107+
} catch (TemplateSelectionForAdaptionException e) {
108+
List<Path> templateJars = e.getTemplateSets();
109+
if (!templateJars.isEmpty()) {
110+
111+
try {
112+
// Adapt process
113+
templateAdapter.adaptTemplateSets(templateJars, false);
114+
115+
// B. Import into Eclipse
116+
importProjectIntoWorkspace(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME, templateSet);
117+
MessageDialog dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Information", null,
118+
"CobiGen_Templates folder is imported sucessfully", MessageDialog.INFORMATION, new String[] { "Ok" }, 1);
119+
dialog.setBlockOnOpen(true);
120+
dialog.open();
121+
122+
} catch (IOException e1) {
123+
LOG.debug("Adapt was not possible", e1);
124+
}
125+
126+
} else {
127+
LOG.info("No template set jars found to extract.");
128+
}
129+
} catch (IOException e) {
130+
LOG.debug("Error occur", e);
131+
} catch (UpgradeTemplatesNotificationException e) {
132+
LOG.debug("Upgrade Process", e);
130133
}
134+
131135
return null;
132136

133137
}
@@ -150,7 +154,7 @@ private void importProjectIntoWorkspace(String projectName, Path projectPath) {
150154
if (projectName == ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME) {
151155
description.setLocationURI(projectPath.toUri());
152156
} else {
153-
description.setLocation(new org.eclipse.core.runtime.Path(this.ws.toPortableString() + "/" + projectName));
157+
description.setLocationURI(projectPath.resolve(ConfigurationConstants.COBIGEN_TEMPLATES).toUri());
154158
}
155159
project.create(description, null);
156160

cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/CobiGenPaths.java

+2-10
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,9 @@ public static Path getTemplatesFolderPath() {
9393
*/
9494
public static Path getTemplatesFolderPath(Path home) {
9595

96-
File file = new File(home.toString());
97-
int lastSlash = file.getName().lastIndexOf("/");
98-
String lastWord = file.getName().substring(lastSlash + 1);
99-
if (lastWord.equals(ConfigurationConstants.TEMPLATES_FOLDER)) {
100-
return home;
101-
} else {
102-
home = home.resolve(ConfigurationConstants.TEMPLATES_FOLDER);
103-
104-
}
96+
Path templatesPath = home.resolve(ConfigurationConstants.TEMPLATES_FOLDER);
10597

106-
return home;
98+
return createFolder(templatesPath);
10799
}
108100

109101
/**

0 commit comments

Comments
 (0)