Skip to content

Commit

Permalink
[java] Test code cleanup, updating to Java 8 features
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Oct 1, 2019
1 parent 982baeb commit e349318
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 35 deletions.
10 changes: 5 additions & 5 deletions java/client/test/org/openqa/selenium/WaitingConditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ boolean compareText(String expectedValue, String actualValue) {
}

public static ExpectedCondition<String> elementTextToEqual(final By locator, final String value) {
return new ExpectedCondition<String>() {
return new ExpectedCondition<>() {

@Override
public String apply(WebDriver driver) {
Expand All @@ -100,7 +100,7 @@ public String toString() {

public static ExpectedCondition<String> elementValueToEqual(
final WebElement element, final String expectedValue) {
return new ExpectedCondition<String>() {
return new ExpectedCondition<>() {

private String lastValue = "";

Expand All @@ -121,7 +121,7 @@ public String toString() {
}

public static ExpectedCondition<String> pageSourceToContain(final String expectedText) {
return new ExpectedCondition<String>() {
return new ExpectedCondition<>() {
@Override
public String apply(WebDriver driver) {
String source = driver.getPageSource();
Expand All @@ -141,7 +141,7 @@ public String toString() {

public static ExpectedCondition<Point> elementLocationToBe(
final WebElement element, final Point expectedLocation) {
return new ExpectedCondition<Point>() {
return new ExpectedCondition<>() {
private Point currentLocation = new Point(0, 0);

@Override
Expand Down Expand Up @@ -181,7 +181,7 @@ public static ExpectedCondition<String> newWindowIsOpened(final Set<String> orig
}

public static ExpectedCondition<WebDriver> windowToBeSwitchedToWithName(final String windowName) {
return new ExpectedCondition<WebDriver>() {
return new ExpectedCondition<>() {

@Override
public WebDriver apply(WebDriver driver) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class InputAtomsTest {
@Test
public void exportsTheExpectedNames() throws IOException {
final String source = JavaScriptLoader.loadResource(RESOURCE_PATH);
ContextFactory.getGlobal().call(new ContextAction() {
ContextFactory.getGlobal().call(new ContextAction<>() {
private ScriptableObject global;

@Override
Expand Down
5 changes: 2 additions & 3 deletions java/client/test/org/openqa/selenium/build/InProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class InProject {
Expand All @@ -44,7 +43,7 @@ public static Path locate(String... paths) {
.map(path -> Paths.get(path))
.filter(path -> Files.exists(path))
.findFirst()
.map(path -> path.toAbsolutePath())
.map(Path::toAbsolutePath)
.orElseGet(() -> {
Path root = findProjectRoot();
return Stream.of(paths)
Expand All @@ -55,7 +54,7 @@ public static Path locate(String... paths) {
.filter(Objects::nonNull)
.findFirst().orElseThrow(() -> new WebDriverException(new FileNotFoundException(
String.format("Could not find any of %s in the project",
Stream.of(paths).collect(Collectors.joining(","))))));
String.join(",", paths)))));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void testFulfillRequest() {
Optional.empty(),
Optional.empty()));
});
List<RequestPattern> patterns = new ArrayList();
List<RequestPattern> patterns = new ArrayList<>();
patterns.add(new RequestPattern("*://*.*", ResourceType.EventSource, RequestStage.Request));
devTools.send(enable(Optional.of(patterns), Optional.empty()));
chromeDriver.get(appServer.whereIs("simpleTest.html"));
Expand All @@ -79,7 +79,7 @@ public void testContinueRequest() {
Assert.assertNotNull(stream);

});
List<RequestPattern> patterns = new ArrayList();
List<RequestPattern> patterns = new ArrayList<>();
patterns.add(new RequestPattern("*://*.*", ResourceType.EventSource, RequestStage.Request));
devTools.send(enable(Optional.of(patterns), Optional.empty()));
chromeDriver.get(appServer.whereIs("simpleTest.html"));
Expand All @@ -93,7 +93,7 @@ public void testFailRequest() {
Assert.assertNotNull(p);
devTools.send(failRequest(p.getRequestId(), ErrorReason.BlockedByClient));
});
List<RequestPattern> patterns = new ArrayList();
List<RequestPattern> patterns = new ArrayList<>();
patterns.add(new RequestPattern("*://*.*", ResourceType.EventSource, RequestStage.Request));
devTools.send(enable(Optional.of(patterns), Optional.empty()));
chromeDriver.get(appServer.whereIs("simpleTest.html"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void getTargetAndSendMessageToTarget() {
devTools.addListener(receivedMessageFromTarget(), this::validateMessage);
allTargets = devTools.send(getTargets());
validateTargetsInfos(allTargets);
ArrayList<TargetInfo> listTargets = new ArrayList(allTargets);
ArrayList<TargetInfo> listTargets = new ArrayList<>(allTargets);
validateTarget(listTargets.get(0));
targetInfo = listTargets.get(0);
devTools.send(activateTarget(targetInfo.getTargetId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class SleepingHandler implements HttpHandler {
@Override
public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
String duration = req.getQueryParameter("time");
long timeout = Long.valueOf(duration) * 1000;
long timeout = Long.parseLong(duration) * 1000;

reallySleep(timeout);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class SleepingServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
String duration = request.getParameter("time");
long timeout = Long.valueOf(duration) * 1000;
long timeout = Long.parseLong(duration) * 1000;

reallySleep(timeout);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,13 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
res.setStatus(200);
res.setHeader("Content-Type", "text/html");
try {
Files.walk(dest, 0).forEach(
path -> {
content.append("<p><a href=\"")
.append(String.format("%s/%s", req.getUri(), path.getFileName()))
.append("\">")
.append(path.getFileName())
.append("</a>");
}
);
Files.walk(dest, 0).forEach(path -> content
.append("<p><a href=\"")
.append(String.format("%s/%s", req.getUri(), path.getFileName()))
.append("\">")
.append(path.getFileName())
.append("</a>")
);

res.setContent(utf8String(content.toString()));
return res;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ private boolean fuzzyPositionMatching(int expectedX, int expectedY, String locat

private ExpectedCondition<Boolean> fuzzyMatchingOfCoordinates(
final WebElement element, final int x, final int y) {
return new ExpectedCondition<Boolean>() {
return new ExpectedCondition<>() {
@Override
public Boolean apply(WebDriver ignored) {
return fuzzyPositionMatching(x, y, element.getText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public void timeoutMessageIncludesToStringOfCondition() {
"Expected condition failed: waiting for toString called "
+ "(tried for 0 second(s) with 500 milliseconds interval)");

Function<Object, Boolean> condition = new Function<Object, Boolean>() {
Function<Object, Boolean> condition = new Function<>() {
@Override
public Boolean apply(Object ignored) {
return false;
Expand Down Expand Up @@ -241,7 +241,7 @@ public void callsDeprecatedHandlerForRuntimeExceptions() {
when(mockCondition.apply(mockDriver)).thenThrow(exception);

final TestException sentinelException = new TestException();
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(mockDriver, mockClock, mockSleeper) {
FluentWait<WebDriver> wait = new FluentWait<>(mockDriver, mockClock, mockSleeper) {
@Override
protected RuntimeException timeoutException(String message, Throwable lastException) {
throw sentinelException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,10 @@ public boolean shouldIgnore(Ignore ignore) {
private boolean shouldIgnore(Stream<Ignore> ignoreList) {
return ignoreList.anyMatch(driver ->
(ignored.contains(driver.value()) || driver.value() == Browser.ALL)
&& (!driver.travis() || isOnTravis())
&& (!driver.travis() || TestUtilities.isOnTravis())
&& isOpen(driver.issue()));
}

private boolean isOnTravis() {
return Boolean.valueOf(System.getenv().getOrDefault("TRAVIS", "false"));
}

private boolean isOpen(String issue) {
if ("".equals(issue)) {
return true; // unknown issue, suppose it's open
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,6 @@ public static boolean isLocal() {
}

public static boolean isOnTravis() {
return Boolean.valueOf(System.getenv("TRAVIS"));
return Boolean.parseBoolean(System.getenv("TRAVIS"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public OutOfProcessSeleniumServer start(String mode, String... extraFlags) {
cmdLine.add("--port");
cmdLine.add(String.valueOf(port));
cmdLine.addAll(Arrays.asList(extraFlags));
command = new CommandLine(cmdLine.toArray(new String[cmdLine.size()]));
command = new CommandLine(cmdLine.toArray(new String[0]));

if (Boolean.getBoolean("webdriver.development")) {
command.copyOutputTo(System.err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private static URL getServiceUrl() {
EdgeDriverService.Builder<?, ?> builder =
StreamSupport.stream(ServiceLoader.load(DriverService.Builder.class).spliterator(), false)
.filter(b -> b instanceof EdgeDriverService.Builder)
.map(b -> (EdgeDriverService.Builder) b)
.map(b -> (EdgeDriverService.Builder<?, ?>) b)
.filter(b -> b.isLegacy() == isLegacy)
.findFirst().orElseThrow(WebDriverException::new);

Expand Down

0 comments on commit e349318

Please sign in to comment.