2 * Copyright 2000-2014 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.refactoring;
18 import com.intellij.JavaTestUtil;
19 import com.intellij.openapi.fileEditor.FileDocumentManager;
20 import com.intellij.openapi.roots.LanguageLevelProjectExtension;
21 import com.intellij.pom.java.LanguageLevel;
22 import com.intellij.psi.PsiClass;
23 import com.intellij.psi.PsiElement;
24 import com.intellij.psi.PsiMember;
25 import com.intellij.psi.PsiModifier;
26 import com.intellij.psi.search.ProjectScope;
27 import com.intellij.refactoring.move.moveMembers.MockMoveMembersOptions;
28 import com.intellij.refactoring.move.moveMembers.MoveMembersProcessor;
29 import com.intellij.util.VisibilityUtil;
30 import org.jetbrains.annotations.NotNull;
32 import java.util.ArrayList;
33 import java.util.LinkedHashSet;
35 public class MoveMembersTest extends MultiFileTestCase {
37 protected String getTestDataPath() {
38 return JavaTestUtil.getJavaTestDataPath();
41 public void testJavadocRefs() throws Exception {
42 doTest("Class1", "Class2", 0);
45 public void testWeirdDeclaration() throws Exception {
49 public void testInnerClass() throws Exception {
53 public void testScr11871() throws Exception {
54 doTest("pack1.A", "pack1.B", 0);
57 public void testOuterClassTypeParameters() throws Exception {
58 doTest("pack1.A", "pack2.B", 0);
61 public void testscr40064() throws Exception {
62 doTest("Test", "Test1", 0);
65 public void testscr40947() throws Exception {
66 doTest("A", "Test", 0, 1);
69 public void testIDEADEV11416() throws Exception {
70 doTest("Y", "X", false, 0);
73 public void testDependantConstants() throws Exception {
74 doTest("A", "B", 0, 1);
77 public void testTwoMethods() throws Exception {
78 doTest("pack1.A", "pack1.C", 0, 1, 2);
81 public void testParameterizedRefOn() throws Exception {
82 doTest("pack1.POne", "pack1.C", 1, 2);
85 public void testIDEADEV12448() throws Exception {
86 doTest("B", "A", false, 0);
89 public void testFieldForwardRef() throws Exception {
90 doTest("A", "Constants", 0);
93 public void testStaticImport() throws Exception {
97 public void testExplicitStaticImport() throws Exception {
101 public void testProtectedConstructor() throws Exception {
102 doTest("pack1.A", "pack1.C", 0);
105 public void testUntouchedVisibility() throws Exception {
106 doTest("pack1.A", "pack1.C", 0, 1);
109 public void testEscalateVisibility() throws Exception {
110 doTest("pack1.A", "pack1.C", true, VisibilityUtil.ESCALATE_VISIBILITY, 0);
113 public void testOtherPackageImport() throws Exception {
114 doTest("pack1.ClassWithStaticMethod", "pack2.OtherClass", 1);
117 public void testEnumConstant() throws Exception {
121 public void testEnumConstantFromCaseStatement() throws Exception {
125 public void testStringConstantFromCaseStatement() throws Exception {
129 public void testDependantFields() throws Exception {
133 public void testStaticImportAndOverridenMethods() throws Exception {
134 doTest("bar.B", "bar.A", 0);
137 public void testWritableField() throws Exception {
140 fail("conflict expected");
142 catch (BaseRefactoringProcessor.ConflictsInTestsException e) {
143 assertEquals("Field <b><code>B.ONE</code></b> has write access but is moved to an interface", e.getMessage());
147 public void testFinalFieldWithInitializer() throws Exception {
150 fail("conflict expected");
152 catch (BaseRefactoringProcessor.ConflictsInTestsException e) {
153 assertEquals("final variable initializer won't be available after move.", e.getMessage());
157 public void testInnerToInterface() throws Exception {
161 public void testStaticToInterface() throws Exception {
162 final LanguageLevelProjectExtension levelProjectExtension = LanguageLevelProjectExtension.getInstance(getProject());
163 final LanguageLevel level = levelProjectExtension.getLanguageLevel();
165 levelProjectExtension.setLanguageLevel(LanguageLevel.JDK_1_8);
169 levelProjectExtension.setLanguageLevel(level);
173 public void testEscalateVisibility1() throws Exception {
174 doTest("A", "B", true, VisibilityUtil.ESCALATE_VISIBILITY, 0);
177 public void testStringConstantInSwitchLabelExpression() throws Exception {
178 doTest("A", "B", true, VisibilityUtil.ESCALATE_VISIBILITY, 0);
181 public void testMultipleWithDependencies() throws Exception {
182 doTest("A", "B", true, VisibilityUtil.ESCALATE_VISIBILITY, 0, 1);
185 public void testMultipleWithDependencies1() throws Exception {
186 doTest("A", "B", true, VisibilityUtil.ESCALATE_VISIBILITY, 0, 1);
189 public void testFromNestedToOuter() throws Exception {
190 doTest("Outer.Inner", "Outer", true, VisibilityUtil.ESCALATE_VISIBILITY, 0);
193 public void testFromNestedToOuterMethodRef() throws Exception {
194 final LanguageLevelProjectExtension projectExtension = LanguageLevelProjectExtension.getInstance(getProject());
195 final LanguageLevel oldLevel = projectExtension.getLanguageLevel();
197 projectExtension.setLanguageLevel(LanguageLevel.HIGHEST);
198 doTest("Outer.Inner", "Outer", true, VisibilityUtil.ESCALATE_VISIBILITY, 0);
201 projectExtension.setLanguageLevel(oldLevel);
207 protected String getTestRoot() {
208 return "/refactoring/moveMembers/";
211 private void doTest(final String sourceClassName, final String targetClassName, final int... memberIndices) throws Exception {
212 doTest(sourceClassName, targetClassName, true, memberIndices);
215 private void doTest(final String sourceClassName,
216 final String targetClassName,
217 final boolean lowercaseFirstLetter,
218 final int... memberIndices) throws Exception {
219 doTest(sourceClassName, targetClassName, lowercaseFirstLetter, null, memberIndices);
222 private void doTest(final String sourceClassName,
223 final String targetClassName,
224 final boolean lowercaseFirstLetter,
225 final String defaultVisibility,
226 final int... memberIndices)
228 doTest((rootDir, rootAfter) -> {
229 MoveMembersTest.this.performAction(sourceClassName, targetClassName, memberIndices, defaultVisibility);
230 }, lowercaseFirstLetter);
233 private void performAction(String sourceClassName, String targetClassName, int[] memberIndices, final String visibility) throws Exception {
234 PsiClass sourceClass = myJavaFacade.findClass(sourceClassName, ProjectScope.getProjectScope(myProject));
235 assertNotNull("Class " + sourceClassName + " not found", sourceClass);
236 PsiClass targetClass = myJavaFacade.findClass(targetClassName, ProjectScope.getProjectScope(myProject));
237 assertNotNull("Class " + targetClassName + " not found", targetClass);
239 PsiElement[] children = sourceClass.getChildren();
240 ArrayList<PsiMember> members = new ArrayList<>();
241 for (PsiElement child : children) {
242 if (child instanceof PsiMember) {
243 members.add(((PsiMember) child));
247 LinkedHashSet<PsiMember> memberSet = new LinkedHashSet<>();
248 for (int index : memberIndices) {
249 PsiMember member = members.get(index);
250 assertTrue(member.hasModifierProperty(PsiModifier.STATIC));
251 memberSet.add(member);
254 MockMoveMembersOptions options = new MockMoveMembersOptions(targetClass.getQualifiedName(), memberSet);
255 options.setMemberVisibility(visibility);
256 new MoveMembersProcessor(myProject, null, options).run();
257 FileDocumentManager.getInstance().saveAllDocuments();