Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

Commit

Permalink
Refactor and remove code duplicates. Add tests for configs.
Browse files Browse the repository at this point in the history
  • Loading branch information
rosolko committed Mar 19, 2019
1 parent af065b5 commit 47b1f9e
Show file tree
Hide file tree
Showing 14 changed files with 271 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private void setup(final CommonConfig config, final String version, final Os os,
final String pattern = config.getUrlPattern();
final String platform = config.getPlatform(os, architecture);
final Extension extension = config.getArchiveExtension(os);
final String binaryName = config.getBinaryName(os);
final String binaryName = config.getBinaryNameWithExtension(os);
final String binaryVariable = config.getBinaryVariable();
final Path binaryPath = fileService.getBinaryPath(config.getBrowserName(), version, platform, binaryName);

Expand Down
23 changes: 20 additions & 3 deletions src/main/java/com/github/rosolko/wdm4j/config/CommonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,35 @@ public interface CommonConfig {
String getBrowserName();

/**
* Get binary name based on operation system name can it return with extension.
* Get binary name.
* <br>
* Example for chromedriver - {@code "chromedriver"}.
*
* @return A binary name
*/
String getBinaryName();

/**
* Get binary full name based on operation system and the name itself can it return with extension.
* <br>
* Example for windows - {@code "chromedriver.exe"}.
* <br>
* Example for mac and linux - {@code "chromedriver"}.
*
* @param os A target operation system
* @param os A target operation system
* @return A binary name
* @see Os
* @see Extension
*/
String getBinaryName(Os os);
default String getBinaryNameWithExtension(final Os os) {
final String name = getBinaryName();
requireNonNull(name);
requireNonNull(os);

return os == Os.WINDOWS
? String.format("%s.%s", name, Extension.EXE.getValue())
: name;
}

/**
* Get latest binary version.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import com.github.rosolko.wdm4j.config.CommonConfig;
import com.github.rosolko.wdm4j.enums.Architecture;
import com.github.rosolko.wdm4j.enums.Extension;
import com.github.rosolko.wdm4j.enums.Os;
import com.github.rosolko.wdm4j.exception.WebDriverManagerException;
import org.jsoup.Jsoup;
Expand Down Expand Up @@ -32,13 +31,8 @@ public String getBrowserName() {
* {@inheritDoc}
*/
@Override
public String getBinaryName(final Os os) {
requireNonNull(os);

final String name = "chromedriver";
return os == Os.WINDOWS
? String.format("%s.%s", name, Extension.EXE.getValue())
: name;
public String getBinaryName() {
return "chromedriver";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ public String getBrowserName() {
* {@inheritDoc}
*/
@Override
public String getBinaryName(final Os os) {
public String getBinaryName() {
return "MicrosoftWebDrive";
}

/**
* {@inheritDoc}
*/
@Override
public String getBinaryNameWithExtension(final Os os) {
return "MicrosoftWebDriver.exe";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,8 @@ public String getBrowserName() {
* {@inheritDoc}
*/
@Override
public String getBinaryName(final Os os) {
requireNonNull(os);

final String name = "geckodriver";
return os == Os.WINDOWS
? String.format("%s.%s", name, Extension.EXE.getValue())
: name;
public String getBinaryName() {
return "geckodriver";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ public String getBrowserName() {
* {@inheritDoc}
*/
@Override
public String getBinaryName(final Os os) {
public String getBinaryName() {
return "IEDriverServer";
}

/**
* {@inheritDoc}
*/
@Override
public String getBinaryNameWithExtension(final Os os) {
return "IEDriverServer.exe";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import com.github.rosolko.wdm4j.config.CommonConfig;
import com.github.rosolko.wdm4j.enums.Architecture;
import com.github.rosolko.wdm4j.enums.Extension;
import com.github.rosolko.wdm4j.enums.Os;
import com.github.rosolko.wdm4j.exception.WebDriverManagerException;
import org.jsoup.Jsoup;
Expand Down Expand Up @@ -32,13 +31,8 @@ public String getBrowserName() {
* {@inheritDoc}
*/
@Override
public String getBinaryName(final Os os) {
requireNonNull(os);

final String name = "operadriver";
return os == Os.WINDOWS
? String.format("%s.%s", name, Extension.EXE.getValue())
: name;
public String getBinaryName() {
return "operadriver";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@ public String getBrowserName() {
* {@inheritDoc}
*/
@Override
public String getBinaryName(final Os os) {
requireNonNull(os);

final String name = "phantomjs";
return os == Os.WINDOWS
? String.format("%s.%s", name, Extension.EXE.getValue())
: name;
public String getBinaryName() {
return "phantomjs";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void setUp() {
void ableToSetUpBinary(final CommonConfig config) {
final String version = config.getLatestVersion();
final String platform = config.getPlatform(os, architecture);
final String binaryName = config.getBinaryName(os);
final String binaryName = config.getBinaryNameWithExtension(os);
final Path binaryPath = fileService.getBinaryPath(config.getBrowserName(), version, platform, binaryName);
final String variable = config.getBinaryVariable();

Expand All @@ -81,7 +81,7 @@ void ableToSetUpBinaryWithSpecificVersion() {

final CommonConfig config = new FirefoxConfig();
final String platform = config.getPlatform(os, architecture);
final String binaryName = config.getBinaryName(os);
final String binaryName = config.getBinaryNameWithExtension(os);
final Path binaryPath = fileService.getBinaryPath(config.getBrowserName(), version, platform, binaryName);
final String variable = config.getBinaryVariable();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.rosolko.wdm4j.config;

import com.github.rosolko.wdm4j.config.impl.ChromeConfig;
import com.github.rosolko.wdm4j.enums.Extension;
import com.github.rosolko.wdm4j.enums.Os;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

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

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@DisplayName("Default config test")
class CommonConfigTest {

@ParameterizedTest
@EnumSource(Os.class)
@DisplayName("Get binary name based on os")
void ableToGetBinaryName(final Os os) {
final ChromeConfig config = new ChromeConfig();
final String binaryName = config.getBinaryName();
final String extension = os == Os.WINDOWS ? Extension.EXE.getValue() : "";
final String binaryNameWithExtension = config.getBinaryNameWithExtension(os);
assertThat(binaryNameWithExtension)
.startsWith(binaryName)
.endsWith(extension);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.github.rosolko.wdm4j.config.impl;

import com.github.rosolko.wdm4j.enums.Architecture;
import com.github.rosolko.wdm4j.enums.Os;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

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

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@DisplayName("Default chrome config test")
class ChromeConfigTest {
private ChromeConfig config;
private Architecture architecture;

@BeforeAll
@DisplayName("Create default chrome config")
void setUp() {
config = new ChromeConfig();
architecture = Architecture.detect();
}

@ParameterizedTest
@EnumSource(Os.class)
@DisplayName("Get platform based on os")
void ableToGetPlatform(final Os os) {
final Architecture outArchitecture = os == Os.WINDOWS ? Architecture.X_86_32 : Architecture.X_86_64;
final String platform = config.getPlatform(os, architecture);
assertThat(platform)
.startsWith(os.getValue())
.endsWith(outArchitecture.getValue());
}

@Test
@DisplayName("Get latest version")
void ableToGetLatestVersion() {
final String latestVersion = config.getLatestVersion();
assertThat(latestVersion).isNotBlank();
assertThat(latestVersion).matches("^\\d+\\.\\d+$");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.github.rosolko.wdm4j.config.impl;

import com.github.rosolko.wdm4j.enums.Architecture;
import com.github.rosolko.wdm4j.enums.Extension;
import com.github.rosolko.wdm4j.enums.Os;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

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

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@DisplayName("Default firefox config test")
class FirefoxConfigTest {
private FirefoxConfig config;
private Architecture architecture;

@BeforeAll
@DisplayName("Create default firefox config")
void setUp() {
config = new FirefoxConfig();
architecture = Architecture.detect();
}

@ParameterizedTest
@EnumSource(Os.class)
@DisplayName("Get platform based on os")
void ableToGetPlatform(final Os os) {
final String expectedArchitecture = os == Os.OSX ? "" : architecture.getValue();
final String platform = config.getPlatform(os, architecture);
assertThat(platform)
.startsWith(os.getValue())
.endsWith(expectedArchitecture);
}

@Test
@DisplayName("Get latest version")
void ableToGetLatestVersion() {
final String latestVersion = config.getLatestVersion();
assertThat(latestVersion).isNotBlank();
assertThat(latestVersion).matches("^v\\d+\\.\\d+\\.\\d+$");
}

@ParameterizedTest
@EnumSource(Os.class)
@DisplayName("Get archive extension")
void ableToGetArchiveExtension(final Os os) {
final Extension extension = os == Os.WINDOWS ? Extension.ZIP : Extension.TAR_GZ;
final Extension archiveExtension = config.getArchiveExtension(os);
assertThat(archiveExtension).isEqualTo(extension);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.github.rosolko.wdm4j.config.impl;

import com.github.rosolko.wdm4j.enums.Architecture;
import com.github.rosolko.wdm4j.enums.Os;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

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

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@DisplayName("Default opera config test")
class OperaConfigTest {
private OperaConfig config;
private Architecture architecture;

@BeforeAll
@DisplayName("Create default opera config")
void setUp() {
config = new OperaConfig();
architecture = Architecture.detect();
}

@ParameterizedTest
@EnumSource(Os.class)
@DisplayName("Get platform based on os")
void ableToGetPlatform(final Os os) {
final Architecture expectedArchitecture = os == Os.WINDOWS ? Architecture.X_86_32 : architecture;
final String platform = config.getPlatform(os, architecture);
assertThat(platform)
.startsWith(os.getValue())
.endsWith(expectedArchitecture.getValue());
}

@Test
@DisplayName("Get latest version")
void ableToGetLatestVersion() {
final String latestVersion = config.getLatestVersion();
assertThat(latestVersion).isNotBlank();
assertThat(latestVersion).matches("^\\d+\\.\\d+$");
}
}
Loading

0 comments on commit 47b1f9e

Please sign in to comment.