Skip to content

Commit

Permalink
[java] Refactoring Firefox executable wrapper and adding tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Nov 2, 2018
1 parent 1c3a037 commit 7d4cb1a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 16 deletions.
25 changes: 9 additions & 16 deletions java/client/src/org/openqa/selenium/firefox/Executable.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;

/**
* Wrapper around Firefox executable.
Expand Down Expand Up @@ -74,9 +73,9 @@ public FirefoxBinary.Channel getChannel() {
}

private void loadApplicationIni() {
Optional<Path> applicationIni = getResource("application.ini");
if (applicationIni.isPresent()) {
try (BufferedReader reader = Files.newBufferedReader(applicationIni.get())) {
Path applicationIni = getResource("application.ini");
if (Files.exists(applicationIni)) {
try (BufferedReader reader = Files.newBufferedReader(applicationIni)) {
reader.lines().map(String::trim).forEach(line -> {
if (line.startsWith("Version=")) {
version = line.substring("Version=".length());
Expand All @@ -93,10 +92,10 @@ private void loadApplicationIni() {
}

private void loadChannelPref() {
Optional<Path> channelPrefs = getResource("defaults/pref/channel-prefs.js");
Path channelPrefs = getResource("defaults/pref/channel-prefs.js");

if (channelPrefs.isPresent()) {
try (BufferedReader reader = Files.newBufferedReader(channelPrefs.get())) {
if (Files.exists(channelPrefs)) {
try (BufferedReader reader = Files.newBufferedReader(channelPrefs)) {
reader.lines().map(String::trim).forEach(line -> {
if (line.startsWith("pref(")) {
channel = FirefoxBinary.Channel.fromString(
Expand All @@ -114,18 +113,12 @@ private void loadChannelPref() {
channel = FirefoxBinary.Channel.RELEASE;
}

private Optional<Path> getResource(String resourceName) {
private Path getResource(String resourceName) {
Path binaryLocation = binary.getAbsoluteFile().toPath();
Path discovered;
if (Platform.getCurrent().is(Platform.MAC)) {
discovered = binaryLocation.getParent().getParent().resolve("Resources").resolve(resourceName);
return binaryLocation.getParent().getParent().resolve("Resources").resolve(resourceName);
} else {
discovered = binaryLocation.getParent().resolve(resourceName);
return binaryLocation.getParent().resolve(resourceName);
}

if (Files.exists(discovered)) {
return Optional.of(discovered);
}
return Optional.empty();
}
}
55 changes: 55 additions & 0 deletions java/client/test/org/openqa/selenium/firefox/ExecutableTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you 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.openqa.selenium.firefox;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Before;
import org.junit.Test;
import org.junit.Assume;
import org.openqa.selenium.WebDriverException;

import java.io.File;

public class ExecutableTest {

private String binaryPath;

@Before
public void setUp() {
try {
binaryPath = new FirefoxBinary().getPath();
} catch (WebDriverException ex) {
ex.printStackTrace();
Assume.assumeTrue(false);
}
}

@Test
public void canFindVersion() {
Executable exe = new Executable(new File(binaryPath));
assertThat(exe.getVersion()).isNotEmpty();
}

@Test
public void canFindChannel() {
Executable exe = new Executable(new File(binaryPath));
assertThat(exe.getChannel()).isNotNull();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
FirefoxProfileTest.class,
MarionetteTest.class,
PreferencesTest.class,
ExecutableTest.class,
})

public class FirefoxSpecificTests {
Expand Down

0 comments on commit 7d4cb1a

Please sign in to comment.