Commit 5eff9353 authored by Magnus Schjølberg's avatar Magnus Schjølberg
Browse files

Update tests for selfcheckout and snakebird to be windows compatible

parent 911ecb63
......@@ -41,7 +41,7 @@ public class HomeFolderReceiptHandler implements IReceiptHandler {
return getReceiptFolderPath().resolve(filename + ".txt").toFile();
}
private static Path getReceiptFolderPath() {
public static Path getReceiptFolderPath() {
return Path.of(System.getProperty("user.home"), "tdt4100files", "receipts");
}
......
package selfcheckout;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import org.junit.jupiter.api.BeforeAll;
public class HomeFolderReceiptHandlerTest extends ReceiptHandlerTest {
@Override
......@@ -7,4 +13,13 @@ public class HomeFolderReceiptHandlerTest extends ReceiptHandlerTest {
return new HomeFolderReceiptHandler();
}
@BeforeAll
public void setup() throws IOException {
Files.createDirectories(HomeFolderReceiptHandler.getReceiptFolderPath().resolve("testing"));
Files.copy(new TemporaryFileReceiptHandler().getReceiptFile("testing/test_receipt").toPath(),
new FileOutputStream(getReceiptHandler().getReceiptFile("testing/test_receipt")));
Files.copy(new TemporaryFileReceiptHandler().getReceiptFile("testing/invalid_receipt").toPath(),
new FileOutputStream(getReceiptHandler().getReceiptFile("testing/invalid_receipt")));
}
}
......@@ -8,28 +8,12 @@ import java.nio.file.Path;
import java.util.Iterator;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public abstract class ReceiptHandlerTest {
private static final String test_receipt_file_content = """
null
Egg, 6 stk;41.59;breakfast
Hvitost, Synnøve;90.0;diary
Leverpostei;29.99;breakfast
""";;
private static final String invalid_receipt_file_content = """
Egg, 6 stk;;breakfast
;90.0;diary
;90.0;
;;
Leverpostei;
""";;
private String adminPassword = "test123";
private String day = "mon";
......@@ -47,16 +31,10 @@ public abstract class ReceiptHandlerTest {
return testSelfCheckoutObject;
}
@BeforeAll
public void setup() throws IOException {
Files.write(getReceiptHandler().getReceiptPath("test_receipt"), test_receipt_file_content.getBytes());
Files.write(getReceiptHandler().getReceiptPath("invalid_receipt"), invalid_receipt_file_content.getBytes());
}
@Test
public void testReadReceipt() throws IOException {
SelfCheckout expectedSelfCheckoutObject = getFilledTestSelfCheckoutObject();
SelfCheckout actualSelfCheckoutObject = getReceiptHandler().readReceipt("test_receipt",
SelfCheckout actualSelfCheckoutObject = getReceiptHandler().readReceipt("testing/test_receipt",
getPlainTestSelfCheckoutObject());
assertEquals(actualSelfCheckoutObject.getPhoneNumber(), expectedSelfCheckoutObject.getPhoneNumber(),
"Phone number is not the same");
......@@ -80,7 +58,7 @@ public abstract class ReceiptHandlerTest {
public void testLoadNonExistingFile() {
assertThrows(
IOException.class,
() -> getReceiptHandler().readReceipt("non_existing_file", getPlainTestSelfCheckoutObject()),
() -> getReceiptHandler().readReceipt("testing/non_existing_file", getPlainTestSelfCheckoutObject()),
"IOException should be thrown if file does not exist!");
}
......@@ -88,23 +66,21 @@ public abstract class ReceiptHandlerTest {
public void testReadInvalidReceipt() {
assertThrows(
IllegalArgumentException.class,
() -> getReceiptHandler().readReceipt("invalid_receipt", getPlainTestSelfCheckoutObject()),
() -> getReceiptHandler().readReceipt("testing/invalid_receipt", getPlainTestSelfCheckoutObject()),
"Only IllegalArgumentException should be thrown if content of file is invalid!");
}
@Test
public void testWriteReceipt() throws IOException {
getReceiptHandler().writeReceipt("new_receipt", getFilledTestSelfCheckoutObject());
Path expectedFile = getReceiptHandler().getReceiptPath("test_receipt");
Path actualFile = getReceiptHandler().getReceiptPath("new_receipt");
assertEquals(Files.mismatch(expectedFile, actualFile), -1,
getReceiptHandler().writeReceipt("testing/new_receipt", getFilledTestSelfCheckoutObject());
Path expectedFile = getReceiptHandler().getReceiptFile("testing/test_receipt").toPath();
Path actualFile = getReceiptHandler().getReceiptFile("testing/new_receipt").toPath();
assertEquals(-1, Files.mismatch(expectedFile, actualFile),
"Contents of files are not the same");
}
@AfterAll
public void teardown() {
getReceiptHandler().getReceiptPath("new_receipt").toFile().delete();
getReceiptHandler().getReceiptPath("test_receipt").toFile().delete();
getReceiptHandler().getReceiptPath("invalid_receipt").toFile().delete();
getReceiptHandler().getReceiptFile("testing/new_receipt").delete();
}
}
......@@ -10,7 +10,7 @@ import java.util.Scanner;
public class SaveHandler implements ISaveHandler {
public void save(String filename, Game game) throws FileNotFoundException {
try (PrintWriter writer = new PrintWriter(new File(getFilePath(filename)))) {
try (PrintWriter writer = new PrintWriter(getFile(filename))) {
writer.println(game.getWidth());
writer.println(game.getHeight());
writer.println(game.isGameOver());
......@@ -34,7 +34,7 @@ public class SaveHandler implements ISaveHandler {
}
public Game load(String filename) throws FileNotFoundException {
try (Scanner scanner = new Scanner(new File(getFilePath(filename)))) {
try (Scanner scanner = new Scanner(getFile(filename))) {
int width = scanner.nextInt();
int height = scanner.nextInt();
Game game = new Game(width, height);
......@@ -68,8 +68,8 @@ public class SaveHandler implements ISaveHandler {
}
}
public static String getFilePath(String filename) {
return SaveHandler.class.getResource("saves/").getFile() + filename + ".txt";
public static File getFile(String filename) {
return new File(SaveHandler.class.getResource("saves/").getFile() + filename + ".txt");
}
}
......@@ -10,7 +10,6 @@ import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
......@@ -88,13 +87,13 @@ public class SaveHandlerTest {
byte[] testFile = null, newFile = null;
try {
testFile = Files.readAllBytes(Path.of(SaveHandler.getFilePath("test-save")));
testFile = Files.readAllBytes(SaveHandler.getFile("test-save").toPath());
} catch (IOException e) {
fail("Could not load test file");
}
try {
newFile = Files.readAllBytes(Path.of(SaveHandler.getFilePath("test-save-new")));
newFile = Files.readAllBytes(SaveHandler.getFile("test-save-new").toPath());
} catch (IOException e) {
fail("Could not load saved file");
}
......@@ -106,7 +105,7 @@ public class SaveHandlerTest {
@AfterAll
static void teardown() {
File newTestSaveFile = new File(SaveHandler.getFilePath("test-save-new"));
File newTestSaveFile = SaveHandler.getFile("test-save-new");
newTestSaveFile.delete();
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment