summaryrefslogtreecommitdiff
path: root/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavadocCompletionTest.groovy
blob: b966d52057761372e76ba61166bd635e1f4e450c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package com.intellij.codeInsight.completion
import com.intellij.JavaTestUtil
import com.intellij.codeInsight.CodeInsightSettings
import com.intellij.codeInsight.lookup.LookupElement
import com.intellij.codeInspection.javaDoc.JavaDocLocalInspection
import com.intellij.lang.StdLanguages
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiReference
import com.intellij.psi.PsiReferenceBase
import com.intellij.psi.PsiReferenceProvider
import com.intellij.psi.codeStyle.CodeStyleSettingsManager
import com.intellij.psi.impl.source.resolve.reference.PsiReferenceRegistrarImpl
import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry
import com.intellij.psi.javadoc.PsiDocTag
import com.intellij.util.ObjectUtils
import com.intellij.util.ProcessingContext
import com.intellij.util.SystemProperties
import org.jetbrains.annotations.NotNull
/**
 * @author mike
 */
public class JavadocCompletionTest extends LightFixtureCompletionTestCase {

  @Override
  protected void tearDown() throws Exception {
    CodeStyleSettingsManager.getSettings(getProject()).USE_FQ_CLASS_NAMES_IN_JAVADOC = true
    super.tearDown()
  }

  @Override
  protected String getBasePath() {
    return JavaTestUtil.getRelativeJavaTestDataPath() + "/codeInsight/completion/javadoc/";
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    myFixture.enableInspections(new JavaDocLocalInspection());
  }

  public void testNamesInPackage() throws Exception {
    myFixture.configureFromExistingVirtualFile(myFixture.copyFileToProject("package-info.java", "p/package-info.java"));
    complete();
    assertStringItems("author", 'author ' + SystemProperties.getUserName(), "deprecated", "see", "since", "version")
  }

  public void testNamesInClass() throws Exception {
    configureByFile("ClassTagName.java");
    assertStringItems("author", 'author ' + SystemProperties.getUserName(), "deprecated", "param", "see", "serial", "since", "version");
  }

  public void testNamesInField() throws Exception {
    configureByFile("FieldTagName.java");
    assertStringItems("deprecated", "see", "serial", "serialField", "since");
  }

  public void testNamesInMethod0() throws Exception {
    configureByFile("MethodTagName0.java");
    assertStringItems("deprecated", "exception", "param", "return", "see", "serialData", "since", "throws");
  }

  public void testNamesInMethod1() throws Exception {
    configureByFile("MethodTagName1.java");
    assertStringItems("see", "serialData", "since", "throws");
  }

  public void testParamValueCompletion() throws Exception {
    configureByFile("ParamValue0.java");
    assertStringItems("a", "b", "c");
  }

  public void testParamValueWithPrefixCompletion() throws Exception {
    configureByFile("ParamValue1.java");
    assertStringItems("a1", "a2", "a3");
  }

  public void testDescribedParameters() throws Exception {
    configureByFile("ParamValue2.java");
    assertStringItems("a2", "a3");
  }

  public void testSee0() throws Exception {
    configureByFile("See0.java");
    myFixture.assertPreferredCompletionItems(0, "foo", "clone", "equals", "hashCode");
  }

  public void testSee1() throws Exception {
    configureByFile("See1.java");
    assertStringItems("notify", "notifyAll");
  }

  public void testSee2() throws Exception {
    configureByFile("See2.java");
    assertStringItems("notify", "notifyAll");
  }

  public void testSee3() throws Exception {
    configureByFile("See3.java");

    assertTrue(getLookupElementStrings().containsAll(Arrays.asList("foo", "myField")));
  }

  @NotNull
  private List<String> getLookupElementStrings() {
    return ObjectUtils.assertNotNull(myFixture.getLookupElementStrings());
  }

  public void testSee4() throws Exception {
    configureByFile("See4.java");

    assertTrue(getLookupElementStrings().containsAll(Arrays.asList("A", "B", "C")));
  }

  public void testSee5() throws Exception {
    configureByFile("See5.java");

    assertTrue(getLookupElementStrings().containsAll(Arrays.asList("foo", "myName")));
  }

  public void testIDEADEV10620() throws Exception {
    configureByFile("IDEADEV10620.java");

    checkResultByFile("IDEADEV10620-after.java");
  }

  public void testException0() throws Exception {
    configureByFile("Exception0.java");
    assertStringItems("deprecated", "exception", "param", "see", "serialData", "since", "throws");
  }

  public void testException1() throws Exception {
    configureByFile("Exception1.java");
    assertTrue(myItems.length > 18);
  }

  public void testException2() throws Exception {
    myFixture.configureByFile("Exception2.java");
    myFixture.complete(CompletionType.SMART);
    assertStringItems("IllegalStateException", "IOException");
  }

  public void testInlineLookup() throws Exception {
    configureByFile("InlineTagName.java");
    assertStringItems("code", "docRoot", "inheritDoc", "link", "linkplain", "literal", "value");
  }

  public void testFinishWithSharp() throws Throwable {
    boolean old = CodeInsightSettings.getInstance().AUTOCOMPLETE_ON_CODE_COMPLETION;
    CodeInsightSettings.getInstance().AUTOCOMPLETE_ON_CODE_COMPLETION = false;
    try {
      checkFinishWithSharp();
    }
    finally {
      CodeInsightSettings.getInstance().AUTOCOMPLETE_ON_CODE_COMPLETION = old;
    }
  }

  private void checkFinishWithSharp() throws Exception {
    myFixture.configureByFile("FinishWithSharp.java");
    myFixture.completeBasic();
    type('#');
    checkResultByFile("FinishWithSharp_after.java");
    final List<LookupElement> items = getLookup().getItems();
    assertEquals("bar", items.get(0).getLookupString());
    assertEquals("foo", items.get(1).getLookupString());
  }

  public void testShortenClassName() throws Throwable {
    CodeStyleSettingsManager.getSettings(getProject()).USE_FQ_CLASS_NAMES_IN_JAVADOC = false;
    doTest();
  }

  public void testMethodBeforeSharp() throws Throwable {
    doTest();
  }

  public void testFieldReferenceInInnerClassJavadoc() throws Throwable {
    doTest();
  }

  public void testShortenClassReference() throws Throwable {
    CodeStyleSettingsManager.getSettings(getProject()).USE_FQ_CLASS_NAMES_IN_JAVADOC = false
    doTest()
  }
  public void testQualifiedClassReference() throws Throwable {
    configureByFile(getTestName(false) + ".java");
    myFixture.complete(CompletionType.BASIC, 2);
    checkResultByFile(getTestName(false) + "_after.java");
  }

  public void testQualifiedImportedClassReference() throws Throwable { doTest() }

  public void testThrowsNonImported() throws Throwable {
    configureByFile(getTestName(false) + ".java");
    myFixture.complete(CompletionType.BASIC, 2);
    checkResultByFile(getTestName(false) + "_after.java");
  }

  private void doTest() throws Exception {
    configureByFile(getTestName(false) + ".java");
    checkResultByFile(getTestName(false) + "_after.java");
  }

  public void testInlinePackageReferenceCompletion() throws Exception {
    configureByFile("InlineReference.java");
    assertTrue(getLookupElementStrings().containsAll(Arrays.asList("io", "lang", "util")));
  }

  public void testQualifyClassReferenceInPackageStatement() throws Exception {
    configureByFile(getTestName(false) + ".java");
    myFixture.type('\n');
    checkResultByFile(getTestName(false) + "_after.java");
  }

  public void "test suggest param names"() {
    myFixture.configureByText "a.java", '''
class Foo {
  /**
  * @par<caret>
  */
  void foo(int intParam, Object param2) {
  }
}
'''
    myFixture.completeBasic()
    myFixture.assertPreferredCompletionItems 0, 'param', 'param intParam', 'param param2'
    myFixture.type('\n intParam\n@para')
    myFixture.completeBasic()
    myFixture.assertPreferredCompletionItems 0, 'param', 'param param2'
  }

  public void "test suggest same param descriptions"() {
    myFixture.configureByText "a.java", '''
class Foo {
  /**
  * @param intParam so<caret> xxx
  * @throws Foo
  */
  void foo2(int intParam, Object param2) { }

  /**
  * @param intParam some integer param
  */
  void foo(int intParam, Object param2) { }
}
'''
    myFixture.completeBasic()
    myFixture.assertPreferredCompletionItems 0, 'some', 'some integer param'
    myFixture.lookup.currentItem = myFixture.lookupElements[1]
    myFixture.type('\t')
    myFixture.checkResult '''
class Foo {
  /**
  * @param intParam some integer param<caret>
  * @throws Foo
  */
  void foo2(int intParam, Object param2) { }

  /**
  * @param intParam some integer param
  */
  void foo(int intParam, Object param2) { }
}
'''
  }

  public void "test see super class"() {
    myFixture.addClass("package foo; public interface Foo {}")
    myFixture.addClass("package bar; public class Bar {} ")
    myFixture.configureByText "a.java", '''
import foo.*;
import bar.*;

/**
 * @se<caret>
 */
class Impl extends Bar implements Foo {}
'''
    myFixture.completeBasic()
    myFixture.assertPreferredCompletionItems 0, 'see', 'see bar.Bar', 'see foo.Foo'
  }

  public void testShortenMethodParameterTypes() {
    CodeStyleSettingsManager.getSettings(getProject()).USE_FQ_CLASS_NAMES_IN_JAVADOC = false
    myFixture.addClass("package foo; public class Foo {}")
    myFixture.addClass("package bar; public class Bar {}")
    myFixture.configureByText "a.java", '''
import foo.*;
import bar.*;

/**
* {@link #go<caret>
*/
class Goo { void goo(Foo foo, Bar bar) {} }
'''
    myFixture.completeBasic()
    assert myFixture.editor.document.text.contains('@link #goo(Foo, Bar)')
  }

  public void testNoMethodsAfterClassDot() {
    def text = '''
/**
* @see java.util.List.<caret>
*/
class Goo { void goo(Foo foo, Bar bar) {} }
'''
    myFixture.configureByText "a.java", text
    assert !myFixture.completeBasic()
    myFixture.checkResult(text)
  }

  public void testCustomReferenceProvider() throws Exception {
    PsiReferenceRegistrarImpl registrar =
      (PsiReferenceRegistrarImpl) ReferenceProvidersRegistry.getInstance().getRegistrar(StdLanguages.JAVA);
    PsiReferenceProvider provider = new PsiReferenceProvider() {
      @Override
      @NotNull
      public PsiReference[] getReferencesByElement(@NotNull final PsiElement element, @NotNull final ProcessingContext context) {
        def ref = new PsiReferenceBase<PsiElement>(element) {

          @Override
          public PsiElement resolve() {
            return element;
          }

          @Override
          @NotNull
          public Object[] getVariants() {
            return ["1", "2", "3"]
          }
        }
        return [ref]
      }
    };
    try {
      registrar.registerReferenceProvider(PsiDocTag.class, provider);
      configureByFile("ReferenceProvider.java");
      assertStringItems("1", "2", "3");
    }
    finally {
      registrar.unregisterReferenceProvider(PsiDocTag.class, provider);
    }
  }
}