From 36b5404139745f690bf707ce9fab0656fff79ef8 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Wed, 10 Sep 2014 14:36:03 +0400 Subject: [PATCH 1/2] junit: write classes to temp file in one place --- .../intellij/execution/junit/TestObject.java | 67 +++++++++---------- .../execution/junit/JUnitForkedStarter.java | 33 ++++----- .../rt/execution/junit/JUnitStarter.java | 15 +++++ 3 files changed, 58 insertions(+), 57 deletions(-) diff --git a/plugins/junit/src/com/intellij/execution/junit/TestObject.java b/plugins/junit/src/com/intellij/execution/junit/TestObject.java index 3a9855add0e1..6ef74a16947b 100644 --- a/plugins/junit/src/com/intellij/execution/junit/TestObject.java +++ b/plugins/junit/src/com/intellij/execution/junit/TestObject.java @@ -480,49 +480,44 @@ public abstract class TestObject implements JavaCommandLine { return StringUtil.compare(o1.getName(), o2.getName(), true); } }) : null; - final PrintWriter writer = new PrintWriter(myTempFile, CharsetToolkit.UTF8); - try { - writer.println(packageName); - final JUnitConfiguration.Data data = myConfiguration.getPersistentData(); - final String category = data.TEST_OBJECT == JUnitConfiguration.TEST_CATEGORY ? data.getCategory() : ""; - writer.println(category); - final List testNames = new ArrayList(); - for (final T element : elements) { - final String name = nameFunction.fun(element); - if (name == null) { - LOG.error("invalid element " + element); - return; - } - if (perModule != null && element instanceof PsiElement) { - final Module module = ModuleUtilCore.findModuleForPsiElement((PsiElement)element); - if (module != null) { - List list = perModule.get(module); - if (list == null) { - list = new ArrayList(); - perModule.put(module, list); - } - list.add(name); + final List testNames = new ArrayList(); + + for (final T element : elements) { + final String name = nameFunction.fun(element); + if (name == null) { + LOG.error("invalid element " + element); + return; + } + + if (perModule != null && element instanceof PsiElement) { + final Module module = ModuleUtilCore.findModuleForPsiElement((PsiElement)element); + if (module != null) { + List list = perModule.get(module); + if (list == null) { + list = new ArrayList(); + perModule.put(module, list); } - } else { - testNames.add(name); + list.add(name); } } - if (perModule != null) { - for (List perModuleClasses : perModule.values()) { - Collections.sort(perModuleClasses); - testNames.addAll(perModuleClasses); - } - } else { - Collections.sort(testNames); //sort tests in FQN order - } - for (String testName : testNames) { - writer.println(testName); + else { + testNames.add(name); } } - finally { - writer.close(); + if (perModule != null) { + for (List perModuleClasses : perModule.values()) { + Collections.sort(perModuleClasses); + testNames.addAll(perModuleClasses); + } } + else { + Collections.sort(testNames); //sort tests in FQN order + } + + final JUnitConfiguration.Data data = myConfiguration.getPersistentData(); + final String category = data.TEST_OBJECT == JUnitConfiguration.TEST_CATEGORY ? data.getCategory() : ""; + JUnitStarter.printClassesList(testNames, packageName, category, myTempFile); if (perModule != null && perModule.size() > 1) { final PrintWriter wWriter = new PrintWriter(myWorkingDirsFile, CharsetToolkit.UTF8); diff --git a/plugins/junit_rt/src/com/intellij/rt/execution/junit/JUnitForkedStarter.java b/plugins/junit_rt/src/com/intellij/rt/execution/junit/JUnitForkedStarter.java index 29735a9f559b..ed01b10fb4f5 100644 --- a/plugins/junit_rt/src/com/intellij/rt/execution/junit/JUnitForkedStarter.java +++ b/plugins/junit_rt/src/com/intellij/rt/execution/junit/JUnitForkedStarter.java @@ -106,32 +106,23 @@ public class JUnitForkedStarter { final String packageName = perDirReader.readLine(); String workingDir; while ((workingDir = perDirReader.readLine()) != null) { + final String classpath = perDirReader.readLine(); try { - File tempFile = File.createTempFile("idea_junit", ".tmp"); - tempFile.deleteOnExit(); - - final FileOutputStream writer = new FileOutputStream(tempFile); - - final String classpath = perDirReader.readLine(); List classNames = new ArrayList(); - try { - final int classNamesSize = Integer.parseInt(perDirReader.readLine()); - writer.write((packageName + ", working directory: \'" + workingDir + "\'\n").getBytes("UTF-8")); //instead of package name - writer.write("\n".getBytes("UTF-8")); //category - for (int i = 0; i < classNamesSize; i++) { - String className = perDirReader.readLine(); - if (className == null) { - System.err.println("Class name is expected. Working dir: " + workingDir); - return -1; - } - classNames.add(className); - writer.write((className + "\n").getBytes("UTF-8")); + final int classNamesSize = Integer.parseInt(perDirReader.readLine()); + for (int i = 0; i < classNamesSize; i++) { + String className = perDirReader.readLine(); + if (className == null) { + System.err.println("Class name is expected. Working dir: " + workingDir); + return -1; } + classNames.add(className); } - finally { - writer.close(); - } + + File tempFile = File.createTempFile("idea_junit", ".tmp"); + tempFile.deleteOnExit(); + JUnitStarter.printClassesList(classNames, packageName + ", working directory: \'" + workingDir + "\'", "", tempFile); final Object rootDescriptor = findByClassName(testRunner, (String)classNames.get(0), description); final int childResult; diff --git a/plugins/junit_rt/src/com/intellij/rt/execution/junit/JUnitStarter.java b/plugins/junit_rt/src/com/intellij/rt/execution/junit/JUnitStarter.java index cb398d9a0262..5c0edbb9ded8 100644 --- a/plugins/junit_rt/src/com/intellij/rt/execution/junit/JUnitStarter.java +++ b/plugins/junit_rt/src/com/intellij/rt/execution/junit/JUnitStarter.java @@ -226,4 +226,19 @@ public class JUnitStarter { : Class.forName("com.intellij.junit3.JUnit3IdeaTestRunner"); } + + public static void printClassesList(List classNames, String packageName, String category, File tempFile) throws IOException { + final PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tempFile), "UTF-8")); + + try { + writer.println(packageName); //package name + writer.println(category); //category + for (int i = 0; i < classNames.size(); i++) { + writer.println(classNames.get(i)); + } + } + finally { + writer.close(); + } + } } From b945dc080101961c411e9b2ec223375b4ee267bd Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Wed, 10 Sep 2014 14:42:34 +0400 Subject: [PATCH 2/2] junit: prepare temp file for fork by module only --- .../intellij/rt/execution/junit/JUnitForkedStarter.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/plugins/junit_rt/src/com/intellij/rt/execution/junit/JUnitForkedStarter.java b/plugins/junit_rt/src/com/intellij/rt/execution/junit/JUnitForkedStarter.java index ed01b10fb4f5..e79ab8e677b3 100644 --- a/plugins/junit_rt/src/com/intellij/rt/execution/junit/JUnitForkedStarter.java +++ b/plugins/junit_rt/src/com/intellij/rt/execution/junit/JUnitForkedStarter.java @@ -120,14 +120,13 @@ public class JUnitForkedStarter { classNames.add(className); } - File tempFile = File.createTempFile("idea_junit", ".tmp"); - tempFile.deleteOnExit(); - JUnitStarter.printClassesList(classNames, packageName + ", working directory: \'" + workingDir + "\'", "", tempFile); - final Object rootDescriptor = findByClassName(testRunner, (String)classNames.get(0), description); final int childResult; final File dir = new File(workingDir); if (forkMode.equals("none")) { + File tempFile = File.createTempFile("idea_junit", ".tmp"); + tempFile.deleteOnExit(); + JUnitStarter.printClassesList(classNames, packageName + ", working directory: \'" + workingDir + "\'", "", tempFile); childResult = runChild(isJUnit4, listeners, out, err, parameters, "@" + tempFile.getAbsolutePath(), dir, String.valueOf(testRunner.getRegistry().getKnownObject(rootDescriptor) - 1), classpath);