1 package com
.intellij
.refactoring
;
3 import com
.intellij
.JavaTestUtil
;
4 import com
.intellij
.codeInsight
.CodeInsightTestCase
;
5 import com
.intellij
.openapi
.fileEditor
.FileDocumentManager
;
6 import com
.intellij
.openapi
.projectRoots
.impl
.JavaSdkImpl
;
7 import com
.intellij
.openapi
.vfs
.LocalFileSystem
;
8 import com
.intellij
.openapi
.vfs
.VirtualFile
;
9 import com
.intellij
.psi
.PsiClass
;
10 import com
.intellij
.psi
.PsiDocumentManager
;
11 import com
.intellij
.psi
.PsiElement
;
12 import com
.intellij
.psi
.codeStyle
.CodeStyleSettingsManager
;
13 import com
.intellij
.psi
.impl
.source
.PostprocessReformattingAspect
;
14 import com
.intellij
.psi
.search
.ProjectScope
;
15 import com
.intellij
.refactoring
.move
.moveClassesOrPackages
.MoveClassToInnerProcessor
;
16 import com
.intellij
.testFramework
.IdeaTestUtil
;
17 import com
.intellij
.testFramework
.PsiTestUtil
;
18 import com
.intellij
.usageView
.UsageInfo
;
19 import com
.intellij
.util
.containers
.MultiMap
;
26 public class MoveClassToInnerTest
extends CodeInsightTestCase
{
27 public void testContextChange1() throws Exception
{
28 doTest(new String
[] { "pack1.Class1" }, "pack2.A");
31 public void testContextChange2() throws Exception
{
32 doTest(new String
[] { "pack1.Class1" }, "pack2.A");
35 public void testInnerImport() throws Exception
{
36 doTest(new String
[] { "pack1.Class1" }, "pack2.A");
39 public void testInsertInnerClassImport() throws Exception
{
40 final boolean imports
= CodeStyleSettingsManager
.getSettings(myProject
).INSERT_INNER_CLASS_IMPORTS
;
42 CodeStyleSettingsManager
.getSettings(myProject
).INSERT_INNER_CLASS_IMPORTS
= true;
43 doTest(new String
[] { "pack1.Class1" }, "pack2.A");
46 CodeStyleSettingsManager
.getSettings(myProject
).INSERT_INNER_CLASS_IMPORTS
= imports
;
50 public void testSimultaneousMove() throws Exception
{
51 doTest(new String
[] { "pack1.Class1", "pack0.Class0" }, "pack2.A");
54 public void testMoveMultiple1() throws Exception
{
55 doTest(new String
[] { "pack1.Class1", "pack1.Class2" }, "pack2.A");
58 public void testRefToInner() throws Exception
{
59 doTest(new String
[] { "pack1.Class1" }, "pack2.A");
62 public void testRefToConstructor() throws Exception
{
63 doTest(new String
[] { "pack1.Class1" }, "pack2.A");
66 public void testSecondaryClass() throws Exception
{
67 doTest(new String
[] { "pack1.Class2" }, "pack1.User");
70 public void testStringsAndComments() throws Exception
{
71 doTest(new String
[] { "pack1.Class1" }, "pack1.A");
74 public void testStringsAndComments2() throws Exception
{
75 doTest(new String
[] { "pack1.Class1" }, "pack1.A");
78 public void testNonJava() throws Exception
{
79 doTest(new String
[] { "pack1.Class1" }, "pack1.A");
82 public void testPackageLocalClass() throws Exception
{
83 doTestConflicts("pack1.Class1", "pack2.A", "Field <b><code>Class1.c2</code></b> uses a package-local class <b><code>pack1.Class2</code></b>.");
86 public void testMoveIntoPackageLocalClass() throws Exception
{
87 doTestConflicts("pack1.Class1", "pack2.A", "Class <b><code>Class1</code></b> will no longer be accessible from field <b><code>Class2.c1</code></b>");
90 public void testMoveOfPackageLocalClass() throws Exception
{
91 doTestConflicts("pack1.Class1", "pack2.A", "Class <b><code>Class1</code></b> will no longer be accessible from field <b><code>Class2.c1</code></b>");
94 public void testMoveIntoPrivateInnerClass() throws Exception
{
95 doTestConflicts("pack1.Class1", "pack1.A.PrivateInner", "Class <b><code>Class1</code></b> will no longer be accessible from field <b><code>Class2.c1</code></b>");
98 public void testMoveWithPackageLocalMember() throws Exception
{
99 doTestConflicts("pack1.Class1", "pack2.A", "Method <b><code>Class1.doStuff()</code></b> will no longer be accessible from method <b><code>Class2.test()</code></b>");
102 public void testDuplicateInner() throws Exception
{
103 doTestConflicts("pack1.Class1", "pack2.A", "Class <b><code>pack2.A</code></b> already contains an inner class named <b><code>Class1</code></b>");
106 private void doTest(String
[] classNames
, String targetClassName
) throws Exception
{
107 VirtualFile rootDir
= prepareTest();
109 performAction(classNames
, targetClassName
);
111 String rootAfter
= getRoot() + "/after";
112 VirtualFile rootDir2
= LocalFileSystem
.getInstance().findFileByPath(rootAfter
.replace(File
.separatorChar
, '/'));
113 myProject
.getComponent(PostprocessReformattingAspect
.class).doPostponedFormatting();
114 IdeaTestUtil
.assertDirectoriesEqual(rootDir2
, rootDir
, IdeaTestUtil
.CVS_FILE_FILTER
);
117 private VirtualFile
prepareTest() throws Exception
{
118 String rootBefore
= getRoot() + "/before";
119 PsiTestUtil
.removeAllRoots(myModule
, JavaSdkImpl
.getMockJdk("java 1.4"));
120 return PsiTestUtil
.createTestProjectStructure(myProject
, myModule
, rootBefore
, myFilesToDelete
);
123 private String
getRoot() {
124 return JavaTestUtil
.getJavaTestDataPath() + "/refactoring/moveClassToInner/" + getTestName(true);
127 private void doTestConflicts(String className
, String targetClassName
, String
... expectedConflicts
) throws Exception
{
129 PsiClass classToMove
= myJavaFacade
.findClass(className
, ProjectScope
.getAllScope(myProject
));
130 PsiClass targetClass
= myJavaFacade
.findClass(targetClassName
, ProjectScope
.getAllScope(myProject
));
131 MoveClassToInnerProcessor processor
= new MoveClassToInnerProcessor(myProject
, new PsiClass
[]{classToMove
}, targetClass
, true, true, null);
132 UsageInfo
[] usages
= processor
.findUsages();
133 MultiMap
<PsiElement
,String
> conflicts
= processor
.getConflicts(usages
);
134 assertSameElements(conflicts
.values() , expectedConflicts
);
137 private void performAction(String
[] classNames
, String targetClassName
) throws Exception
{
138 final PsiClass
[] classes
= new PsiClass
[classNames
.length
];
139 for(int i
= 0; i
< classes
.length
; i
++){
140 String className
= classNames
[i
];
141 classes
[i
] = myJavaFacade
.findClass(className
, ProjectScope
.getAllScope(myProject
));
142 assertNotNull("Class " + className
+ " not found", classes
[i
]);
145 PsiClass targetClass
= myJavaFacade
.findClass(targetClassName
, ProjectScope
.getAllScope(myProject
));
146 assertNotNull(targetClass
);
148 new MoveClassToInnerProcessor(myProject
, classes
, targetClass
, true, true, null).run();
149 PsiDocumentManager
.getInstance(myProject
).commitAllDocuments();
150 FileDocumentManager
.getInstance().saveAllDocuments();