5 package com
.intellij
.refactoring
;
7 import com
.intellij
.JavaTestUtil
;
8 import com
.intellij
.openapi
.fileEditor
.FileDocumentManager
;
9 import com
.intellij
.openapi
.project
.Project
;
10 import com
.intellij
.openapi
.vfs
.LocalFileSystem
;
11 import com
.intellij
.openapi
.vfs
.VirtualFile
;
12 import com
.intellij
.psi
.*;
13 import com
.intellij
.psi
.search
.GlobalSearchScope
;
14 import com
.intellij
.psi
.util
.PropertyUtil
;
15 import com
.intellij
.refactoring
.encapsulateFields
.EncapsulateFieldsDescriptor
;
16 import com
.intellij
.refactoring
.encapsulateFields
.EncapsulateFieldsProcessor
;
17 import junit
.framework
.Assert
;
18 import org
.jetbrains
.annotations
.Nullable
;
20 public class EncapsulateFieldsTest
extends MultiFileTestCase
{
21 public void testAlreadyExist() throws Exception
{
25 public void testDiffWithReturnTypeOnly() throws Exception
{
26 doTest("i", "There already is a method <b><code>Test setI(int)</code></b> which differs from setter <b><code>setI</code></b> by return type only.");
29 public void testDiffWithReturnTypeOnlyInHierarchy() throws Exception
{
30 doTest("i", "There already is a method <b><code>Super setI(int)</code></b> which differs from setter <b><code>setI</code></b> by return type only.");
33 public void testHideOverriderMethod() throws Exception
{
34 doTest("i", "A", "There is already a <b><code>method <b><code>B.getI()</code></b></code></b> which would hide generated getter for a.i");
37 public void testHideOuterclassMethod() throws Exception
{
38 doTest("i", "A.B", "There is already a <b><code>method <b><code>A.getI()</code></b></code></b> which would be hidden by generated getter");
42 protected String
getTestDataPath() {
43 return JavaTestUtil
.getJavaTestDataPath();
46 protected String
getTestRoot() {
47 return "/refactoring/encapsulateFields/";
51 private void doTest(final String fieldName
, final String conflicts
) throws Exception
{
52 doTest(fieldName
, "Test", conflicts
);
55 private void doTest(final String fieldName
, final String className
, final String conflicts
) throws Exception
{
56 doTest(new PerformAction() {
57 public void performAction(final VirtualFile rootDir
, final VirtualFile rootAfter
) throws Exception
{
58 PsiClass aClass
= myJavaFacade
.findClass(className
, GlobalSearchScope
.projectScope(myProject
));
60 assertNotNull("Tested class not found", aClass
);
63 doTest(aClass
, aClass
.findFieldByName(fieldName
, false), conflicts
, true, true);
70 private static void doTest(final PsiClass aClass
, final PsiField field
, final String conflicts
,
71 final boolean generateGetters
, final boolean generateSetters
) {
73 final Project project
= aClass
.getProject();
74 EncapsulateFieldsProcessor processor
= new EncapsulateFieldsProcessor(project
, new EncapsulateFieldsDescriptor() {
75 public PsiField
[] getSelectedFields() {
76 return new PsiField
[]{field
};
79 public String
[] getGetterNames() {
80 return new String
[]{PropertyUtil
.suggestGetterName(project
, field
)};
83 public String
[] getSetterNames() {
84 return new String
[]{PropertyUtil
.suggestSetterName(project
, field
)};
88 public PsiMethod
[] getGetterPrototypes() {
89 return isToEncapsulateGet() ?
new PsiMethod
[]{PropertyUtil
.generateGetterPrototype(field
)} : null;
93 public PsiMethod
[] getSetterPrototypes() {
94 return isToEncapsulateSet() ?
new PsiMethod
[]{PropertyUtil
.generateSetterPrototype(field
)} : null;
97 public boolean isToEncapsulateGet() {
98 return generateGetters
;
101 public boolean isToEncapsulateSet() {
102 return generateSetters
;
105 public boolean isToUseAccessorsWhenAccessible() {
110 public String
getFieldsVisibility() {
115 public String
getAccessorsVisibility() {
116 return PsiModifier
.PUBLIC
;
120 LocalFileSystem
.getInstance().refresh(false);
121 FileDocumentManager
.getInstance().saveAllDocuments();
123 catch (BaseRefactoringProcessor
.ConflictsInTestsException e
) {
124 if (conflicts
!= null) {
125 Assert
.assertEquals(conflicts
, e
.getMessage());
129 fail(e
.getMessage());
132 if (conflicts
!= null) {
133 fail("Conflicts were not detected: " + conflicts
);