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.util.io.FileUtil;
import junit.framework.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.compiler.ant.AntRunner;
import org.jetbrains.jet.compiler.download.SDKDownloader;
@@ -53,6 +54,8 @@ public class CodegenTestsOnAndroidRunner {
TestSuite suite = new TestSuite("MySuite");
String resultOutput = runTests();
if (resultOutput == null) return suite;
// Test name -> stackTrace
final Map<String, String> resultMap = parseOutputForFailedTests(resultOutput);
final Statistics statistics;
@@ -63,7 +66,7 @@ public class CodegenTestsOnAndroidRunner {
}
else {
statistics = parseOutputForTestsNumberIfThereIsFailedTests(resultOutput);
for (final Map.Entry<String, String> entry : resultMap.entrySet()) {
suite.addTest(new TestCase("run") {
@@ -79,7 +82,7 @@ public class CodegenTestsOnAndroidRunner {
});
}
}
Assert.assertNotNull("Cannot parse number of failed tests from final line", statistics);
Assert.assertEquals("Number of stackTraces != failed tests on the final line", resultMap.size(),
statistics.failed + statistics.errors);
@@ -119,7 +122,7 @@ public class CodegenTestsOnAndroidRunner {
[exec] ...............
[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>();
StringBuilder builder = new StringBuilder();
String failedTestNamePrefix = " Error in ";
@@ -165,6 +168,7 @@ public class CodegenTestsOnAndroidRunner {
}
@Nullable
public String runTests() {
ApplicationManager.setApplication(null, new Disposable() {
@Override
@@ -198,19 +202,17 @@ public class CodegenTestsOnAndroidRunner {
antRunner.installApplicationOnEmulator();
return antRunner.runTestsOnEmulator();
}
catch (RuntimeException e) {
e.printStackTrace();
}
finally {
try {
emulator.stopEmulator();
}
catch (Throwable t) {
System.err.println("Exception during stopping emulator:");
t.printStackTrace();
}
emulator.stopEmulator();
}
}
finally {
emulator.finishEmulatorProcesses();
}
return null;
}
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 org.jetbrains.jet.compiler.OutputUtils;
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.result.RunResult;
@@ -68,7 +69,9 @@ public class AntRunner {
return;
}
else {
System.out.println(resultOutput);
if (result.getStatus()) {
System.out.println(resultOutput);
}
}
OutputUtils.checkResult(result);
}
@@ -83,14 +86,9 @@ public class AntRunner {
private static boolean isInstallSuccessful(String output) {
if (output.contains("Is the system running?")) {
try {
System.out.println("Device not ready. Waiting for 20 sec.");
Thread.sleep(20000);
return false;
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("Device not ready. Waiting for 20 sec.");
ThreadUtils.sleep(20);
return false;
}
return true;
}
@@ -21,6 +21,7 @@ import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.jet.compiler.OutputUtils;
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.result.RunResult;
@@ -32,8 +33,7 @@ import java.util.regex.Pattern;
public class Emulator {
private final static Pattern EMULATOR_PATTERN = Pattern.compile("emulator-([0-9])*");
private final PathManager pathManager;
public Emulator(PathManager pathManager) {
@@ -119,50 +119,50 @@ public class Emulator {
public void stopEmulator() {
System.out.println("Stopping emulator...");
OutputUtils.checkResult(RunUtils.execute(getStopCommand()));
if (SystemInfo.isUnix) {
finishProcess("emulator-arm");
}
finishProcess("emulator-arm");
}
//Only for Unix
private void stopDdmsProcess() {
GeneralCommandLine listOfEmulatorProcess = new GeneralCommandLine();
listOfEmulatorProcess.setExePath("sh");
listOfEmulatorProcess.addParameter("-c");
listOfEmulatorProcess.addParameter("ps aux | grep emulator");
RunResult runResult = RunUtils.execute(listOfEmulatorProcess);
OutputUtils.checkResult(runResult);
String pidFromPsCommand = OutputUtils.getPidFromPsCommand(runResult.getOutput());
if (pidFromPsCommand != null) {
GeneralCommandLine killCommand = new GeneralCommandLine();
killCommand.setExePath("kill");
killCommand.addParameter(pidFromPsCommand);
OutputUtils.checkResult(RunUtils.execute(killCommand));
if (SystemInfo.isUnix) {
GeneralCommandLine listOfEmulatorProcess = new GeneralCommandLine();
listOfEmulatorProcess.setExePath("sh");
listOfEmulatorProcess.addParameter("-c");
listOfEmulatorProcess.addParameter("ps aux | grep emulator");
RunResult runResult = RunUtils.execute(listOfEmulatorProcess);
OutputUtils.checkResult(runResult);
String pidFromPsCommand = OutputUtils.getPidFromPsCommand(runResult.getOutput());
if (pidFromPsCommand != null) {
GeneralCommandLine killCommand = new GeneralCommandLine();
killCommand.setExePath("kill");
killCommand.addParameter(pidFromPsCommand);
RunUtils.execute(killCommand);
}
}
}
public void finishEmulatorProcesses() {
System.out.println("Stopping adb...");
OutputUtils.checkResult(RunUtils.execute(getStopCommandForAdb()));
if (SystemInfo.isUnix) {
finishProcess("adb");
stopDdmsProcess();
}
finishProcess("adb");
stopDdmsProcess();
}
//Only for Unix
private void finishProcess(String processName) {
GeneralCommandLine pidOfProcess = new GeneralCommandLine();
pidOfProcess.setExePath("pidof");
pidOfProcess.addParameter(processName);
RunResult runResult = RunUtils.execute(pidOfProcess);
String processIdsStr = runResult.getOutput().substring(("pidof " + processName).length());
List<String> processIds = StringUtil.getWordsIn(processIdsStr);
for (String pid : processIds) {
GeneralCommandLine killCommand = new GeneralCommandLine();
killCommand.setExePath("kill");
killCommand.addParameter(pid);
OutputUtils.checkResult(RunUtils.execute(killCommand));
if (SystemInfo.isUnix) {
GeneralCommandLine pidOfProcess = new GeneralCommandLine();
pidOfProcess.setExePath("pidof");
pidOfProcess.addParameter(processName);
RunResult runResult = RunUtils.execute(pidOfProcess);
String processIdsStr = runResult.getOutput().substring(("pidof " + processName).length());
List<String> processIds = StringUtil.getWordsIn(processIdsStr);
for (String pid : processIds) {
GeneralCommandLine killCommand = new GeneralCommandLine();
killCommand.setExePath("kill");
killCommand.addParameter(pid);
RunUtils.execute(killCommand);
}
}
}
@@ -203,5 +203,5 @@ public class Emulator {
}
OutputUtils.checkResult(RunUtils.execute(commandLineForListOfDevices));
}
}
@@ -26,8 +26,10 @@ import com.intellij.execution.process.ProcessOutputTypes;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.compiler.OutputUtils;
import org.jetbrains.jet.compiler.ThreadUtils;
import org.jetbrains.jet.compiler.run.result.RunResult;
import java.io.Closeable;
@@ -66,18 +68,14 @@ public class RunUtils {
t.start();
if (waitForEnd) {
try {
t.wait(600000);
return resultRef.get();
}
catch (InterruptedException e) {
new RunResult(false, getStackTrace(e));
}
ThreadUtils.wait(t, 600);
return resultRef.get();
}
return new RunResult(true, "OK");
}
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 stdErr = new StringBuilder();
@@ -96,24 +94,24 @@ public class RunUtils {
return new RunResult(false, getStackTrace(e));
}
final ProcessAdapter listener = new ProcessAdapter() {
handler.addProcessListener(new ProcessAdapter() {
@Override
public void onTextAvailable(final ProcessEvent event, final Key outputType) {
public void onTextAvailable(ProcessEvent event, Key outputType) {
String str = event.getText();
if (outputType == ProcessOutputTypes.STDOUT || outputType == ProcessOutputTypes.SYSTEM) {
stdOut.append(str);
if (!commandLine.getCommandLineString().contains("install")) {
System.out.print(str);
}
appendToContent(stdOut, str);
}
else if (outputType == ProcessOutputTypes.STDERR) {
stdErr.append(str);
System.err.print(str);
appendToContent(stdErr, str);
}
}
};
handler.addProcessListener(listener);
private synchronized void appendToContent(StringBuilder content, String line) {
content.append(StringUtil.trimTrailing(line));
content.append("\n");
}
});
handler.startNotify();
try {
@@ -127,8 +125,6 @@ public class RunUtils {
return new RunResult(false, "Timeout exception: execution was terminated after 5 min.");
}
handler.removeProcessListener(listener);
int exitCode = handler.getProcess().exitValue();
if (exitCode != 0) {
@@ -139,6 +135,9 @@ public class RunUtils {
if (OutputUtils.isBuildFailed(output)) {
return new RunResult(false, output);
}
if (!commandLine.getCommandLineString().contains("install")) {
System.out.print(output);
}
return new RunResult(true, output);
}
}
@@ -171,5 +170,5 @@ public class RunUtils {
}
return writer.toString();
}
}