Skip to content
This repository has been archived by the owner on Mar 5, 2023. It is now read-only.

Commit

Permalink
ClockRule: Migrate to ClockExtension
Browse files Browse the repository at this point in the history
Currently, we use ClockRule to implement a clock for unit testing.

Rules are part of JUnit 4, and have been replaced by extensions in JUnit 5. As we migrate to JUnit 5, we should update ClockRule to follow the new naming convenion and use JUnit 5 extension style.

Let's rewrite ClockRule to implement an injectable clock using JUnit5 callbacks.
  • Loading branch information
sijie123 committed Feb 24, 2019
1 parent bf4987d commit c77742a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
13 changes: 7 additions & 6 deletions src/test/java/systemtests/AddressBookSystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
import java.util.Date;
import java.util.List;

import org.junit.ClassRule;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;

import guitests.guihandles.BrowserPanelHandle;
Expand All @@ -46,15 +46,16 @@
* for test verification.
*/
public abstract class AddressBookSystemTest {
@ClassRule
public static ClockRule clockRule = new ClockRule();

@TempDir
public static Path tempDir;

public static final String SAVE_FILENAME_FOR_TESTING = "sampleData.json";
public static final String CONFIG_FILENAME_FOR_TESTING = "pref_testing.json";

@RegisterExtension
public static ClockExtension clockExtension = new ClockExtension();

private static final List<String> COMMAND_BOX_DEFAULT_STYLE = Arrays.asList("text-input", "text-field");
private static final List<String> COMMAND_BOX_ERROR_STYLE =
Arrays.asList("text-input", "text-field", CommandBox.ERROR_STYLE_CLASS);
Expand Down Expand Up @@ -137,7 +138,7 @@ protected void executeCommand(String command) {
rememberStates();
// Injects a fixed clock before executing a command so that the time stamp shown in the status bar
// after each command is predictable and also different from the previous command.
clockRule.setInjectedClockToCurrentTime();
clockExtension.setInjectedClockToCurrentTime();

mainWindowHandle.getCommandBox().run(command);

Expand Down Expand Up @@ -266,11 +267,11 @@ protected void assertStatusBarUnchanged() {

/**
* Asserts that only the sync status in the status bar was changed to the timing of
* {@code ClockRule#getInjectedClock()}, while the save location remains the same.
* {@code ClockExtension#getInjectedClock()}, while the save location remains the same.
*/
protected void assertStatusBarUnchangedExceptSyncStatus() {
StatusBarFooterHandle handle = getStatusBarFooter();
String timestamp = new Date(clockRule.getInjectedClock().millis()).toString();
String timestamp = new Date(clockExtension.getInjectedClock().millis()).toString();
String expectedSyncStatus = String.format(SYNC_STATUS_UPDATED, timestamp);
assertEquals(expectedSyncStatus, handle.getSyncStatus());
assertFalse(handle.isSaveLocationChanged());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import java.time.Instant;
import java.time.ZoneId;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

import seedu.address.ui.StatusBarFooter;

Expand All @@ -16,31 +16,18 @@
* At the end of the test, the rule restores the original clock.
* @see Clock#fixed(Instant, ZoneId)
*/
public class ClockRule implements TestRule {
public class ClockExtension implements BeforeEachCallback, AfterEachCallback {
private Clock injectedClock;
private final Clock originalClock = StatusBarFooter.getClock();

protected void before() {
setInjectedClockToCurrentTime();
}

protected void after() {
@Override
public void afterEach(ExtensionContext context) throws Exception {
StatusBarFooter.setClock(originalClock);
}

@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
before();
try {
base.evaluate();
} finally {
after();
}
}
};
public void beforeEach(ExtensionContext context) throws Exception {
setInjectedClockToCurrentTime();
}

public Clock getInjectedClock() {
Expand All @@ -54,4 +41,5 @@ public void setInjectedClockToCurrentTime() {
injectedClock = Clock.fixed(Instant.now(), ZoneId.systemDefault());
StatusBarFooter.setClock(injectedClock);
}

}

0 comments on commit c77742a

Please sign in to comment.