Skip to content

Commit

Permalink
Use java 8 features in closure test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Jan 17, 2017
1 parent 79685db commit 8250fc9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;

import com.google.common.base.Function;
import com.google.common.base.Stopwatch;
import com.google.common.base.Supplier;
import org.junit.runners.model.Statement;

import java.net.URL;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.logging.Logger;

public class ClosureTestStatement extends Statement {
Expand All @@ -43,8 +43,11 @@ public class ClosureTestStatement extends Statement {
private final Function<String, URL> filePathToUrlFn;
private final long timeoutSeconds;

public ClosureTestStatement(Supplier<WebDriver> driverSupplier,
String testPath, Function<String, URL> filePathToUrlFn, long timeoutSeconds) {
public ClosureTestStatement(
Supplier<WebDriver> driverSupplier,
String testPath,
Function<String, URL> filePathToUrlFn,
long timeoutSeconds) {
this.driverSupplier = driverSupplier;
this.testPath = testPath;
this.filePathToUrlFn = filePathToUrlFn;
Expand Down Expand Up @@ -103,14 +106,14 @@ private String getString(JavascriptExecutor executor, Query query) {
return (String) executor.executeScript(query.script);
}

private static enum Query {
private enum Query {
IS_FINISHED("return !!tr && tr.isFinished();"),
IS_SUCCESS("return !!tr && tr.isSuccess();"),
GET_REPORT("return tr.getReport(true);");

private final String script;

private Query(String script) {
Query(String script) {
this.script = "var tr = window.top.G_testRunner;" + script;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;

import org.junit.runner.Description;
import org.junit.runner.Runner;
Expand All @@ -44,6 +41,8 @@
import java.net.URL;
import java.nio.file.Path;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

/**
* JUnit4 test runner for Closure-based JavaScript tests.
Expand All @@ -68,33 +67,28 @@ public JavaScriptTestSuite(Class<?> testClass) throws InitializationError, IOExc
private static ImmutableList<Runner> createChildren(
final Supplier<WebDriver> driverSupplier, final long timeout) throws IOException {
final Path baseDir = InProject.locate("Rakefile").getParent();
final Function<String, URL> pathToUrlFn = new Function<String, URL>() {
@Override
public URL apply(String s) {
AppServer appServer = GlobalTestEnvironment.get().getAppServer();
try {
return new URL(appServer.whereIs("/" + s));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
final Function<String, URL> pathToUrlFn = s -> {
AppServer appServer = GlobalTestEnvironment.get().getAppServer();
try {
return new URL(appServer.whereIs("/" + s));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
};


List<Path> tests = TestFileLocator.findTestFiles();
Iterable<Runner> runners = Iterables.transform(tests, new Function<Path, Runner>() {
@Override
public Runner apply(Path file) {
final String path = TestFileLocator.getTestFilePath(baseDir, file);
Description description = Description.createSuiteDescription(
path.replaceAll(".html$", ""));

Statement testStatement = new ClosureTestStatement(
driverSupplier, path, pathToUrlFn, timeout);
return new StatementRunner(testStatement, description);
}
});
return ImmutableList.copyOf(runners);
return tests.stream()
.map(file -> {
final String path = TestFileLocator.getTestFilePath(baseDir, file);
Description description = Description.createSuiteDescription(
path.replaceAll(".html$", ""));

Statement testStatement = new ClosureTestStatement(
driverSupplier, path, pathToUrlFn, timeout);
return new StatementRunner(testStatement, description);
})
.collect(ImmutableList.toImmutableList());
}

@Override
Expand Down

0 comments on commit 8250fc9

Please sign in to comment.