Skip to content

Commit 62c8011

Browse files
committed
test: provide test case for Jakarta quick fixes
Signed-off-by: azerr <[email protected]>
1 parent 5e07cd7 commit 62c8011

File tree

5 files changed

+499
-28
lines changed

5 files changed

+499
-28
lines changed

Diff for: src/main/java/com/redhat/devtools/lsp4ij/features/diagnostics/LSPDiagnosticHandler.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@ private void updateDiagnostics(@NotNull PublishDiagnosticsParams params, @NotNul
9191
synchronized (data) {
9292
data.updateDiagnostics(params.getDiagnostics());
9393
}
94-
// Trigger Intellij validation to execute
95-
// {@link LSPDiagnosticAnnotator}.
96-
// which translates LSP Diagnostics into Intellij Annotation
97-
DaemonCodeAnalyzer.getInstance(project).restart(psiFile);
94+
if (!ApplicationManager.getApplication().isUnitTestMode()) {
95+
// Trigger Intellij validation to execute
96+
// {@link LSPDiagnosticAnnotator}.
97+
// which translates LSP Diagnostics into Intellij Annotation
98+
DaemonCodeAnalyzer.getInstance(project).restart(psiFile);
99+
}
98100
}
99101
}
100102
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Red Hat, Inc.
3+
* Distributed under license by Red Hat, Inc. All rights reserved.
4+
* This program is made available under the terms of the
5+
* Eclipse Public License v2.0 which accompanies this distribution,
6+
* and is available at http://www.eclipse.org/legal/epl-v20.html
7+
*
8+
* Contributors:
9+
* Red Hat, Inc. - initial API and implementation
10+
******************************************************************************/
11+
package com.redhat.devtools.lsp4ij.features.codeAction;
12+
13+
import com.redhat.devtools.lsp4ij.fixtures.LSPCodeActionFixtureTestCase;
14+
15+
/**
16+
* Test case for InvalidWebFilter quick fix
17+
*/
18+
public class WebFilterQuickFixTest extends LSPCodeActionFixtureTestCase {
19+
20+
// NOTE: Using an extension other than "java" here to avoid having native Java language support skew results
21+
private static final String NOT_JAVA_EXTENSION = "javax";
22+
private static final String TEST_FILE_NAME = "InvalidWebFilter." + NOT_JAVA_EXTENSION;
23+
24+
public WebFilterQuickFixTest() {
25+
super("*." + NOT_JAVA_EXTENSION);
26+
}
27+
28+
public void testWebFilterQuickFix() {
29+
var allQuickFixes = assertCodeActions(TEST_FILE_NAME,
30+
IntentionActionKind.QUICK_FIX_ONLY,
31+
// language=JAVA
32+
"""
33+
package io.openliberty.sample.jakarta.servlet;
34+
35+
import jakarta.servlet.Filter;
36+
import jakarta.servlet.annotation.WebFilter;
37+
38+
@WebFilter(<caret>)
39+
public abstract class InvalidWebFilter implements Filter {
40+
41+
}""",
42+
// language=JSON
43+
"""
44+
[
45+
{
46+
"title": "Add the `servletNames` attribute to @WebFilter",
47+
"kind": "quickfix",
48+
"diagnostics": [
49+
{
50+
"range": {
51+
"start": {
52+
"line": 5,
53+
"character": 0
54+
},
55+
"end": {
56+
"line": 5,
57+
"character": 12
58+
}
59+
},
60+
"severity": 1,
61+
"code": "CompleteWebFilterAttributes",
62+
"source": "jakarta-servlet",
63+
"message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027."
64+
}
65+
],
66+
"data": {
67+
"participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix",
68+
"documentUri": "unused",
69+
"range": {
70+
"start": {
71+
"line": 5,
72+
"character": 0
73+
},
74+
"end": {
75+
"line": 5,
76+
"character": 12
77+
}
78+
},
79+
"extendedData": {
80+
"annotation": "jakarta.servlet.annotation.WebFilter",
81+
"attribute": "servletNames",
82+
"diagnosticCode": "CompleteWebFilterAttributes"
83+
},
84+
"resourceOperationSupported": true,
85+
"commandConfigurationUpdateSupported": false
86+
}
87+
},
88+
{
89+
"title": "Add the `urlPatterns` attribute to @WebFilter",
90+
"kind": "quickfix",
91+
"diagnostics": [
92+
{
93+
"range": {
94+
"start": {
95+
"line": 5,
96+
"character": 0
97+
},
98+
"end": {
99+
"line": 5,
100+
"character": 12
101+
}
102+
},
103+
"severity": 1,
104+
"code": "CompleteWebFilterAttributes",
105+
"source": "jakarta-servlet",
106+
"message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027."
107+
}
108+
],
109+
"data": {
110+
"participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix",
111+
"documentUri": "unused",
112+
"range": {
113+
"start": {
114+
"line": 5,
115+
"character": 0
116+
},
117+
"end": {
118+
"line": 5,
119+
"character": 12
120+
}
121+
},
122+
"extendedData": {
123+
"annotation": "jakarta.servlet.annotation.WebFilter",
124+
"attribute": "urlPatterns",
125+
"diagnosticCode": "CompleteWebFilterAttributes"
126+
},
127+
"resourceOperationSupported": true,
128+
"commandConfigurationUpdateSupported": false
129+
}
130+
},
131+
{
132+
"title": "Add the `value` attribute to @WebFilter",
133+
"kind": "quickfix",
134+
"diagnostics": [
135+
{
136+
"range": {
137+
"start": {
138+
"line": 5,
139+
"character": 0
140+
},
141+
"end": {
142+
"line": 5,
143+
"character": 12
144+
}
145+
},
146+
"severity": 1,
147+
"code": "CompleteWebFilterAttributes",
148+
"source": "jakarta-servlet",
149+
"message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027."
150+
}
151+
],
152+
"data": {
153+
"participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix",
154+
"documentUri": "unused",
155+
"range": {
156+
"start": {
157+
"line": 5,
158+
"character": 0
159+
},
160+
"end": {
161+
"line": 5,
162+
"character": 12
163+
}
164+
},
165+
"extendedData": {
166+
"annotation": "jakarta.servlet.annotation.WebFilter",
167+
"attribute": "value",
168+
"diagnosticCode": "CompleteWebFilterAttributes"
169+
},
170+
"resourceOperationSupported": true,
171+
"commandConfigurationUpdateSupported": false
172+
}
173+
}
174+
]"""
175+
,
176+
"Add the `servletNames` attribute to @WebFilter",
177+
"Add the `urlPatterns` attribute to @WebFilter",
178+
"Add the `value` attribute to @WebFilter");
179+
180+
// Get 'Add the `servletNames` attribute to @WebFilter' quick fix
181+
var addServletNameQuickFix = assertFindIntentionByText("Add the `servletNames` attribute to @WebFilter", allQuickFixes);
182+
183+
// Apply 'Add the `servletNames` attribute to @WebFilter' quick fix
184+
assertApplyCodeAction(
185+
// language=JAVA
186+
"""
187+
package io.openliberty.sample.jakarta.servlet;
188+
189+
import jakarta.servlet.Filter;
190+
import jakarta.servlet.annotation.WebFilter;
191+
192+
@WebFilter(servletNames="")
193+
public abstract class InvalidWebFilter implements Filter {
194+
195+
}""",
196+
// language=JSON
197+
"""
198+
{
199+
"title": "Add the `servletNames` attribute to @WebFilter",
200+
"kind": "quickfix",
201+
"diagnostics": [
202+
{
203+
"range": {
204+
"start": {
205+
"line": 5,
206+
"character": 0
207+
},
208+
"end": {
209+
"line": 5,
210+
"character": 12
211+
}
212+
},
213+
"severity": 1,
214+
"code": "CompleteWebFilterAttributes",
215+
"source": "jakarta-servlet",
216+
"message": "The annotation @WebFilter must define the attribute \\u0027urlPatterns\\u0027, \\u0027servletNames\\u0027 or \\u0027value\\u0027."
217+
}
218+
],
219+
"edit": {
220+
"changes": {},
221+
"documentChanges": [
222+
{
223+
"textDocument": {
224+
"version": 0,
225+
"uri": "%s"
226+
},
227+
"edits": [
228+
{
229+
"range": {
230+
"start": {
231+
"line": 0,
232+
"character": 0
233+
},
234+
"end": {
235+
"line": 11,
236+
"character": 0
237+
}
238+
},
239+
"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}"
240+
}
241+
]
242+
}
243+
]
244+
},
245+
"data": {
246+
"participantId": "io.openliberty.tools.intellij.lsp4jakarta.lsp4ij.servlet.CompleteFilterAnnotationQuickFix",
247+
"documentUri": "unused",
248+
"range": {
249+
"start": {
250+
"line": 5,
251+
"character": 0
252+
},
253+
"end": {
254+
"line": 5,
255+
"character": 12
256+
}
257+
},
258+
"extendedData": {
259+
"annotation": "jakarta.servlet.annotation.WebFilter",
260+
"attribute": "servletNames",
261+
"diagnosticCode": "CompleteWebFilterAttributes"
262+
},
263+
"resourceOperationSupported": true,
264+
"commandConfigurationUpdateSupported": false
265+
}
266+
}
267+
""",
268+
addServletNameQuickFix);
269+
}
270+
271+
}

0 commit comments

Comments
 (0)