add junit4.jar together with hamcrest (IDEA-119626)

(cherry picked from commit 8adb1aadfc02ee5450f6f40d6e62892bef5a97e0)
This commit is contained in:
Anna Kozlova
2014-03-12 10:15:26 +01:00
parent 10760d9bbf
commit 912e13d8af
5 changed files with 48 additions and 11 deletions
@@ -43,6 +43,10 @@ import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.PsiShortNamesCache;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.util.Function;
import com.intellij.util.PathUtil;
import com.intellij.util.containers.ContainerUtil;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -116,8 +120,35 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
@Override
public void invoke(@NotNull Project project, @Nullable Editor editor, PsiFile file) {
String jarPath = isJunit4 ? JavaSdkUtil.getJunit4JarPath() : JavaSdkUtil.getJunit3JarPath();
addBundledJarToRoots(project, editor, currentModule, reference, className, jarPath);
if (isJunit4) {
final VirtualFile location = PsiUtilCore.getVirtualFile(reference != null ? reference.getElement() : null);
boolean inTests = location != null && ModuleRootManager.getInstance(currentModule).getFileIndex().isInTestSourceContent(location);
try {
final String[] junit4Paths = {JavaSdkUtil.getJunit4JarPath(),
PathUtil.getJarPathForClass(Class.forName("org.hamcrest.Matcher")),
PathUtil.getJarPathForClass(Class.forName("org.hamcrest.Matchers"))};
ModuleRootModificationUtil.addModuleLibrary(currentModule,
"JUnit4",
ContainerUtil.map(junit4Paths,
new Function<String, String>() {
@Override
public String fun(String libPath) {
return convertToLibraryRoot(libPath).getUrl();
}
}),
Collections.<String>emptyList(), inTests ? DependencyScope.TEST : DependencyScope.COMPILE);
final GlobalSearchScope scope = GlobalSearchScope.moduleWithLibrariesScope(currentModule);
final PsiClass aClass = JavaPsiFacade.getInstance(project).findClass(className, scope);
if (aClass != null && editor != null && reference != null) {
new AddImportAction(project, reference, editor, aClass).execute();
}
}
catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
} else {
addBundledJarToRoots(project, editor, currentModule, reference, className, JavaSdkUtil.getJunit3JarPath());
}
}
};
registrar.register(fix);
@@ -297,11 +328,7 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
}
public static void addJarToRoots(String libPath, final Module module, @Nullable PsiElement location) {
final File libraryRoot = new File(libPath);
LocalFileSystem.getInstance().refreshAndFindFileByIoFile(libraryRoot);
String url = VfsUtil.getUrlForLibraryRoot(libraryRoot);
VirtualFile libVirtFile = VirtualFileManager.getInstance().findFileByUrl(url);
assert libVirtFile != null : libPath;
VirtualFile libVirtFile = convertToLibraryRoot(libPath);
boolean inTests = false;
if (location != null) {
@@ -314,6 +341,16 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
Collections.<String>emptyList(), inTests ? DependencyScope.TEST : DependencyScope.COMPILE);
}
@NotNull
private static VirtualFile convertToLibraryRoot(String libPath) {
final File libraryRoot = new File(libPath);
LocalFileSystem.getInstance().refreshAndFindFileByIoFile(libraryRoot);
String url = VfsUtil.getUrlForLibraryRoot(libraryRoot);
VirtualFile libVirtFile = VirtualFileManager.getInstance().findFileByUrl(url);
assert libVirtFile != null : libPath;
return libVirtFile;
}
public static boolean ensureAnnotationsJarInPath(final Module module) {
if (isAnnotationsJarInPath(module)) return true;
if (module == null) return false;
@@ -1,4 +1,4 @@
// "Add 'junit.jar' to classpath" "true"
// "Add junit to classpath" "true"
package x;
public class DoTest extends TestCase<caret>{
@@ -1,4 +1,4 @@
// "Add 'junit.jar' to classpath" "true"
// "Add junit to classpath" "true"
package x;
public class DoTest4 {
@@ -1,4 +1,4 @@
// "Add 'junit.jar' to classpath" "true"
// "Add junit to classpath" "true"
package x;
public class DoTest4junit {
@@ -231,7 +231,7 @@ orderEntry.fix.add.library.to.classpath=Add library ''{0}'' to classpath
orderEntry.fix.family.add.library.to.classpath=Add library to classpath
orderEntry.fix.circular.dependency.warning=Adding dependency on module ''{0}'' will introduce circular dependency between modules ''{1}'' and ''{2}''.\nAdd dependency anyway?
orderEntry.fix.title.circular.dependency.warning=Circular Dependency Warning
orderEntry.fix.add.junit.jar.to.classpath=Add 'junit.jar' to classpath
orderEntry.fix.add.junit.jar.to.classpath=Add junit to classpath
orderEntry.fix.add.annotations.jar.to.classpath=Add 'annotations.jar' to classpath
static.import.method.text=Static Import Method
static.import.method.choose.method.to.import=Choose Method to Import