summaryrefslogtreecommitdiff
path: root/plugins/typeMigration/test/com/intellij/refactoring/ChangeTypeSignatureTest.java
blob: e85b2f79fd301269343aa42c1789c2850ee82fcd (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
/*
 * User: anna
 * Date: 18-Mar-2008
 */
package com.intellij.refactoring;

import com.intellij.openapi.application.PluginPathManager;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.PsiImmediateClassType;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.typeMigration.TypeMigrationLabeler;
import com.intellij.refactoring.typeMigration.TypeMigrationProcessor;
import com.intellij.refactoring.typeMigration.TypeMigrationRules;
import com.intellij.testFramework.LightCodeInsightTestCase;
import org.jetbrains.annotations.NotNull;

public class ChangeTypeSignatureTest extends LightCodeInsightTestCase {
  @NotNull
  @Override
  protected String getTestDataPath() {
    return PluginPathManager.getPluginHomePath("typeMigration") + "/testData";
  }

  private void doTest(boolean success, String migrationTypeText) throws Exception {
    String dataPath = "/refactoring/changeTypeSignature/";
    configureByFile(dataPath + getTestName(false) + ".java");
    final PsiFile file = getFile();
    final PsiElement element = file.findElementAt(getEditor().getCaretModel().getOffset());
    final PsiReferenceParameterList parameterList = PsiTreeUtil.getParentOfType(element, PsiReferenceParameterList.class);
    assert parameterList != null;
    final PsiClass superClass = (PsiClass)((PsiJavaCodeReferenceElement)parameterList.getParent()).resolve();
    assert superClass != null;

    PsiType migrationType = getJavaFacade().getElementFactory().createTypeFromText(migrationTypeText, null);

    try {
      final TypeMigrationRules rules = new TypeMigrationRules(TypeMigrationLabeler.getElementType(parameterList));
      rules.setMigrationRootType(PsiSubstitutor.EMPTY.put(superClass.getTypeParameters()[0], migrationType).substitute(new PsiImmediateClassType(superClass, PsiSubstitutor.EMPTY)));
      rules.setBoundScope(GlobalSearchScope.projectScope(getProject()));
      new TypeMigrationProcessor(getProject(), parameterList, rules).run();
      if (success) {
        checkResultByFile(dataPath + getTestName(false) + ".java.after");
      } else {
        fail("Conflicts should be detected");
      }
    }
    catch (RuntimeException e) {
      if (success) {
        e.printStackTrace();
        fail("Conflicts should not appear");
      }
    }
  }

  private void doTest(boolean success) throws Exception {
    doTest(success, CommonClassNames.JAVA_LANG_OBJECT);
  }

  public void testListTypeArguments() throws Exception {
    doTest(true);
  }

  public void testFieldUsage() throws Exception {
    doTest(true);
  }

  public void testFieldUsage1() throws Exception {
    doTest(true);
  }

  public void testReturnType() throws Exception {
    doTest(true);
  }

  public void testReturnType1() throws Exception {
    doTest(true);
  }

  public void testReturnType2() throws Exception {
    doTest(true);
  }

  public void testPassedParameter() throws Exception {
    doTest(true);
  }

  public void testPassedParameter1() throws Exception {
    doTest(true, "java.lang.Integer");
  }

  public void testPassedParameter2() throws Exception {
    doTest(true);
  }

  public void testUsedInSuper() throws Exception {
    doTest(true);
  }

  public void testCompositeReturnType() throws Exception {
    doTest(true);
  }

  public void testTypeHierarchy() throws Exception {
    doTest(true);
  }

  public void testTypeHierarchy1() throws Exception {
    doTest(true);
  }

  public void testTypeHierarchy2() throws Exception {
    doTest(true);
  }

  public void testTypeHierarchyFieldUsage() throws Exception {
    doTest(true);
  }

  public void testTypeHierarchyFieldUsageConflict() throws Exception {
    doTest(true);
  }

  public void testParameterMigration() throws Exception {
    doTest(true);
  }

  public void testParameterMigration1() throws Exception {
    doTest(true, "java.lang.Integer");
  }

  public void testParameterMigration2() throws Exception {
    doTest(true, "java.lang.Integer");
  }

  public void testFieldTypeMigration() throws Exception {
    doTest(true, "java.lang.String");
  }

  public void testMethodReturnTypeMigration() throws Exception {
    doTest(true, "java.lang.Integer");
  }

  @Override
  protected boolean isRunInWriteAction() {
    return false;
  }
}