Java CodeInsight test util
authorRoman Shevchenko <roman.shevchenko@jetbrains.com>
Fri, 26 Nov 2010 19:57:23 +0000 (22:57 +0300)
committerRoman Shevchenko <roman.shevchenko@jetbrains.com>
Sat, 27 Nov 2010 09:44:38 +0000 (12:44 +0300)
java/java-impl/src/com/intellij/refactoring/inline/InlineConstantFieldProcessor.java
java/testFramework/src/com/intellij/testFramework/fixtures/JavaCodeInsightTestUtil.java [new file with mode: 0644]

index f33755c9c1efa06966d424bea71933eb012dd9d3..179f9be4e53a701fc74de14d2dfceadd5dde6575 100644 (file)
@@ -40,13 +40,13 @@ import java.util.HashSet;
 /**
  * @author ven
  */
-class InlineConstantFieldProcessor extends BaseRefactoringProcessor {
+public class InlineConstantFieldProcessor extends BaseRefactoringProcessor {
   private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.inline.InlineConstantFieldProcessor");
   private PsiField myField;
   private final PsiReferenceExpression myRefExpr;
   private final boolean myInlineThisOnly;
 
-  InlineConstantFieldProcessor(PsiField field, Project project, PsiReferenceExpression ref, boolean isInlineThisOnly) {
+  public InlineConstantFieldProcessor(PsiField field, Project project, PsiReferenceExpression ref, boolean isInlineThisOnly) {
     super(project);
     myField = field;
     myRefExpr = ref;
diff --git a/java/testFramework/src/com/intellij/testFramework/fixtures/JavaCodeInsightTestUtil.java b/java/testFramework/src/com/intellij/testFramework/fixtures/JavaCodeInsightTestUtil.java
new file mode 100644 (file)
index 0000000..dac9920
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2000-2010 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.intellij.testFramework.fixtures;
+
+import com.intellij.codeInsight.TargetElementUtilBase;
+import com.intellij.openapi.application.Result;
+import com.intellij.openapi.command.WriteCommandAction;
+import com.intellij.openapi.editor.Editor;
+import com.intellij.psi.*;
+import com.intellij.refactoring.inline.InlineConstantFieldProcessor;
+import com.intellij.refactoring.inline.InlineLocalHandler;
+import com.intellij.refactoring.inline.InlineMethodProcessor;
+import com.intellij.refactoring.inline.InlineParameterHandler;
+import com.intellij.refactoring.util.InlineUtil;
+import org.jetbrains.annotations.NotNull;
+
+
+public class JavaCodeInsightTestUtil {
+  private static final int TARGET_FOR_INLINE_FLAGS =
+    TargetElementUtilBase.ELEMENT_NAME_ACCEPTED | TargetElementUtilBase.REFERENCED_ELEMENT_ACCEPTED;
+
+  private JavaCodeInsightTestUtil() { }
+
+  public static void doInlineLocalTest(@NotNull final CodeInsightTestFixture fixture,
+                                       @NotNull final String before, @NotNull final String after) {
+    fixture.configureByFile(before);
+    new WriteCommandAction(fixture.getProject()) {
+      @Override
+      protected void run(final Result result) throws Throwable {
+        final Editor editor = fixture.getEditor();
+        final PsiElement element = TargetElementUtilBase.findTargetElement(editor, TARGET_FOR_INLINE_FLAGS);
+        assert element instanceof PsiLocalVariable : element;
+        InlineLocalHandler.invoke(fixture.getProject(), editor, (PsiLocalVariable)element, null);
+      }
+    }.execute();
+    fixture.checkResultByFile(after, false);
+  }
+
+  public static void doInlineParameterTest(@NotNull final CodeInsightTestFixture fixture,
+                                           @NotNull final String before, @NotNull final String after) {
+    fixture.configureByFile(before);
+    new WriteCommandAction(fixture.getProject()) {
+      @Override
+      protected void run(final Result result) throws Throwable {
+        final Editor editor = fixture.getEditor();
+        final PsiElement element = TargetElementUtilBase.findTargetElement(editor, TARGET_FOR_INLINE_FLAGS);
+        assert element instanceof PsiParameter : element;
+        new InlineParameterHandler().inlineElement(getProject(), editor, element);
+      }
+    }.execute();
+    fixture.checkResultByFile(after, false);
+  }
+
+  public static void doInlineMethodTest(@NotNull final CodeInsightTestFixture fixture,
+                                        @NotNull final String before, @NotNull final String after) {
+    fixture.configureByFile(before);
+    new WriteCommandAction(fixture.getProject()) {
+      @Override
+      protected void run(final Result result) throws Throwable {
+        final Editor editor = fixture.getEditor();
+        final PsiElement element = TargetElementUtilBase.findTargetElement(editor, TARGET_FOR_INLINE_FLAGS);
+        assert element instanceof PsiMethod : element;
+
+        final PsiReference ref = fixture.getFile().findReferenceAt(editor.getCaretModel().getOffset());
+        final PsiReferenceExpression refExpr = ref instanceof PsiReferenceExpression ? (PsiReferenceExpression)ref : null;
+
+        final PsiMethod method = (PsiMethod)element;
+        assert !(InlineMethodProcessor.checkBadReturns(method) && !InlineUtil.allUsagesAreTailCalls(method)) : "Bad returns found";
+        new InlineMethodProcessor(getProject(), method, refExpr, editor, false).run();
+      }
+    }.execute();
+    fixture.checkResultByFile(after, false);
+  }
+
+  public static void doInlineConstantTest(@NotNull final CodeInsightTestFixture fixture,
+                                          @NotNull final String before, @NotNull final String after) {
+    fixture.configureByFile(before);
+    new WriteCommandAction(fixture.getProject()) {
+      @Override
+      protected void run(final Result result) throws Throwable {
+        final Editor editor = fixture.getEditor();
+        final PsiElement element = TargetElementUtilBase.findTargetElement(editor, TARGET_FOR_INLINE_FLAGS);
+        assert element instanceof PsiField : element;
+
+        final PsiReference ref = fixture.getFile().findReferenceAt(editor.getCaretModel().getOffset());
+        final PsiReferenceExpression refExpr = ref instanceof PsiReferenceExpression ? (PsiReferenceExpression)ref : null;
+
+        new InlineConstantFieldProcessor((PsiField)element, getProject(), refExpr, false).run();
+      }
+    }.execute();
+    fixture.checkResultByFile(after, false);
+  }
+}
\ No newline at end of file