parent
5d4ed5441b
commit
155dee3a55
1 changed files with 71 additions and 52 deletions
|
@ -18,6 +18,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
|||
/**
|
||||
* Class that contains methods which will be used to test that the JSON package classes are
|
||||
* correctly loading and saving to and from the JSON file.
|
||||
*
|
||||
* @author Tom Perry [top19]
|
||||
* @version 0.1 Initial development.
|
||||
* @see JsonProcessing
|
||||
|
@ -25,67 +26,85 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
|||
*/
|
||||
|
||||
public class JSONTest {
|
||||
static LinkedList<DictionaryEntry> testList;
|
||||
LinkedList<DictionaryEntry> loadedList;
|
||||
static File testFile;
|
||||
static JsonProcessing processor = new JsonProcessing();
|
||||
|
||||
// //////////////// //
|
||||
// Class variables. //
|
||||
// //////////////// //
|
||||
|
||||
/**
|
||||
* Setup method that is run before all of the tests, setting up a test list of DictionaryEntry's that it saved to a JSON test file.
|
||||
* @see JsonProcessing
|
||||
* @see DictionaryEntry
|
||||
*/
|
||||
@BeforeAll
|
||||
public static void setupTest() {
|
||||
static LinkedList<DictionaryEntry> testList;
|
||||
LinkedList<DictionaryEntry> loadedList;
|
||||
static File testFile;
|
||||
static JsonProcessing processor = new JsonProcessing();
|
||||
|
||||
// Populate a test list with DictionaryEntrys that is to be used for the loading/saving tests.
|
||||
testList = new LinkedList<>(Arrays.asList(new DictionaryEntry("abbey","abaty", DictionaryEntry.wordTypeEnum.other), new DictionaryEntry("about to", "ar fin", DictionaryEntry.wordTypeEnum.other),
|
||||
new DictionaryEntry("above","uwchben", DictionaryEntry.wordTypeEnum.other), new DictionaryEntry("abroad","dramor", DictionaryEntry.wordTypeEnum.other),
|
||||
new DictionaryEntry("abstract","haniaethol", DictionaryEntry.wordTypeEnum.other)));
|
||||
// ////////////// //
|
||||
// Class methods. //
|
||||
// ////////////// //
|
||||
|
||||
// Create a JSON test file in the test package.
|
||||
testFile = new File("src/main/java/uk/ac/aber/cs22120/group20/test/jsontest.json");
|
||||
/**
|
||||
* Setup method that is run before all of the tests, setting up a test list of DictionaryEntry's that it saved to a JSON test file.
|
||||
*
|
||||
* @see JsonProcessing
|
||||
* @see DictionaryEntry
|
||||
*/
|
||||
@BeforeAll
|
||||
public static void setupTest() {
|
||||
|
||||
// Save the testList to the testFile.
|
||||
processor.writeOutJson("src/main/java/uk/ac/aber/cs22120/group20/test/jsontest.json", testList);
|
||||
}
|
||||
// Populate a test list with DictionaryEntrys that is to be used for the loading/saving tests.
|
||||
testList = new LinkedList<>(Arrays.asList(new DictionaryEntry("abbey","abaty", DictionaryEntry.wordTypeEnum.other), new DictionaryEntry("about to", "ar fin", DictionaryEntry.wordTypeEnum.other),
|
||||
new DictionaryEntry("above","uwchben", DictionaryEntry.wordTypeEnum.other), new DictionaryEntry("abroad","dramor", DictionaryEntry.wordTypeEnum.other),
|
||||
new DictionaryEntry("abstract","haniaethol", DictionaryEntry.wordTypeEnum.other)));
|
||||
|
||||
/**
|
||||
* JUnit test to check that the JSON file has been correctly loaded.
|
||||
* @see JsonProcessing
|
||||
* @see DictionaryEntry
|
||||
*/
|
||||
@Test
|
||||
public void testLoad(){
|
||||
// Create a JSON test file in the test package.
|
||||
testFile = new File("src/main/java/uk/ac/aber/cs22120/group20/test/jsontest.json");
|
||||
|
||||
// Load the DictionaryEntry's from testFile and check if the loaded list matches the test list.
|
||||
loadedList = processor.readInJson(testFile);
|
||||
assertArrayEquals(testList.toArray(),loadedList.toArray());
|
||||
}
|
||||
// Save the testList to the testFile.
|
||||
processor.writeOutJson("src/main/java/uk/ac/aber/cs22120/group20/test/jsontest.json", testList);
|
||||
}
|
||||
|
||||
/**
|
||||
* JUnit test to check that any changes to the list of definitions are
|
||||
* updated and saved to the JSON file accordingly.
|
||||
* @see JsonProcessing
|
||||
* @see DictionaryEntry
|
||||
*/
|
||||
@Test public void testSave(){
|
||||
// Add an additional word to the testList and save it to jsontest.
|
||||
testList.add(new DictionaryEntry("beer", "cwrw", DictionaryEntry.wordTypeEnum.nm));
|
||||
processor.writeOutJson("src/main/java/uk/ac/aber/cs22120/group20/test/jsontest.json", testList);
|
||||
/**
|
||||
* Method that is ran after the JUnit tests have finished to remove the JSON test file from program.
|
||||
*/
|
||||
@AfterAll
|
||||
public static void deleteFile() {
|
||||
testFile.delete();
|
||||
}
|
||||
|
||||
// Load the DictionaryEntry's back from the file and check that they match the testList.
|
||||
loadedList = processor.readInJson(testFile);
|
||||
assertArrayEquals(testList.toArray(), loadedList.toArray());
|
||||
}
|
||||
// //////// //
|
||||
// Methods. //
|
||||
// //////// //
|
||||
|
||||
/**
|
||||
* JUnit test to check that the JSON file has been correctly loaded. This works by loading the test file and check
|
||||
* its contents match the list that was saved to it.
|
||||
*
|
||||
* @see JsonProcessing
|
||||
* @see DictionaryEntry
|
||||
*/
|
||||
@Test
|
||||
public void testLoad(){
|
||||
|
||||
// Load the DictionaryEntry's from testFile and check if the loaded list matches the test list.
|
||||
loadedList = processor.readInJson(testFile);
|
||||
assertArrayEquals(testList.toArray(),loadedList.toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* JUnit test to check that changes to the list of definitions are
|
||||
* updated and saved to the JSON file accordingly. This is done by adding a new item to the JSON test list and
|
||||
* saving it to the file before reloading it to check the loaded list matches the updated test list.
|
||||
*
|
||||
* @see JsonProcessing
|
||||
* @see DictionaryEntry
|
||||
*/
|
||||
@Test public void testSave(){
|
||||
// Add an additional word to the testList and save it to jsontest.
|
||||
testList.add(new DictionaryEntry("beer", "cwrw", DictionaryEntry.wordTypeEnum.nm));
|
||||
processor.writeOutJson("src/main/java/uk/ac/aber/cs22120/group20/test/jsontest.json", testList);
|
||||
|
||||
// Load the DictionaryEntry's back from the file and check that they match the testList.
|
||||
loadedList = processor.readInJson(testFile);
|
||||
assertArrayEquals(testList.toArray(), loadedList.toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method that is ran after the JUnit tests have finished to remove the JSON test file from program.
|
||||
*/
|
||||
@AfterAll
|
||||
public static void deleteFile() {
|
||||
testFile.delete();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Reference in a new issue