public static PipelineOptions createPipelineOptions(Map<String, String> map) { final String[] args = new String[map.size()]; int i = 0; for (Map.Entry<String, String> entry : map.entrySet()) { args[i++] = "--" + entry.getKey() + "=" + entry.getValue(); } PipelineOptions options = PipelineOptionsFactory.fromArgs(args).withValidation().create(); options.as(ApplicationNameOptions.class).setAppName("BeamSql"); return options; }
@Override public String toString() { return "TestPipeline#" + options.as(ApplicationNameOptions.class).getAppName(); }
if (appNameOptions.getAppName() == null) { appNameOptions.setAppName(defaultAppName);
public static void configureJobName(PipelineOptions options, String dbName, String tableName) { try { options.as(ApplicationNameOptions.class).setAppName("JdbcAvroJob"); } catch (Exception e) { LOGGER.warn("Unable to configure ApplicationName", e); } if (options.getJobName() == null || "auto".equals(options.getJobName())) { String randomPart = Integer.toHexString(ThreadLocalRandom.current().nextInt()); options.setJobName( String.join("-", "dbeam", normalizeString(dbName), normalizeString(tableName), randomPart)); } } }
@Override public String create(PipelineOptions options) { String appName = options.as(ApplicationNameOptions.class).getAppName(); String normalizedAppName = appName == null || appName.length() == 0 ? "BeamApp" : appName.toLowerCase().replaceAll("[^a-z0-9]", "0").replaceAll("^[^a-z]", "a"); String userName = MoreObjects.firstNonNull(System.getProperty("user.name"), ""); String normalizedUserName = userName.toLowerCase().replaceAll("[^a-z0-9]", "0"); String datePart = FORMATTER.print(DateTimeUtils.currentTimeMillis()); String randomPart = Integer.toHexString(ThreadLocalRandom.current().nextInt()); return String.format( "%s-%s-%s-%s", normalizedAppName, normalizedUserName, datePart, randomPart); } }
@Override public void evaluate() throws Throwable { options.as(ApplicationNameOptions.class).setAppName(getAppName(description)); setDeducedEnforcementLevel(); // statement.evaluate() essentially runs the user code contained in the unit test at hand. // Exceptions thrown during the execution of the user's test code will propagate here, // unless the user explicitly handles them with a "catch" clause in his code. If the // exception is handled by a user's "catch" clause, is does not interrupt the flow and // we move on to invoking the configured enforcements. // If the user does not handle a thrown exception, it will propagate here and interrupt // the flow, preventing the enforcement(s) from being activated. // The motivation for this is avoiding enforcements over faulty pipelines. statement.evaluate(); enforcement.get().afterUserCodeFinished(); } };
@Test public void testAppNameIsSet() { ApplicationNameOptions options = PipelineOptionsFactory.as(ApplicationNameOptions.class); assertEquals(PipelineOptionsFactoryTest.class.getSimpleName(), options.getAppName()); }
@Parameters(name = "{index}: {0}") public static Iterable<? extends PipelineOptions> options() { PipelineOptionsFactory.register(TestUnserializableOptions.class); PipelineOptionsFactory.register(TestDefaultOptions.class); PipelineOptionsFactory.register(TestOptions.class); PipelineOptions emptyOptions = PipelineOptionsFactory.create(); TestUnserializableOptions withNonSerializable = PipelineOptionsFactory.as(TestUnserializableOptions.class); withNonSerializable.setUnserializable(new Object()); TestOptions withCustomField = PipelineOptionsFactory.as(TestOptions.class); withCustomField.setExample(99); PipelineOptions withSettings = PipelineOptionsFactory.create(); withSettings.as(ApplicationNameOptions.class).setAppName("my_app"); withSettings.setJobName("my_job"); PipelineOptions withParsedSettings = PipelineOptionsFactory.fromArgs("--jobName=my_job --appName=my_app").create(); return ImmutableList.of( emptyOptions, withNonSerializable, withCustomField, withSettings, withParsedSettings); }
@Test public void testAppNameIsSetWhenUsingAs() { TestPipelineOptions options = PipelineOptionsFactory.as(TestPipelineOptions.class); assertEquals( PipelineOptionsFactoryTest.class.getSimpleName(), options.as(ApplicationNameOptions.class).getAppName()); }
@Test public void testAppNameIsNotOverriddenWhenPassedInViaCommandLine() { ApplicationNameOptions options = PipelineOptionsFactory.fromArgs("--appName=testAppName").as(ApplicationNameOptions.class); assertEquals("testAppName", options.getAppName()); }
@Test public void testCreationOfPipelineOptionsFromReallyVerboselyNamedTestCase() throws Exception { PipelineOptions options = pipeline.getOptions(); assertThat( options.as(ApplicationNameOptions.class).getAppName(), startsWith( "TestPipelineTest$TestPipelineCreationTest" + "-testCreationOfPipelineOptionsFromReallyVerboselyNamedTestCase")); }