From 0fd8dd176b5605f6b2253749273969c57eae0a7b Mon Sep 17 00:00:00 2001 From: Max Bechtold Date: Sun, 21 Jan 2018 10:23:29 +0100 Subject: [PATCH] #3 Refactoring and tests --- .../approval/ApprovalIdResolver.java | 38 ++++++++++ .../approval/TemplatedTestPathMapper.java | 19 +++-- .../RunInvocationContextProvider.java | 27 ++----- .../approval/ApprovalIdResolverTest.java | 76 +++++++++++++++++++ .../approval/TemplatedTestPathMapperTest.java | 54 ++----------- 5 files changed, 139 insertions(+), 75 deletions(-) create mode 100644 src/main/java/maxbe/goldenmaster/approval/ApprovalIdResolver.java create mode 100644 src/test/java/maxbe/goldenmaster/approval/ApprovalIdResolverTest.java diff --git a/src/main/java/maxbe/goldenmaster/approval/ApprovalIdResolver.java b/src/main/java/maxbe/goldenmaster/approval/ApprovalIdResolver.java new file mode 100644 index 0000000..7e33c77 --- /dev/null +++ b/src/main/java/maxbe/goldenmaster/approval/ApprovalIdResolver.java @@ -0,0 +1,38 @@ +package maxbe.goldenmaster.approval; + +import org.junit.jupiter.api.extension.ExtensionContext; + +import maxbe.goldenmaster.junit.extension.GoldenMasterRun; + +public class ApprovalIdResolver { + + private final GoldenMasterRun goldenMasterRunAnnotation; + + public ApprovalIdResolver(GoldenMasterRun goldenMasterRunAnnotation) { + this.goldenMasterRunAnnotation = goldenMasterRunAnnotation; + } + + public String resolveApprovalIdFor(ExtensionContext context) { + return getApprovalId(context); + + } + + private String getApprovalId(ExtensionContext context) { + if (!GoldenMasterRun.AUTO_ID.equals(goldenMasterRunAnnotation.id())) { + return goldenMasterRunAnnotation.id(); + } + return context.getRequiredTestMethod().getName(); + } + + public String resolveRunIdFor(ExtensionContext context) { + return getApprovalId(context) + getRunIdSuffix(context.getDisplayName()); + } + + private String getRunIdSuffix(String displayName) { + // REVIEW #3 Can this be done better? + String idWithoutBraces = displayName.substring(1, displayName.length() - 1); + int runId = Integer.valueOf(idWithoutBraces) - 1; + return "[" + runId + "]"; + } + +} diff --git a/src/main/java/maxbe/goldenmaster/approval/TemplatedTestPathMapper.java b/src/main/java/maxbe/goldenmaster/approval/TemplatedTestPathMapper.java index 0cba29b..79ec69a 100644 --- a/src/main/java/maxbe/goldenmaster/approval/TemplatedTestPathMapper.java +++ b/src/main/java/maxbe/goldenmaster/approval/TemplatedTestPathMapper.java @@ -7,20 +7,19 @@ import com.github.approval.PathMapper; -// FIXME MAX There's a bootstrap problem, the initial approval files are empty... public class TemplatedTestPathMapper implements PathMapper { - private final Path approvalPath; + private final Path approvalPath; - public TemplatedTestPathMapper(ExtensionContext context, Path basePath, String approvalId) { - Class testClass = context.getRequiredTestClass(); + public TemplatedTestPathMapper(ExtensionContext context, Path basePath, String approvalId) { + Class testClass = context.getRequiredTestClass(); - approvalPath = basePath.resolve(testClass.getName().replace(".", File.separator)).resolve(approvalId); - } + approvalPath = basePath.resolve(testClass.getName().replace(".", File.separator)).resolve(approvalId); + } - @Override - public Path getPath(T value, Path approvalFilePath) { - return approvalPath.resolve(approvalFilePath); - } + @Override + public Path getPath(T value, Path approvalFilePath) { + return approvalPath.resolve(approvalFilePath); + } } diff --git a/src/main/java/maxbe/goldenmaster/junit/extension/RunInvocationContextProvider.java b/src/main/java/maxbe/goldenmaster/junit/extension/RunInvocationContextProvider.java index 49d5a74..f861e44 100644 --- a/src/main/java/maxbe/goldenmaster/junit/extension/RunInvocationContextProvider.java +++ b/src/main/java/maxbe/goldenmaster/junit/extension/RunInvocationContextProvider.java @@ -2,6 +2,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.stream.IntStream; import java.util.stream.Stream; @@ -18,6 +19,7 @@ import com.github.approval.Approval; +import maxbe.goldenmaster.approval.ApprovalIdResolver; import maxbe.goldenmaster.approval.ApprovalScriptWriter; import maxbe.goldenmaster.approval.FileConverter; import maxbe.goldenmaster.approval.JUnitReporter; @@ -77,9 +79,9 @@ public void beforeAll(ExtensionContext context) throws Exception { @Override public void beforeEach(ExtensionContext context) throws Exception { - String approvalId = getApprovalId(context) + getRunIdSuffix(context.getDisplayName()); - pathMapper = new TemplatedTestPathMapper<>(context, Paths.get("src", "test", "resources", "approved"), - approvalId); + String runId = new ApprovalIdResolver(getAnnotation(context)).resolveRunIdFor(context); + Path basePath = Paths.get("src", "test", "resources", "approved"); + pathMapper = new TemplatedTestPathMapper<>(context, basePath, runId); } @Override @@ -90,23 +92,10 @@ public void afterTestExecution(ExtensionContext context) throws Exception { .withConveter(new FileConverter())// .withReporter(get(context, JUnitReporter.class, REPORTER_KEY)).build(); - String fileName = getApprovalId(context) + ".approved"; - approval.verify(outputFile, Paths.get(fileName)); - } - - private String getApprovalId(ExtensionContext context) { - GoldenMasterRun annotation = getAnnotation(context); - if (!GoldenMasterRun.AUTO_ID.equals(annotation.id())) { - return annotation.id(); - } - return context.getRequiredTestMethod().getName(); - } + ApprovalIdResolver approvalIdResolver = new ApprovalIdResolver(getAnnotation(context)); - private String getRunIdSuffix(String displayName) { - // REVIEW #3 Can this be done better? - String idWithoutBraces = displayName.substring(1, displayName.length() - 1); - int runId = Integer.valueOf(idWithoutBraces) - 1; - return "[" + runId + "]"; + String fileName = approvalIdResolver.resolveApprovalIdFor(context) + ".approved"; + approval.verify(outputFile, Paths.get(fileName)); } @Override diff --git a/src/test/java/maxbe/goldenmaster/approval/ApprovalIdResolverTest.java b/src/test/java/maxbe/goldenmaster/approval/ApprovalIdResolverTest.java new file mode 100644 index 0000000..8a7085a --- /dev/null +++ b/src/test/java/maxbe/goldenmaster/approval/ApprovalIdResolverTest.java @@ -0,0 +1,76 @@ +package maxbe.goldenmaster.approval; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.mockito.Mockito; + +import maxbe.goldenmaster.junit.extension.GoldenMasterRun; + +public class ApprovalIdResolverTest { + + private ExtensionContext context; + + @BeforeEach + void setUp() { + context = Mockito.mock(ExtensionContext.class); + Mockito.doReturn(someMethod()).when(context).getRequiredTestMethod(); + Mockito.doReturn("[123]").when(context).getDisplayName(); + } + + @Test + void resolvesIdIfSpecified() throws Exception { + String testId = "test-id"; + + ApprovalIdResolver resolver = new ApprovalIdResolver(goldenMasterRun(testId)); + + String approvalId = resolver.resolveApprovalIdFor(context); + + assertThat(approvalId).isEqualTo(testId); + } + + @Test + void resolvesDisplayNameForAutoId() throws Exception { + ApprovalIdResolver resolver = new ApprovalIdResolver(goldenMasterRun(GoldenMasterRun.AUTO_ID)); + + assertThat(resolver.resolveApprovalIdFor(context)).isEqualTo(someMethod().getName()); + } + + @Test + void resolvesRunIdBasedOnApprovalId() throws Exception { + ApprovalIdResolver resolver = new ApprovalIdResolver(goldenMasterRun(GoldenMasterRun.AUTO_ID)); + + String runId = resolver.resolveRunIdFor(context); + + assertThat(runId).isEqualTo(someMethod().getName() + "[122]"); // index = JUnit execution number - 1; + } + + private Method someMethod() { + return getClass().getMethods()[0]; + } + + private GoldenMasterRun goldenMasterRun(String customId) { + return new GoldenMasterRun() { + + @Override + public Class annotationType() { + return GoldenMasterRun.class; + } + + @Override + public int repetitions() { + return GoldenMasterRun.DEFAULT_REPETITIONS; + } + + @Override + public String id() { + return customId; + } + }; + } +} diff --git a/src/test/java/maxbe/goldenmaster/approval/TemplatedTestPathMapperTest.java b/src/test/java/maxbe/goldenmaster/approval/TemplatedTestPathMapperTest.java index 5ecd280..d808710 100644 --- a/src/test/java/maxbe/goldenmaster/approval/TemplatedTestPathMapperTest.java +++ b/src/test/java/maxbe/goldenmaster/approval/TemplatedTestPathMapperTest.java @@ -3,19 +3,13 @@ import static org.assertj.core.api.Assertions.assertThat; import java.io.File; -import java.lang.annotation.Annotation; import java.nio.file.Path; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtensionContext; import org.mockito.Mockito; -import maxbe.goldenmaster.junit.extension.GoldenMasterRun; - -// TODO MAX Probably not meaningful anymore -@Disabled public class TemplatedTestPathMapperTest { private ExtensionContext context; @@ -24,57 +18,25 @@ public class TemplatedTestPathMapperTest { void setUp() { context = Mockito.mock(ExtensionContext.class); Mockito.doReturn(getClass()).when(context).getRequiredTestClass(); - Mockito.doReturn(TemplatedTestPathMapperTest.class.getMethods()[0]).when(context).getRequiredTestMethod(); - Mockito.doReturn("[123]").when(context).getDisplayName(); } @Test - void usesTestIdIfSpecified() throws Exception { - String testId = "test-id"; + void mapsPackageStructureToPath() throws Exception { + String approvalId = "test-id"; Path tempDir = new File("root").toPath(); - TemplatedTestPathMapper mapper = new TemplatedTestPathMapper<>(context, tempDir, null); + TemplatedTestPathMapper mapper = new TemplatedTestPathMapper<>(context, tempDir, approvalId); Path approvalFilePath = new File("file").toPath(); Path approvalPath = mapper.getPath(new Object(), approvalFilePath); - String separator = File.separator; - String classPath = getClass().getName().replace('.', File.separatorChar); - - assertThat(approvalPath.toString()).isEqualTo(tempDir.toString() + separator + classPath + separator + "test-id" - + separator + approvalFilePath.toString()); - } - - @Test - void usesDisplayNameWithIndexWithoutId() throws Exception { - Path tempDir = new File("root").toPath(); - TemplatedTestPathMapper mapper = new TemplatedTestPathMapper<>(context, tempDir, null); - Path approvalFilePath = new File("file").toPath(); - Path approvalPath = mapper.getPath(new Object(), approvalFilePath); String separator = File.separator; String classPath = getClass().getName().replace('.', File.separatorChar); - String basePath = tempDir.toString() + separator + classPath + separator; - String testSpec = context.getRequiredTestMethod().getName() + "[122]"; // index = JUnit execution - 1; - assertThat(approvalPath.toString()).isEqualTo(basePath + testSpec + separator + approvalFilePath.toString()); + assertThat(approvalPath.toString()) // + .isEqualTo(tempDir.toString() + separator // + + classPath + separator // + + approvalId + separator // + + approvalFilePath.toString()); } - private GoldenMasterRun goldenMasterRun(String testId) { - return new GoldenMasterRun() { - - @Override - public Class annotationType() { - return GoldenMasterRun.class; - } - - @Override - public int repetitions() { - return GoldenMasterRun.DEFAULT_REPETITIONS; - } - - @Override - public String id() { - return testId; - } - }; - } }