From 53ffc0145d07c33b7f621afc4dc695631c60636f Mon Sep 17 00:00:00 2001 From: Thomas Groh Date: Fri, 18 Mar 2016 15:20:49 -0700 Subject: [PATCH] Improve PipelineOptionsFactoryTest 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. --- .../options/PipelineOptionsFactoryTest.java | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/sdk/src/test/java/com/google/cloud/dataflow/sdk/options/PipelineOptionsFactoryTest.java b/sdk/src/test/java/com/google/cloud/dataflow/sdk/options/PipelineOptionsFactoryTest.java index 045a8ad0f2577..8b5dff5397454 100644 --- a/sdk/src/test/java/com/google/cloud/dataflow/sdk/options/PipelineOptionsFactoryTest.java +++ b/sdk/src/test/java/com/google/cloud/dataflow/sdk/options/PipelineOptionsFactoryTest.java @@ -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; @@ -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> 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); @@ -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 @@ -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 registeredRunners = PipelineOptionsFactory.getRegisteredRunners().keySet(); + assertThat(registeredRunners, hasItem(DEFAULT_RUNNER_CLASS.getSimpleName())); + for (String registeredRunner : registeredRunners) { + expectedException.expectMessage(registeredRunner); + } + PipelineOptionsFactory.fromArgs(args).create(); } @@ -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(); } @@ -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.")); } @@ -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.")); } @@ -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.")); } @@ -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.")); }