From 2650c6fe060b18811d5c264c775c89a70b84995b Mon Sep 17 00:00:00 2001 From: Cedric Betom Date: Wed, 18 Jan 2023 12:46:43 +0100 Subject: [PATCH 01/10] #1509 Restored original code for adaptTemplatesAndGenerate Test #1622 --- .../eclipse/test/AdaptTemplatesTest.java | 143 +++++++++--------- .../common/utils/EclipseCobiGenUtils.java | 8 +- 2 files changed, 77 insertions(+), 74 deletions(-) diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java b/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java index f1d6e334a7..92a0ace483 100644 --- a/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java +++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java @@ -6,6 +6,7 @@ import java.io.File; import org.apache.commons.io.FileUtils; +import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.ResourcesPlugin; @@ -19,9 +20,9 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; -import com.devonfw.cobigen.api.constants.ConfigurationConstants; 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; @@ -31,71 +32,77 @@ */ public class AdaptTemplatesTest extends SystemTest { - /** Temporary files rule to create temporary folders or files */ - @Rule - public TemporaryFolder tempFolder = new TemporaryFolder(); - - /** Root path of the Test Resources */ - private static final String resourcesRootPath = "src/main/resources/OpenAPITest/"; - - /** Line separator, e.g. for windows '\r\n' */ - public static final String LINE_SEPARATOR = java.lang.System.getProperty("line.separator"); - - /** - * Setup workbench appropriately for tests - * - * @throws Exception test fails - */ - @BeforeClass - public static void setupClass() throws Exception { - - EclipseUtils.cleanWorkspace(bot, true); - - } - - /** - * Test generation with OpenAPI input after adapting templates - * - * @throws Exception test fails - */ - public void testBasicOpenAPIGenerationWithAdaptTemplates() throws Exception { - - // copy sample project to external location and import it into the workspace - String testProjName = "ExtTestProj"; - IJavaProject project = this.tmpMavenProjectRule.createProject(testProjName); - FileUtils.copyFile(new File(resourcesRootPath + "input/adapt-templates.yml"), - project.getUnderlyingResource().getLocation().append("adapt-templates.yml").toFile()); - project.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); - this.tmpMavenProjectRule.updateProject(); - - EclipseCobiGenUtils.runAndCaptureUpdateTemplates(bot); - EclipseCobiGenUtils.runAndCaptureAdaptTemplates(bot); - EclipseUtils.updateMavenProject(bot, ResourceConstants.CONFIG_PROJECT_NAME); - - EclipseUtils.openErrorsTreeInProblemsView(bot); - - // expand the new file in the package explorer - SWTBotView view = bot.viewById(JavaUI.ID_PACKAGES); - SWTBotTreeItem javaClassItem = view.bot().tree().expandNode(testProjName, "adapt-templates.yml"); - javaClassItem.select(); - - IProject adaptedTemplatesProj = ResourcesPlugin.getWorkspace().getRoot() - .getProject(ConfigurationConstants.COBIGEN_TEMPLATES); - - assertThat(adaptedTemplatesProj.exists()).isTrue(); - } - - /** - * Test of testBasicOpenAPIGenerationWithAdaptTemplates with custom COBIGEN_HOME - * environment variable - * - * @throws Exception test fails - */ - @Test - public void testAdaptTemplates() throws Exception { - - File tmpProject = this.tempFolder.newFolder("playground", "project", "templates"); - withEnvironmentVariable(ConfigurationConstants.CONFIG_ENV_HOME, tmpProject.getParentFile().getAbsolutePath()) - .execute(() -> testBasicOpenAPIGenerationWithAdaptTemplates()); - } + /** Temporary files rule to create temporary folders or files */ + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + + /** Root path of the Test Resources */ + private static final String resourcesRootPath = "src/main/resources/OpenAPITest/"; + + /** Line separator, e.g. for windows '\r\n' */ + public static final String LINE_SEPARATOR = java.lang.System.getProperty("line.separator"); + + /** + * Setup workbench appropriately for tests + * + * @throws Exception test fails + */ + @BeforeClass + public static void setupClass() throws Exception { + + EclipseUtils.cleanWorkspace(bot, true); + } + + /** + * Test for external projects (not in workspace) taken as input for generation + * + * @throws Exception test fails + */ + public void testBasicOpenAPIGenerationWithAdaptTemplates() throws Exception { + + // copy sample project to external location and import it into the workspace + String testProjName = "ExtTestProj"; + IJavaProject project = this.tmpMavenProjectRule.createProject(testProjName); + FileUtils.copyFile(new File(resourcesRootPath + "input/adapt-templates.yml"), + project.getUnderlyingResource().getLocation().append("adapt-templates.yml").toFile()); + project.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); + this.tmpMavenProjectRule.updateProject(); + + EclipseCobiGenUtils.runAndCaptureUpdateTemplates(bot); + EclipseCobiGenUtils.runAndCaptureAdaptTemplates(bot); + EclipseUtils.updateMavenProject(bot, ResourceConstants.CONFIG_PROJECT_NAME); + + EclipseUtils.openErrorsTreeInProblemsView(bot); + + // expand the new file in the package explorer + SWTBotView view = bot.viewById(JavaUI.ID_PACKAGES); + SWTBotTreeItem javaClassItem = view.bot().tree().expandNode(testProjName, "adapt-templates.yml"); + javaClassItem.select(); + + // execute CobiGen + EclipseCobiGenUtils.processCobiGen(bot, javaClassItem, 25000, "CRUD devon4j Server>CRUD REST services"); + bot.waitUntil(new AllJobsAreFinished(), 10000); + // increase timeout as the openAPI parser is slow on initialization + EclipseCobiGenUtils.confirmSuccessfullGeneration(bot, 40000); + + bot.waitUntil(new AllJobsAreFinished(), 10000); + IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject(testProjName); + IFile generationResult = proj.getFile( + "src/main/java/com/devonfw/test/sampledatamanagement/service/impl/rest/SampledatamanagementRestServiceImpl.java"); + + assertThat(generationResult.exists()).isTrue(); + } + + /** + * Test of testBasicOpenAPIGenerationWithAdaptTemplates with custom COBIGEN_HOME environment variable + * + * @throws Exception test fails + */ + @Test + public void testAdaptTemplatesAndGenerate() throws Exception { + + File tmpProject = this.tempFolder.newFolder("playground", "project"); + withEnvironmentVariable("COBIGEN_HOME", tmpProject.toPath().toString()) + .execute(() -> testBasicOpenAPIGenerationWithAdaptTemplates()); + } } 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 0bd01c5ee6..4496365a04 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 @@ -3,6 +3,8 @@ import static org.eclipse.swtbot.swt.finder.waits.Conditions.shellIsActive; import static org.eclipse.swtbot.swt.finder.waits.Conditions.widgetIsEnabled; +import java.util.concurrent.TimeoutException; + import org.eclipse.core.resources.IncrementalProjectBuilder; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; @@ -16,8 +18,6 @@ import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton; import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell; import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; -import org.eclipse.swtbot.swt.finder.widgets.TimeoutException; -import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.devonfw.cobigen.eclipse.common.constants.external.CobiGenDialogConstants; @@ -394,10 +394,6 @@ public static void runAndCaptureAdaptTemplates(SWTWorkbenchBot bot) throws Excep SWTBotShell warningDialog = bot.shell("Warning!"); warningDialog.bot().button("Ok").click(); - takeScreenshot(bot, "Create new POM!"); - SWTBotShell finishDialog = bot.shell("Create new POM"); - finishDialog.bot().button("Finish").click(); - SWTBotShell informationDialog = bot.shell("Information"); bot.waitUntil(new AnyShellIsActive("Information"), DEFAULT_TIMEOUT); takeScreenshot(bot, "Adapt Templates Information"); From 53a1c2ba5a6ca80ab7c483d8ee02bee76aba765e Mon Sep 17 00:00:00 2001 From: Cedric Betom Date: Wed, 18 Jan 2023 16:04:02 +0100 Subject: [PATCH 02/10] #1509 Resolved compiler errors #1622 --- .../eclipse/test/common/utils/EclipseCobiGenUtils.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 4496365a04..1175f3e229 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 @@ -3,8 +3,6 @@ import static org.eclipse.swtbot.swt.finder.waits.Conditions.shellIsActive; import static org.eclipse.swtbot.swt.finder.waits.Conditions.widgetIsEnabled; -import java.util.concurrent.TimeoutException; - import org.eclipse.core.resources.IncrementalProjectBuilder; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; @@ -18,6 +16,8 @@ import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton; import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell; import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; +import org.eclipse.swtbot.swt.finder.widgets.TimeoutException; +import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.devonfw.cobigen.eclipse.common.constants.external.CobiGenDialogConstants; From 7711e7b36a538f4b384a33e3f1db4de36723ebbb Mon Sep 17 00:00:00 2001 From: Cedric Betom Date: Thu, 19 Jan 2023 12:28:42 +0100 Subject: [PATCH 03/10] try to fix p2 cache enableCrossOsArchive in restore p2 cache --- .github/workflows/maven-build-test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/maven-build-test.yml b/.github/workflows/maven-build-test.yml index 719f812d13..a26da37341 100644 --- a/.github/workflows/maven-build-test.yml +++ b/.github/workflows/maven-build-test.yml @@ -158,6 +158,7 @@ jobs: with: path: ./**/target/repository/ key: p2-plugins-${{ github.sha }} # key hit will not update cache + enableCrossOsArchive: true - name: Run tests (Linux) if: ${{ matrix.os == 'ubuntu-latest' }} From 02d03d6b224b0d4895525bd2c2965c79aaa1adc2 Mon Sep 17 00:00:00 2001 From: Cedric Betom Date: Tue, 31 Jan 2023 17:36:33 +0100 Subject: [PATCH 04/10] #1509 taking the import of template-sets into Eclipse either before or after the adapt command into consideration --- .../eclipse/test/AdaptTemplatesTest.java | 13 ++- .../constants/external/ResourceConstants.java | 5 + .../common/tools/ResourcesPluginUtil.java | 2 +- .../handler/AdaptTemplatesHandler.java | 96 +++++++++++-------- 4 files changed, 74 insertions(+), 42 deletions(-) diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java b/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java index 92a0ace483..d27be7cef1 100644 --- a/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java +++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java @@ -4,6 +4,7 @@ import static org.assertj.core.api.Assertions.assertThat; import java.io.File; +import java.nio.file.Path; import org.apache.commons.io.FileUtils; import org.eclipse.core.resources.IFile; @@ -68,7 +69,10 @@ public void testBasicOpenAPIGenerationWithAdaptTemplates() throws Exception { project.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); this.tmpMavenProjectRule.updateProject(); - EclipseCobiGenUtils.runAndCaptureUpdateTemplates(bot); + /** + * TODO Before the templates are made available online, the update (download) command cannot be tested. + */ + // EclipseCobiGenUtils.runAndCaptureUpdateTemplates(bot); EclipseCobiGenUtils.runAndCaptureAdaptTemplates(bot); EclipseUtils.updateMavenProject(bot, ResourceConstants.CONFIG_PROJECT_NAME); @@ -101,7 +105,14 @@ public void testBasicOpenAPIGenerationWithAdaptTemplates() throws Exception { @Test public void testAdaptTemplatesAndGenerate() throws Exception { + Path devTemplatesPath = new File( + AdaptTemplatesTest.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile() + .getParentFile().toPath().resolve("cobigen-templates").resolve("crud-openapi-java-server-app") + .resolve("target").resolve("crud-openapi-java-server-app-2021.12.007-SNAPSHOT.jar"); + File tmpProject = this.tempFolder.newFolder("playground", "project"); + File downloaded = this.tempFolder.newFolder("playground", "project", "template-sets", "downloaded"); + FileUtils.copyFileToDirectory(devTemplatesPath.toFile(), downloaded); withEnvironmentVariable("COBIGEN_HOME", tmpProject.toPath().toString()) .execute(() -> testBasicOpenAPIGenerationWithAdaptTemplates()); } diff --git a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/constants/external/ResourceConstants.java b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/constants/external/ResourceConstants.java index 726ed6e605..1a43b9ee53 100644 --- a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/constants/external/ResourceConstants.java +++ b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/constants/external/ResourceConstants.java @@ -10,4 +10,9 @@ public class ResourceConstants { */ public static final String CONFIG_PROJECT_NAME = "CobiGen_Templates"; + /** + * Generator Configuration Project Name of template-sets + */ + public static final String TEMPLATE_SETS_CONFIG_PROJECT_NAME = "template-sets"; + } diff --git a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java index ac34c79203..d8f7f593dd 100644 --- a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java +++ b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java @@ -219,7 +219,7 @@ private static File getTemplatesDirectory() { * * @return the templateDirectory */ - private static File getTemplateSetDirectory() { + public static File getTemplateSetDirectory() { return CobiGenPaths.getTemplateSetsFolderPath().toFile(); } diff --git a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java index 6bf1bdc853..67672f7969 100644 --- a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java +++ b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java @@ -53,52 +53,68 @@ public Object execute(ExecutionEvent event) throws ExecutionException { MDC.put(InfrastructureConstants.CORRELATION_ID, UUID.randomUUID().toString()); IProject generatorProj = ResourcesPlugin.getWorkspace().getRoot().getProject(ResourceConstants.CONFIG_PROJECT_NAME); + IProject generatorProjOfTempltesSets = ResourcesPlugin.getWorkspace().getRoot() + .getProject(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME); - if (generatorProj.exists()) { - MessageDialog dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Info!", null, - "CobiGen_Templates folder is already imported, click on Update templates button to update with latest. ", - MessageDialog.INFORMATION, new String[] { "Ok" }, 1); - dialog.setBlockOnOpen(true); - dialog.open(); + if (generatorProjOfTempltesSets.exists()) { + + // 1. TODO import project, do not adapt(no need to extract the jar files) + } + + else if (ResourcesPluginUtil.getTemplateSetDirectory().exists()) { + // 2.TODO donwloaded exists? first adapt the jar file (files), then import project to eclipse + // 3. TODO downloaded does not exists? update command must be executed. then go to 2. + // (step 3 can be ignored for now until the new template-sets are deployed online.) + // } else { - MessageDialog dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Warning!", null, - "Clicking on ok button will override existing CobiGen_Templates in workspace.", MessageDialog.WARNING, - new String[] { "Ok", "Cancel" }, 1); - dialog.setBlockOnOpen(true); - int result = dialog.open(); - - if (result == 0) { - try { - String fileName = ResourcesPluginUtil.getJarPath(true); - if (fileName.equals("")) { - result = createUpdateTemplatesDialog(); - if (result == 1) { - MessageDialog.openWarning(Display.getDefault().getActiveShell(), "Warning", - "Templates have not been found, please download them!"); - throw new NullPointerException("Templates have not been found!"); - } else { - fileName = ResourcesPluginUtil.downloadJar(true); - } + if (generatorProj.exists()) { + MessageDialog dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Info!", null, + "CobiGen_Templates folder is already imported, click on Update templates button to update with latest. ", + MessageDialog.INFORMATION, new String[] { "Ok" }, 1); + dialog.setBlockOnOpen(true); + dialog.open(); + } else { + + MessageDialog dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Warning!", null, + "Clicking on ok button will override existing CobiGen_Templates in workspace.", MessageDialog.WARNING, + new String[] { "Ok", "Cancel" }, 1); + dialog.setBlockOnOpen(true); + int result = dialog.open(); + + if (result == 0) { + try { + String fileName = ResourcesPluginUtil.getJarPath(true); + if (fileName.equals("")) { + result = createUpdateTemplatesDialog(); + if (result == 1) { + MessageDialog.openWarning(Display.getDefault().getActiveShell(), "Warning", + "Templates have not been found, please download them!"); + throw new NullPointerException("Templates have not been found!"); + } else { + fileName = ResourcesPluginUtil.downloadJar(true); + } + } + ResourcesPluginUtil.processJar(fileName); + + importProjectIntoWorkspace(); + dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Information", null, + "CobiGen_Templates folder is imported sucessfully", MessageDialog.INFORMATION, new String[] { "Ok" }, + 1); + dialog.setBlockOnOpen(true); + dialog.open(); + } catch (MalformedURLException e) { + LOG.error("An exception with download url of maven central", e); + PlatformUIUtil.openErrorDialog("An exception with download url of maven central", e); + } catch (IOException e) { + LOG.error("An exception occurred while writing Jar files to .metadata folder", e); + PlatformUIUtil.openErrorDialog("An exception occurred while writing Jar files to .metadata folder", e); + } catch (Throwable e) { + ExceptionHandler.handle(e, HandlerUtil.getActiveShell(event)); } - ResourcesPluginUtil.processJar(fileName); - - importProjectIntoWorkspace(); - dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Information", null, - "CobiGen_Templates folder is imported sucessfully", MessageDialog.INFORMATION, new String[] { "Ok" }, 1); - dialog.setBlockOnOpen(true); - dialog.open(); - } catch (MalformedURLException e) { - LOG.error("An exception with download url of maven central", e); - PlatformUIUtil.openErrorDialog("An exception with download url of maven central", e); - } catch (IOException e) { - LOG.error("An exception occurred while writing Jar files to .metadata folder", e); - PlatformUIUtil.openErrorDialog("An exception occurred while writing Jar files to .metadata folder", e); - } catch (Throwable e) { - ExceptionHandler.handle(e, HandlerUtil.getActiveShell(event)); } + MDC.remove(InfrastructureConstants.CORRELATION_ID); } - MDC.remove(InfrastructureConstants.CORRELATION_ID); } return null; From ee7c05f2e51ae121b827c76cf4ae5bcdc7d97b49 Mon Sep 17 00:00:00 2001 From: MansourD Date: Wed, 1 Feb 2023 16:51:08 +0100 Subject: [PATCH 05/10] #1509 Integrated the templateSetsAdapter into eclipse to adapt template-set->downloaded to adapted directory --- .../eclipse/test/AdaptTemplatesTest.java | 6 +- .../common/utils/EclipseCobiGenUtils.java | 15 ++++ .../common/tools/ResourcesPluginUtil.java | 87 ++++++++++++------- .../generator/GeneratorWrapperFactory.java | 1 - .../handler/AdaptTemplatesHandler.java | 24 +++-- 5 files changed, 88 insertions(+), 45 deletions(-) diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java b/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java index d27be7cef1..888a84ccbe 100644 --- a/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java +++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java @@ -21,7 +21,6 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; -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; @@ -73,11 +72,8 @@ public void testBasicOpenAPIGenerationWithAdaptTemplates() throws Exception { * TODO Before the templates are made available online, the update (download) command cannot be tested. */ // EclipseCobiGenUtils.runAndCaptureUpdateTemplates(bot); - EclipseCobiGenUtils.runAndCaptureAdaptTemplates(bot); - EclipseUtils.updateMavenProject(bot, ResourceConstants.CONFIG_PROJECT_NAME); - + EclipseCobiGenUtils.runAndCaptureAdaptTemplatesSets(bot); EclipseUtils.openErrorsTreeInProblemsView(bot); - // expand the new file in the package explorer SWTBotView view = bot.viewById(JavaUI.ID_PACKAGES); SWTBotTreeItem javaClassItem = view.bot().tree().expandNode(testProjName, "adapt-templates.yml"); 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 1175f3e229..71bd2e280b 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 @@ -400,6 +400,21 @@ public static void runAndCaptureAdaptTemplates(SWTWorkbenchBot bot) throws Excep informationDialog.bot().button("Ok").click(); } + /** + * Checks the CobiGen Adapt TemplateSets and takes screenshots of it. + * + * @param bot to process the Adapt Templates command + * @throws Exception test fails + */ + public static void runAndCaptureAdaptTemplatesSets(SWTWorkbenchBot bot) throws Exception { + + ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor()); + bot.waitUntil(new AllJobsAreFinished(), DEFAULT_TIMEOUT); // build might take some time + + SWTBotView view = bot.viewById(JavaUI.ID_PACKAGES); + view.bot().tree().contextMenu("CobiGen").menu("Adapt Templates...").click(); + } + /** * Takes a screenshot (*.jpeg) of the current screen encoding test method and class and appends the given identifier * to the file name diff --git a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java index d8f7f593dd..d09d183c75 100644 --- a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java +++ b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java @@ -7,15 +7,13 @@ import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; import org.apache.commons.lang3.StringUtils; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; -import com.devonfw.cobigen.eclipse.common.constants.external.ResourceConstants; -import com.devonfw.cobigen.eclipse.common.exceptions.GeneratorProjectNotExistentException; -import com.devonfw.cobigen.eclipse.updatetemplates.UpdateTemplatesDialog; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.jface.dialogs.MessageDialog; @@ -24,12 +22,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; - - import com.devonfw.cobigen.api.TemplateAdapter; import com.devonfw.cobigen.api.constants.ConfigurationConstants; import com.devonfw.cobigen.api.util.CobiGenPaths; import com.devonfw.cobigen.api.util.TemplatesJarUtil; +import com.devonfw.cobigen.eclipse.common.constants.external.ResourceConstants; +import com.devonfw.cobigen.eclipse.common.exceptions.GeneratorProjectNotExistentException; +import com.devonfw.cobigen.eclipse.updatetemplates.UpdateTemplatesDialog; import com.devonfw.cobigen.impl.CobiGenFactory; import com.devonfw.cobigen.impl.adapter.TemplateAdapterImpl; @@ -107,16 +106,29 @@ public static void refreshConfigurationProject() { */ public static IProject getGeneratorConfigurationProject() throws GeneratorProjectNotExistentException, CoreException { - File templatesDirectory = getTemplatesDirectory(); - - generatorProj = ResourcesPlugin.getWorkspace().getRoot().getProject(ResourceConstants.CONFIG_PROJECT_NAME); + Path templateSetDirectory = getTemplateSetDirectory(); + + if (!Files.exists(templateSetDirectory)) { + File templatesDirectory = getTemplatesDirectory(); + generatorProj = ResourcesPlugin.getWorkspace().getRoot().getProject(ResourceConstants.CONFIG_PROJECT_NAME); + + if (!generatorProj.exists()) { + if (!isUpdateDialogShown) { + if (templatesDirectory.exists()) { + Path jarFilePath = TemplatesJarUtil.getJarFile(false, templatesDirectory.toPath()); + // If we don't find at least one jar, then we do need to download new templates + if (jarFilePath == null || !Files.exists(jarFilePath)) { + int result = createUpdateTemplatesDialog(); + isUpdateDialogShown = true; + if (result == 1) { + // User does not want to download templates. + userWantsToDownloadTemplates = false; + } else { + userWantsToDownloadTemplates = true; + } + } - if (!generatorProj.exists()) { - if (!isUpdateDialogShown) { - if (templatesDirectory.exists()) { - Path jarFilePath = TemplatesJarUtil.getJarFile(false, templatesDirectory.toPath()); - // If we don't find at least one jar, then we do need to download new templates - if (jarFilePath == null || !Files.exists(jarFilePath)) { + } else { int result = createUpdateTemplatesDialog(); isUpdateDialogShown = true; if (result == 1) { @@ -126,24 +138,15 @@ public static IProject getGeneratorConfigurationProject() throws GeneratorProjec userWantsToDownloadTemplates = true; } } - - } else { - int result = createUpdateTemplatesDialog(); - isUpdateDialogShown = true; - if (result == 1) { - // User does not want to download templates. - userWantsToDownloadTemplates = false; - } else { - userWantsToDownloadTemplates = true; - } } } + if (userWantsToDownloadTemplates) { + return generatorProj; + } else { + return null; + } } - if (userWantsToDownloadTemplates) { - return generatorProj; - } else { - return null; - } + return null; } /** @@ -219,9 +222,9 @@ private static File getTemplatesDirectory() { * * @return the templateDirectory */ - public static File getTemplateSetDirectory() { + public static Path getTemplateSetDirectory() { - return CobiGenPaths.getTemplateSetsFolderPath().toFile(); + return CobiGenPaths.getTemplateSetsFolderPath(); } /** @@ -271,6 +274,27 @@ public static void processJar(String fileName) throws MalformedURLException, IOE } } + /** + * Process Jar method is responsible for unzip the Jar files of templateSets from downloaded to adapted + * + * @param fileName Name of source jar file downloaded + * @throws IOException {@link IOException} occurred + * @throws MalformedURLException {@link MalformedURLException} occurred + */ + public static void adaptTemplateSet(Path templateSetsLocation) { + + try { + TemplateAdapter templateAdapter = new TemplateAdapterImpl(templateSetsLocation); + List jarFilePath = TemplatesJarUtil.getJarFiles(templateSetsLocation.resolve("downloaded")); + templateAdapter.adaptTemplateSets(jarFilePath, templateSetsLocation.resolve("adapted"), false); + + } catch (Exception e) { + LOG.error("An exception occurred while processing Jar files to create CobiGen_Templates folder", e); + PlatformUIUtil + .openErrorDialog("An exception occurred while processing Jar file to create CobiGen_Templates folder", e); + } + } + /** * @return workspace location */ @@ -298,7 +322,6 @@ public static void startTemplatesUpgrader(Path configurationPath) { setTemplateSetPathAfterUpgrade(CobiGenFactory.startTemplatesUpgrader(configurationPath)); - // FIXME after the upgrade adapt the new template-set folder. should be done in #1587 } 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 4c0a6f5fd9..266c564749 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 @@ -216,7 +216,6 @@ private static CobiGen initializeGenerator(boolean allowMonolithicConfiguration) throws InvalidConfigurationException, GeneratorCreationException { try { - ResourcesPluginUtil.refreshConfigurationProject(); generatorProj = ResourcesPluginUtil.getGeneratorConfigurationProject(); initializeCobiGen(generatorProj, allowMonolithicConfiguration); diff --git a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java index 67672f7969..40aaf6121b 100644 --- a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java +++ b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java @@ -55,17 +55,27 @@ public Object execute(ExecutionEvent event) throws ExecutionException { IProject generatorProj = ResourcesPlugin.getWorkspace().getRoot().getProject(ResourceConstants.CONFIG_PROJECT_NAME); IProject generatorProjOfTempltesSets = ResourcesPlugin.getWorkspace().getRoot() .getProject(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME); + Path templateSet = ResourcesPluginUtil.getTemplateSetDirectory(); if (generatorProjOfTempltesSets.exists()) { + // 1. the project is imported so, do not adapt(no need to extract the jar files) For now nothing to do. - // 1. TODO import project, do not adapt(no need to extract the jar files) - } + } else if (Files.exists(templateSet)) { + + // 2. downloaded exists? first adapt the jar file (files), then import project to eclipse + + Path downloadedPath = templateSet.resolve("downloaded"); + Path adaptedPath = templateSet.resolve("adapted"); + + // A. adapt the jar files if not already adapted + if (Files.exists(downloadedPath) && !Files.exists(adaptedPath)) + ResourcesPluginUtil.adaptTemplateSet(templateSet); - else if (ResourcesPluginUtil.getTemplateSetDirectory().exists()) { - // 2.TODO donwloaded exists? first adapt the jar file (files), then import project to eclipse - // 3. TODO downloaded does not exists? update command must be executed. then go to 2. - // (step 3 can be ignored for now until the new template-sets are deployed online.) - // + // B. TODO Import the project + /* + * 3. TODO downloaded does not exists? update command must be executed. then go to 2. // (step 3 can be ignored + * for now until the new template-sets are deployed online.) + */ } else { if (generatorProj.exists()) { MessageDialog dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Info!", null, From 79296a07fd21a6dec364e807a0008c164c2bd933 Mon Sep 17 00:00:00 2001 From: MansourD Date: Thu, 2 Feb 2023 17:25:41 +0100 Subject: [PATCH 06/10] #1509 New tests for Adaption in Eclipse --- .../eclipse/test/AdaptTemplatesTest.java | 122 +++++++++++++++++- .../constants/external/ResourceConstants.java | 10 ++ .../common/tools/ResourcesPluginUtil.java | 6 +- .../handler/AdaptTemplatesHandler.java | 25 ++-- 4 files changed, 148 insertions(+), 15 deletions(-) diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java b/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java index 888a84ccbe..854558bd81 100644 --- a/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java +++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java @@ -4,7 +4,9 @@ import static org.assertj.core.api.Assertions.assertThat; import java.io.File; +import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import org.apache.commons.io.FileUtils; import org.eclipse.core.resources.IFile; @@ -17,10 +19,12 @@ import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; import org.junit.BeforeClass; +import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +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; @@ -58,7 +62,7 @@ public static void setupClass() throws Exception { * * @throws Exception test fails */ - public void testBasicOpenAPIGenerationWithAdaptTemplates() throws Exception { + public void testBasicOpenAPIGenerationWithAdaptTemplateSets() throws Exception { // copy sample project to external location and import it into the workspace String testProjName = "ExtTestProj"; @@ -94,12 +98,123 @@ public void testBasicOpenAPIGenerationWithAdaptTemplates() throws Exception { } /** + * Test for external projects (not in workspace) taken as input + * + * @throws Exception test fails + */ + public void testAdaptTemplatesAndImportIntoEclipse() throws Exception { + + // copy sample project to external location and import it into the workspace + String testProjName = "ExtTestProj"; + IJavaProject project = this.tmpMavenProjectRule.createProject(testProjName); + FileUtils.copyFile(new File(resourcesRootPath + "input/adapt-templates.yml"), + project.getUnderlyingResource().getLocation().append("adapt-templates.yml").toFile()); + project.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); + this.tmpMavenProjectRule.updateProject(); + + EclipseCobiGenUtils.runAndCaptureAdaptTemplatesSets(bot); + + EclipseUtils.openErrorsTreeInProblemsView(bot); + + // expand the new file in the package explorer + SWTBotView view = bot.viewById(JavaUI.ID_PACKAGES); + SWTBotTreeItem javaClassItem = view.bot().tree().expandNode(testProjName, "adapt-templates.yml"); + javaClassItem.select(); + + IProject generatorProjOfTempltesSets = ResourcesPlugin.getWorkspace().getRoot() + .getProject(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME); + bot.waitUntil(new AllJobsAreFinished(), 10000000); + IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject(testProjName); + + Path adaptedFolder = Paths.get(generatorProjOfTempltesSets.getLocationURI()) + .resolve(ResourceConstants.TEMPLATE_SETS_ADAPTED); + + assertThat(generatorProjOfTempltesSets.exists()).isTrue(); + assertThat(Files.exists(adaptedFolder)).isTrue(); + + } + + /** + * Test for external projects (not in workspace) taken as input for generation + * + * @throws Exception test fails + */ + public void testBasicOpenAPIGenerationWithAdaptMonolithicTemplates() throws Exception { + + // copy sample project to external location and import it into the workspace + String testProjName = "ExtTestProj"; + IJavaProject project = this.tmpMavenProjectRule.createProject(testProjName); + FileUtils.copyFile(new File(resourcesRootPath + "input/adapt-templates.yml"), + project.getUnderlyingResource().getLocation().append("adapt-templates.yml").toFile()); + project.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); + this.tmpMavenProjectRule.updateProject(); + + EclipseCobiGenUtils.runAndCaptureUpdateTemplates(bot); + EclipseCobiGenUtils.runAndCaptureAdaptTemplates(bot); + EclipseUtils.updateMavenProject(bot, ResourceConstants.CONFIG_PROJECT_NAME); + + EclipseUtils.openErrorsTreeInProblemsView(bot); + + // expand the new file in the package explorer + SWTBotView view = bot.viewById(JavaUI.ID_PACKAGES); + SWTBotTreeItem javaClassItem = view.bot().tree().expandNode(testProjName, "adapt-templates.yml"); + javaClassItem.select(); + + // execute CobiGen + EclipseCobiGenUtils.processCobiGen(bot, javaClassItem, 25000, "CRUD devon4j Server>CRUD REST services"); + bot.waitUntil(new AllJobsAreFinished(), 10000); + // increase timeout as the openAPI parser is slow on initialization + EclipseCobiGenUtils.confirmSuccessfullGeneration(bot, 40000); + + bot.waitUntil(new AllJobsAreFinished(), 10000); + IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject(testProjName); + IFile generationResult = proj.getFile( + "src/main/java/com/devonfw/test/sampledatamanagement/service/impl/rest/SampledatamanagementRestServiceImpl.java"); + + assertThat(generationResult.exists()).isTrue(); + } + + /* + * * Test of testBasicOpenAPIGenerationWithAdaptTemplates with custom COBIGEN_HOME environment variable * + */ + @Test + public void testAdaptMonolithicTemplatesAndGenerate() throws Exception { + + File tmpProject = this.tempFolder.newFolder("playground", "project"); + withEnvironmentVariable("COBIGEN_HOME", tmpProject.toPath().toString()) + .execute(() -> testBasicOpenAPIGenerationWithAdaptMonolithicTemplates()); + } + + /** + * TODO Test of testBasicOpenAPIGenerationWithAdaptTemplates with custom template-sets after adapt the jar files + * * @throws Exception test fails */ @Test - public void testAdaptTemplatesAndGenerate() throws Exception { + @Ignore + public void testAdaptTemplateSetsAndGenerate() throws Exception { + + Path devTemplatesPath = new File( + AdaptTemplatesTest.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile() + .getParentFile().toPath().resolve("cobigen-templates").resolve("crud-openapi-java-server-app") + .resolve("target").resolve("crud-openapi-java-server-app-2021.12.007-SNAPSHOT.jar"); + + File tmpProject = this.tempFolder.newFolder("playground", "project"); + File downloaded = this.tempFolder.newFolder("playground", "project", "template-sets", "downloaded"); + FileUtils.copyFileToDirectory(devTemplatesPath.toFile(), downloaded); + withEnvironmentVariable("COBIGEN_HOME", tmpProject.toPath().toString()) + .execute(() -> testBasicOpenAPIGenerationWithAdaptTemplateSets()); + } + + /* + * + * Test adaption of template-sets/downloaded/.jar files and importing the project into Eclipse + * + */ + @Test + public void testAdaptTemplateSetsAndImport() throws Exception { Path devTemplatesPath = new File( AdaptTemplatesTest.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile() @@ -110,6 +225,7 @@ public void testAdaptTemplatesAndGenerate() throws Exception { File downloaded = this.tempFolder.newFolder("playground", "project", "template-sets", "downloaded"); FileUtils.copyFileToDirectory(devTemplatesPath.toFile(), downloaded); withEnvironmentVariable("COBIGEN_HOME", tmpProject.toPath().toString()) - .execute(() -> testBasicOpenAPIGenerationWithAdaptTemplates()); + .execute(() -> testAdaptTemplatesAndImportIntoEclipse()); + } } diff --git a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/constants/external/ResourceConstants.java b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/constants/external/ResourceConstants.java index 1a43b9ee53..d515161532 100644 --- a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/constants/external/ResourceConstants.java +++ b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/constants/external/ResourceConstants.java @@ -15,4 +15,14 @@ public class ResourceConstants { */ public static final String TEMPLATE_SETS_CONFIG_PROJECT_NAME = "template-sets"; + /** + * Name of template-sets/downloaded folder + */ + public static final String TEMPLATE_SETS_DOWNLOADED = "downloaded"; + + /** + * Name of template-sets/adapted folder + */ + public static final String TEMPLATE_SETS_ADAPTED = "adapted"; + } diff --git a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java index d09d183c75..c829188c1a 100644 --- a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java +++ b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java @@ -285,8 +285,10 @@ public static void adaptTemplateSet(Path templateSetsLocation) { try { TemplateAdapter templateAdapter = new TemplateAdapterImpl(templateSetsLocation); - List jarFilePath = TemplatesJarUtil.getJarFiles(templateSetsLocation.resolve("downloaded")); - templateAdapter.adaptTemplateSets(jarFilePath, templateSetsLocation.resolve("adapted"), false); + List jarFilePath = TemplatesJarUtil + .getJarFiles(templateSetsLocation.resolve(ResourceConstants.TEMPLATE_SETS_DOWNLOADED)); + templateAdapter.adaptTemplateSets(jarFilePath, + templateSetsLocation.resolve(ResourceConstants.TEMPLATE_SETS_ADAPTED), false); } catch (Exception e) { LOG.error("An exception occurred while processing Jar files to create CobiGen_Templates folder", e); diff --git a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java index 40aaf6121b..3d5aae2c09 100644 --- a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java +++ b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java @@ -64,14 +64,15 @@ public Object execute(ExecutionEvent event) throws ExecutionException { // 2. downloaded exists? first adapt the jar file (files), then import project to eclipse - Path downloadedPath = templateSet.resolve("downloaded"); - Path adaptedPath = templateSet.resolve("adapted"); + Path downloadedPath = templateSet.resolve(ResourceConstants.TEMPLATE_SETS_DOWNLOADED); + Path adaptedPath = templateSet.resolve(ResourceConstants.TEMPLATE_SETS_ADAPTED); // A. adapt the jar files if not already adapted if (Files.exists(downloadedPath) && !Files.exists(adaptedPath)) ResourcesPluginUtil.adaptTemplateSet(templateSet); - // B. TODO Import the project + // B. Import the project + importProjectIntoWorkspace(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME, templateSet); /* * 3. TODO downloaded does not exists? update command must be executed. then go to 2. // (step 3 can be ignored * for now until the new template-sets are deployed online.) @@ -107,7 +108,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException { } ResourcesPluginUtil.processJar(fileName); - importProjectIntoWorkspace(); + importProjectIntoWorkspace(ResourceConstants.CONFIG_PROJECT_NAME, null); dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Information", null, "CobiGen_Templates folder is imported sucessfully", MessageDialog.INFORMATION, new String[] { "Ok" }, 1); @@ -133,7 +134,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException { /** * CobiGen_Templates folder created at main folder using source jar will be imported into workspace */ - private void importProjectIntoWorkspace() { + private void importProjectIntoWorkspace(String projectName, Path projectPath) { ProgressMonitorDialog progressMonitor = new ProgressMonitorDialog(Display.getDefault().getActiveShell()); @@ -143,9 +144,13 @@ private void importProjectIntoWorkspace() { progressMonitor.open(); progressMonitor.getProgressMonitor().beginTask("Importing templates...", 0); try { - IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(ResourceConstants.CONFIG_PROJECT_NAME); + IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); IProjectDescription description = ResourcesPlugin.getWorkspace().newProjectDescription(project.getName()); - description.setLocation(new org.eclipse.core.runtime.Path(this.ws.toPortableString() + "/CobiGen_Templates")); + if (projectName == ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME) { + description.setLocationURI(projectPath.toUri()); + } else { + description.setLocation(new org.eclipse.core.runtime.Path(this.ws.toPortableString() + "/" + projectName)); + } project.create(description, null); // We set the current project to be converted to a Maven project @@ -153,16 +158,16 @@ private void importProjectIntoWorkspace() { mavenConverter.selectionChanged(null, sel); project.open(null); - // Converts the current project to a Maven project - mavenConverter.run(null); + if (projectName == ResourceConstants.CONFIG_PROJECT_NAME) + mavenConverter.run(null); progressMonitor.close(); } catch (CoreException e) { progressMonitor.close(); e.printStackTrace(); MessageDialog.openWarning(Display.getDefault().getActiveShell(), "Warning", - "Some Exception occurred while importing CobiGen_Templates into workspace"); + "Some Exception occurred while importing " + projectName + " into workspace"); } } From 801773d8afd31620f7388fdfd7277c662e5de5fa Mon Sep 17 00:00:00 2001 From: Cedric Betom Date: Mon, 13 Feb 2023 12:00:51 +0100 Subject: [PATCH 07/10] #1509 Adapted testAdaptMonolithicTemplatesAndGenerate to make the download works --- .../cobigen/eclipse/test/AdaptTemplatesTest.java | 2 +- .../com/devonfw/cobigen/api/util/CobiGenPaths.java | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java b/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java index 854558bd81..9254a553cc 100644 --- a/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java +++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java @@ -182,7 +182,7 @@ public void testBasicOpenAPIGenerationWithAdaptMonolithicTemplates() throws Exce @Test public void testAdaptMonolithicTemplatesAndGenerate() throws Exception { - File tmpProject = this.tempFolder.newFolder("playground", "project"); + File tmpProject = this.tempFolder.newFolder("playground", "project", "templates"); withEnvironmentVariable("COBIGEN_HOME", tmpProject.toPath().toString()) .execute(() -> testBasicOpenAPIGenerationWithAdaptMonolithicTemplates()); } diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/CobiGenPaths.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/CobiGenPaths.java index c2178fb3b2..0ab76baaf8 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/CobiGenPaths.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/CobiGenPaths.java @@ -74,8 +74,16 @@ public static Path getTemplatesFolderPath() { */ public static Path getTemplatesFolderPath(Path home) { - Path templatesPath = home.resolve(ConfigurationConstants.TEMPLATES_FOLDER); - return templatesPath; + File file = new File(home.toString()); + int lastSlash = file.getName().lastIndexOf("/"); + String lastWord = file.getName().substring(lastSlash + 1); + if (lastWord.equals(ConfigurationConstants.TEMPLATES_FOLDER)) { + return home; + } else { + home = home.resolve(ConfigurationConstants.TEMPLATES_FOLDER); + } + + return home; } /** From c998068722015eb3daedeabe8538ff0b6397b713 Mon Sep 17 00:00:00 2001 From: Cedric Betom Date: Tue, 14 Feb 2023 14:15:14 +0100 Subject: [PATCH 08/10] #1509 further processing of the testAdaptMonolithicTemplatesAndGenerate --- .../eclipse/test/common/utils/EclipseCobiGenUtils.java | 4 +++- .../cobigen/eclipse/common/tools/ResourcesPluginUtil.java | 7 ++++--- .../workbenchcontrol/handler/AdaptTemplatesHandler.java | 3 ++- 3 files changed, 9 insertions(+), 5 deletions(-) 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 71bd2e280b..fcceaa36db 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 @@ -3,6 +3,7 @@ import static org.eclipse.swtbot.swt.finder.waits.Conditions.shellIsActive; import static org.eclipse.swtbot.swt.finder.waits.Conditions.widgetIsEnabled; +import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.IncrementalProjectBuilder; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; @@ -359,7 +360,8 @@ private static void captureAdvancedHealthCheck(SWTWorkbenchBot bot) { */ public static void runAndCaptureUpdateTemplates(SWTWorkbenchBot bot) throws Exception { - ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor()); + IWorkspace workspace = ResourcesPlugin.getWorkspace(); + workspace.build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor()); bot.waitUntil(new AllJobsAreFinished(), DEFAULT_TIMEOUT); // build might take some time SWTBotView view = bot.viewById(JavaUI.ID_PACKAGES); diff --git a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java index c829188c1a..7e465edc29 100644 --- a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java +++ b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java @@ -212,7 +212,7 @@ public static String getJarPath(boolean isSource) { * * @return the templateDirectory */ - private static File getTemplatesDirectory() { + public static File getTemplatesDirectory() { return CobiGenPaths.getTemplatesFolderPath().toFile(); } @@ -239,6 +239,7 @@ public static void processJar(String fileName) throws MalformedURLException, IOE String pathForCobigenTemplates = ""; IPath ws = ResourcesPluginUtil.getWorkspaceLocation(); + Path templateDirectory = ResourcesPluginUtil.getTemplatesDirectory().toPath(); try { pathForCobigenTemplates = ws.toPortableString() @@ -264,8 +265,8 @@ public static void processJar(String fileName) throws MalformedURLException, IOE } try { - TemplateAdapter templateAdapter = new TemplateAdapterImpl(cobigenFolderPath); - templateAdapter.adaptMonolithicTemplates(cobigenFolderPath.resolve(ConfigurationConstants.COBIGEN_TEMPLATES), + TemplateAdapter templateAdapter = new TemplateAdapterImpl(templateDirectory); + templateAdapter.adaptMonolithicTemplates(templateDirectory.resolve(ConfigurationConstants.COBIGEN_TEMPLATES), false); } catch (Exception e) { LOG.error("An exception occurred while processing Jar files to create CobiGen_Templates folder", e); diff --git a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java index 3d5aae2c09..746460d832 100644 --- a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java +++ b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java @@ -108,7 +108,8 @@ public Object execute(ExecutionEvent event) throws ExecutionException { } ResourcesPluginUtil.processJar(fileName); - importProjectIntoWorkspace(ResourceConstants.CONFIG_PROJECT_NAME, null); + importProjectIntoWorkspace(ResourceConstants.CONFIG_PROJECT_NAME, + ResourcesPluginUtil.getTemplatesDirectory().toPath()); dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Information", null, "CobiGen_Templates folder is imported sucessfully", MessageDialog.INFORMATION, new String[] { "Ok" }, 1); From 4c5cfab3849f9b322809388d5ac96666eddc04a1 Mon Sep 17 00:00:00 2001 From: cedricarnauld123 Date: Mon, 27 Mar 2023 16:43:41 +0200 Subject: [PATCH 09/10] #1509 implemented testAdaptTemplateSetsAndGenerate and modify testBasicOpenAPIGenerationWithAdaptMonolithicTemplates --- cobigen-eclipse/cobigen-eclipse-test/pom.xml | 50 ++++++++++ .../eclipse/test/AdaptTemplatesTest.java | 38 +++++--- .../common/utils/EclipseCobiGenUtils.java | 5 + .../common/tools/ResourcesPluginUtil.java | 45 ++------- .../handler/AdaptTemplatesHandler.java | 92 ++++++++++--------- .../cobigen/api/util/CobiGenPaths.java | 12 +-- .../impl/adapter/TemplateAdapterImpl.java | 1 - 7 files changed, 138 insertions(+), 105 deletions(-) diff --git a/cobigen-eclipse/cobigen-eclipse-test/pom.xml b/cobigen-eclipse/cobigen-eclipse-test/pom.xml index 8fc4f9cf42..37aae63f6b 100644 --- a/cobigen-eclipse/cobigen-eclipse-test/pom.xml +++ b/cobigen-eclipse/cobigen-eclipse-test/pom.xml @@ -13,6 +13,9 @@ ${surefireArgLineIntegration} -Xms512m -Xmx2048m -XX:+DisableExplicitGC true + src\main\resources\AdaptTemplatesTest\template-sets\adapted\crud-java-server-app + src\main\resources\AdaptTemplatesTest\template-sets\adapted\crud-java-server-app-complex + @@ -58,6 +61,53 @@ + + + + org.codehaus.mojo + exec-maven-plugin + 3.0.0 + + + Install test adaptTemplatesTests1 + test-compile + + exec + + + mvn + ${maven.test.AdaptTemplatesTest1} + + install + + -Djansi.force=true + -Djansi.passthrough=true + -B + -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn + + + + + Install test adaptTemplatesTests2 + test-compile + + exec + + + mvn + ${maven.test.AdaptTemplatesTest2} + + install + + -Djansi.force=true + -Djansi.passthrough=true + -B + -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn + + + + + org.apache.maven.plugins maven-clean-plugin diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java b/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java index bc1edc3bf1..a888c2126c 100644 --- a/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java +++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/java/com/devonfw/cobigen/eclipse/test/AdaptTemplatesTest.java @@ -48,6 +48,9 @@ public class AdaptTemplatesTest extends SystemTest { /** Line separator, e.g. for windows '\r\n' */ public static final String LINE_SEPARATOR = java.lang.System.getProperty("line.separator"); + /* Root path for templateSets jar */ + private static final String testResource = "src/main/resources/AdaptTemplatesTest/template-sets/downloaded"; + /** * Setup workbench appropriately for tests * @@ -116,17 +119,25 @@ public void testAdaptTemplatesAndImportIntoEclipse() throws Exception { EclipseCobiGenUtils.runAndCaptureAdaptTemplatesSets(bot); - EclipseUtils.openErrorsTreeInProblemsView(bot); + // EclipseUtils.updateMavenProject(bot, ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME); // expand the new file in the package explorer SWTBotView view = bot.viewById(JavaUI.ID_PACKAGES); SWTBotTreeItem javaClassItem = view.bot().tree().expandNode(testProjName, "adapt-templates.yml"); javaClassItem.select(); + // execute CobiGen + EclipseCobiGenUtils.processCobiGen(bot, javaClassItem, 25000, "CRUD devon4j Server>CRUD REST services"); + bot.waitUntil(new AllJobsAreFinished(), 10000); + // increase timeout as the openAPI parser is slow on initialization + EclipseCobiGenUtils.confirmSuccessfullGeneration(bot, 40000); + bot.waitUntil(new AllJobsAreFinished(), 10000); + + // EclipseUtils.openErrorsTreeInProblemsView(bot); + IProject generatorProjOfTempltesSets = ResourcesPlugin.getWorkspace().getRoot() .getProject(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME); - bot.waitUntil(new AllJobsAreFinished(), 10000000); - IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject(testProjName); + bot.waitUntil(new AllJobsAreFinished(), 10000); Path adaptedFolder = Paths.get(generatorProjOfTempltesSets.getLocationURI()) .resolve(ResourceConstants.TEMPLATE_SETS_ADAPTED); @@ -151,7 +162,11 @@ public void testBasicOpenAPIGenerationWithAdaptMonolithicTemplates() throws Exce project.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); this.tmpMavenProjectRule.updateProject(); - EclipseCobiGenUtils.runAndCaptureUpdateTemplates(bot); + // retrieve CobiGen home directory (overwritten through environment variables) + File templatesDirectory = CobiGenPaths.getTemplatesFolderPath().toFile(); + // create templates directory as this should be present in this scenario + TemplatesJarUtil.downloadLatestDevon4jTemplates(true, templatesDirectory); + TemplatesJarUtil.downloadLatestDevon4jTemplates(false, templatesDirectory); EclipseCobiGenUtils.runAndCaptureAdaptTemplates(bot); EclipseUtils.updateMavenProject(bot, ResourceConstants.CONFIG_PROJECT_NAME); @@ -184,7 +199,7 @@ public void testBasicOpenAPIGenerationWithAdaptMonolithicTemplates() throws Exce @Test public void testAdaptMonolithicTemplatesAndGenerate() throws Exception { - File tmpProject = this.tempFolder.newFolder("playground", "project", "templates"); + File tmpProject = this.tempFolder.newFolder("playground", "project"); withEnvironmentVariable("COBIGEN_HOME", tmpProject.toPath().toString()) .execute(() -> testBasicOpenAPIGenerationWithAdaptMonolithicTemplates()); } @@ -210,22 +225,19 @@ public void testAdaptTemplateSetsAndGenerate() throws Exception { .execute(() -> testBasicOpenAPIGenerationWithAdaptTemplateSets()); } - /* - * + /** * Test adaption of template-sets/downloaded/.jar files and importing the project into Eclipse * + * @throws Exception */ @Test public void testAdaptTemplateSetsAndImport() throws Exception { - Path devTemplatesPath = new File( - AdaptTemplatesTest.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile() - .getParentFile().toPath().resolve("cobigen-templates").resolve("crud-openapi-java-server-app") - .resolve("target").resolve("crud-openapi-java-server-app-2021.12.007-SNAPSHOT.jar"); - File tmpProject = this.tempFolder.newFolder("playground", "project"); File downloaded = this.tempFolder.newFolder("playground", "project", "template-sets", "downloaded"); - FileUtils.copyFileToDirectory(devTemplatesPath.toFile(), downloaded); + + FileUtils.copyDirectory(new File(testResource), downloaded); + withEnvironmentVariable("COBIGEN_HOME", tmpProject.toPath().toString()) .execute(() -> testAdaptTemplatesAndImportIntoEclipse()); 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 1c20af25fe..82aa5ca9f2 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 @@ -415,6 +415,11 @@ public static void runAndCaptureAdaptTemplatesSets(SWTWorkbenchBot bot) throws E SWTBotView view = bot.viewById(JavaUI.ID_PACKAGES); view.bot().tree().contextMenu("CobiGen").menu("Adapt Templates...").click(); + + SWTBotShell informationDialog = bot.shell("Information"); + bot.waitUntil(new AnyShellIsActive("Information"), DEFAULT_TIMEOUT); + takeScreenshot(bot, "Adapt Templates Information"); + informationDialog.bot().button("Ok").click(); } /** diff --git a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java index 7e465edc29..76eae149f4 100644 --- a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java +++ b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/common/tools/ResourcesPluginUtil.java @@ -106,46 +106,17 @@ public static void refreshConfigurationProject() { */ public static IProject getGeneratorConfigurationProject() throws GeneratorProjectNotExistentException, CoreException { - Path templateSetDirectory = getTemplateSetDirectory(); - - if (!Files.exists(templateSetDirectory)) { - File templatesDirectory = getTemplatesDirectory(); + generatorProj = ResourcesPlugin.getWorkspace().getRoot() + .getProject(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME); + if (generatorProj.exists()) { + return generatorProj; + } else { generatorProj = ResourcesPlugin.getWorkspace().getRoot().getProject(ResourceConstants.CONFIG_PROJECT_NAME); - - if (!generatorProj.exists()) { - if (!isUpdateDialogShown) { - if (templatesDirectory.exists()) { - Path jarFilePath = TemplatesJarUtil.getJarFile(false, templatesDirectory.toPath()); - // If we don't find at least one jar, then we do need to download new templates - if (jarFilePath == null || !Files.exists(jarFilePath)) { - int result = createUpdateTemplatesDialog(); - isUpdateDialogShown = true; - if (result == 1) { - // User does not want to download templates. - userWantsToDownloadTemplates = false; - } else { - userWantsToDownloadTemplates = true; - } - } - - } else { - int result = createUpdateTemplatesDialog(); - isUpdateDialogShown = true; - if (result == 1) { - // User does not want to download templates. - userWantsToDownloadTemplates = false; - } else { - userWantsToDownloadTemplates = true; - } - } - } - } - if (userWantsToDownloadTemplates) { + if (generatorProj.exists()) { return generatorProj; - } else { - return null; } } + return null; } @@ -265,7 +236,7 @@ public static void processJar(String fileName) throws MalformedURLException, IOE } try { - TemplateAdapter templateAdapter = new TemplateAdapterImpl(templateDirectory); + TemplateAdapter templateAdapter = new TemplateAdapterImpl(null); templateAdapter.adaptMonolithicTemplates(templateDirectory.resolve(ConfigurationConstants.COBIGEN_TEMPLATES), false); } catch (Exception e) { diff --git a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java index 746460d832..033758709b 100644 --- a/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java +++ b/cobigen-eclipse/cobigen-eclipse/src/com/devonfw/cobigen/eclipse/workbenchcontrol/handler/AdaptTemplatesHandler.java @@ -2,10 +2,10 @@ import java.io.File; import java.io.IOException; -import java.net.MalformedURLException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Comparator; +import java.util.List; import java.util.UUID; import org.eclipse.core.commands.AbstractHandler; @@ -27,11 +27,15 @@ import org.slf4j.LoggerFactory; import org.slf4j.MDC; +import com.devonfw.cobigen.api.TemplateAdapter; +import com.devonfw.cobigen.api.constants.ConfigurationConstants; +import com.devonfw.cobigen.api.exception.TemplateSelectionForAdaptionException; +import com.devonfw.cobigen.api.exception.UpgradeTemplatesNotificationException; import com.devonfw.cobigen.eclipse.common.constants.InfrastructureConstants; import com.devonfw.cobigen.eclipse.common.constants.external.ResourceConstants; import com.devonfw.cobigen.eclipse.common.tools.ExceptionHandler; -import com.devonfw.cobigen.eclipse.common.tools.PlatformUIUtil; import com.devonfw.cobigen.eclipse.common.tools.ResourcesPluginUtil; +import com.devonfw.cobigen.impl.adapter.TemplateAdapterImpl; /** * Handler for the Package-Explorer EventfimportProjectIntoWorkspace @@ -53,31 +57,23 @@ public Object execute(ExecutionEvent event) throws ExecutionException { MDC.put(InfrastructureConstants.CORRELATION_ID, UUID.randomUUID().toString()); IProject generatorProj = ResourcesPlugin.getWorkspace().getRoot().getProject(ResourceConstants.CONFIG_PROJECT_NAME); - IProject generatorProjOfTempltesSets = ResourcesPlugin.getWorkspace().getRoot() - .getProject(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME); - Path templateSet = ResourcesPluginUtil.getTemplateSetDirectory(); - - if (generatorProjOfTempltesSets.exists()) { - // 1. the project is imported so, do not adapt(no need to extract the jar files) For now nothing to do. - - } else if (Files.exists(templateSet)) { - // 2. downloaded exists? first adapt the jar file (files), then import project to eclipse - - Path downloadedPath = templateSet.resolve(ResourceConstants.TEMPLATE_SETS_DOWNLOADED); - Path adaptedPath = templateSet.resolve(ResourceConstants.TEMPLATE_SETS_ADAPTED); + Path templateSet = ResourcesPluginUtil.getTemplateSetDirectory(); + File template = ResourcesPluginUtil.getTemplatesDirectory(); - // A. adapt the jar files if not already adapted - if (Files.exists(downloadedPath) && !Files.exists(adaptedPath)) - ResourcesPluginUtil.adaptTemplateSet(templateSet); + TemplateAdapter templateAdapter; - // B. Import the project - importProjectIntoWorkspace(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME, templateSet); - /* - * 3. TODO downloaded does not exists? update command must be executed. then go to 2. // (step 3 can be ignored - * for now until the new template-sets are deployed online.) - */ + if (Files.exists(templateSet)) { + templateAdapter = new TemplateAdapterImpl(templateSet); + } else if (template.exists()) { + templateAdapter = new TemplateAdapterImpl(template.toPath()); } else { + LOG.debug("No configuration project was found"); + return null; + } + try { + templateAdapter.adaptTemplates(); + if (generatorProj.exists()) { MessageDialog dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Info!", null, "CobiGen_Templates folder is already imported, click on Update templates button to update with latest. ", @@ -94,20 +90,6 @@ public Object execute(ExecutionEvent event) throws ExecutionException { if (result == 0) { try { - String fileName = ResourcesPluginUtil.getJarPath(true); - if (fileName.equals("")) { - result = createUpdateTemplatesDialog(); - if (result == 1) { - MessageDialog.openWarning(Display.getDefault().getActiveShell(), "Warning", - "Templates have not been found, please download them!"); - throw new NullPointerException("Templates have not been found!"); - } else { - fileName = ResourcesPluginUtil.downloadJar(true); - } - - } - ResourcesPluginUtil.processJar(fileName); - importProjectIntoWorkspace(ResourceConstants.CONFIG_PROJECT_NAME, ResourcesPluginUtil.getTemplatesDirectory().toPath()); dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Information", null, @@ -115,19 +97,41 @@ public Object execute(ExecutionEvent event) throws ExecutionException { 1); dialog.setBlockOnOpen(true); dialog.open(); - } catch (MalformedURLException e) { - LOG.error("An exception with download url of maven central", e); - PlatformUIUtil.openErrorDialog("An exception with download url of maven central", e); - } catch (IOException e) { - LOG.error("An exception occurred while writing Jar files to .metadata folder", e); - PlatformUIUtil.openErrorDialog("An exception occurred while writing Jar files to .metadata folder", e); } catch (Throwable e) { ExceptionHandler.handle(e, HandlerUtil.getActiveShell(event)); } } MDC.remove(InfrastructureConstants.CORRELATION_ID); } + + } catch (TemplateSelectionForAdaptionException e) { + List templateJars = e.getTemplateSets(); + if (!templateJars.isEmpty()) { + + try { + // Adapt process + templateAdapter.adaptTemplateSets(templateJars, false); + + // B. Import into Eclipse + importProjectIntoWorkspace(ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME, templateSet); + MessageDialog dialog = new MessageDialog(Display.getDefault().getActiveShell(), "Information", null, + "CobiGen_Templates folder is imported sucessfully", MessageDialog.INFORMATION, new String[] { "Ok" }, 1); + dialog.setBlockOnOpen(true); + dialog.open(); + + } catch (IOException e1) { + LOG.debug("Adapt was not possible", e1); + } + + } else { + LOG.info("No template set jars found to extract."); + } + } catch (IOException e) { + LOG.debug("Error occur", e); + } catch (UpgradeTemplatesNotificationException e) { + LOG.debug("Upgrade Process", e); } + return null; } @@ -150,7 +154,7 @@ private void importProjectIntoWorkspace(String projectName, Path projectPath) { if (projectName == ResourceConstants.TEMPLATE_SETS_CONFIG_PROJECT_NAME) { description.setLocationURI(projectPath.toUri()); } else { - description.setLocation(new org.eclipse.core.runtime.Path(this.ws.toPortableString() + "/" + projectName)); + description.setLocationURI(projectPath.resolve(ConfigurationConstants.COBIGEN_TEMPLATES).toUri()); } project.create(description, null); diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/CobiGenPaths.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/CobiGenPaths.java index 83373ced56..32a55384a9 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/CobiGenPaths.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/CobiGenPaths.java @@ -93,17 +93,9 @@ public static Path getTemplatesFolderPath() { */ public static Path getTemplatesFolderPath(Path home) { - File file = new File(home.toString()); - int lastSlash = file.getName().lastIndexOf("/"); - String lastWord = file.getName().substring(lastSlash + 1); - if (lastWord.equals(ConfigurationConstants.TEMPLATES_FOLDER)) { - return home; - } else { - home = home.resolve(ConfigurationConstants.TEMPLATES_FOLDER); - - } + Path templatesPath = home.resolve(ConfigurationConstants.TEMPLATES_FOLDER); - return home; + return createFolder(templatesPath); } /** diff --git a/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/adapter/TemplateAdapterImpl.java b/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/adapter/TemplateAdapterImpl.java index bbb91ae75e..29aceef3a2 100644 --- a/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/adapter/TemplateAdapterImpl.java +++ b/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/adapter/TemplateAdapterImpl.java @@ -80,7 +80,6 @@ public void adaptTemplates() if (isMonolithicTemplatesConfiguration()) { Path destinationPath = this.templatesLocation.resolve(ConfigurationConstants.COBIGEN_TEMPLATES); adaptMonolithicTemplates(destinationPath, false); - throw new UpgradeTemplatesNotificationException(); } else { throw new TemplateSelectionForAdaptionException(getTemplateSetJars()); } From 7ef6a4f02b56f0da38cd33fc15c8ae744f77b8bc Mon Sep 17 00:00:00 2001 From: cedricarnauld123 Date: Mon, 27 Mar 2023 16:49:07 +0200 Subject: [PATCH 10/10] #1509 implemented testAdaptMonolithicTemplatesAndGenerate and testBasicOpenAPIGenerationWithAdaptMonolithicTemplates --- .../crud-java-server-app-complex/pom.xml | 83 +++++++++++++++++++ .../src/main/java/com/test.java | 0 .../src/main/resources/functions.ftl | 0 .../src/main/resources/template-set.xml | 40 +++++++++ .../to/${variables.entityName}Eto.java.ftl | 1 + .../adapted/crud-java-server-app/pom.xml | 83 +++++++++++++++++++ .../src/main/java/com/test.java | 0 .../src/main/resources/functions.ftl | 0 .../src/main/resources/template-set.xml | 40 +++++++++ .../to/${variables.entityName}Eto.java.ftl | 1 + 10 files changed, 248 insertions(+) create mode 100644 cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app-complex/pom.xml create mode 100644 cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app-complex/src/main/java/com/test.java create mode 100644 cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app-complex/src/main/resources/functions.ftl create mode 100644 cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app-complex/src/main/resources/template-set.xml create mode 100644 cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app-complex/src/main/resources/templates/java/${variables.rootPackage}/${variables.component}/logic/api/to/${variables.entityName}Eto.java.ftl create mode 100644 cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app/pom.xml create mode 100644 cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app/src/main/java/com/test.java create mode 100644 cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app/src/main/resources/functions.ftl create mode 100644 cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app/src/main/resources/template-set.xml create mode 100644 cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app/src/main/resources/templates/java/${variables.rootPackage}/${variables.component}/logic/api/to/${variables.entityName}Eto.java.ftl diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app-complex/pom.xml b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app-complex/pom.xml new file mode 100644 index 0000000000..3c69a2539d --- /dev/null +++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app-complex/pom.xml @@ -0,0 +1,83 @@ + + + 4.0.0 + com.devonfw.test + crud-java-server-app-complex + 1.0.0 + jar + + 1.8 + 1.8 + + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.3.1 + + ../../downloaded + + + + org.apache.maven.plugins + maven-source-plugin + + + attach-sources + + jar + + + + + ${skip.deployment} + ../../downloaded + + + + org.apache.maven.plugins + maven-resources-plugin + 2.7 + + UTF-8 + + + + + org.codehaus.mojo + templating-maven-plugin + 1.0.0 + + + generate-maven-properties-class + + filter-sources + + + + + + org.apache.maven.plugins + maven-antrun-plugin + + + prepare-package + + run + + + + Integrate POM manually + + + + + + + + + + \ No newline at end of file diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app-complex/src/main/java/com/test.java b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app-complex/src/main/java/com/test.java new file mode 100644 index 0000000000..e69de29bb2 diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app-complex/src/main/resources/functions.ftl b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app-complex/src/main/resources/functions.ftl new file mode 100644 index 0000000000..e69de29bb2 diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app-complex/src/main/resources/template-set.xml b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app-complex/src/main/resources/template-set.xml new file mode 100644 index 0000000000..5cfe219789 --- /dev/null +++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app-complex/src/main/resources/template-set.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app-complex/src/main/resources/templates/java/${variables.rootPackage}/${variables.component}/logic/api/to/${variables.entityName}Eto.java.ftl b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app-complex/src/main/resources/templates/java/${variables.rootPackage}/${variables.component}/logic/api/to/${variables.entityName}Eto.java.ftl new file mode 100644 index 0000000000..0c233890ea --- /dev/null +++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app-complex/src/main/resources/templates/java/${variables.rootPackage}/${variables.component}/logic/api/to/${variables.entityName}Eto.java.ftl @@ -0,0 +1 @@ +package test; \ No newline at end of file diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app/pom.xml b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app/pom.xml new file mode 100644 index 0000000000..3558920d14 --- /dev/null +++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app/pom.xml @@ -0,0 +1,83 @@ + + + 4.0.0 + com.devonfw.test + crud-java-server-app + 1.0.0 + jar + + 1.8 + 1.8 + + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.3.1 + + ../../downloaded + + + + org.apache.maven.plugins + maven-source-plugin + + + attach-sources + + jar + + + + + ${skip.deployment} + ../../downloaded + + + + org.apache.maven.plugins + maven-resources-plugin + 2.7 + + UTF-8 + + + + + org.codehaus.mojo + templating-maven-plugin + 1.0.0 + + + generate-maven-properties-class + + filter-sources + + + + + + org.apache.maven.plugins + maven-antrun-plugin + + + prepare-package + + run + + + + Integrate POM manually + + + + + + + + + + \ No newline at end of file diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app/src/main/java/com/test.java b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app/src/main/java/com/test.java new file mode 100644 index 0000000000..e69de29bb2 diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app/src/main/resources/functions.ftl b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app/src/main/resources/functions.ftl new file mode 100644 index 0000000000..e69de29bb2 diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app/src/main/resources/template-set.xml b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app/src/main/resources/template-set.xml new file mode 100644 index 0000000000..4515b452ec --- /dev/null +++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app/src/main/resources/template-set.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app/src/main/resources/templates/java/${variables.rootPackage}/${variables.component}/logic/api/to/${variables.entityName}Eto.java.ftl b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app/src/main/resources/templates/java/${variables.rootPackage}/${variables.component}/logic/api/to/${variables.entityName}Eto.java.ftl new file mode 100644 index 0000000000..0c233890ea --- /dev/null +++ b/cobigen-eclipse/cobigen-eclipse-test/src/main/resources/AdaptTemplatesTest/template-sets/adapted/crud-java-server-app/src/main/resources/templates/java/${variables.rootPackage}/${variables.component}/logic/api/to/${variables.entityName}Eto.java.ftl @@ -0,0 +1 @@ +package test; \ No newline at end of file