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
|
* 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.
|
* correctly loading and saving to and from the JSON file.
|
||||||
|
*
|
||||||
* @author Tom Perry [top19]
|
* @author Tom Perry [top19]
|
||||||
* @version 0.1 Initial development.
|
* @version 0.1 Initial development.
|
||||||
* @see JsonProcessing
|
* @see JsonProcessing
|
||||||
|
@ -25,67 +26,85 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class JSONTest {
|
public class JSONTest {
|
||||||
static LinkedList<DictionaryEntry> testList;
|
|
||||||
LinkedList<DictionaryEntry> loadedList;
|
|
||||||
static File testFile;
|
|
||||||
static JsonProcessing processor = new JsonProcessing();
|
|
||||||
|
|
||||||
|
// //////////////// //
|
||||||
|
// Class variables. //
|
||||||
|
// //////////////// //
|
||||||
|
|
||||||
/**
|
static LinkedList<DictionaryEntry> testList;
|
||||||
* 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.
|
LinkedList<DictionaryEntry> loadedList;
|
||||||
* @see JsonProcessing
|
static File testFile;
|
||||||
* @see DictionaryEntry
|
static JsonProcessing processor = new JsonProcessing();
|
||||||
*/
|
|
||||||
@BeforeAll
|
|
||||||
public static void setupTest() {
|
|
||||||
|
|
||||||
// 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),
|
// Class methods. //
|
||||||
new DictionaryEntry("above","uwchben", DictionaryEntry.wordTypeEnum.other), new DictionaryEntry("abroad","dramor", DictionaryEntry.wordTypeEnum.other),
|
// ////////////// //
|
||||||
new DictionaryEntry("abstract","haniaethol", DictionaryEntry.wordTypeEnum.other)));
|
|
||||||
|
|
||||||
// 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.
|
// Populate a test list with DictionaryEntrys that is to be used for the loading/saving tests.
|
||||||
processor.writeOutJson("src/main/java/uk/ac/aber/cs22120/group20/test/jsontest.json", testList);
|
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)));
|
||||||
|
|
||||||
/**
|
// Create a JSON test file in the test package.
|
||||||
* JUnit test to check that the JSON file has been correctly loaded.
|
testFile = new File("src/main/java/uk/ac/aber/cs22120/group20/test/jsontest.json");
|
||||||
* @see JsonProcessing
|
|
||||||
* @see DictionaryEntry
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testLoad(){
|
|
||||||
|
|
||||||
// Load the DictionaryEntry's from testFile and check if the loaded list matches the test list.
|
// Save the testList to the testFile.
|
||||||
loadedList = processor.readInJson(testFile);
|
processor.writeOutJson("src/main/java/uk/ac/aber/cs22120/group20/test/jsontest.json", testList);
|
||||||
assertArrayEquals(testList.toArray(),loadedList.toArray());
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JUnit test to check that any changes to the list of definitions are
|
* Method that is ran after the JUnit tests have finished to remove the JSON test file from program.
|
||||||
* updated and saved to the JSON file accordingly.
|
*/
|
||||||
* @see JsonProcessing
|
@AfterAll
|
||||||
* @see DictionaryEntry
|
public static void deleteFile() {
|
||||||
*/
|
testFile.delete();
|
||||||
@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);
|
// Methods. //
|
||||||
assertArrayEquals(testList.toArray(), loadedList.toArray());
|
// //////// //
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* 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