Skip to content

Commit

Permalink
Improve PipelineOptionsFactoryTest
Browse files Browse the repository at this point in the history
Currently the test uses the literal string for the default runner and
available runners. Instead, refer to the default runner class and
extract the simple name from that class.

Automatically figure out portions of the error message for unknown runners.
  • Loading branch information
tgroh authored and davorbonaci committed Mar 23, 2016
1 parent 2f90258 commit 53ffc01
Showing 1 changed file with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.dataflow.sdk.options;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -53,10 +54,14 @@
import java.io.PrintStream;
import java.util.List;
import java.util.Map;
import java.util.Set;

/** Tests for {@link PipelineOptionsFactory}. */
@RunWith(JUnit4.class)
public class PipelineOptionsFactoryTest {
private static final Class<? extends PipelineRunner<?>> DEFAULT_RUNNER_CLASS =
DirectPipelineRunner.class;

@Rule public ExpectedException expectedException = ExpectedException.none();
@Rule public TestRule restoreSystemProperties = new RestoreSystemProperties();
@Rule public ExpectedLogs expectedLogs = ExpectedLogs.none(PipelineOptionsFactory.class);
Expand All @@ -68,8 +73,9 @@ public void testAutomaticRegistrationOfPipelineOptions() {

@Test
public void testAutomaticRegistrationOfRunners() {
assertEquals(DirectPipelineRunner.class,
PipelineOptionsFactory.getRegisteredRunners().get("DirectPipelineRunner"));
assertEquals(
DEFAULT_RUNNER_CLASS,
PipelineOptionsFactory.getRegisteredRunners().get(DEFAULT_RUNNER_CLASS.getSimpleName()));
}

@Test
Expand Down Expand Up @@ -840,9 +846,14 @@ public void testSettingRunnerFullName() {
public void testSettingUnknownRunner() {
String[] args = new String[] {"--runner=UnknownRunner"};
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Unknown 'runner' specified 'UnknownRunner', supported "
+ "pipeline runners [BlockingDataflowPipelineRunner, DataflowPipelineRunner, "
+ "DirectPipelineRunner]");
expectedException.expectMessage(
"Unknown 'runner' specified 'UnknownRunner', supported " + "pipeline runners");
Set<String> registeredRunners = PipelineOptionsFactory.getRegisteredRunners().keySet();
assertThat(registeredRunners, hasItem(DEFAULT_RUNNER_CLASS.getSimpleName()));
for (String registeredRunner : registeredRunners) {
expectedException.expectMessage(registeredRunner);
}

PipelineOptionsFactory.fromArgs(args).create();
}

Expand Down Expand Up @@ -927,13 +938,19 @@ public void testUsingArgumentStartingWithIllegalCharacterIsIgnoredWithoutStrictP

@Test
public void testEmptyArgumentIsIgnored() {
String[] args = new String[] {"", "--diskSizeGb=100", "", "", "--runner=DirectPipelineRunner"};
String[] args =
new String[] {
"", "--diskSizeGb=100", "", "", "--runner=" + DEFAULT_RUNNER_CLASS.getSimpleName()
};
PipelineOptionsFactory.fromArgs(args).create();
}

@Test
public void testNullArgumentIsIgnored() {
String[] args = new String[] {"--diskSizeGb=100", null, null, "--runner=DirectPipelineRunner"};
String[] args =
new String[] {
"--diskSizeGb=100", null, null, "--runner=" + DEFAULT_RUNNER_CLASS.getSimpleName()
};
PipelineOptionsFactory.fromArgs(args).create();
}

Expand Down Expand Up @@ -985,7 +1002,7 @@ public void testSpecificHelpAsArgument() {
String output = new String(baos.toByteArray());
assertThat(output, containsString("com.google.cloud.dataflow.sdk.options.PipelineOptions"));
assertThat(output, containsString("--runner"));
assertThat(output, containsString("Default: DirectPipelineRunner"));
assertThat(output, containsString("Default: " + DEFAULT_RUNNER_CLASS.getSimpleName()));
assertThat(output,
containsString("The pipeline runner that will be used to execute the pipeline."));
}
Expand All @@ -1000,7 +1017,7 @@ public void testSpecificHelpAsArgumentWithSimpleClassName() {
String output = new String(baos.toByteArray());
assertThat(output, containsString("com.google.cloud.dataflow.sdk.options.PipelineOptions"));
assertThat(output, containsString("--runner"));
assertThat(output, containsString("Default: DirectPipelineRunner"));
assertThat(output, containsString("Default: " + DEFAULT_RUNNER_CLASS.getSimpleName()));
assertThat(output,
containsString("The pipeline runner that will be used to execute the pipeline."));
}
Expand All @@ -1015,7 +1032,7 @@ public void testSpecificHelpAsArgumentWithClassNameSuffix() {
String output = new String(baos.toByteArray());
assertThat(output, containsString("com.google.cloud.dataflow.sdk.options.PipelineOptions"));
assertThat(output, containsString("--runner"));
assertThat(output, containsString("Default: DirectPipelineRunner"));
assertThat(output, containsString("Default: " + DEFAULT_RUNNER_CLASS.getSimpleName()));
assertThat(output,
containsString("The pipeline runner that will be used to execute the pipeline."));
}
Expand Down Expand Up @@ -1096,7 +1113,7 @@ public void testProgrammaticPrintHelpForSpecificType() {
String output = new String(baos.toByteArray());
assertThat(output, containsString("com.google.cloud.dataflow.sdk.options.PipelineOptions"));
assertThat(output, containsString("--runner"));
assertThat(output, containsString("Default: DirectPipelineRunner"));
assertThat(output, containsString("Default: " + DEFAULT_RUNNER_CLASS.getSimpleName()));
assertThat(output,
containsString("The pipeline runner that will be used to execute the pipeline."));
}
Expand Down

0 comments on commit 53ffc01

Please sign in to comment.