package com.intellij.openapi.projectRoots.ex;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.ProjectJdk;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.rt.compiler.JavacRunner;
import com.intellij.util.PathUtil;
import com.intellij.util.PathsList;
import com.intellij.util.Function;
import com.intellij.util.containers.*;
import static com.intellij.util.containers.ContainerUtil.*;

import java.util.*;

import org.jetbrains.annotations.NonNls;

/**
 * @author Eugene Zhuravlev
 *         Date: Apr 14, 2004
 */
public class PathUtilEx {
  @NonNls private static final String IDEA_PREPEND_RTJAR = "idea.prepend.rtjar";

  private static final Function<Module, ProjectJdk> MODULE_JDK = new Function<Module, ProjectJdk>() {
    public ProjectJdk fun(Module module) {
      return ModuleRootManager.getInstance(module).getJdk();
    }
  };
  private static final Convertor<ProjectJdk, String> JDK_VERSION = new Convertor<ProjectJdk, String>() {
    public String convert(ProjectJdk jdk) {
      return jdk.getVersionString();
    }
  };

  public static void addRtJar(PathsList pathsList) {
    final String ideaRtJarPath = getIdeaRtJarPath();
    if (Boolean.getBoolean(IDEA_PREPEND_RTJAR)) {
      pathsList.addFirst(ideaRtJarPath);
    }
    else {
      pathsList.addTail(ideaRtJarPath);
    }
  }

  public static String getIdeaRtJarPath() {
    final Class aClass = JavacRunner.class;
    return PathUtil.getJarPathForClass(aClass);
  }

  public static ProjectJdk getAnyJdk(Project project) {
    return chooseJdk(project, Arrays.asList(ModuleManager.getInstance(project).getModules()));
  }

  public static ProjectJdk chooseJdk(Project project, Collection<Module> modules) {
    ProjectJdk projectJdk = ProjectRootManager.getInstance(project).getProjectJdk();
    if (projectJdk != null) {
      return projectJdk;
    }
    return chooseJdk(modules);
  }

  public static ProjectJdk chooseJdk(Collection<Module> modules) {
    List<ProjectJdk> jdks = skipNulls(map(skipNulls(modules), MODULE_JDK)); 
    if (jdks.isEmpty()) {
      return null;
    }
    Collections.sort(jdks, ComparatorUtil.compareBy(JDK_VERSION, String.CASE_INSENSITIVE_ORDER));
    return jdks.get(jdks.size() - 1);
  }
}

@@ -379,24 +379,101 @@
 avacRunner;%0A
+import com.intellij.rt.junit4.JUnit4Util;%0Aimport com.intellij.util.Function;%0A
 import com.i
@@ -552,32 +552,49 @@
 tellij.util.
-Function
+containers.ComparatorUtil
 ;%0Aimport com
@@ -582,32 +582,39 @@
 torUtil;%0Aimport 
+static 
 com.intellij.uti
@@ -626,17 +626,33 @@
 tainers.
-*
+ContainerUtil.map
 ;%0Aimport
@@ -706,72 +706,213 @@
 til.
-*;%0A%0Aimport java.util.*;%0A%0Aimport org.jetbrains.annotations.NonNls
+skipNulls;%0Aimport com.intellij.util.containers.Convertor;%0Aimport org.jetbrains.annotations.NonNls;%0A%0Aimport java.util.Arrays;%0Aimport java.util.Collection;%0Aimport java.util.Collections;%0Aimport java.util.List
 ;%0A%0A/
@@ -1759,25 +1759,24 @@
 ;%0A    %7D%0A  %7D%0A
-%0A
   public sta
@@ -1783,79 +1783,304 @@
 tic 
-String getIdeaRt
+void addJunit4
 Jar
+(
 Path
-() %7B%0A    final Class aClass = JavacRunner.class;
+sList pathsList) %7B%0A    final String path = PathUtil.getJarPathForClass(JUnit4Util.class);%0A    if (Boolean.getBoolean(IDEA_PREPEND_RTJAR)) %7B%0A      pathsList.addFirst(path);%0A    %7D%0A    else %7B%0A      pathsList.addTail(path);%0A    %7D%0A  %7D%0A%0A  public static String getIdeaRtJarPath() %7B
 %0A   
@@ -2119,10 +2119,21 @@
 ass(
-aC
+JavacRunner.c
 lass

allow to run junit4 tests regardless of junit4 module packaging