package com.intellij.testAssistant;

import com.intellij.codeHighlighting.Pass;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.daemon.LineMarkerInfo;
import com.intellij.codeInsight.daemon.LineMarkerProvider;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.util.PlatformIcons;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author yole
 */
public class TestDataLineMarkerProvider implements LineMarkerProvider {
  public LineMarkerInfo getLineMarkerInfo(PsiElement element) {
    if (!(element instanceof PsiMethod)) {
      return null;
    }
    final PsiMethod method = (PsiMethod)element;
    if (isTestMethod(method)) {
      return new LineMarkerInfo<PsiMethod>(method, method.getTextOffset(), PlatformIcons.TEST_SOURCE_FOLDER, Pass.UPDATE_ALL, null,
                                           new TestDataNavigationHandler());
    }
    return null;
  }

  private static boolean isTestMethod(@NotNull PsiMethod method) {
    if (isTestMethodWithAnnotation(method)) {
      return true;
    }

    final List<String> files = TestDataGuessByExistingFilesUtil.collectTestDataByExistingFiles(method);
    return files != null && !files.isEmpty();
  }

  private static boolean isTestMethodWithAnnotation(@NotNull PsiMethod method) {
    String name = method.getName();
    if (!name.startsWith("test")) {
      return false;
    }
    String testDataPath = getTestDataBasePath(method.getContainingClass());
    if (testDataPath == null) {
      return false;
    }
    List<String> fileNames = new TestDataReferenceCollector(testDataPath, name.substring(4)).collectTestDataReferences(method);
    return fileNames != null && !fileNames.isEmpty();
  }
  
  public void collectSlowLineMarkers(List<PsiElement> elements, Collection<LineMarkerInfo> result) {
  }

  @Nullable
  public static String getTestDataBasePath(PsiClass psiClass) {
    final PsiAnnotation annotation = AnnotationUtil.findAnnotationInHierarchy(psiClass, Collections.singleton("com.intellij.testFramework.TestDataPath"));
    if (annotation != null) {
      final PsiAnnotationMemberValue value = annotation.findAttributeValue(PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME);
      if (value instanceof PsiExpression) {
        final Project project = value.getProject();
        final PsiConstantEvaluationHelper evaluationHelper = JavaPsiFacade.getInstance(project).getConstantEvaluationHelper();
        final Object constantValue = evaluationHelper.computeConstantExpression(value, false);
        if (constantValue instanceof String) {
          String path = (String) constantValue;
          if (path.indexOf("$CONTENT_ROOT") >= 0) {
            final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
            final VirtualFile contentRoot = fileIndex.getContentRootForFile(psiClass.getContainingFile().getVirtualFile());
            if (contentRoot == null) return null;
            path = path.replace("$CONTENT_ROOT", contentRoot.getPath());
          }
          if (path.indexOf("$PROJECT_ROOT") >= 0) {
            final VirtualFile baseDir = project.getBaseDir();
            if (baseDir == null) {
              return null;
            }
            path = path.replace("$PROJECT_ROOT", baseDir.getPath());
          }
          return path;
        }
      }
    }
    return null;
  }

}

@@ -1,8 +1,609 @@
+/*%0A * Copyright 2000-2011 JetBrains s.r.o.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 
@@ -832,24 +832,84 @@
 erProvider;%0A
+import com.intellij.openapi.application.ApplicationManager;%0A
 import com.i
@@ -1492,372 +1492,8 @@
 ) %7B%0A
-    if (!(element instanceof PsiMethod)) %7B%0A      return null;%0A    %7D%0A    final PsiMethod method = (PsiMethod)element;%0A    if (isTestMethod(method)) %7B%0A      return new LineMarkerInfo%3CPsiMethod%3E(method, method.getTextOffset(), PlatformIcons.TEST_SOURCE_FOLDER, Pass.UPDATE_ALL, null,%0A                                           new TestDataNavigationHandler());%0A    %7D%0A
     
@@ -2407,16 +2407,505 @@
 sult) %7B%0A
+    if (ApplicationManager.getApplication().isUnitTestMode()) %7B%0A      return;%0A    %7D%0A    for (PsiElement element : elements) %7B%0A      if (!(element instanceof PsiMethod)) %7B%0A        continue;%0A      %7D%0A      final PsiMethod method = (PsiMethod)element;%0A      if (isTestMethod(method)) %7B%0A        result.add(new LineMarkerInfo%3CPsiMethod%3E(%0A          method, method.getTextOffset(), PlatformIcons.TEST_SOURCE_FOLDER, Pass.UPDATE_ALL, null, new TestDataNavigationHandler()%0A        ));%0A      %7D%0A    %7D%0A
   %7D%0A%0A  @
@@ -3716,23 +3716,24 @@
 f (path.
-indexOf
+contains
 (%22$CONTE
@@ -3733,37 +3733,32 @@
 %22$CONTENT_ROOT%22)
- %3E= 0
 ) %7B%0A            
@@ -3882,99 +3882,215 @@
 ile 
-contentRoot = fileIndex.getContentRootForFile(psiClass.getContainingFile().getVirtualFile()
+file = psiClass.getContainingFile().getVirtualFile();%0A            if (file == null) %7B%0A              return null;%0A            %7D%0A            final VirtualFile contentRoot = fileIndex.getContentRootForFile(file
 );%0A 
@@ -4246,15 +4246,16 @@
 ath.
-indexOf
+contains
 (%22$P
@@ -4271,13 +4271,8 @@
 OT%22)
- %3E= 0
 ) %7B%0A

Test fixes 1. Don't try to match test data during tests execution; 2. Moved matching logic to 'collect slow line markers';