-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
142a6aa
commit 0fd8dd1
Showing
5 changed files
with
139 additions
and
75 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/main/java/maxbe/goldenmaster/approval/ApprovalIdResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "]"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/test/java/maxbe/goldenmaster/approval/ApprovalIdResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters