Skip to content

Commit

Permalink
#3 Refactoring and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbechtold committed Jan 21, 2018
1 parent 142a6aa commit 0fd8dd1
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 75 deletions.
38 changes: 38 additions & 0 deletions src/main/java/maxbe/goldenmaster/approval/ApprovalIdResolver.java
Original file line number Diff line number Diff line change
@@ -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 + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> implements PathMapper<T> {

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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<? extends Annotation> annotationType() {
return GoldenMasterRun.class;
}

@Override
public int repetitions() {
return GoldenMasterRun.DEFAULT_REPETITIONS;
}

@Override
public String id() {
return customId;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Object> mapper = new TemplatedTestPathMapper<>(context, tempDir, null);
TemplatedTestPathMapper<Object> 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<Object> 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<? extends Annotation> annotationType() {
return GoldenMasterRun.class;
}

@Override
public int repetitions() {
return GoldenMasterRun.DEFAULT_REPETITIONS;
}

@Override
public String id() {
return testId;
}
};
}
}

0 comments on commit 0fd8dd1

Please sign in to comment.