summaryrefslogtreecommitdiff
path: root/java/java-tests/testSrc/com/intellij/codeInsight/daemon/ImplementationsViewTest.java
blob: d12e7201f11e6e76e6dccb3f4171d43e21fdafb0 (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
package com.intellij.codeInsight.daemon;

import com.intellij.codeInsight.TargetElementUtilBase;
import com.intellij.codeInsight.hint.ImplementationViewComponent;
import com.intellij.codeInsight.navigation.ClassImplementationsSearch;
import com.intellij.codeInsight.navigation.MethodImplementationsSearch;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.search.searches.OverridingMethodsSearch;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import com.intellij.util.CommonProcessors;
import org.junit.Assert;

import java.util.*;

/**
 * User: anna
 */
public class ImplementationsViewTest extends LightCodeInsightFixtureTestCase {
  public void testFromCompletion() {
    myFixture.configureByText("a.java", "public class Foo {\n" +
                                        "    private final String text;\n" +
                                        "\n" +
                                        "    public Foo(String text) {\n" +
                                        "//        this.text = text;\n" +
                                        "    }\n" +
                                        "\n" +
                                        "    public Foo(int i) {\n" +
                                        "    }\n" +
                                        "\n" +
                                        "    public static void main(String[] args) {\n" +
                                        "        final Foo foo = new Foo(\"\");\n" +
                                        "        foo.to<caret>\n" +
                                        "    }\n" +
                                        "\n" +
                                        "    @Override\n" +
                                        "    public String toString() {\n" +
                                        "        return \"text\";\n" +
                                        "    }\n" +
                                        "    public void totttt(){}" +
                                        "}");
    myFixture.completeBasic();

    PsiElement element =
      TargetElementUtilBase.findTargetElement(myFixture.getEditor(), TargetElementUtilBase.getInstance().getAllAccepted());

    assert element != null;
    final String newText = ImplementationViewComponent.getNewText(element);

    assertEquals("    @Override\n" +
                 "    public String toString() {\n" +
                 "        return \"text\";\n" +
                 "    }", newText);
  }

  public void testFromEditor() {
    myFixture.configureByText("a.java", "public class Foo {\n" +
                                        "    private final String text;\n" +
                                        "\n" +
                                        "    public Foo(String text) {\n" +
                                        "//        this.text = text;\n" +
                                        "    }\n" +
                                        "\n" +
                                        "    public Foo(int i) {\n" +
                                        "    }\n" +
                                        "\n" +
                                        "    @Override\n" +
                                        "    public String to<caret>String() {\n" +
                                        "        return \"text\";\n" +
                                        "    }\n" +
                                        "}");
    PsiElement element =
      TargetElementUtilBase.findTargetElement(myFixture.getEditor(), TargetElementUtilBase.getInstance().getAllAccepted());

    assert element != null;
    final String newText = ImplementationViewComponent.getNewText(element);

    assertEquals("    @Override\n" +
                 "    public String toString() {\n" +
                 "        return \"text\";\n" +
                 "    }", newText);
  }

  private static Collection<PsiElement> getClassImplementations(final PsiClass psiClass) {
    CommonProcessors.CollectProcessor<PsiElement> processor = new CommonProcessors.CollectProcessor<PsiElement>();
    ClassImplementationsSearch.processImplementations(psiClass, processor, psiClass.getUseScope());

    return processor.getResults();
  }

  private static Collection<PsiElement> getMethodImplementations(final PsiMethod psiMethod) {
    CommonProcessors.CollectProcessor<PsiElement> processor = new CommonProcessors.CollectProcessor<PsiElement>();
    MethodImplementationsSearch.processImplementations( psiMethod, processor, psiMethod.getUseScope());

    return processor.getResults();
  }

  public void testInnerClasses() {
    myFixture.configureByText("a.java", "abstract class AF<caret>oo{\n" +
                                        "    abstract boolean aaa();\n" +
                                        "    static class AFoo1 extends AFoo {\n" +
                                        "        @Override\n" +
                                        "        boolean aaa() {\n" +
                                        "            return false;\n" +
                                        "        }\n" +
                                        "    }\n" +
                                        "    static class AFoo3 extends AFoo {\n" +
                                        "        @Override\n" +
                                        "        boolean aaa() {\n" +
                                        "            return false;\n" +
                                        "        }\n" +
                                        "    }\n" +
                                        "    static class AFoo2 extends AFoo {\n" +
                                        "        @Override\n" +
                                        "        boolean aaa() {\n" +
                                        "            return false;\n" +
                                        "        }\n" +
                                        "    }\n" +
                                        "    \n" +
                                        "}");
    PsiClass psiClass =
      (PsiClass)TargetElementUtilBase.findTargetElement(myFixture.getEditor(), TargetElementUtilBase.getInstance().getAllAccepted());

    assert psiClass != null;
    final Collection<PsiElement> classes = getClassImplementations(psiClass);
    List<PsiElement> all = new ArrayList<PsiElement>();
    all.add(psiClass);
    all.addAll(classes);
    final ImplementationViewComponent component =
      new ImplementationViewComponent(all.toArray(new PsiElement[all.size()]), 0);
    try {
      final String[] visibleFiles = component.getVisibleFiles();
      assertTrue(visibleFiles.length > 0);
      assertEquals(visibleFiles[0], "a.java (AFoo)");
      Arrays.sort(visibleFiles);
      Assert.assertArrayEquals(Arrays.toString(visibleFiles),
                               new String[]{"a.java (AFoo)", "a.java (AFoo1 in AFoo)", "a.java (AFoo2 in AFoo)", "a.java (AFoo3 in AFoo)"}, visibleFiles);
    }
    finally {
      component.removeNotify();
    }
  }

  public void testFunctionalInterface() {
    myFixture.configureByText("a.java", "interface AF<caret>oo{\n" +
                                        "    boolean aaa();\n" +
                                        "}\n" +
                                        "class AFooImpl {\n" +
                                        "        {\n" +
                                        "             AFoo a = () -> {return false;};\n" +            
                                        "        }\n" +
                                        "}");
    PsiClass psiClass =
      (PsiClass)TargetElementUtilBase.findTargetElement(myFixture.getEditor(), TargetElementUtilBase.getInstance().getAllAccepted());

    assert psiClass != null;
    final Collection<PsiElement> classes = getClassImplementations(psiClass);
    List<PsiElement> all = new ArrayList<PsiElement>();
    all.add(psiClass);
    all.addAll(classes);
    final ImplementationViewComponent component = new ImplementationViewComponent(all.toArray(new PsiElement[all.size()]), 0);
    assertContent(component, new String[]{"a.java (AFoo)", "a.java"});
  }

  public void testInterfaceMethodOfFunctionalInterface() {
    myFixture.configureByText("a.java", "interface AFoo{\n" +
                                        "    boolean a<caret>aa();\n" +
                                        "}\n" +
                                        "class AFooImpl {\n" +
                                        "        {\n" +
                                        "             AFoo a = () -> {return false;};\n" +            
                                        "        }\n" +
                                        "}");
    PsiMethod psiMethod =
      (PsiMethod)TargetElementUtilBase.findTargetElement(myFixture.getEditor(), TargetElementUtilBase.getInstance().getAllAccepted());

    assert psiMethod != null;
    final Collection<PsiElement> methods = getMethodImplementations(psiMethod);
    List<PsiElement> all = new ArrayList<PsiElement>();
    all.add(psiMethod);
    all.addAll(methods);
    final ImplementationViewComponent component = new ImplementationViewComponent(all.toArray(new PsiElement[all.size()]), 0);
    assertContent(component, new String[]{"a.java (AFoo)", "a.java"});
  }

  public void testDefaultMethodOfFunctionalInterface() {
    myFixture.configureByText("a.java", "interface AFoo{\n" +
                                        "    default boolean a<caret>aa(){}\n" +
                                        "    boolean bbb();" +
                                        "}\n" +
                                        "class AFooImpl {\n" +
                                        "        {\n" +
                                        "             AFoo a = () -> {return false;};\n" +            
                                        "        }\n" +
                                        "}");
    PsiMethod psiMethod =
      (PsiMethod)TargetElementUtilBase.findTargetElement(myFixture.getEditor(), TargetElementUtilBase.getInstance().getAllAccepted());

    assert psiMethod != null;
    final Collection<PsiElement> methods = getMethodImplementations(psiMethod);
    List<PsiElement> all = new ArrayList<PsiElement>();
    all.add(psiMethod);
    all.addAll(methods);
    final ImplementationViewComponent component = new ImplementationViewComponent(all.toArray(new PsiElement[all.size()]), 0);
    assertContent(component, new String[]{"a.java (AFoo)"});
  }

  public void testMethodsInInnerClasses() {
      myFixture.configureByText("a.java", "abstract class AFoo{\n" +
                                          "    abstract boolean a<caret>aa();\n" +
                                          "    static class AFoo1 extends AFoo {\n" +
                                          "        @Override\n" +
                                          "        boolean aaa() {\n" +
                                          "            return false;\n" +
                                          "        }\n" +
                                          "    }\n" +
                                          "    static class AFoo3 extends AFoo {\n" +
                                          "        @Override\n" +
                                          "        boolean aaa() {\n" +
                                          "            return false;\n" +
                                          "        }\n" +
                                          "    }\n" +
                                          "    static class AFoo2 extends AFoo {\n" +
                                          "        @Override\n" +
                                          "        boolean aaa() {\n" +
                                          "            return false;\n" +
                                          "        }\n" +
                                          "    }\n" +
                                          "    \n" +
                                          "}");
      PsiMethod psiMethod =
        (PsiMethod)TargetElementUtilBase.findTargetElement(myFixture.getEditor(), TargetElementUtilBase.getInstance().getAllAccepted());

    assert psiMethod != null;
    final Collection<PsiMethod> methods = OverridingMethodsSearch.search(psiMethod).findAll();
    List<PsiMethod> all = new ArrayList<PsiMethod>();
    all.add(psiMethod);
    all.addAll(methods);

    //make sure they are in predefined order
    Collections.sort(all, new Comparator<PsiMethod>() {
      @Override
      public int compare(PsiMethod o1, PsiMethod o2) {
        return o1.getContainingClass().getQualifiedName()
          .compareTo(o2.getContainingClass().getQualifiedName());
      }
    });
    final ImplementationViewComponent component =
      new ImplementationViewComponent(all.toArray(new PsiElement[all.size()]), 0);
    assertContent(component, new String[]{"a.java (AFoo)", "a.java (AFoo1 in AFoo)", "a.java (AFoo2 in AFoo)", "a.java (AFoo3 in AFoo)"});
  }

  public static void assertContent(ImplementationViewComponent component, String[] expects) {
    try {
      final String[] visibleFiles = component.getVisibleFiles();
      Assert.assertArrayEquals(Arrays.toString(visibleFiles), expects, visibleFiles);
    }
    finally {
      component.removeNotify();
    }
  }
}