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

Commit

Permalink
TemporaryFolder: Migrate to JUnit 5 TempDir API
Browse files Browse the repository at this point in the history
We use JUnit 4's TemporaryFolder rule to initialise a temporary
folder.

Rules are no longer supported by JUnit 5 test runners. In addition,
JUnit 5.4.0 introduces the TempDir extension that supersedes the
TemporaryFolder rule.

Let's migrate our usage of TemporaryFolder rules to TempDir.

As TempDirs in JUnit 5 are Paths, it is no longer necessary to invoke
methods like getRoot().toPath() on the TempDir. Additionally, the way
to get a URL resource from the TempDir is also different from
TemporaryFolder. The relevant methods have been updated.

In LogicManagerTest and MainWindowCloseTest, the newFile() API is not
supported by JUnit 5's TempDir. However, as the random file name
provided by newFile() is not necessary (we have full control over the
contents of the temporary directory), instead of re-implementing
newFile() to match the behavior of existing tests, we explicitly give
the file names and use Path.resolve() instead.
  • Loading branch information
sijie123 committed Apr 30, 2019
1 parent 03c5617 commit fb7dfce
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 58 deletions.
12 changes: 5 additions & 7 deletions src/test/java/seedu/address/commons/util/ConfigUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
import java.util.Optional;
import java.util.logging.Level;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import seedu.address.commons.core.Config;
import seedu.address.commons.exceptions.DataConversionException;
Expand All @@ -21,9 +20,8 @@ public class ConfigUtilTest {

private static final Path TEST_DATA_FOLDER = Paths.get("src", "test", "data", "ConfigUtilTest");


@Rule
public TemporaryFolder testFolder = new TemporaryFolder();
@TempDir
public Path tempDir;

@Test
public void read_null_throwsNullPointerException() {
Expand Down Expand Up @@ -89,7 +87,7 @@ public void save_nullFile_throwsNullPointerException() {
public void saveConfig_allInOrder_success() throws DataConversionException, IOException {
Config original = getTypicalConfig();

Path configFilePath = testFolder.getRoot().toPath().resolve("TempConfig.json");
Path configFilePath = tempDir.resolve("TempConfig.json");

//Try writing when the file doesn't exist
ConfigUtil.saveConfig(original, configFilePath);
Expand Down
27 changes: 14 additions & 13 deletions src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
import java.io.IOException;
import java.nio.file.Path;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.CommandResult;
Expand All @@ -37,16 +36,17 @@
public class LogicManagerTest {
private static final IOException DUMMY_IO_EXCEPTION = new IOException("dummy exception");

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@TempDir
public Path temporaryFolder;

private Model model = new ModelManager();
private Logic logic;

@Before
public void setUp() throws Exception {
JsonAddressBookStorage addressBookStorage = new JsonAddressBookStorage(temporaryFolder.newFile().toPath());
JsonUserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(temporaryFolder.newFile().toPath());
@BeforeEach
public void setUp() {
JsonAddressBookStorage addressBookStorage =
new JsonAddressBookStorage(temporaryFolder.resolve("addressBook.json"));
JsonUserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(temporaryFolder.resolve("userPrefs.json"));
StorageManager storage = new StorageManager(addressBookStorage, userPrefsStorage);
logic = new LogicManager(model, storage);
}
Expand All @@ -73,11 +73,12 @@ public void execute_validCommand_success() throws Exception {
}

@Test
public void execute_storageThrowsIoException_throwsCommandException() throws Exception {
public void execute_storageThrowsIoException_throwsCommandException() {
// Setup LogicManager with JsonAddressBookIoExceptionThrowingStub
JsonAddressBookStorage addressBookStorage =
new JsonAddressBookIoExceptionThrowingStub(temporaryFolder.newFile().toPath());
JsonUserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(temporaryFolder.newFile().toPath());
new JsonAddressBookIoExceptionThrowingStub(temporaryFolder.resolve("ioExceptionAddressBook.json"));
JsonUserPrefsStorage userPrefsStorage =
new JsonUserPrefsStorage(temporaryFolder.resolve("ioExceptionUserPrefs.json"));
StorageManager storage = new StorageManager(addressBookStorage, userPrefsStorage);
logic = new LogicManager(model, storage);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import seedu.address.commons.exceptions.DataConversionException;
import seedu.address.model.AddressBook;
Expand All @@ -23,9 +22,8 @@
public class JsonAddressBookStorageTest {
private static final Path TEST_DATA_FOLDER = Paths.get("src", "test", "data", "JsonAddressBookStorageTest");


@Rule
public TemporaryFolder testFolder = new TemporaryFolder();
@TempDir
public Path testFolder;

@Test
public void readAddressBook_nullFilePath_throwsNullPointerException() {
Expand Down Expand Up @@ -64,7 +62,7 @@ public void readAddressBook_invalidAndValidPersonAddressBook_throwDataConversion

@Test
public void readAndSaveAddressBook_allInOrder_success() throws Exception {
Path filePath = testFolder.getRoot().toPath().resolve("TempAddressBook.json");
Path filePath = testFolder.resolve("TempAddressBook.json");
AddressBook original = getTypicalAddressBook();
JsonAddressBookStorage jsonAddressBookStorage = new JsonAddressBookStorage(filePath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
import java.nio.file.Paths;
import java.util.Optional;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.exceptions.DataConversionException;
Expand All @@ -21,9 +20,8 @@ public class JsonUserPrefsStorageTest {

private static final Path TEST_DATA_FOLDER = Paths.get("src", "test", "data", "JsonUserPrefsStorageTest");


@Rule
public TemporaryFolder testFolder = new TemporaryFolder();
@TempDir
public Path testFolder;

@Test
public void readUserPrefs_nullFilePath_throwsNullPointerException() {
Expand Down Expand Up @@ -107,7 +105,7 @@ public void saveUserPrefs_allInOrder_success() throws DataConversionException, I
UserPrefs original = new UserPrefs();
original.setGuiSettings(new GuiSettings(1200, 200, 0, 2));

Path pefsFilePath = testFolder.getRoot().toPath().resolve("TempPrefs.json");
Path pefsFilePath = testFolder.resolve("TempPrefs.json");
JsonUserPrefsStorage jsonUserPrefsStorage = new JsonUserPrefsStorage(pefsFilePath);

//Try writing when the file doesn't exist
Expand Down
16 changes: 7 additions & 9 deletions src/test/java/seedu/address/storage/StorageManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

import java.nio.file.Path;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import seedu.address.commons.core.GuiSettings;
import seedu.address.model.AddressBook;
Expand All @@ -18,23 +17,22 @@

public class StorageManagerTest {

@Rule
public TemporaryFolder testFolder = new TemporaryFolder();
@TempDir
public Path testFolder;

private StorageManager storageManager;

@Before
@BeforeEach
public void setUp() {
JsonAddressBookStorage addressBookStorage = new JsonAddressBookStorage(getTempFilePath("ab"));
JsonUserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(getTempFilePath("prefs"));
storageManager = new StorageManager(addressBookStorage, userPrefsStorage);
}

private Path getTempFilePath(String fileName) {
return testFolder.getRoot().toPath().resolve(fileName);
return testFolder.resolve(fileName);
}


@Test
public void prefsReadSave() throws Exception {
/*
Expand Down
19 changes: 10 additions & 9 deletions src/test/java/seedu/address/ui/MainWindowCloseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.nio.file.Path;
import java.util.Collections;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.testfx.api.FxToolkit;

import guitests.guihandles.HelpWindowHandle;
Expand All @@ -25,17 +25,18 @@
* Contains tests for closing of the {@code MainWindow}.
*/
public class MainWindowCloseTest extends GuiUnitTest {
@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
@TempDir
public Path temporaryFolder;

private MainWindow mainWindow;
private EmptyMainWindowHandle mainWindowHandle;
private Stage stage;

@Before
@BeforeEach
public void setUp() throws Exception {
JsonAddressBookStorage jsonAddressBookStorage = new JsonAddressBookStorage(temporaryFolder.newFile().toPath());
JsonUserPrefsStorage jsonUserPrefsStorage = new JsonUserPrefsStorage(temporaryFolder.newFile().toPath());
JsonAddressBookStorage jsonAddressBookStorage =
new JsonAddressBookStorage(temporaryFolder.resolve("addressBook.json"));
JsonUserPrefsStorage jsonUserPrefsStorage = new JsonUserPrefsStorage(temporaryFolder.resolve("userPrefs.json"));
StorageManager storageManager = new StorageManager(jsonAddressBookStorage, jsonUserPrefsStorage);
FxToolkit.setupStage(stage -> {
this.stage = stage;
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/seedu/address/ui/UiPartTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import static seedu.address.testutil.Assert.assertThrows;

import java.net.URL;
import java.nio.file.Path;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import javafx.fxml.FXML;
import seedu.address.MainApp;
Expand All @@ -21,8 +21,8 @@ public class UiPartTest {
private static final String VALID_FILE_WITH_FX_ROOT_PATH = "UiPartTest/validFileWithFxRoot.fxml";
private static final TestFxmlObject VALID_FILE_ROOT = new TestFxmlObject("Hello World!");

@Rule
public TemporaryFolder testFolder = new TemporaryFolder();
@TempDir
public Path testFolder;

@Test
public void constructor_nullFileUrl_throwsNullPointerException() {
Expand All @@ -32,7 +32,7 @@ public void constructor_nullFileUrl_throwsNullPointerException() {

@Test
public void constructor_missingFileUrl_throwsAssertionError() throws Exception {
URL missingFileUrl = new URL(testFolder.getRoot().toURI().toURL(), MISSING_FILE_PATH);
URL missingFileUrl = new URL(testFolder.toUri().toURL(), MISSING_FILE_PATH);
assertThrows(AssertionError.class, () -> new TestUiPart<Object>(missingFileUrl));
assertThrows(AssertionError.class, () -> new TestUiPart<Object>(missingFileUrl, new Object()));
}
Expand Down

0 comments on commit fb7dfce

Please sign in to comment.