aboutsummaryrefslogtreecommitdiff
path: root/src/test/model
diff options
context:
space:
mode:
authorYuuta Liang <yuutaw@student.cs.ubc.ca>2023-11-28 11:33:45 -0800
committerYuuta Liang <yuutaw@student.cs.ubc.ca>2023-11-28 11:33:45 -0800
commitca46721f155da0e0fd4d772cdbe4d590cc2f6913 (patch)
treedfd3f87ee2bbf882c280ad9d998f4cbfa092bce4 /src/test/model
parent096b6866408da72db37051565f8b5b450df920d7 (diff)
downloadjca-ca46721f155da0e0fd4d772cdbe4d590cc2f6913.tar
jca-ca46721f155da0e0fd4d772cdbe4d590cc2f6913.tar.gz
jca-ca46721f155da0e0fd4d772cdbe4d590cc2f6913.tar.bz2
jca-ca46721f155da0e0fd4d772cdbe4d590cc2f6913.zip
Import Event / EventLog
Signed-off-by: Yuuta Liang <yuutaw@student.cs.ubc.ca>
Diffstat (limited to 'src/test/model')
-rw-r--r--src/test/model/EventLogTest.java56
-rw-r--r--src/test/model/EventTest.java64
2 files changed, 120 insertions, 0 deletions
diff --git a/src/test/model/EventLogTest.java b/src/test/model/EventLogTest.java
new file mode 100644
index 0000000..948aa4e
--- /dev/null
+++ b/src/test/model/EventLogTest.java
@@ -0,0 +1,56 @@
+package model;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Unit tests for the EventLog class
+ */
+public class EventLogTest {
+ private Event e1;
+ private Event e2;
+ private Event e3;
+
+ @BeforeEach
+ public void loadEvents() {
+ e1 = new Event("A1");
+ e2 = new Event("A2");
+ e3 = new Event("A3");
+ EventLog el = EventLog.getInstance();
+ el.logEvent(e1);
+ el.logEvent(e2);
+ el.logEvent(e3);
+ }
+
+ @Test
+ public void testLogEvent() {
+ List<Event> l = new ArrayList<Event>();
+
+ EventLog el = EventLog.getInstance();
+ for (Event next : el) {
+ l.add(next);
+ }
+
+ assertTrue(l.contains(e1));
+ assertTrue(l.contains(e2));
+ assertTrue(l.contains(e3));
+ }
+
+ @Test
+ public void testClear() {
+ EventLog el = EventLog.getInstance();
+ el.clear();
+ Iterator<Event> itr = el.iterator();
+ assertTrue(itr.hasNext()); // After log is cleared, the clear log event is added
+ assertEquals("Event log cleared.", itr.next().getDescription());
+ assertFalse(itr.hasNext());
+ }
+}
diff --git a/src/test/model/EventTest.java b/src/test/model/EventTest.java
new file mode 100644
index 0000000..a74dcd4
--- /dev/null
+++ b/src/test/model/EventTest.java
@@ -0,0 +1,64 @@
+package model;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.Calendar;
+import java.util.Date;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+/**
+ * Unit tests for the Event class
+ */
+public class EventTest {
+ private Event e;
+ private Date d;
+
+ //NOTE: these tests might fail if time at which line (2) below is executed
+ //is different from time that line (1) is executed. Lines (1) and (2) must
+ //run in same millisecond for this test to make sense and pass.
+
+ @BeforeEach
+ public void runBefore() {
+ e = new Event("Sensor open at door"); // (1)
+ d = Calendar.getInstance().getTime(); // (2)
+ }
+
+ @Test
+ public void testEvent() {
+ assertEquals("Sensor open at door", e.getDescription());
+ assertEquals(d, e.getDate());
+ }
+
+ @Test
+ public void testToString() {
+ assertEquals(d.toString() + "\n" + "Sensor open at door", e.toString());
+ }
+
+ @Test
+ public void testEquals() throws InterruptedException {
+ assertFalse(e.equals(null));
+ assertFalse(e.equals(this));
+ assertFalse(e.equals(new Event("114514!!!")));
+ // Assuming the process doesn't get descheduled too long ...
+ // Much better to mock here.
+ final Event e1 = new Event("114514");
+ final Event e2 = new Event("114514");
+ Thread.sleep(1000);
+ final Event e3 = new Event("114514");
+ assertTrue(e1.equals(e2));
+ assertFalse(e1.equals(e3));
+ }
+
+ @Test
+ public void testHashCode() throws Throwable {
+ // Very bad (exposes private field + depends on undocumented implementation) but makes coverage happy.
+ final java.lang.reflect.Field f = Event.class.getDeclaredField("HASH_CONSTANT");
+ f.setAccessible(true);
+ assertEquals((int) f.get(null)
+ * e.getDate().hashCode()
+ + e.getDescription().hashCode(),
+ e.hashCode());
+ }
+}