|
| 1 | +/******************************************************************************* |
| 2 | +* Copyright (c) 2022, 2023 IBM Corporation and others. |
| 3 | +* |
| 4 | +* This program and the accompanying materials are made available under the |
| 5 | +* terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | +* http://www.eclipse.org/legal/epl-2.0. |
| 7 | +* |
| 8 | +* SPDX-License-Identifier: EPL-2.0 |
| 9 | +* |
| 10 | +* Contributors: |
| 11 | +* IBM Corporation - initial implementation |
| 12 | +*******************************************************************************/ |
| 13 | +package io.openliberty.tools.eclipse.test.it; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 16 | + |
| 17 | +import java.io.File; |
| 18 | +import java.nio.file.Path; |
| 19 | +import java.nio.file.Paths; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 25 | +import org.junit.jupiter.api.AfterAll; |
| 26 | +import org.junit.jupiter.api.BeforeAll; |
| 27 | +import org.junit.jupiter.api.Disabled; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import io.openliberty.tools.eclipse.test.it.utils.LibertyPluginTestUtils; |
| 31 | +import io.openliberty.tools.eclipse.test.it.utils.SWTBotPluginOperations; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests LSP4Jakarta functionality within Liberty Tools for Eclipse |
| 35 | + */ |
| 36 | +public class LibertyPluginSWTBotLSP4JakartaTest extends AbstractLibertyPluginSWTBotTest { |
| 37 | + |
| 38 | + /** |
| 39 | + * Wrapper Application name. |
| 40 | + */ |
| 41 | + static final String MVN_WRAPPER_APP_NAME = "liberty.maven.test.wrapper.app"; |
| 42 | + |
| 43 | + /** |
| 44 | + * Test app relative path. |
| 45 | + */ |
| 46 | + static final Path wrapperProjectPath = Paths.get("resources", "applications", "maven", "liberty-maven-test-wrapper-app"); |
| 47 | + |
| 48 | + static ArrayList<String> projectPaths = new ArrayList<String>(); |
| 49 | + |
| 50 | + /** |
| 51 | + * Text to add to editor |
| 52 | + */ |
| 53 | + static final String WEB_SERVLET_IMPORT_STRING = "import jakarta.servlet.annotation.WebServlet;\r\n"; |
| 54 | + static final String WEB_SERVLET_EMPTY_ANNO_STRING = "@WebServlet()\r\n"; |
| 55 | + |
| 56 | + /** |
| 57 | + * Expected quick-fixes |
| 58 | + */ |
| 59 | + static String[] webServlet_quickFixes = new String[] { "Add the `urlPatterns` attribute to @WebServlet", |
| 60 | + "Add the `value` attribute to @WebServlet" }; |
| 61 | + |
| 62 | + /** |
| 63 | + * Expected type-ahead options when at highest level in class. |
| 64 | + */ |
| 65 | + static String[] typeAheadOptions_classLevel = new String[] { "rest_class", "persist_entity", "servlet_doget", "servlet_dopost", |
| 66 | + "servlet_generic", "servlet_webfilter" }; |
| 67 | + |
| 68 | + /** |
| 69 | + * Expected type-ahead options within REST class |
| 70 | + */ |
| 71 | + static String[] typeAheadOptions_inClass = new String[] { "persist_context", "persist_context_extended", |
| 72 | + "persist_context_extended_unsync", "rest_head", "rest_get", "rest_post", "rest_put", "rest_delete", "tx_user_inject", |
| 73 | + "tx_user_jndi" }; |
| 74 | + |
| 75 | + /** |
| 76 | + * Setup. |
| 77 | + */ |
| 78 | + @BeforeAll |
| 79 | + public static void setup() { |
| 80 | + |
| 81 | + commonSetup(); |
| 82 | + |
| 83 | + File workspaceRoot = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile(); |
| 84 | + projectPaths.add(wrapperProjectPath.toString()); |
| 85 | + |
| 86 | + // Maybe redundant but we really want to cleanup. We really want to |
| 87 | + // avoid wasting time debugging tricky differences in behavior because of a dirty re-run |
| 88 | + for (String p : projectPaths) { |
| 89 | + cleanupProject(p); |
| 90 | + } |
| 91 | + |
| 92 | + importMavenProjects(workspaceRoot, projectPaths); |
| 93 | + } |
| 94 | + |
| 95 | + @AfterAll |
| 96 | + public static void cleanup() { |
| 97 | + for (String p : projectPaths) { |
| 98 | + cleanupProject(p); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Verify the class level snippets are available |
| 104 | + */ |
| 105 | + @Test |
| 106 | + public void testClassLevelSnippets() { |
| 107 | + |
| 108 | + try { |
| 109 | + // Open new class file |
| 110 | + SWTBotPluginOperations.createNewClass(bot, MVN_WRAPPER_APP_NAME, "MyClass", true); |
| 111 | + |
| 112 | + // Get type-ahead list |
| 113 | + List<String> typeAheadOptions = SWTBotPluginOperations.getTypeAheadList(bot, "MyClass.java", "", 0, 0); |
| 114 | + |
| 115 | + boolean allFound = true; |
| 116 | + List<String> missingOptions = new ArrayList<String>(); |
| 117 | + for (String option : typeAheadOptions_classLevel) { |
| 118 | + if (!typeAheadOptions.contains(option)) { |
| 119 | + allFound = false; |
| 120 | + missingOptions.add(option); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + assertTrue(allFound, "Missing type-ahead options: " + Arrays.toString(missingOptions.toArray())); |
| 125 | + |
| 126 | + } finally { |
| 127 | + |
| 128 | + // Delete new file |
| 129 | + LibertyPluginTestUtils.deleteFile(new File(wrapperProjectPath + "/src/main/java/test/maven/liberty/web/app/MyClass.java")); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Verify the in class snippets are available |
| 135 | + */ |
| 136 | + @Test |
| 137 | + public void testInClassSnippets() { |
| 138 | + |
| 139 | + try { |
| 140 | + // Open new class file |
| 141 | + SWTBotPluginOperations.createNewClass(bot, MVN_WRAPPER_APP_NAME, "MyClass", false); |
| 142 | + |
| 143 | + // Get type-ahead list |
| 144 | + List<String> typeAheadOptions = SWTBotPluginOperations.getTypeAheadList(bot, "MyClass.java", "", 3, 0); |
| 145 | + |
| 146 | + boolean allFound = true; |
| 147 | + List<String> missingOptions = new ArrayList<String>(); |
| 148 | + for (String option : typeAheadOptions_inClass) { |
| 149 | + if (!typeAheadOptions.contains(option)) { |
| 150 | + allFound = false; |
| 151 | + missingOptions.add(option); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + assertTrue(allFound, "Missing type-ahead options: " + Arrays.toString(missingOptions.toArray())); |
| 156 | + |
| 157 | + } finally { |
| 158 | + |
| 159 | + // Delete new file |
| 160 | + LibertyPluginTestUtils.deleteFile(new File(wrapperProjectPath + "/src/main/java/test/maven/liberty/web/app/MyClass.java")); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Verify diagnostics and quick fixes |
| 166 | + */ |
| 167 | + @Test |
| 168 | + @Disabled("Issue 377") |
| 169 | + public void testDiagnosticsAndQuickFixes() { |
| 170 | + |
| 171 | + try { |
| 172 | + // Open new class file |
| 173 | + SWTBotPluginOperations.createNewClass(bot, MVN_WRAPPER_APP_NAME, "MyClass", true); |
| 174 | + |
| 175 | + // Select the "servlet_generic" snippet |
| 176 | + SWTBotPluginOperations.selectTypeAheadOption(bot, "MyClass.java", "servlet_generic", 0, 0); |
| 177 | + |
| 178 | + // Add WebServlet annotation |
| 179 | + SWTBotPluginOperations.addTextToEditor(bot, "MyClass.java", WEB_SERVLET_EMPTY_ANNO_STRING, 8, 0); |
| 180 | + SWTBotPluginOperations.addTextToEditor(bot, "MyClass.java", WEB_SERVLET_IMPORT_STRING, 7, 0); |
| 181 | + |
| 182 | + // Get quick-fix list |
| 183 | + List<String> quickFixes = SWTBotPluginOperations.getQuickFixList(bot, "MyClass.java"); |
| 184 | + |
| 185 | + boolean allFound = true; |
| 186 | + List<String> missingFixes = new ArrayList<String>(); |
| 187 | + for (String fix : webServlet_quickFixes) { |
| 188 | + if (!quickFixes.contains(fix)) { |
| 189 | + allFound = false; |
| 190 | + missingFixes.add(fix); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + assertTrue(allFound, "Missing quick-fixes: " + Arrays.toString(missingFixes.toArray())); |
| 195 | + |
| 196 | + } finally { |
| 197 | + |
| 198 | + // Delete new file |
| 199 | + LibertyPluginTestUtils.deleteFile(new File(wrapperProjectPath + "/src/main/java/test/maven/liberty/web/app/MyClass.java")); |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments