Refactoring in android tests

This commit is contained in:
Natalia.Ukhorskaya
2013-01-22 16:05:27 +04:00
parent 3b41627b9f
commit db81672d23
5 changed files with 112 additions and 72 deletions
@@ -20,6 +20,7 @@ import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.io.FileUtil;
import junit.framework.*; import junit.framework.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.compiler.ant.AntRunner; import org.jetbrains.jet.compiler.ant.AntRunner;
import org.jetbrains.jet.compiler.download.SDKDownloader; import org.jetbrains.jet.compiler.download.SDKDownloader;
@@ -53,6 +54,8 @@ public class CodegenTestsOnAndroidRunner {
TestSuite suite = new TestSuite("MySuite"); TestSuite suite = new TestSuite("MySuite");
String resultOutput = runTests(); String resultOutput = runTests();
if (resultOutput == null) return suite;
// Test name -> stackTrace // Test name -> stackTrace
final Map<String, String> resultMap = parseOutputForFailedTests(resultOutput); final Map<String, String> resultMap = parseOutputForFailedTests(resultOutput);
final Statistics statistics; final Statistics statistics;
@@ -119,7 +122,7 @@ public class CodegenTestsOnAndroidRunner {
[exec] ............... [exec] ...............
[exec] Error in testKt529: [exec] Error in testKt529:
*/ */
private Map<String, String> parseOutputForFailedTests(String output) { private Map<String, String> parseOutputForFailedTests(@NotNull String output) {
Map<String, String> result = new HashMap<String, String>(); Map<String, String> result = new HashMap<String, String>();
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
String failedTestNamePrefix = " Error in "; String failedTestNamePrefix = " Error in ";
@@ -165,6 +168,7 @@ public class CodegenTestsOnAndroidRunner {
} }
@Nullable
public String runTests() { public String runTests() {
ApplicationManager.setApplication(null, new Disposable() { ApplicationManager.setApplication(null, new Disposable() {
@Override @Override
@@ -198,19 +202,17 @@ public class CodegenTestsOnAndroidRunner {
antRunner.installApplicationOnEmulator(); antRunner.installApplicationOnEmulator();
return antRunner.runTestsOnEmulator(); return antRunner.runTestsOnEmulator();
} }
catch (RuntimeException e) {
e.printStackTrace();
}
finally { finally {
try { emulator.stopEmulator();
emulator.stopEmulator();
}
catch (Throwable t) {
System.err.println("Exception during stopping emulator:");
t.printStackTrace();
}
} }
} }
finally { finally {
emulator.finishEmulatorProcesses(); emulator.finishEmulatorProcesses();
} }
return null;
} }
private static class Statistics { private static class Statistics {
@@ -0,0 +1,41 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.compiler;
import java.lang.InterruptedException;
import java.lang.RuntimeException;
import java.lang.Thread;
public class ThreadUtils {
public static void wait(Thread thread, int seconds) {
try {
thread.wait(seconds * 1000);
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public static void sleep(int seconds) {
try {
Thread.sleep(seconds * 1000);
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
@@ -20,6 +20,7 @@ import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.util.SystemInfo;
import org.jetbrains.jet.compiler.OutputUtils; import org.jetbrains.jet.compiler.OutputUtils;
import org.jetbrains.jet.compiler.PathManager; import org.jetbrains.jet.compiler.PathManager;
import org.jetbrains.jet.compiler.ThreadUtils;
import org.jetbrains.jet.compiler.run.RunUtils; import org.jetbrains.jet.compiler.run.RunUtils;
import org.jetbrains.jet.compiler.run.result.RunResult; import org.jetbrains.jet.compiler.run.result.RunResult;
@@ -68,7 +69,9 @@ public class AntRunner {
return; return;
} }
else { else {
System.out.println(resultOutput); if (result.getStatus()) {
System.out.println(resultOutput);
}
} }
OutputUtils.checkResult(result); OutputUtils.checkResult(result);
} }
@@ -83,14 +86,9 @@ public class AntRunner {
private static boolean isInstallSuccessful(String output) { private static boolean isInstallSuccessful(String output) {
if (output.contains("Is the system running?")) { if (output.contains("Is the system running?")) {
try { System.out.println("Device not ready. Waiting for 20 sec.");
System.out.println("Device not ready. Waiting for 20 sec."); ThreadUtils.sleep(20);
Thread.sleep(20000); return false;
return false;
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}
} }
return true; return true;
} }
@@ -21,6 +21,7 @@ import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.jet.compiler.OutputUtils; import org.jetbrains.jet.compiler.OutputUtils;
import org.jetbrains.jet.compiler.PathManager; import org.jetbrains.jet.compiler.PathManager;
import org.jetbrains.jet.compiler.ThreadUtils;
import org.jetbrains.jet.compiler.run.RunUtils; import org.jetbrains.jet.compiler.run.RunUtils;
import org.jetbrains.jet.compiler.run.result.RunResult; import org.jetbrains.jet.compiler.run.result.RunResult;
@@ -33,7 +34,6 @@ public class Emulator {
private final static Pattern EMULATOR_PATTERN = Pattern.compile("emulator-([0-9])*"); private final static Pattern EMULATOR_PATTERN = Pattern.compile("emulator-([0-9])*");
private final PathManager pathManager; private final PathManager pathManager;
public Emulator(PathManager pathManager) { public Emulator(PathManager pathManager) {
@@ -119,50 +119,50 @@ public class Emulator {
public void stopEmulator() { public void stopEmulator() {
System.out.println("Stopping emulator..."); System.out.println("Stopping emulator...");
OutputUtils.checkResult(RunUtils.execute(getStopCommand())); OutputUtils.checkResult(RunUtils.execute(getStopCommand()));
if (SystemInfo.isUnix) { finishProcess("emulator-arm");
finishProcess("emulator-arm");
}
} }
//Only for Unix //Only for Unix
private void stopDdmsProcess() { private void stopDdmsProcess() {
GeneralCommandLine listOfEmulatorProcess = new GeneralCommandLine(); if (SystemInfo.isUnix) {
listOfEmulatorProcess.setExePath("sh"); GeneralCommandLine listOfEmulatorProcess = new GeneralCommandLine();
listOfEmulatorProcess.addParameter("-c"); listOfEmulatorProcess.setExePath("sh");
listOfEmulatorProcess.addParameter("ps aux | grep emulator"); listOfEmulatorProcess.addParameter("-c");
RunResult runResult = RunUtils.execute(listOfEmulatorProcess); listOfEmulatorProcess.addParameter("ps aux | grep emulator");
OutputUtils.checkResult(runResult); RunResult runResult = RunUtils.execute(listOfEmulatorProcess);
String pidFromPsCommand = OutputUtils.getPidFromPsCommand(runResult.getOutput()); OutputUtils.checkResult(runResult);
if (pidFromPsCommand != null) { String pidFromPsCommand = OutputUtils.getPidFromPsCommand(runResult.getOutput());
GeneralCommandLine killCommand = new GeneralCommandLine(); if (pidFromPsCommand != null) {
killCommand.setExePath("kill"); GeneralCommandLine killCommand = new GeneralCommandLine();
killCommand.addParameter(pidFromPsCommand); killCommand.setExePath("kill");
OutputUtils.checkResult(RunUtils.execute(killCommand)); killCommand.addParameter(pidFromPsCommand);
RunUtils.execute(killCommand);
}
} }
} }
public void finishEmulatorProcesses() { public void finishEmulatorProcesses() {
System.out.println("Stopping adb..."); System.out.println("Stopping adb...");
OutputUtils.checkResult(RunUtils.execute(getStopCommandForAdb())); OutputUtils.checkResult(RunUtils.execute(getStopCommandForAdb()));
if (SystemInfo.isUnix) { finishProcess("adb");
finishProcess("adb"); stopDdmsProcess();
stopDdmsProcess();
}
} }
//Only for Unix //Only for Unix
private void finishProcess(String processName) { private void finishProcess(String processName) {
GeneralCommandLine pidOfProcess = new GeneralCommandLine(); if (SystemInfo.isUnix) {
pidOfProcess.setExePath("pidof"); GeneralCommandLine pidOfProcess = new GeneralCommandLine();
pidOfProcess.addParameter(processName); pidOfProcess.setExePath("pidof");
RunResult runResult = RunUtils.execute(pidOfProcess); pidOfProcess.addParameter(processName);
String processIdsStr = runResult.getOutput().substring(("pidof " + processName).length()); RunResult runResult = RunUtils.execute(pidOfProcess);
List<String> processIds = StringUtil.getWordsIn(processIdsStr); String processIdsStr = runResult.getOutput().substring(("pidof " + processName).length());
for (String pid : processIds) { List<String> processIds = StringUtil.getWordsIn(processIdsStr);
GeneralCommandLine killCommand = new GeneralCommandLine(); for (String pid : processIds) {
killCommand.setExePath("kill"); GeneralCommandLine killCommand = new GeneralCommandLine();
killCommand.addParameter(pid); killCommand.setExePath("kill");
OutputUtils.checkResult(RunUtils.execute(killCommand)); killCommand.addParameter(pid);
RunUtils.execute(killCommand);
}
} }
} }
@@ -26,8 +26,10 @@ import com.intellij.execution.process.ProcessOutputTypes;
import com.intellij.openapi.progress.ProcessCanceledException; import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.util.Key; import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.compiler.OutputUtils; import org.jetbrains.jet.compiler.OutputUtils;
import org.jetbrains.jet.compiler.ThreadUtils;
import org.jetbrains.jet.compiler.run.result.RunResult; import org.jetbrains.jet.compiler.run.result.RunResult;
import java.io.Closeable; import java.io.Closeable;
@@ -66,18 +68,14 @@ public class RunUtils {
t.start(); t.start();
if (waitForEnd) { if (waitForEnd) {
try { ThreadUtils.wait(t, 600);
t.wait(600000); return resultRef.get();
return resultRef.get();
}
catch (InterruptedException e) {
new RunResult(false, getStackTrace(e));
}
} }
return new RunResult(true, "OK"); return new RunResult(true, "OK");
} }
private static RunResult run(final GeneralCommandLine commandLine, @Nullable final String input) { private static RunResult run(final GeneralCommandLine commandLine, @Nullable final String input) {
System.out.println("RUN COMMAND: " + commandLine.getCommandLineString());
final StringBuilder stdOut = new StringBuilder(); final StringBuilder stdOut = new StringBuilder();
final StringBuilder stdErr = new StringBuilder(); final StringBuilder stdErr = new StringBuilder();
@@ -96,24 +94,24 @@ public class RunUtils {
return new RunResult(false, getStackTrace(e)); return new RunResult(false, getStackTrace(e));
} }
final ProcessAdapter listener = new ProcessAdapter() { handler.addProcessListener(new ProcessAdapter() {
@Override @Override
public void onTextAvailable(final ProcessEvent event, final Key outputType) { public void onTextAvailable(ProcessEvent event, Key outputType) {
String str = event.getText(); String str = event.getText();
if (outputType == ProcessOutputTypes.STDOUT || outputType == ProcessOutputTypes.SYSTEM) { if (outputType == ProcessOutputTypes.STDOUT || outputType == ProcessOutputTypes.SYSTEM) {
stdOut.append(str); appendToContent(stdOut, str);
if (!commandLine.getCommandLineString().contains("install")) {
System.out.print(str);
}
} }
else if (outputType == ProcessOutputTypes.STDERR) { else if (outputType == ProcessOutputTypes.STDERR) {
stdErr.append(str); appendToContent(stdErr, str);
System.err.print(str);
} }
} }
};
handler.addProcessListener(listener); private synchronized void appendToContent(StringBuilder content, String line) {
content.append(StringUtil.trimTrailing(line));
content.append("\n");
}
});
handler.startNotify(); handler.startNotify();
try { try {
@@ -127,8 +125,6 @@ public class RunUtils {
return new RunResult(false, "Timeout exception: execution was terminated after 5 min."); return new RunResult(false, "Timeout exception: execution was terminated after 5 min.");
} }
handler.removeProcessListener(listener);
int exitCode = handler.getProcess().exitValue(); int exitCode = handler.getProcess().exitValue();
if (exitCode != 0) { if (exitCode != 0) {
@@ -139,6 +135,9 @@ public class RunUtils {
if (OutputUtils.isBuildFailed(output)) { if (OutputUtils.isBuildFailed(output)) {
return new RunResult(false, output); return new RunResult(false, output);
} }
if (!commandLine.getCommandLineString().contains("install")) {
System.out.print(output);
}
return new RunResult(true, output); return new RunResult(true, output);
} }
} }