From 9728ddf471c533cacda239fd7cbc8fee6f549b2b Mon Sep 17 00:00:00 2001 From: dessina-devasia <143582034+dessina-devasia@users.noreply.github.com> Date: Thu, 6 Nov 2025 19:02:22 +0530 Subject: [PATCH 1/5] Added testcase to verify LSP4IJ handles cancelled jakarta/java/codeAction requests gracefully --- .../JakartaCodeActionCancellationTest.java | 67 +++++++++++++++++++ .../lsp4ij/mock/MockLanguageServer.java | 6 ++ .../lsp4ij/mock/MockTextDocumentService.java | 11 ++- 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/JakartaCodeActionCancellationTest.java diff --git a/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/JakartaCodeActionCancellationTest.java b/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/JakartaCodeActionCancellationTest.java new file mode 100644 index 000000000..93ab2955f --- /dev/null +++ b/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/JakartaCodeActionCancellationTest.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (c) 2025 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package com.redhat.devtools.lsp4ij.features.codeAction; + +import com.redhat.devtools.lsp4ij.fixtures.LSPCodeActionFixtureTestCase; +import com.redhat.devtools.lsp4ij.mock.MockLanguageServer; +import org.eclipse.lsp4j.CodeActionParams; +import org.eclipse.lsp4j.Command; +import org.eclipse.lsp4j.CodeAction; +import org.eclipse.lsp4j.jsonrpc.messages.Either; + +import java.util.List; +import java.util.concurrent.CompletableFuture; + +/** + * Test case that emulates the language server cancelling a 'textDocument/codeAction' request. + */ +public class JakartaCodeActionCancellationTest extends LSPCodeActionFixtureTestCase { + + private static final String NOT_JAVA_EXTENSION = "javax"; + private static final String TEST_FILE_NAME = "InvalidWebFilter." + NOT_JAVA_EXTENSION; + + public JakartaCodeActionCancellationTest() { + super("*." + NOT_JAVA_EXTENSION); + } + + /** + * Simulates the language server cancelling a jakarta/java/codeAction request (LSP error -32800) + * and verifies that LSP4IJ handles the cancellation gracefully. + */ + public void testJakartaCodeActionCancelledRequest() { + // Configures mock language server to cancel the codeAction request + MockLanguageServer.INSTANCE.setCodeActionHandler((CodeActionParams params) -> { + CompletableFuture>> cancelledFuture = new CompletableFuture<>(); + cancelledFuture.cancel(true); + return cancelledFuture; + }); + + // Configure the Jakarta sample file + myFixture.configureByText(TEST_FILE_NAME, + // language=JAVA + """ +package io.openliberty.sample.jakarta.servlet; + +import jakarta.servlet.Filter; +import jakarta.servlet.annotation.WebFilter; + +@WebFilter() +public abstract class InvalidWebFilter implements Filter { + +}"""); + + // Attempt to retrieve quick fixes (should trigger textDocument/codeAction) + var allQuickFixes = myFixture.getAvailableIntentions(); + + // Verify that the cancellation results in no quick fixes + assertEmpty("No quick fixes should be returned when the language server cancels the request", allQuickFixes); + } +} diff --git a/src/test/java/com/redhat/devtools/lsp4ij/mock/MockLanguageServer.java b/src/test/java/com/redhat/devtools/lsp4ij/mock/MockLanguageServer.java index 3ce8ff309..7a6815046 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/mock/MockLanguageServer.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/mock/MockLanguageServer.java @@ -309,6 +309,12 @@ public void setDocumentSymbols(DocumentSymbol... documentSymbols) { this.textDocumentService.setDocumentSymbols(Arrays.asList(documentSymbols)); } + public void setCodeActionHandler(Function>>> handler) { + if (this.textDocumentService != null) { + this.textDocumentService.setCodeActionHandler(handler); + } + } + @Override public NotebookDocumentService getNotebookDocumentService() { return new NotebookDocumentService() { diff --git a/src/test/java/com/redhat/devtools/lsp4ij/mock/MockTextDocumentService.java b/src/test/java/com/redhat/devtools/lsp4ij/mock/MockTextDocumentService.java index 0b795700f..792e37e06 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/mock/MockTextDocumentService.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/mock/MockTextDocumentService.java @@ -68,6 +68,7 @@ public class MockTextDocumentService implements TextDocumentService { private SemanticTokens mockSemanticTokens; private List foldingRanges; public int codeActionRequests = 0; + private Function>>> codeActionHandler; public MockTextDocumentService(Function> futureFactory) { this._futureFactory = futureFactory; @@ -147,11 +148,15 @@ public CompletableFuture> documentLink(DocumentLinkParams par @Override public CompletableFuture>> codeAction(CodeActionParams params) { codeActionRequests++; + // For a custom handler + if (codeActionHandler != null) { + return codeActionHandler.apply(params); + } // Filter code actions by using params.getContext().getOnly() var only = (params.getContext() != null && params.getContext().getOnly() != null && !params.getContext().getOnly().isEmpty()) ? params.getContext().getOnly() : null; var filteredCodeActions = mockCodeActions; - if (only != null) { + if (only != null && mockCodeActions != null) { filteredCodeActions = mockCodeActions .stream() .filter(ca -> { @@ -411,6 +416,10 @@ public void setMockSelectionRanges(List mockSelectionRanges) { this.mockSelectionRanges = mockSelectionRanges; } + public void setCodeActionHandler(Function>>> handler) { + this.codeActionHandler = handler; + } + @Override public CompletableFuture> selectionRange(SelectionRangeParams params) { // Find the mock selection ranges that apply to the specified position. This allows us to have a single mock From e5544be75f761f0f243a7bb4571d1471c79f69d9 Mon Sep 17 00:00:00 2001 From: dessina-devasia <143582034+dessina-devasia@users.noreply.github.com> Date: Fri, 7 Nov 2025 16:17:41 +0530 Subject: [PATCH 2/5] test: added test case for code action cancellation --- .../CodeActionCancellationTest.java | 183 ++++++++++++++++++ .../JakartaCodeActionCancellationTest.java | 67 ------- .../codeAction/WebFilterQuickFixTest.java | 1 + .../LSPCodeActionFixtureTestCase.java | 19 +- 4 files changed, 201 insertions(+), 69 deletions(-) create mode 100644 src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/CodeActionCancellationTest.java delete mode 100644 src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/JakartaCodeActionCancellationTest.java diff --git a/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/CodeActionCancellationTest.java b/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/CodeActionCancellationTest.java new file mode 100644 index 000000000..99c0e23b2 --- /dev/null +++ b/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/CodeActionCancellationTest.java @@ -0,0 +1,183 @@ +/******************************************************************************* + * Copyright (c) 2025 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package com.redhat.devtools.lsp4ij.features.codeAction; + +import com.redhat.devtools.lsp4ij.fixtures.LSPCodeActionFixtureTestCase; + +/** + * Test case for InvalidWebFilter quick fix + */ +public class CodeActionCancellationTest extends LSPCodeActionFixtureTestCase { + + private static final String NOT_JAVA_EXTENSION = "javax"; + private static final String TEST_FILE_NAME = "InvalidWebFilter." + NOT_JAVA_EXTENSION; + + public CodeActionCancellationTest() { + super("*." + NOT_JAVA_EXTENSION); + } + + public void testWebFilterQuickFix() { + + assertCodeActions(TEST_FILE_NAME, + IntentionActionKind.QUICK_FIX_ONLY, + // language=JAVA + """ + package io.openliberty.sample.jakarta.servlet; + + import jakarta.servlet.Filter; + import jakarta.servlet.annotation.WebFilter; + + @WebFilter() + public abstract class InvalidWebFilter implements Filter { + + }""", + // language=JSON + """ + [ + { + "title": "Add the `servletNames` attribute to @WebFilter", + "kind": "quickfix", + "diagnostics": [ + { + "range": { + "start": { + "line": 5, + "character": 0 + }, + "end": { + "line": 5, + "character": 12 + } + }, + "severity": 1, + "code": "CompleteWebFilterAttributes", + "source": "jakarta-servlet", + "message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027." + } + ], + "data": { + "participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix", + "documentUri": "unused", + "range": { + "start": { + "line": 5, + "character": 0 + }, + "end": { + "line": 5, + "character": 12 + } + }, + "extendedData": { + "annotation": "jakarta.servlet.annotation.WebFilter", + "attribute": "servletNames", + "diagnosticCode": "CompleteWebFilterAttributes" + }, + "resourceOperationSupported": true, + "commandConfigurationUpdateSupported": false + } + }, + { + "title": "Add the `urlPatterns` attribute to @WebFilter", + "kind": "quickfix", + "diagnostics": [ + { + "range": { + "start": { + "line": 5, + "character": 0 + }, + "end": { + "line": 5, + "character": 12 + } + }, + "severity": 1, + "code": "CompleteWebFilterAttributes", + "source": "jakarta-servlet", + "message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027." + } + ], + "data": { + "participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix", + "documentUri": "unused", + "range": { + "start": { + "line": 5, + "character": 0 + }, + "end": { + "line": 5, + "character": 12 + } + }, + "extendedData": { + "annotation": "jakarta.servlet.annotation.WebFilter", + "attribute": "urlPatterns", + "diagnosticCode": "CompleteWebFilterAttributes" + }, + "resourceOperationSupported": true, + "commandConfigurationUpdateSupported": false + } + }, + { + "title": "Add the `value` attribute to @WebFilter", + "kind": "quickfix", + "diagnostics": [ + { + "range": { + "start": { + "line": 5, + "character": 0 + }, + "end": { + "line": 5, + "character": 12 + } + }, + "severity": 1, + "code": "CompleteWebFilterAttributes", + "source": "jakarta-servlet", + "message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027." + } + ], + "data": { + "participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix", + "documentUri": "unused", + "range": { + "start": { + "line": 5, + "character": 0 + }, + "end": { + "line": 5, + "character": 12 + } + }, + "extendedData": { + "annotation": "jakarta.servlet.annotation.WebFilter", + "attribute": "value", + "diagnosticCode": "CompleteWebFilterAttributes" + }, + "resourceOperationSupported": true, + "commandConfigurationUpdateSupported": false + } + } + ] + """, + true, // simulateCancellation = true + "Add the `servletNames` attribute to @WebFilter", + "Add the `urlPatterns` attribute to @WebFilter", + "Add the `value` attribute to @WebFilter" + ); + } + +} \ No newline at end of file diff --git a/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/JakartaCodeActionCancellationTest.java b/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/JakartaCodeActionCancellationTest.java deleted file mode 100644 index 93ab2955f..000000000 --- a/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/JakartaCodeActionCancellationTest.java +++ /dev/null @@ -1,67 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2025 Red Hat, Inc. - * Distributed under license by Red Hat, Inc. All rights reserved. - * This program is made available under the terms of the - * Eclipse Public License v2.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v20.html - * - * Contributors: - * Red Hat, Inc. - initial API and implementation - ******************************************************************************/ -package com.redhat.devtools.lsp4ij.features.codeAction; - -import com.redhat.devtools.lsp4ij.fixtures.LSPCodeActionFixtureTestCase; -import com.redhat.devtools.lsp4ij.mock.MockLanguageServer; -import org.eclipse.lsp4j.CodeActionParams; -import org.eclipse.lsp4j.Command; -import org.eclipse.lsp4j.CodeAction; -import org.eclipse.lsp4j.jsonrpc.messages.Either; - -import java.util.List; -import java.util.concurrent.CompletableFuture; - -/** - * Test case that emulates the language server cancelling a 'textDocument/codeAction' request. - */ -public class JakartaCodeActionCancellationTest extends LSPCodeActionFixtureTestCase { - - private static final String NOT_JAVA_EXTENSION = "javax"; - private static final String TEST_FILE_NAME = "InvalidWebFilter." + NOT_JAVA_EXTENSION; - - public JakartaCodeActionCancellationTest() { - super("*." + NOT_JAVA_EXTENSION); - } - - /** - * Simulates the language server cancelling a jakarta/java/codeAction request (LSP error -32800) - * and verifies that LSP4IJ handles the cancellation gracefully. - */ - public void testJakartaCodeActionCancelledRequest() { - // Configures mock language server to cancel the codeAction request - MockLanguageServer.INSTANCE.setCodeActionHandler((CodeActionParams params) -> { - CompletableFuture>> cancelledFuture = new CompletableFuture<>(); - cancelledFuture.cancel(true); - return cancelledFuture; - }); - - // Configure the Jakarta sample file - myFixture.configureByText(TEST_FILE_NAME, - // language=JAVA - """ -package io.openliberty.sample.jakarta.servlet; - -import jakarta.servlet.Filter; -import jakarta.servlet.annotation.WebFilter; - -@WebFilter() -public abstract class InvalidWebFilter implements Filter { - -}"""); - - // Attempt to retrieve quick fixes (should trigger textDocument/codeAction) - var allQuickFixes = myFixture.getAvailableIntentions(); - - // Verify that the cancellation results in no quick fixes - assertEmpty("No quick fixes should be returned when the language server cancels the request", allQuickFixes); - } -} diff --git a/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/WebFilterQuickFixTest.java b/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/WebFilterQuickFixTest.java index f6539aaf4..415907dfa 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/WebFilterQuickFixTest.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/WebFilterQuickFixTest.java @@ -173,6 +173,7 @@ public abstract class InvalidWebFilter implements Filter { } ]""" , + false, "Add the `servletNames` attribute to @WebFilter", "Add the `urlPatterns` attribute to @WebFilter", "Add the `value` attribute to @WebFilter"); diff --git a/src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPCodeActionFixtureTestCase.java b/src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPCodeActionFixtureTestCase.java index fc55a4800..c5df0f941 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPCodeActionFixtureTestCase.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPCodeActionFixtureTestCase.java @@ -31,6 +31,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; /** @@ -61,12 +62,13 @@ protected List assertCodeActions(@NotNull String fileName, @NotNull IntentionActionKind kind, @NotNull String editorContentText, @NotNull String codeActionsJson, + boolean simulateCancellation, @NotNull String... expectedActions) { List codeActions = JSONUtils.getLsp4jGson() .fromJson(codeActionsJson, new TypeToken>() { }.getType()); - return assertCodeActions(fileName, kind, editorContentText, codeActions, expectedActions); + return assertCodeActions(fileName, kind, editorContentText, codeActions, simulateCancellation, expectedActions); } /** @@ -82,6 +84,7 @@ protected List assertCodeActions(@NotNull String fileName, @NotNull IntentionActionKind kind, @NotNull String editorContentText, @NotNull List codeActions, + boolean simulateCancellation, @NotNull String... expectedActions) { List> wrappedCodeActions = codeActions.stream() .distinct() @@ -96,8 +99,20 @@ protected List assertCodeActions(@NotNull String fileName, .collect(Collectors.toList()); MockLanguageServer.INSTANCE.setTimeToProceedQueries(200); - MockLanguageServer.INSTANCE.setCodeActions(wrappedCodeActions); MockLanguageServer.INSTANCE.setPublishDiagnostics(diagnostics); + + if (simulateCancellation) { + // Simulate a cancelled CodeAction request + MockLanguageServer.INSTANCE.setCodeActionHandler(params -> { + CompletableFuture>> cancelled = new CompletableFuture<>(); + cancelled.cancel(true); + return cancelled; + }); + } else { + // Normal code action handling + MockLanguageServer.INSTANCE.setCodeActions(wrappedCodeActions); + } + myFixture.configureByText(fileName, editorContentText); // Collect IntelliJ Quick fixes / Intention actions From 8477a03b563384f6b496923be8f667b87a77a1fc Mon Sep 17 00:00:00 2001 From: dessina-devasia <143582034+dessina-devasia@users.noreply.github.com> Date: Fri, 7 Nov 2025 16:21:23 +0530 Subject: [PATCH 3/5] test: resolved format related issue --- .../CodeActionCancellationTest.java | 281 +++++++++--------- .../codeAction/WebFilterQuickFixTest.java | 2 +- 2 files changed, 141 insertions(+), 142 deletions(-) diff --git a/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/CodeActionCancellationTest.java b/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/CodeActionCancellationTest.java index 99c0e23b2..1a25bcd52 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/CodeActionCancellationTest.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/CodeActionCancellationTest.java @@ -27,157 +27,156 @@ public CodeActionCancellationTest() { public void testWebFilterQuickFix() { assertCodeActions(TEST_FILE_NAME, - IntentionActionKind.QUICK_FIX_ONLY, - // language=JAVA - """ - package io.openliberty.sample.jakarta.servlet; - - import jakarta.servlet.Filter; - import jakarta.servlet.annotation.WebFilter; - - @WebFilter() - public abstract class InvalidWebFilter implements Filter { - - }""", - // language=JSON - """ - [ + IntentionActionKind.QUICK_FIX_ONLY, + // language=JAVA + """ + package io.openliberty.sample.jakarta.servlet; + + import jakarta.servlet.Filter; + import jakarta.servlet.annotation.WebFilter; + + @WebFilter() + public abstract class InvalidWebFilter implements Filter { + + }""", + // language=JSON + """ + [ + { + "title": "Add the `servletNames` attribute to @WebFilter", + "kind": "quickfix", + "diagnostics": [ { - "title": "Add the `servletNames` attribute to @WebFilter", - "kind": "quickfix", - "diagnostics": [ - { - "range": { - "start": { - "line": 5, - "character": 0 - }, - "end": { - "line": 5, - "character": 12 - } - }, - "severity": 1, - "code": "CompleteWebFilterAttributes", - "source": "jakarta-servlet", - "message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027." - } - ], - "data": { - "participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix", - "documentUri": "unused", - "range": { - "start": { - "line": 5, - "character": 0 - }, - "end": { - "line": 5, - "character": 12 - } - }, - "extendedData": { - "annotation": "jakarta.servlet.annotation.WebFilter", - "attribute": "servletNames", - "diagnosticCode": "CompleteWebFilterAttributes" + "range": { + "start": { + "line": 5, + "character": 0 }, - "resourceOperationSupported": true, - "commandConfigurationUpdateSupported": false + "end": { + "line": 5, + "character": 12 + } + }, + "severity": 1, + "code": "CompleteWebFilterAttributes", + "source": "jakarta-servlet", + "message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027." + } + ], + "data": { + "participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix", + "documentUri": "unused", + "range": { + "start": { + "line": 5, + "character": 0 + }, + "end": { + "line": 5, + "character": 12 } }, + "extendedData": { + "annotation": "jakarta.servlet.annotation.WebFilter", + "attribute": "servletNames", + "diagnosticCode": "CompleteWebFilterAttributes" + }, + "resourceOperationSupported": true, + "commandConfigurationUpdateSupported": false + } + }, + { + "title": "Add the `urlPatterns` attribute to @WebFilter", + "kind": "quickfix", + "diagnostics": [ { - "title": "Add the `urlPatterns` attribute to @WebFilter", - "kind": "quickfix", - "diagnostics": [ - { - "range": { - "start": { - "line": 5, - "character": 0 - }, - "end": { - "line": 5, - "character": 12 - } - }, - "severity": 1, - "code": "CompleteWebFilterAttributes", - "source": "jakarta-servlet", - "message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027." - } - ], - "data": { - "participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix", - "documentUri": "unused", - "range": { - "start": { - "line": 5, - "character": 0 - }, - "end": { - "line": 5, - "character": 12 - } - }, - "extendedData": { - "annotation": "jakarta.servlet.annotation.WebFilter", - "attribute": "urlPatterns", - "diagnosticCode": "CompleteWebFilterAttributes" + "range": { + "start": { + "line": 5, + "character": 0 }, - "resourceOperationSupported": true, - "commandConfigurationUpdateSupported": false + "end": { + "line": 5, + "character": 12 + } + }, + "severity": 1, + "code": "CompleteWebFilterAttributes", + "source": "jakarta-servlet", + "message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027." + } + ], + "data": { + "participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix", + "documentUri": "unused", + "range": { + "start": { + "line": 5, + "character": 0 + }, + "end": { + "line": 5, + "character": 12 } }, + "extendedData": { + "annotation": "jakarta.servlet.annotation.WebFilter", + "attribute": "urlPatterns", + "diagnosticCode": "CompleteWebFilterAttributes" + }, + "resourceOperationSupported": true, + "commandConfigurationUpdateSupported": false + } + }, + { + "title": "Add the `value` attribute to @WebFilter", + "kind": "quickfix", + "diagnostics": [ { - "title": "Add the `value` attribute to @WebFilter", - "kind": "quickfix", - "diagnostics": [ - { - "range": { - "start": { - "line": 5, - "character": 0 - }, - "end": { - "line": 5, - "character": 12 - } - }, - "severity": 1, - "code": "CompleteWebFilterAttributes", - "source": "jakarta-servlet", - "message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027." - } - ], - "data": { - "participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix", - "documentUri": "unused", - "range": { - "start": { - "line": 5, - "character": 0 - }, - "end": { - "line": 5, - "character": 12 - } - }, - "extendedData": { - "annotation": "jakarta.servlet.annotation.WebFilter", - "attribute": "value", - "diagnosticCode": "CompleteWebFilterAttributes" + "range": { + "start": { + "line": 5, + "character": 0 }, - "resourceOperationSupported": true, - "commandConfigurationUpdateSupported": false - } + "end": { + "line": 5, + "character": 12 + } + }, + "severity": 1, + "code": "CompleteWebFilterAttributes", + "source": "jakarta-servlet", + "message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027." } - ] - """, - true, // simulateCancellation = true - "Add the `servletNames` attribute to @WebFilter", - "Add the `urlPatterns` attribute to @WebFilter", - "Add the `value` attribute to @WebFilter" - ); + ], + "data": { + "participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix", + "documentUri": "unused", + "range": { + "start": { + "line": 5, + "character": 0 + }, + "end": { + "line": 5, + "character": 12 + } + }, + "extendedData": { + "annotation": "jakarta.servlet.annotation.WebFilter", + "attribute": "value", + "diagnosticCode": "CompleteWebFilterAttributes" + }, + "resourceOperationSupported": true, + "commandConfigurationUpdateSupported": false + } + } + ] + """, + true, // simulateCancellation = true + "Add the `servletNames` attribute to @WebFilter", + "Add the `urlPatterns` attribute to @WebFilter", + "Add the `value` attribute to @WebFilter" + ); } - } \ No newline at end of file diff --git a/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/WebFilterQuickFixTest.java b/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/WebFilterQuickFixTest.java index 415907dfa..a8435a7bc 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/WebFilterQuickFixTest.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/WebFilterQuickFixTest.java @@ -173,7 +173,7 @@ public abstract class InvalidWebFilter implements Filter { } ]""" , - false, + false, // simulateCancellation = false "Add the `servletNames` attribute to @WebFilter", "Add the `urlPatterns` attribute to @WebFilter", "Add the `value` attribute to @WebFilter"); From 5707c8ad37e5b041dcb3575d2f756b11bd79463a Mon Sep 17 00:00:00 2001 From: dessina-devasia <143582034+dessina-devasia@users.noreply.github.com> Date: Fri, 7 Nov 2025 18:26:26 +0530 Subject: [PATCH 4/5] test: changed WebFilterQuuickFixTest to handle cancellation test case --- .../CodeActionCancellationTest.java | 182 ------- .../codeAction/WebFilterQuickFixTest.java | 479 +++++++++--------- 2 files changed, 245 insertions(+), 416 deletions(-) delete mode 100644 src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/CodeActionCancellationTest.java diff --git a/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/CodeActionCancellationTest.java b/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/CodeActionCancellationTest.java deleted file mode 100644 index 1a25bcd52..000000000 --- a/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/CodeActionCancellationTest.java +++ /dev/null @@ -1,182 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2025 Red Hat, Inc. - * Distributed under license by Red Hat, Inc. All rights reserved. - * This program is made available under the terms of the - * Eclipse Public License v2.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v20.html - * - * Contributors: - * Red Hat, Inc. - initial API and implementation - ******************************************************************************/ -package com.redhat.devtools.lsp4ij.features.codeAction; - -import com.redhat.devtools.lsp4ij.fixtures.LSPCodeActionFixtureTestCase; - -/** - * Test case for InvalidWebFilter quick fix - */ -public class CodeActionCancellationTest extends LSPCodeActionFixtureTestCase { - - private static final String NOT_JAVA_EXTENSION = "javax"; - private static final String TEST_FILE_NAME = "InvalidWebFilter." + NOT_JAVA_EXTENSION; - - public CodeActionCancellationTest() { - super("*." + NOT_JAVA_EXTENSION); - } - - public void testWebFilterQuickFix() { - - assertCodeActions(TEST_FILE_NAME, - IntentionActionKind.QUICK_FIX_ONLY, - // language=JAVA - """ - package io.openliberty.sample.jakarta.servlet; - - import jakarta.servlet.Filter; - import jakarta.servlet.annotation.WebFilter; - - @WebFilter() - public abstract class InvalidWebFilter implements Filter { - - }""", - // language=JSON - """ - [ - { - "title": "Add the `servletNames` attribute to @WebFilter", - "kind": "quickfix", - "diagnostics": [ - { - "range": { - "start": { - "line": 5, - "character": 0 - }, - "end": { - "line": 5, - "character": 12 - } - }, - "severity": 1, - "code": "CompleteWebFilterAttributes", - "source": "jakarta-servlet", - "message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027." - } - ], - "data": { - "participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix", - "documentUri": "unused", - "range": { - "start": { - "line": 5, - "character": 0 - }, - "end": { - "line": 5, - "character": 12 - } - }, - "extendedData": { - "annotation": "jakarta.servlet.annotation.WebFilter", - "attribute": "servletNames", - "diagnosticCode": "CompleteWebFilterAttributes" - }, - "resourceOperationSupported": true, - "commandConfigurationUpdateSupported": false - } - }, - { - "title": "Add the `urlPatterns` attribute to @WebFilter", - "kind": "quickfix", - "diagnostics": [ - { - "range": { - "start": { - "line": 5, - "character": 0 - }, - "end": { - "line": 5, - "character": 12 - } - }, - "severity": 1, - "code": "CompleteWebFilterAttributes", - "source": "jakarta-servlet", - "message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027." - } - ], - "data": { - "participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix", - "documentUri": "unused", - "range": { - "start": { - "line": 5, - "character": 0 - }, - "end": { - "line": 5, - "character": 12 - } - }, - "extendedData": { - "annotation": "jakarta.servlet.annotation.WebFilter", - "attribute": "urlPatterns", - "diagnosticCode": "CompleteWebFilterAttributes" - }, - "resourceOperationSupported": true, - "commandConfigurationUpdateSupported": false - } - }, - { - "title": "Add the `value` attribute to @WebFilter", - "kind": "quickfix", - "diagnostics": [ - { - "range": { - "start": { - "line": 5, - "character": 0 - }, - "end": { - "line": 5, - "character": 12 - } - }, - "severity": 1, - "code": "CompleteWebFilterAttributes", - "source": "jakarta-servlet", - "message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027." - } - ], - "data": { - "participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix", - "documentUri": "unused", - "range": { - "start": { - "line": 5, - "character": 0 - }, - "end": { - "line": 5, - "character": 12 - } - }, - "extendedData": { - "annotation": "jakarta.servlet.annotation.WebFilter", - "attribute": "value", - "diagnosticCode": "CompleteWebFilterAttributes" - }, - "resourceOperationSupported": true, - "commandConfigurationUpdateSupported": false - } - } - ] - """, - true, // simulateCancellation = true - "Add the `servletNames` attribute to @WebFilter", - "Add the `urlPatterns` attribute to @WebFilter", - "Add the `value` attribute to @WebFilter" - ); - } -} \ No newline at end of file diff --git a/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/WebFilterQuickFixTest.java b/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/WebFilterQuickFixTest.java index a8435a7bc..80f132eec 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/WebFilterQuickFixTest.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/WebFilterQuickFixTest.java @@ -10,7 +10,11 @@ ******************************************************************************/ package com.redhat.devtools.lsp4ij.features.codeAction; +import com.intellij.codeInsight.intention.IntentionAction; import com.redhat.devtools.lsp4ij.fixtures.LSPCodeActionFixtureTestCase; +import org.jetbrains.annotations.NotNull; + +import java.util.List; /** * Test case for InvalidWebFilter quick fix @@ -25,248 +29,255 @@ public WebFilterQuickFixTest() { super("*." + NOT_JAVA_EXTENSION); } - public void testWebFilterQuickFix() { - var allQuickFixes = assertCodeActions(TEST_FILE_NAME, - IntentionActionKind.QUICK_FIX_ONLY, - // language=JAVA - """ -package io.openliberty.sample.jakarta.servlet; - -import jakarta.servlet.Filter; -import jakarta.servlet.annotation.WebFilter; - -@WebFilter() -public abstract class InvalidWebFilter implements Filter { - -}""", - // language=JSON - """ - [ - { - "title": "Add the `servletNames` attribute to @WebFilter", - "kind": "quickfix", - "diagnostics": [ - { - "range": { - "start": { - "line": 5, - "character": 0 - }, - "end": { - "line": 5, - "character": 12 - } - }, - "severity": 1, - "code": "CompleteWebFilterAttributes", - "source": "jakarta-servlet", - "message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027." - } - ], - "data": { - "participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix", - "documentUri": "unused", - "range": { - "start": { - "line": 5, - "character": 0 - }, - "end": { - "line": 5, - "character": 12 - } - }, - "extendedData": { - "annotation": "jakarta.servlet.annotation.WebFilter", - "attribute": "servletNames", - "diagnosticCode": "CompleteWebFilterAttributes" - }, - "resourceOperationSupported": true, - "commandConfigurationUpdateSupported": false - } - }, - { - "title": "Add the `urlPatterns` attribute to @WebFilter", - "kind": "quickfix", - "diagnostics": [ - { - "range": { - "start": { - "line": 5, - "character": 0 - }, - "end": { - "line": 5, - "character": 12 - } - }, - "severity": 1, - "code": "CompleteWebFilterAttributes", - "source": "jakarta-servlet", - "message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027." - } - ], - "data": { - "participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix", - "documentUri": "unused", - "range": { - "start": { - "line": 5, - "character": 0 - }, - "end": { - "line": 5, - "character": 12 - } - }, - "extendedData": { - "annotation": "jakarta.servlet.annotation.WebFilter", - "attribute": "urlPatterns", - "diagnosticCode": "CompleteWebFilterAttributes" - }, - "resourceOperationSupported": true, - "commandConfigurationUpdateSupported": false - } - }, - { - "title": "Add the `value` attribute to @WebFilter", - "kind": "quickfix", - "diagnostics": [ - { - "range": { - "start": { - "line": 5, - "character": 0 - }, - "end": { - "line": 5, - "character": 12 - } - }, - "severity": 1, - "code": "CompleteWebFilterAttributes", - "source": "jakarta-servlet", - "message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027." - } - ], - "data": { - "participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix", - "documentUri": "unused", - "range": { - "start": { - "line": 5, - "character": 0 - }, - "end": { - "line": 5, - "character": 12 - } - }, - "extendedData": { - "annotation": "jakarta.servlet.annotation.WebFilter", - "attribute": "value", - "diagnosticCode": "CompleteWebFilterAttributes" - }, - "resourceOperationSupported": true, - "commandConfigurationUpdateSupported": false - } - } - ]""" - , - false, // simulateCancellation = false - "Add the `servletNames` attribute to @WebFilter", - "Add the `urlPatterns` attribute to @WebFilter", - "Add the `value` attribute to @WebFilter"); + private List assertCodeActions(boolean simulateCancellation) { + return assertCodeActions(TEST_FILE_NAME, + IntentionActionKind.QUICK_FIX_ONLY, + // language=JAVA + """ + package io.openliberty.sample.jakarta.servlet; + + import jakarta.servlet.Filter; + import jakarta.servlet.annotation.WebFilter; + + @WebFilter() + public abstract class InvalidWebFilter implements Filter { + + }""", + // language=JSON + """ + [ + { + "title": "Add the `servletNames` attribute to @WebFilter", + "kind": "quickfix", + "diagnostics": [ + { + "range": { + "start": { + "line": 5, + "character": 0 + }, + "end": { + "line": 5, + "character": 12 + } + }, + "severity": 1, + "code": "CompleteWebFilterAttributes", + "source": "jakarta-servlet", + "message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027." + } + ], + "data": { + "participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix", + "documentUri": "unused", + "range": { + "start": { + "line": 5, + "character": 0 + }, + "end": { + "line": 5, + "character": 12 + } + }, + "extendedData": { + "annotation": "jakarta.servlet.annotation.WebFilter", + "attribute": "servletNames", + "diagnosticCode": "CompleteWebFilterAttributes" + }, + "resourceOperationSupported": true, + "commandConfigurationUpdateSupported": false + } + }, + { + "title": "Add the `urlPatterns` attribute to @WebFilter", + "kind": "quickfix", + "diagnostics": [ + { + "range": { + "start": { + "line": 5, + "character": 0 + }, + "end": { + "line": 5, + "character": 12 + } + }, + "severity": 1, + "code": "CompleteWebFilterAttributes", + "source": "jakarta-servlet", + "message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027." + } + ], + "data": { + "participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix", + "documentUri": "unused", + "range": { + "start": { + "line": 5, + "character": 0 + }, + "end": { + "line": 5, + "character": 12 + } + }, + "extendedData": { + "annotation": "jakarta.servlet.annotation.WebFilter", + "attribute": "urlPatterns", + "diagnosticCode": "CompleteWebFilterAttributes" + }, + "resourceOperationSupported": true, + "commandConfigurationUpdateSupported": false + } + }, + { + "title": "Add the `value` attribute to @WebFilter", + "kind": "quickfix", + "diagnostics": [ + { + "range": { + "start": { + "line": 5, + "character": 0 + }, + "end": { + "line": 5, + "character": 12 + } + }, + "severity": 1, + "code": "CompleteWebFilterAttributes", + "source": "jakarta-servlet", + "message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027." + } + ], + "data": { + "participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix", + "documentUri": "unused", + "range": { + "start": { + "line": 5, + "character": 0 + }, + "end": { + "line": 5, + "character": 12 + } + }, + "extendedData": { + "annotation": "jakarta.servlet.annotation.WebFilter", + "attribute": "value", + "diagnosticCode": "CompleteWebFilterAttributes" + }, + "resourceOperationSupported": true, + "commandConfigurationUpdateSupported": false + } + } + ]""" + , + simulateCancellation, + "Add the `servletNames` attribute to @WebFilter", + "Add the `urlPatterns` attribute to @WebFilter", + "Add the `value` attribute to @WebFilter" + ); + } + public void testWebFilterQuickFix() { + var allQuickFixes = assertCodeActions(false); // Get 'Add the `servletNames` attribute to @WebFilter' quick fix var addServletNameQuickFix = assertFindIntentionByText("Add the `servletNames` attribute to @WebFilter", allQuickFixes); // Apply 'Add the `servletNames` attribute to @WebFilter' quick fix assertApplyCodeAction( - // language=JAVA - """ -package io.openliberty.sample.jakarta.servlet; - -import jakarta.servlet.Filter; -import jakarta.servlet.annotation.WebFilter; - -@WebFilter(servletNames="") -public abstract class InvalidWebFilter implements Filter { - -}""", - // language=JSON - """ - { - "title": "Add the `servletNames` attribute to @WebFilter", - "kind": "quickfix", - "diagnostics": [ - { - "range": { - "start": { - "line": 5, - "character": 0 - }, - "end": { - "line": 5, - "character": 12 - } - }, - "severity": 1, - "code": "CompleteWebFilterAttributes", - "source": "jakarta-servlet", - "message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027." - } - ], - "edit": { - "changes": {}, - "documentChanges": [ - { - "textDocument": { - "version": 0, - "uri": "%s" - }, - "edits": [ - { - "range": { - "start": { - "line": 0, - "character": 0 - }, - "end": { - "line": 11, - "character": 0 - } - }, - "newText": "package io.openliberty.sample.jakarta.servlet;\\n\\nimport jakarta.servlet.Filter;\\nimport jakarta.servlet.annotation.WebFilter;\\n\\n@WebFilter(servletNames\\u003d\\"\\")\\npublic abstract class InvalidWebFilter implements Filter {\\n\\n}" - } - ] - } - ] + // language=JAVA + """ + package io.openliberty.sample.jakarta.servlet; + + import jakarta.servlet.Filter; + import jakarta.servlet.annotation.WebFilter; + + @WebFilter(servletNames="") + public abstract class InvalidWebFilter implements Filter { + + }""", + // language=JSON + """ + { + "title": "Add the `servletNames` attribute to @WebFilter", + "kind": "quickfix", + "diagnostics": [ + { + "range": { + "start": { + "line": 5, + "character": 0 + }, + "end": { + "line": 5, + "character": 12 + } + }, + "severity": 1, + "code": "CompleteWebFilterAttributes", + "source": "jakarta-servlet", + "message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027." + } + ], + "edit": { + "changes": {}, + "documentChanges": [ + { + "textDocument": { + "version": 0, + "uri": "%s" + }, + "edits": [ + { + "range": { + "start": { + "line": 0, + "character": 0 }, - "data": { - "participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix", - "documentUri": "unused", - "range": { - "start": { - "line": 5, - "character": 0 - }, - "end": { - "line": 5, - "character": 12 - } - }, - "extendedData": { - "annotation": "jakarta.servlet.annotation.WebFilter", - "attribute": "servletNames", - "diagnosticCode": "CompleteWebFilterAttributes" - }, - "resourceOperationSupported": true, - "commandConfigurationUpdateSupported": false + "end": { + "line": 11, + "character": 0 } - } - """, - addServletNameQuickFix); + }, + "newText": "package io.openliberty.sample.jakarta.servlet;\\n\\nimport jakarta.servlet.Filter;\\nimport jakarta.servlet.annotation.WebFilter;\\n\\n@WebFilter(servletNames\\u003d\\"\\")\\npublic abstract class InvalidWebFilter implements Filter {\\n\\n}" + } + ] + } + ] + }, + "data": { + "participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix", + "documentUri": "unused", + "range": { + "start": { + "line": 5, + "character": 0 + }, + "end": { + "line": 5, + "character": 12 + } + }, + "extendedData": { + "annotation": "jakarta.servlet.annotation.WebFilter", + "attribute": "servletNames", + "diagnosticCode": "CompleteWebFilterAttributes" + }, + "resourceOperationSupported": true, + "commandConfigurationUpdateSupported": false + } + } + """, + addServletNameQuickFix); } + public void testWebFilterQuickFixWithCancellation() { + assertCodeActions(true); + } } \ No newline at end of file From f2bb45ee0ff9fbabfd832dded216927e51ff56f8 Mon Sep 17 00:00:00 2001 From: dessina-devasia <143582034+dessina-devasia@users.noreply.github.com> Date: Fri, 7 Nov 2025 18:32:59 +0530 Subject: [PATCH 5/5] test: removed unused imports --- .../lsp4ij/features/codeAction/WebFilterQuickFixTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/WebFilterQuickFixTest.java b/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/WebFilterQuickFixTest.java index 80f132eec..2e533a3a5 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/WebFilterQuickFixTest.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/features/codeAction/WebFilterQuickFixTest.java @@ -12,7 +12,6 @@ import com.intellij.codeInsight.intention.IntentionAction; import com.redhat.devtools.lsp4ij.fixtures.LSPCodeActionFixtureTestCase; -import org.jetbrains.annotations.NotNull; import java.util.List;