Commit 485c0e33 authored by Magnus Schjølberg's avatar Magnus Schjølberg
Browse files

Test new regex solution

parent 423d77ba
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
......@@ -13,13 +7,4 @@ 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")));
}
}
......@@ -5,8 +5,10 @@ import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Iterator;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
......@@ -17,6 +19,21 @@ public abstract class ReceiptHandlerTest {
private String adminPassword = "test123";
private String day = "mon";
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
""".replaceAll("\\R", "\n");
private static final String invalid_receipt_file_content = """
Egg, 6 stk;;breakfast
;90.0;diary
;90.0;
;;
Leverpostei;
""".replaceAll("\\R", "\n");
protected abstract IReceiptHandler getReceiptHandler();
private SelfCheckout getPlainTestSelfCheckoutObject() {
......@@ -31,10 +48,17 @@ public abstract class ReceiptHandlerTest {
return testSelfCheckoutObject;
}
@BeforeAll
public void setup() throws IOException {
Files.write(getReceiptHandler().getReceiptFile("test_receipt").toPath(), test_receipt_file_content.getBytes());
Files.write(getReceiptHandler().getReceiptFile("invalid_receipt").toPath(),
invalid_receipt_file_content.getBytes());
}
@Test
public void testReadReceipt() throws IOException {
SelfCheckout expectedSelfCheckoutObject = getFilledTestSelfCheckoutObject();
SelfCheckout actualSelfCheckoutObject = getReceiptHandler().readReceipt("testing/test_receipt",
SelfCheckout actualSelfCheckoutObject = getReceiptHandler().readReceipt("test_receipt",
getPlainTestSelfCheckoutObject());
assertEquals(actualSelfCheckoutObject.getPhoneNumber(), expectedSelfCheckoutObject.getPhoneNumber(),
"Phone number is not the same");
......@@ -58,7 +82,7 @@ public abstract class ReceiptHandlerTest {
public void testLoadNonExistingFile() {
assertThrows(
IOException.class,
() -> getReceiptHandler().readReceipt("testing/non_existing_file", getPlainTestSelfCheckoutObject()),
() -> getReceiptHandler().readReceipt("non_existing_file", getPlainTestSelfCheckoutObject()),
"IOException should be thrown if file does not exist!");
}
......@@ -66,21 +90,23 @@ public abstract class ReceiptHandlerTest {
public void testReadInvalidReceipt() {
assertThrows(
IllegalArgumentException.class,
() -> getReceiptHandler().readReceipt("testing/invalid_receipt", getPlainTestSelfCheckoutObject()),
() -> getReceiptHandler().readReceipt("invalid_receipt", getPlainTestSelfCheckoutObject()),
"Only IllegalArgumentException should be thrown if content of file is invalid!");
}
@Test
public void testWriteReceipt() throws IOException {
getReceiptHandler().writeReceipt("testing/new_receipt", getFilledTestSelfCheckoutObject());
Path expectedFile = getReceiptHandler().getReceiptFile("testing/test_receipt").toPath();
Path actualFile = getReceiptHandler().getReceiptFile("testing/new_receipt").toPath();
getReceiptHandler().writeReceipt("new_receipt", getFilledTestSelfCheckoutObject());
Path expectedFile = getReceiptHandler().getReceiptFile("test_receipt").toPath();
Path actualFile = getReceiptHandler().getReceiptFile("new_receipt").toPath();
assertEquals(-1, Files.mismatch(expectedFile, actualFile),
"Contents of files are not the same");
}
@AfterAll
public void teardown() {
getReceiptHandler().getReceiptFile("testing/new_receipt").delete();
getReceiptHandler().getReceiptFile("new_receipt").delete();
getReceiptHandler().getReceiptFile("test_receipt").delete();
getReceiptHandler().getReceiptFile("invalid_receipt").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