Skip to content

Commit

Permalink
Test refactoring, less code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Dec 29, 2017
1 parent 54ed175 commit 65f7425
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ public void testToJson() {

@Test
public void basicCommandLineParam() {
GridNodeConfiguration config = new GridNodeConfiguration();
JCommander.newBuilder().addObject(config).build().parse(
GridNodeConfiguration config = parseCliOptions(
"-role", "wd", "-hubHost", "ABC", "-hubPort", "1234", "-host","localhost");
RegistrationRequest req = RegistrationRequest.build(config);

Expand All @@ -98,8 +97,7 @@ public void basicCommandLineParam() {

@Test
public void commandLineParamDefault() {
GridNodeConfiguration config = new GridNodeConfiguration();
JCommander.newBuilder().addObject(config).build().parse("-role", "wd");
GridNodeConfiguration config = parseCliOptions("-role", "wd");
RegistrationRequest req = RegistrationRequest.build(config);
// the hub defaults to current IP.
assertNotNull(req.getConfiguration().getHubHost());
Expand All @@ -111,8 +109,7 @@ public void commandLineParamDefault() {

@Test
public void commandLineParamDefaultCapabilities() {
GridNodeConfiguration config = new GridNodeConfiguration();
JCommander.newBuilder().addObject(config).build().parse(
GridNodeConfiguration config = parseCliOptions(
"-role", "wd", "-hubHost", "ABC", "-hubPort", "1234", "-host","localhost");
RegistrationRequest req = RegistrationRequest.build(config);
assertEquals("ABC", req.getConfiguration().getHubHost());
Expand All @@ -121,23 +118,20 @@ public void commandLineParamDefaultCapabilities() {

@Test
public void registerParam() {
GridNodeConfiguration config = new GridNodeConfiguration();
JCommander.newBuilder().addObject(config).build().parse(
GridNodeConfiguration config = parseCliOptions(
"-role", "wd", "-hubHost", "ABC", "-host","localhost");
RegistrationRequest req = RegistrationRequest.build(config);
assertEquals(true, req.getConfiguration().register);

config = new GridNodeConfiguration();
JCommander.newBuilder().addObject(config).build().parse(
config = parseCliOptions(
"-role", "wd", "-hubHost", "ABC", "-hubPort", "1234","-host","localhost", "-register","false");
RegistrationRequest req2 = RegistrationRequest.build(config);
assertEquals(false, req2.getConfiguration().register);
}

@Test
public void ensurePre2_9HubCompatibility() {
GridNodeConfiguration config = new GridNodeConfiguration();
JCommander.newBuilder().addObject(config).build().parse(
GridNodeConfiguration config = parseCliOptions(
"-role", "wd", "-host","example.com", "-port", "5555");
RegistrationRequest req = RegistrationRequest.build(config);

Expand All @@ -146,8 +140,7 @@ public void ensurePre2_9HubCompatibility() {

@Test(expected = GridConfigurationException.class)
public void validateWithException() {
GridNodeConfiguration config = new GridNodeConfiguration();
JCommander.newBuilder().addObject(config).build().parse(
GridNodeConfiguration config = parseCliOptions(
"-role", "node", "-hubHost", "localhost", "-hub", "localhost:4444");
RegistrationRequest req = new RegistrationRequest(config);

Expand Down Expand Up @@ -382,4 +375,10 @@ private void assertConstruction(RegistrationRequest req) {
assertNotNull(capabilities.getCapability("seleniumProtocol"));
}
}

private GridNodeConfiguration parseCliOptions(String... args) {
GridNodeConfiguration config = new GridNodeConfiguration();
JCommander.newBuilder().addObject(config).build().parse(args);
return config;
}
}
20 changes: 10 additions & 10 deletions java/server/test/org/openqa/grid/internal/BaseRemoteProxyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ public void proxyConfigIsInheritedFromRegistry() {
GridRegistry registry = DefaultGridRegistry.newInstance(new Hub(new GridHubConfiguration()));
registry.getHub().getConfiguration().cleanUpCycle = 42;

GridNodeConfiguration nodeConfiguration = new GridNodeConfiguration();
JCommander.newBuilder().addObject(nodeConfiguration).build().parse("-role", "webdriver");
GridNodeConfiguration nodeConfiguration = parseCliOptions("-role", "webdriver");
RegistrationRequest req = RegistrationRequest.build(nodeConfiguration);
req.getConfiguration().proxy = null;

Expand All @@ -108,8 +107,7 @@ public void proxyConfigOverwritesRegistryConfig() {
registry.getConfiguration().cleanUpCycle = 42;
registry.getConfiguration().maxSession = 1;

GridNodeConfiguration nodeConfiguration = new GridNodeConfiguration();
JCommander.newBuilder().addObject(nodeConfiguration).build().parse(
GridNodeConfiguration nodeConfiguration = parseCliOptions(
"-role", "webdriver", "-cleanUpCycle", "100", "-maxSession", "50");
RegistrationRequest req = RegistrationRequest.build(nodeConfiguration);
req.getConfiguration().proxy = null;
Expand All @@ -127,8 +125,7 @@ public void proxyTakesRemoteAsIdIfIdNotSpecified() {
String remoteHost ="http://machine1:5555";
GridRegistry registry = DefaultGridRegistry.newInstance();

GridNodeConfiguration nodeConfiguration = new GridNodeConfiguration();
JCommander.newBuilder().addObject(nodeConfiguration).build().parse(
GridNodeConfiguration nodeConfiguration = parseCliOptions(
"-role", "webdriver","-host", "machine1", "-port", "5555");
RegistrationRequest req = RegistrationRequest.build(nodeConfiguration);
req.getConfiguration().proxy = null;
Expand All @@ -141,8 +138,7 @@ public void proxyTakesRemoteAsIdIfIdNotSpecified() {
@Test
public void proxyWithIdSpecified() {
GridRegistry registry = DefaultGridRegistry.newInstance();
GridNodeConfiguration nodeConfiguration = new GridNodeConfiguration();
JCommander.newBuilder().addObject(nodeConfiguration).build().parse(
GridNodeConfiguration nodeConfiguration = parseCliOptions(
"-role", "webdriver","-host", "machine1", "-port", "5555","-id", "abc");
RegistrationRequest req = RegistrationRequest.build(nodeConfiguration);
req.getConfiguration().proxy = null;
Expand All @@ -155,8 +151,7 @@ public void proxyWithIdSpecified() {
@Test
public void timeouts() {
GridRegistry registry = DefaultGridRegistry.newInstance();
GridNodeConfiguration nodeConfiguration = new GridNodeConfiguration();
JCommander.newBuilder().addObject(nodeConfiguration).build().parse(
GridNodeConfiguration nodeConfiguration = parseCliOptions(
"-role", "webdriver","-host", "machine1", "-port", "5555","-id", "abc","-timeout", "23", "-browserTimeout", "12");
RegistrationRequest req = RegistrationRequest.build(nodeConfiguration);
req.getConfiguration().proxy = null;
Expand Down Expand Up @@ -194,4 +189,9 @@ public void teardown() {
registry.stop();
}

private GridNodeConfiguration parseCliOptions(String... args) {
GridNodeConfiguration config = new GridNodeConfiguration();
JCommander.newBuilder().addObject(config).build().parse(args);
return config;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,8 @@ public void testConstructorEqualsDefaultConfig() {

@Test
public void testAsJson() {
final String[] args = new String[] { "-capabilities", "browserName=chrome,platform=linux" };
GridNodeConfiguration gnc = new GridNodeConfiguration();
JCommander.newBuilder().addObject(gnc).build().parse(args);
GridNodeConfiguration gnc = parseCliOptions(
"-capabilities", "browserName=chrome,platform=linux");

assertEquals("{\"capabilities\":"
+ "[{\"browserName\":\"chrome\",\"platform\":\"LINUX\"}],"
Expand Down Expand Up @@ -207,10 +206,8 @@ public void testWithCapabilitiesArgs() {

@Test
public void testWithCapabilitiesArgsWithExtraSpacing() {
final String[] args = new String[] { "-capabilities",
"browserName= chrome, platform =linux, maxInstances=10, boolean = false " };
GridNodeConfiguration gnc = new GridNodeConfiguration();
JCommander.newBuilder().addObject(gnc).build().parse(args);
GridNodeConfiguration gnc = parseCliOptions(
"-capabilities", "browserName= chrome, platform =linux, maxInstances=10, boolean = false ");
assertTrue(gnc.capabilities.size() == 1);
assertEquals("chrome", gnc.capabilities.get(0).getBrowserName());
assertEquals(10L, gnc.capabilities.get(0).getCapability("maxInstances"));
Expand All @@ -220,25 +217,19 @@ public void testWithCapabilitiesArgsWithExtraSpacing() {

@Test
public void testGetHubHost() {
final String[] args = new String[]{"-hubHost", "dummyhost", "-hubPort", "1234"};
GridNodeConfiguration gnc = new GridNodeConfiguration();
JCommander.newBuilder().addObject(gnc).build().parse(args);
GridNodeConfiguration gnc = parseCliOptions("-hubHost", "dummyhost", "-hubPort", "1234");
assertEquals("dummyhost", gnc.getHubHost());
}

@Test
public void testGetHubHostFromHubOption() {
final String[] args = new String[]{"-hub", "http://dummyhost:1234/wd/hub"};
GridNodeConfiguration gnc = new GridNodeConfiguration();
JCommander.newBuilder().addObject(gnc).build().parse(args);
GridNodeConfiguration gnc = parseCliOptions("-hub", "http://dummyhost:1234/wd/hub");
assertEquals("dummyhost", gnc.getHubHost());
}

@Test
public void testOneOfHubOrHubHostShouldBePresent() {
final String[] args = new String[]{"-hubPort", "1234"};
GridNodeConfiguration gnc = new GridNodeConfiguration();
JCommander.newBuilder().addObject(gnc).build().parse(args);
GridNodeConfiguration gnc = parseCliOptions("-hubPort", "1234");
Throwable t = catchThrowable(gnc::getHubHost);
assertThat(t, CoreMatchers.instanceOf(RuntimeException.class));
t = catchThrowable(gnc::getHubPort);
Expand All @@ -247,34 +238,26 @@ public void testOneOfHubOrHubHostShouldBePresent() {

@Test
public void testHubOptionHasPrecedenceOverHubHost() {
final String[] args = new String[]{"-hub", "http://smarthost:4321/wd/hub",
"-hubHost", "dummyhost", "-hubPort", "1234"};
GridNodeConfiguration gnc = new GridNodeConfiguration();
JCommander.newBuilder().addObject(gnc).build().parse(args);
GridNodeConfiguration gnc = parseCliOptions(
"-hub", "http://smarthost:4321/wd/hub", "-hubHost", "dummyhost", "-hubPort", "1234");
assertEquals("smarthost", gnc.getHubHost());
}

@Test
public void testGetHubPort() {
final String[] args = new String[]{"-hubHost", "dummyhost", "-hubPort", "1234"};
GridNodeConfiguration gnc = new GridNodeConfiguration();
JCommander.newBuilder().addObject(gnc).build().parse(args);
GridNodeConfiguration gnc = parseCliOptions("-hubHost", "dummyhost", "-hubPort", "1234");
assertEquals(1234, gnc.getHubPort().intValue());
}

@Test
public void testGetHubPortFromHubOption() {
final String[] args = new String[]{"-hub", "http://dummyhost:1234/wd/hub"};
GridNodeConfiguration gnc = new GridNodeConfiguration();
JCommander.newBuilder().addObject(gnc).build().parse(args);
GridNodeConfiguration gnc = parseCliOptions("-hub", "http://dummyhost:1234/wd/hub");
assertEquals(1234, gnc.getHubPort().intValue());
}

@Test
public void testOneOfHubOrHubPortShouldBePresent() {
final String[] args = new String[]{"-hubHost", "dummyhost"};
GridNodeConfiguration gnc = new GridNodeConfiguration();
JCommander.newBuilder().addObject(gnc).build().parse(args);
GridNodeConfiguration gnc = parseCliOptions("-hubHost", "dummyhost");
Throwable t = catchThrowable(gnc::getHubHost);
assertThat(t, CoreMatchers.instanceOf(RuntimeException.class));
t = catchThrowable(gnc::getHubPort);
Expand All @@ -283,10 +266,8 @@ public void testOneOfHubOrHubPortShouldBePresent() {

@Test
public void testHubOptionHasPrecedenceOverHubPort() {
final String[] args = new String[]{"-hub", "http://smarthost:4321/wd/hub",
"-hubHost", "dummyhost", "-hubPort", "1234"};
GridNodeConfiguration gnc = new GridNodeConfiguration();
JCommander.newBuilder().addObject(gnc).build().parse(args);
GridNodeConfiguration gnc = parseCliOptions(
"-hub", "http://smarthost:4321/wd/hub", "-hubHost", "dummyhost", "-hubPort", "1234");
assertEquals(4321, gnc.getHubPort().intValue());
}

Expand Down Expand Up @@ -375,4 +356,9 @@ public void testFixupCapabilitiesAddsUUID() {
.allMatch(cap -> cap.getCapability(GridNodeConfiguration.CONFIG_UUID_CAPABILITY) != null));
}

private GridNodeConfiguration parseCliOptions(String... args) {
GridNodeConfiguration config = new GridNodeConfiguration();
JCommander.newBuilder().addObject(config).build().parse(args);
return config;
}
}

0 comments on commit 65f7425

Please sign in to comment.