Skip to content

test: provide test case for Jakarta quick fixes #862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.concurrent.TimeUnit;

/**
* The lazy IJ Quick fix / Intention.
*/
Expand Down Expand Up @@ -87,18 +89,36 @@ public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws
if (codeAction.getEdit() == null && codeAction.getCommand() == null
&& languageServer.getClientFeatures().getCodeActionFeature().isResolveCodeActionSupported(file)) {
// Unresolved code action "edit" property. Resolve it.
languageServer
.getInitializedServer()
.thenApply(ls ->
ls.getTextDocumentService().resolveCodeAction(codeAction)
.thenAccept(resolved -> {
ApplicationManager.getApplication().invokeLater(() -> {
DocumentUtil.writeInRunUndoTransparentAction(() -> {
apply(resolved != null ? resolved : codeAction, file, editor, languageServer);
if (ApplicationManager.getApplication().isUnitTestMode()) {
// In Test mode, collect the resolve code actions synchronously, otherwise tests will fail
try {
var resolved = languageServer.getServer().getTextDocumentService().resolveCodeAction(codeAction)
.get(10000, TimeUnit.MILLISECONDS);
ApplicationManager.getApplication().invokeLater(() -> {
DocumentUtil.writeInRunUndoTransparentAction(() -> {
apply(resolved != null ? resolved : codeAction, file, editor, languageServer);
});
});
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
// In runtime mode, collect the resolve code action asynchronously to avoid freezing the IDE.
languageServer
.getInitializedServer()
.thenApply(ls ->
ls.getTextDocumentService().resolveCodeAction(codeAction)
.thenAccept(resolved -> {
ApplicationManager.getApplication().invokeLater(() -> {
DocumentUtil.writeInRunUndoTransparentAction(() -> {
apply(resolved != null ? resolved : codeAction, file, editor, languageServer);
});
});
});
})
);
})
);
}
} else {
apply(codeAction, file, editor, languageServer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
/*******************************************************************************
* 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 WebFilterQuickFixTest extends LSPCodeActionFixtureTestCase {

// NOTE: Using an extension other than "java" here to avoid having native Java language support skew results
private static final String NOT_JAVA_EXTENSION = "javax";
private static final String TEST_FILE_NAME = "InvalidWebFilter." + NOT_JAVA_EXTENSION;

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(<caret>)
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
}
}
]"""
,
"Add the `servletNames` attribute to @WebFilter",
"Add the `urlPatterns` attribute to @WebFilter",
"Add the `value` attribute to @WebFilter");

// 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}"
}
]
}
]
},
"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);
}

}
Loading
Loading