/*
 * Copyright 2000-2007 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.
 */

/*
 * User: anna
 * Date: 15-Aug-2007
 */
package com.intellij.execution.junit;

import com.intellij.codeInsight.TestFramework;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;

public class JUnitTestFramework implements TestFramework {
   public boolean isTestKlass(final PsiClass psiClass) {
    return JUnitUtil.isTestClass(psiClass);
  }

  public PsiMethod findSetUpMethod(final PsiClass psiClass) throws IncorrectOperationException {
    final PsiManager manager = psiClass.getManager();
    final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
    final PsiMethod patternMethod = factory.createMethodFromText("protected void setUp() throws Exception {\nsuper.setUp();\n}", null);

    final PsiClass baseClass = psiClass.getSuperClass();
    if (baseClass != null) {
      final PsiMethod baseMethod = baseClass.findMethodBySignature(patternMethod, false);
      if (baseMethod != null && baseMethod.hasModifierProperty(PsiModifier.PUBLIC)) {
        patternMethod.getModifierList().setModifierProperty(PsiModifier.PROTECTED, false);
        patternMethod.getModifierList().setModifierProperty(PsiModifier.PUBLIC, true);
      }
    }

    PsiMethod inClass = psiClass.findMethodBySignature(patternMethod, false);
    if (inClass == null) {
      return (PsiMethod)psiClass.add(patternMethod);
    }
    else if (inClass.getBody() == null) {
      return (PsiMethod)inClass.replace(patternMethod);
    }
    return inClass;
  }
}
@@ -676,16 +676,64 @@
 junit;%0A%0A
+import com.intellij.codeInsight.AnnotationUtil;%0A
 import c
@@ -1272,24 +1272,413 @@
 tFactory();%0A
+    if (JUnitUtil.isJUnit4TestClass(psiClass)) %7B%0A      final PsiMethod%5B%5D methods = psiClass.getMethods();%0A      for (PsiMethod method : methods) %7B%0A        if (AnnotationUtil.isAnnotated(method, %22org.junit.Before%22, false)) return method;%0A      %7D%0A      return (PsiMethod)psiClass.add(factory.createMethodFromText(%22@org.junit.Before public void setUp() throws Exception %7B%5Cn%7D%22, null));%0A    %7D%0A%0A
     final Ps

move initializer to junit 4 setup method (IDEADEV-29552)