@Override
  protected void setUp() throws Exception {
    super.setUp();

    String root = JavaTestUtil.getJavaTestDataPath() + "/psi/repositoryUse/modifyAnnotations";
    PsiTestUtil.removeAllRoots(myModule, IdeaTestUtil.getMockJdk17("mock 1.5"));
    PsiTestUtil.createTestProjectStructure(myProject, myModule, root, myFilesToDelete);
  }
 @Override
 protected void initApplication() throws Exception {
   super.initApplication();
   JavaTestUtil.setupTestJDK();
   DebuggerSettings.getInstance().DEBUGGER_TRANSPORT = DebuggerSettings.SOCKET_TRANSPORT;
   DebuggerSettings.getInstance().SKIP_CONSTRUCTORS = false;
   DebuggerSettings.getInstance().SKIP_GETTERS = false;
   NodeRendererSettings.getInstance().getClassRenderer().SHOW_DECLARED_TYPE = true;
 }
  @Override
  protected void setUp() throws Exception {
    super.setUp();

    String root =
        JavaTestUtil.getJavaTestDataPath() + "/psi/search/findUsages/" + getTestName(true);
    PsiTestUtil.removeAllRoots(myModule, JavaSdkImpl.getMockJdk17());
    PsiTestUtil.createTestProjectStructure(myProject, myModule, root, myFilesToDelete);
  }
 private void verifyJavaDoc(final PsiElement field) throws IOException {
   final File htmlPath =
       new File(
           JavaTestUtil.getJavaTestDataPath()
               + "/codeInsight/javadocIG/"
               + getTestName(true)
               + ".html");
   String htmlText = FileUtil.loadFile(htmlPath);
   String docInfo = new JavaDocInfoGenerator(getProject(), field).generateDocInfo(null);
   assertNotNull(docInfo);
   assertEquals(
       StringUtil.convertLineSeparators(htmlText.trim()),
       StringUtil.convertLineSeparators(docInfo.trim()));
 }
  private void loadAndSetupProject(String path) throws Exception {
    LocalFileSystem.getInstance().refreshIoFiles(myFilesToDelete);

    myProject = ProjectManager.getInstance().loadAndOpenProject(path);

    setUpModule();

    final String root = JavaTestUtil.getJavaTestDataPath() + "/psi/search/updateCache";
    PsiTestUtil.createTestProjectStructure(myProject, myModule, root, myFilesToDelete);

    setUpJdk();

    myProjectManager.openTestProject(myProject);
    runStartupActivities();
  }
 public void testEnumConstantOrdinal() throws Exception {
   PsiClass psiClass = getTestClass();
   PsiField field = psiClass.getFields()[0];
   final File htmlPath =
       new File(
           JavaTestUtil.getJavaTestDataPath()
               + "/codeInsight/javadocIG/"
               + getTestName(true)
               + ".html");
   String htmlText = FileUtil.loadFile(htmlPath);
   String docInfo = new JavaDocumentationProvider().getQuickNavigateInfo(field, field);
   assertNotNull(docInfo);
   assertEquals(
       StringUtil.convertLineSeparators(htmlText.trim()),
       StringUtil.convertLineSeparators(docInfo.trim()));
 }
 public void testPackageInfo() throws Exception {
   final String path = JavaTestUtil.getJavaTestDataPath() + "/codeInsight/javadocIG/";
   final String packageInfo = path + getTestName(true);
   PsiTestUtil.createTestProjectStructure(myProject, myModule, path, myFilesToDelete);
   final String info =
       new JavaDocInfoGenerator(
               getProject(),
               JavaPsiFacade.getInstance(getProject()).findPackage(getTestName(true)))
           .generateDocInfo(null);
   String htmlText =
       FileUtil.loadFile(new File(packageInfo + File.separator + "packageInfo.html"));
   assertNotNull(info);
   assertEquals(
       StringUtil.convertLineSeparators(htmlText.trim()),
       StringUtil.convertLineSeparators(info.trim()));
 }
 public void testClassTypeParamsPresentation() throws Exception {
   PsiClass psiClass = getTestClass();
   final PsiReferenceList extendsList = psiClass.getExtendsList();
   final PsiJavaCodeReferenceElement referenceElement = extendsList.getReferenceElements()[0];
   final PsiClass superClass = extendsList.getReferencedTypes()[0].resolve();
   final File htmlPath =
       new File(
           JavaTestUtil.getJavaTestDataPath()
               + "/codeInsight/javadocIG/"
               + getTestName(true)
               + ".html");
   String htmlText = FileUtil.loadFile(htmlPath);
   String docInfo =
       new JavaDocumentationProvider().getQuickNavigateInfo(superClass, referenceElement);
   assertNotNull(docInfo);
   assertEquals(
       StringUtil.convertLineSeparators(htmlText.trim()),
       StringUtil.convertLineSeparators(docInfo.trim()));
 }
 @Override
 protected String getTestDataPath() {
   return JavaTestUtil.getJavaTestDataPath();
 }
/**
 * Base class for java formatter tests that holds utility methods.
 *
 * @author Denis Zhdanov
 * @since Apr 27, 2010 6:26:29 PM
 */
public abstract class AbstractJavaFormatterTest extends LightIdeaTestCase {
  @NotNull
  public static String shiftIndentInside(
      @NotNull String initial, final int i, boolean shiftEmptyLines) {
    StringBuilder result = new StringBuilder(initial.length());
    List<byte[]> lines;
    try {
      LineReader reader =
          new LineReader(new ByteArrayInputStream(initial.getBytes(CharsetToolkit.UTF8_CHARSET)));
      lines = reader.readLines();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    boolean first = true;
    for (byte[] line : lines) {
      try {
        if (!first) result.append('\n');
        if (line.length > 0 || shiftEmptyLines) {
          StringUtil.repeatSymbol(result, ' ', i);
        }
        result.append(new String(line));
      } finally {
        first = false;
      }
    }

    return result.toString();
  }

  protected enum Action {
    REFORMAT,
    INDENT
  }

  public static JavaCodeStyleSettings getJavaSettings() {
    return getSettings().getRootSettings().getCustomSettings(JavaCodeStyleSettings.class);
  }

  private interface TestFormatAction {
    void run(PsiFile psiFile, int startOffset, int endOffset);
  }

  private static final Map<Action, TestFormatAction> ACTIONS =
      new EnumMap<Action, TestFormatAction>(Action.class);

  static {
    ACTIONS.put(
        Action.REFORMAT,
        new TestFormatAction() {
          @Override
          public void run(PsiFile psiFile, int startOffset, int endOffset) {
            CodeStyleManager.getInstance(getProject())
                .reformatText(psiFile, startOffset, endOffset);
          }
        });
    ACTIONS.put(
        Action.INDENT,
        new TestFormatAction() {
          @Override
          public void run(PsiFile psiFile, int startOffset, int endOffset) {
            CodeStyleManager.getInstance(getProject()).adjustLineIndent(psiFile, startOffset);
          }
        });
  }

  private static final String BASE_PATH =
      JavaTestUtil.getJavaTestDataPath() + "/psi/formatter/java";

  public TextRange myTextRange;
  public TextRange myLineRange;

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.HIGHEST);
  }

  public static CommonCodeStyleSettings getSettings() {
    CodeStyleSettings rootSettings = CodeStyleSettingsManager.getSettings(getProject());
    return rootSettings.getCommonSettings(JavaLanguage.INSTANCE);
  }

  public static CommonCodeStyleSettings.IndentOptions getIndentOptions() {
    return getSettings().getRootSettings().getIndentOptions(StdFileTypes.JAVA);
  }

  public void doTest() {
    doTest(getTestName(false) + ".java", getTestName(false) + "_after.java");
  }

  public void doTest(@NonNls String fileNameBefore, @NonNls String fileNameAfter) {
    doTextTest(Action.REFORMAT, loadFile(fileNameBefore), loadFile(fileNameAfter));
  }

  public void doTestWithDetectableIndentOptions(@NonNls String text, @NonNls String textAfter) {
    DetectableIndentOptionsProvider provider = DetectableIndentOptionsProvider.getInstance();
    assertNotNull("DetectableIndentOptionsProvider not found", provider);
    provider.setEnabledInTest(true);
    try {
      doTextTest(text, textAfter);
    } finally {
      provider.setEnabledInTest(false);
    }
  }

  public void doTextTest(@NonNls final String text, @NonNls String textAfter)
      throws IncorrectOperationException {
    doTextTest(Action.REFORMAT, text, textAfter);
  }

  public void doTextTest(
      @NotNull final Action action, @NotNull String text, @NotNull String textAfter)
      throws IncorrectOperationException {
    final PsiFile file = createFile("A.java", text);
    final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
    final Document document = manager.getDocument(file);
    if (document == null) {
      fail("Document is null");
      return;
    }
    replaceAndProcessDocument(action, text, file, document);
    assertEquals(textAfter, document.getText());
    manager.commitDocument(document);
    assertEquals(textAfter, file.getText());
  }

  public void formatEveryoneAndCheckIfResultEqual(@NotNull final String... before) {
    assert before.length > 1;
    final PsiFile file = createFile("A.java", "");
    final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
    final Document document = manager.getDocument(file);
    String afterFirst = replaceAndProcessDocument(Action.REFORMAT, before[0], file, document);
    for (String nextBefore : before) {
      assertEquals(
          afterFirst, replaceAndProcessDocument(Action.REFORMAT, nextBefore, file, document));
    }
  }

  @NotNull
  private String replaceAndProcessDocument(
      @NotNull final Action action,
      @NotNull final String text,
      @NotNull final PsiFile file,
      @Nullable final Document document)
      throws IncorrectOperationException {
    if (document == null) {
      fail("Don't expect the document to be null");
      return null;
    }
    if (myLineRange != null) {
      final DocumentImpl doc = new DocumentImpl(text);
      myTextRange =
          new TextRange(
              doc.getLineStartOffset(myLineRange.getStartOffset()),
              doc.getLineEndOffset(myLineRange.getEndOffset()));
    }
    final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
    CommandProcessor.getInstance()
        .executeCommand(
            getProject(),
            new Runnable() {
              @Override
              public void run() {
                ApplicationManager.getApplication()
                    .runWriteAction(
                        new Runnable() {
                          @Override
                          public void run() {
                            document.replaceString(0, document.getTextLength(), text);
                            manager.commitDocument(document);
                            try {
                              TextRange rangeToUse = myTextRange;
                              if (rangeToUse == null) {
                                rangeToUse = file.getTextRange();
                              }
                              ACTIONS
                                  .get(action)
                                  .run(
                                      file, rangeToUse.getStartOffset(), rangeToUse.getEndOffset());
                            } catch (IncorrectOperationException e) {
                              assertTrue(e.getLocalizedMessage(), false);
                            }
                          }
                        });
              }
            },
            action == Action.REFORMAT ? ReformatCodeProcessor.COMMAND_NAME : "",
            "");

    return document.getText();
  }

  public void doMethodTest(@NonNls final String before, @NonNls final String after) {
    doTextTest(
        Action.REFORMAT,
        "class Foo{\n" + "    void foo() {\n" + before + '\n' + "    }\n" + "}",
        "class Foo {\n"
            + "    void foo() {\n"
            + shiftIndentInside(after, 8, false)
            + '\n'
            + "    }\n"
            + "}");
  }

  public void doClassTest(@NonNls final String before, @NonNls final String after) {
    doTextTest(
        Action.REFORMAT,
        "class Foo{\n" + before + '\n' + "}",
        "class Foo {\n" + shiftIndentInside(after, 4, false) + '\n' + "}");
  }

  private static String loadFile(String name) {
    String fullName = BASE_PATH + File.separatorChar + name;
    try {
      String text = FileUtil.loadFile(new File(fullName));
      return StringUtil.convertLineSeparators(text);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
}
 @NotNull
 @Override
 protected String getTestDataPath() {
   return JavaTestUtil.getJavaTestDataPath() + "/inspection";
 }
 @Override
 protected String getTestDataPath() {
   return JavaTestUtil.getJavaTestDataPath() + "/inspection/dataFlow/fixture/";
 }
 @Override
 protected String getTestDataPath() {
   return JavaTestUtil.getJavaTestDataPath() + "/inspection/redundantTypeArgs/";
 }
 @Override
 protected String getBasePath() {
   return JavaTestUtil.getRelativeJavaTestDataPath()
       + "/codeInspection/redundantLambdaParameterType";
 }
 @Override
 protected String getTestDataPath() {
   return JavaTestUtil.getJavaTestDataPath()
       + "/codeInsight/daemonCodeAnalyzer/quickFix/addSingleStaticImport";
 }