Repository: beam Updated Branches: refs/heads/master 0d927ef6a -> d5101750e
[BEAM-2630] TestPipeline: construct job/app names based on Description in junit TestRule. Project: http://git-wip-us.apache.org/repos/asf/beam/repo Commit: http://git-wip-us.apache.org/repos/asf/beam/commit/bdf5bd6e Tree: http://git-wip-us.apache.org/repos/asf/beam/tree/bdf5bd6e Diff: http://git-wip-us.apache.org/repos/asf/beam/diff/bdf5bd6e Branch: refs/heads/master Commit: bdf5bd6e50fa9f44ad7560714cd41ac3f346d124 Parents: 0d927ef Author: Pei He <p...@apache.org> Authored: Mon Jul 17 23:34:27 2017 +0800 Committer: Pei He <p...@apache.org> Committed: Wed Jul 19 11:30:12 2017 +0800 ---------------------------------------------------------------------- .../apache/beam/sdk/testing/TestPipeline.java | 63 ++++---------------- .../beam/sdk/testing/TestPipelineTest.java | 38 +----------- 2 files changed, 13 insertions(+), 88 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/beam/blob/bdf5bd6e/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/TestPipeline.java ---------------------------------------------------------------------- diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/TestPipeline.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/TestPipeline.java index 9206e04..34f1c83 100644 --- a/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/TestPipeline.java +++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/TestPipeline.java @@ -31,9 +31,7 @@ import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.base.Strings; import com.google.common.collect.FluentIterable; -import com.google.common.collect.Iterators; import java.io.IOException; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; @@ -307,6 +305,7 @@ public class TestPipeline extends Pipeline implements TestRule { @Override public void evaluate() throws Throwable { + options.as(ApplicationNameOptions.class).setAppName(getAppName(description)); setDeducedEnforcementLevel(); @@ -402,7 +401,6 @@ public class TestPipeline extends Pipeline implements TestRule { MAPPER.readValue(beamTestPipelineOptions, String[].class)) .as(TestPipelineOptions.class); - options.as(ApplicationNameOptions.class).setAppName(getAppName()); // If no options were specified, set some reasonable defaults if (Strings.isNullOrEmpty(beamTestPipelineOptions)) { // If there are no provided options, check to see if a dummy runner should be used. @@ -450,56 +448,17 @@ public class TestPipeline extends Pipeline implements TestRule { } } - /** Returns the class + method name of the test, or a default name. */ - private static String getAppName() { - Optional<StackTraceElement> stackTraceElement = findCallersStackTrace(); - if (stackTraceElement.isPresent()) { - String methodName = stackTraceElement.get().getMethodName(); - String className = stackTraceElement.get().getClassName(); - if (className.contains(".")) { - className = className.substring(className.lastIndexOf(".") + 1); - } - return className + "-" + methodName; - } - return "UnitTest"; - } - - /** Returns the {@link StackTraceElement} of the calling class. */ - private static Optional<StackTraceElement> findCallersStackTrace() { - Iterator<StackTraceElement> elements = - Iterators.forArray(Thread.currentThread().getStackTrace()); - // First find the TestPipeline class in the stack trace. - while (elements.hasNext()) { - StackTraceElement next = elements.next(); - if (TestPipeline.class.getName().equals(next.getClassName())) { - break; - } - } - // Then find the first instance after that is not the TestPipeline - Optional<StackTraceElement> firstInstanceAfterTestPipeline = Optional.absent(); - while (elements.hasNext()) { - StackTraceElement next = elements.next(); - if (!TestPipeline.class.getName().equals(next.getClassName())) { - if (!firstInstanceAfterTestPipeline.isPresent()) { - firstInstanceAfterTestPipeline = Optional.of(next); - } - try { - Class<?> nextClass = Class.forName(next.getClassName()); - for (Method method : nextClass.getMethods()) { - if (method.getName().equals(next.getMethodName())) { - if (method.isAnnotationPresent(org.junit.Test.class)) { - return Optional.of(next); - } else if (method.isAnnotationPresent(org.junit.Before.class)) { - break; - } - } - } - } catch (Throwable t) { - break; - } - } + /** Returns the class + method name of the test. */ + private String getAppName(Description description) { + String methodName = description.getMethodName(); + Class<?> testClass = description.getTestClass(); + if (testClass.isMemberClass()) { + return String.format( + "%s$%s-%s", + testClass.getEnclosingClass().getSimpleName(), testClass.getSimpleName(), methodName); + } else { + return String.format("%s-%s", testClass.getSimpleName(), methodName); } - return firstInstanceAfterTestPipeline; } /** http://git-wip-us.apache.org/repos/asf/beam/blob/bdf5bd6e/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/TestPipelineTest.java ---------------------------------------------------------------------- diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/TestPipelineTest.java b/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/TestPipelineTest.java index 05abb59..664f2f4 100644 --- a/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/TestPipelineTest.java +++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/TestPipelineTest.java @@ -100,7 +100,7 @@ public class TestPipelineTest implements Serializable { @Test public void testCreationOfPipelineOptionsFromReallyVerboselyNamedTestCase() throws Exception { - PipelineOptions options = TestPipeline.testingPipelineOptions(); + PipelineOptions options = pipeline.getOptions(); assertThat( options.as(ApplicationNameOptions.class).getAppName(), startsWith( @@ -112,23 +112,7 @@ public class TestPipelineTest implements Serializable { public void testToString() { assertEquals( "TestPipeline#TestPipelineTest$TestPipelineCreationTest-testToString", - TestPipeline.create().toString()); - } - - @Test - public void testToStringNestedMethod() { - TestPipeline p = nestedMethod(); - - assertEquals( - "TestPipeline#TestPipelineTest$TestPipelineCreationTest-testToStringNestedMethod", - p.toString()); - assertEquals( - "TestPipelineTest$TestPipelineCreationTest-testToStringNestedMethod", - p.getOptions().as(ApplicationNameOptions.class).getAppName()); - } - - private TestPipeline nestedMethod() { - return TestPipeline.create(); + pipeline.toString()); } @Test @@ -144,24 +128,6 @@ public class TestPipelineTest implements Serializable { } @Test - public void testToStringNestedClassMethod() { - TestPipeline p = new NestedTester().p(); - - assertEquals( - "TestPipeline#TestPipelineTest$TestPipelineCreationTest-testToStringNestedClassMethod", - p.toString()); - assertEquals( - "TestPipelineTest$TestPipelineCreationTest-testToStringNestedClassMethod", - p.getOptions().as(ApplicationNameOptions.class).getAppName()); - } - - private static class NestedTester { - public TestPipeline p() { - return TestPipeline.create(); - } - } - - @Test public void testRunWithDummyEnvironmentVariableFails() { System.getProperties() .setProperty(TestPipeline.PROPERTY_USE_DEFAULT_DUMMY_RUNNER, Boolean.toString(true));