Skip to content

Commit 78fc95c

Browse files
committed
No CodeLens over method with @Query. Unit tests
1 parent 6d926d4 commit 78fc95c

File tree

14 files changed

+1111
-0
lines changed

14 files changed

+1111
-0
lines changed

headless-services/commons/language-server-test-harness/src/main/java/org/springframework/ide/vscode/languageserver/testharness/Editor.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,25 @@ public void assertHoverContains(String hoverOver, int occurrence, String snippet
612612
Hover hover = harness.getHover(doc, doc.toPosition(hoverPosition));
613613
assertContains(snippet, hoverString(hover));
614614
}
615+
616+
public void assertCodeLens(String over, int occurrence, String codeLensText) throws Exception {
617+
int offset = getHoverPosition(over, occurrence);
618+
List<? extends CodeLens> matchingCodeLenses = harness.getCodeLenses(doc).stream()
619+
.filter(cl -> doc.toOffset(cl.getRange().getStart()) <= offset
620+
&& doc.toOffset(cl.getRange().getEnd()) >= offset)
621+
.collect(Collectors.toList());
622+
if (matchingCodeLenses.isEmpty()) {
623+
if (codeLensText == null) {
624+
// Success! No code lenses expected at this spot
625+
return;
626+
}
627+
fail("No CodeLenses over the string `%s`".formatted(over));
628+
}
629+
if (!matchingCodeLenses.stream().anyMatch(cl -> cl.getCommand().getTitle().contains(codeLensText))) {
630+
fail("None of the found CodeLenses: [ %s ] contained '%s'".formatted(matchingCodeLenses.stream()
631+
.map(cl -> "'%s'".formatted(cl.getCommand().getTitle())).collect(Collectors.joining(", ")), codeLensText));
632+
}
633+
}
615634

616635
public void assertLiveCodeLensContains(String codeLensOver, int occurrence, String snippet) throws Exception {
617636
int cmPosition = getHoverPosition(codeLensOver, occurrence);

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/data/DataRepositoryAotMetadataCodeLensProvider.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414

1515
import org.eclipse.jdt.core.dom.ASTVisitor;
1616
import org.eclipse.jdt.core.dom.CompilationUnit;
17+
import org.eclipse.jdt.core.dom.IAnnotationBinding;
1718
import org.eclipse.jdt.core.dom.IMethodBinding;
19+
import org.eclipse.jdt.core.dom.ITypeBinding;
1820
import org.eclipse.jdt.core.dom.MethodDeclaration;
1921
import org.eclipse.lsp4j.CodeLens;
2022
import org.eclipse.lsp4j.Command;
2123
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
2224
import org.slf4j.Logger;
2325
import org.slf4j.LoggerFactory;
26+
import org.springframework.ide.vscode.boot.java.Annotations;
2427
import org.springframework.ide.vscode.boot.java.handlers.CodeLensProvider;
2528
import org.springframework.ide.vscode.commons.java.IJavaProject;
2629
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
@@ -58,6 +61,15 @@ protected void provideCodeLens(CancelChecker cancelToken, MethodDeclaration node
5861

5962
IMethodBinding methodBinding = node.resolveBinding();
6063

64+
// Don't show CodeLens if annotated with `@Query` or `@NativeQuery`
65+
for (IAnnotationBinding a : methodBinding.getAnnotations()) {
66+
ITypeBinding t = a.getAnnotationType();
67+
if (t != null
68+
&& (Annotations.DATA_JPA_QUERY.equals(t.getQualifiedName()) || Annotations.DATA_JPA_NATIVE_QUERY.equals(t.getQualifiedName()))) {
69+
return;
70+
}
71+
}
72+
6173
if (methodBinding == null || methodBinding.getDeclaringClass() == null
6274
|| methodBinding.getMethodDeclaration() == null
6375
|| methodBinding.getDeclaringClass().getBinaryName() == null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.springframework.ide.vscode.boot.java.data.test;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
import java.util.concurrent.CompletableFuture;
8+
import java.util.concurrent.TimeUnit;
9+
10+
import org.eclipse.lsp4j.TextDocumentIdentifier;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
import org.junit.jupiter.api.extension.ExtendWith;
14+
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.context.annotation.Import;
16+
import org.springframework.ide.vscode.boot.app.SpringSymbolIndex;
17+
import org.springframework.ide.vscode.boot.bootiful.BootLanguageServerTest;
18+
import org.springframework.ide.vscode.boot.bootiful.SymbolProviderTestConf;
19+
import org.springframework.ide.vscode.commons.java.IJavaProject;
20+
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
21+
import org.springframework.ide.vscode.commons.util.text.LanguageId;
22+
import org.springframework.ide.vscode.languageserver.testharness.Editor;
23+
import org.springframework.ide.vscode.project.harness.BootLanguageServerHarness;
24+
import org.springframework.ide.vscode.project.harness.ProjectsHarness;
25+
import org.springframework.test.context.junit.jupiter.SpringExtension;
26+
27+
@ExtendWith(SpringExtension.class)
28+
@BootLanguageServerTest
29+
@Import(SymbolProviderTestConf.class)
30+
public class DataRepositoryAotMetadataCodeLensProviderTest {
31+
32+
@Autowired private BootLanguageServerHarness harness;
33+
@Autowired private JavaProjectFinder projectFinder;
34+
@Autowired private SpringSymbolIndex indexer;
35+
36+
private IJavaProject testProject;
37+
38+
@BeforeEach
39+
public void setup() throws Exception {
40+
testProject = ProjectsHarness.INSTANCE.mavenProject("aot-generation");
41+
harness.useProject(testProject);
42+
harness.intialize(null);
43+
44+
// trigger project creation
45+
projectFinder.find(new TextDocumentIdentifier(testProject.getLocationUri().toASCIIString())).get();
46+
47+
CompletableFuture<Void> initProject = indexer.waitOperation();
48+
initProject.get(5, TimeUnit.SECONDS);
49+
}
50+
51+
@Test
52+
void codeLensOverMethod() throws Exception {
53+
Path filePath = Paths.get(testProject.getLocationUri())
54+
.resolve("src/main/java/example/springdata/aot/UserRepository.java");
55+
Editor editor = harness.newEditor(LanguageId.JAVA, new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8), filePath.toUri().toASCIIString());
56+
57+
editor.assertCodeLens("findUserByUsername", 1, "SELECT u FROM example.springdata.aot.User u WHERE u.username = :username");
58+
}
59+
60+
@Test
61+
void noCodeLensOverMethodWithQueryAnnotation() throws Exception {
62+
Path filePath = Paths.get(testProject.getLocationUri())
63+
.resolve("src/main/java/example/springdata/aot/UserRepository.java");
64+
Editor editor = harness.newEditor(LanguageId.JAVA, new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8), filePath.toUri().toASCIIString());
65+
66+
editor.assertCodeLens("usersWithUsernamesStartingWith", 1, null);
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
wrapperVersion=3.3.2
18+
distributionType=only-script
19+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.3/apache-maven-3.9.3-bin.zip

0 commit comments

Comments
 (0)