Commit 89d98c86 authored by Magnus Schjølberg's avatar Magnus Schjølberg
Browse files

Test new regex solution

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