// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package com.intellij;

import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.text.NameUtilCore;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class ClassFinder {
  private final List<String> classNameList = new ArrayList<>();
  private final int startPackageName;
  private final boolean includeUnconventionallyNamedTests;

  public ClassFinder(final File classPathRoot, final String rootPackage, boolean includeUnconventionallyNamedTests) {
    this.includeUnconventionallyNamedTests = includeUnconventionallyNamedTests;
    startPackageName = classPathRoot.getAbsolutePath().length() + 1;
    String directoryOffset = rootPackage.replace('.', File.separatorChar);
    findAndStoreTestClasses(new File(classPathRoot, directoryOffset));
  }

  @Nullable
  private String computeClassName(final File file) {
    String absPath = file.getAbsolutePath();
    if (!includeUnconventionallyNamedTests) {
      if (absPath.endsWith("Test.class")) {
        return StringUtil.trimEnd(absPath.substring(startPackageName), ".class").replace(File.separatorChar, '.');
      }
    }
    else {
      String className = file.getName();
      if (className.endsWith(".class")) {
        int dollar = className.lastIndexOf("$");
        if (dollar != -1) {
          className = className.substring(dollar + 1);
          // most likely something like RecursionManagerTest$_testMayCache_closure5 or other anonymous class
          // may cause https://issues.apache.org/jira/browse/GROOVY-5351
          if (!Character.isUpperCase(className.charAt(0))) return null;
        }

        // A test may be named Test*, *Test, *Tests*, *TestCase, *TestSuite, *Suite, etc
        List<String> words = Arrays.asList(NameUtilCore.nameToWords(className));

        if (words.contains("Test") || words.contains("Tests") || words.contains("Suite")) {
          String fqn = StringUtil.trimEnd(absPath.substring(startPackageName), ".class").replace(File.separatorChar, '.');
          if (!Arrays.asList("com.intellij.tests.BootstrapTests", "com.intellij.AllTests").contains(fqn)) {
            return fqn;
          }
        }
      }
    }
    return null;
  }

  private void findAndStoreTestClasses(@NotNull File current) {
    if (current.isDirectory()) {
      File[] files = current.listFiles();
      if (files != null) {
        for (File file : files) {
          findAndStoreTestClasses(file);
        }
      }
    }
    else {
      ContainerUtil.addIfNotNull(classNameList, computeClassName(current));
    }
  }

  public Collection<String> getClasses() {
    return classNameList;
  }
}

@@ -431,94 +431,9 @@
 til.
-ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.Collection;%0Aimport java.util.List
+*
 ;%0A%0Ap
@@ -1391,17 +1391,23 @@
  String 
-c
+nestedC
 lassName
@@ -1435,17 +1435,24 @@
     if (
-c
+!nestedC
 lassName
@@ -1486,55 +1486,179 @@
     
-int dollar = className.lastIndexOf(%22
+return null;%0A      %7D%0A      nestedClassName = StringUtil.trimEnd(nestedClassName, %22.class%22);%0A%0A      List%3CString%3E names = Arrays.asList(nestedClassName.split(%22%5C%5C
 $%22)
+)
 ;%0A      
   if
@@ -1657,87 +1657,75 @@
     
-  if (dollar != -1) %7B%0A          className = className.substring(dollar + 1);%0A  
+Collections.reverse(names);%0A      for (String className : names) %7B%0A
     
@@ -1827,18 +1827,16 @@
 s class%0A
-  
         
@@ -1902,26 +1902,24 @@
 351%0A        
-  
 if (!Charact
@@ -1967,26 +1967,16 @@
 rn null;
-%0A        %7D
 %0A%0A      

Improve algorithm of picking tests with unconventional name