diff options
author | Yuuta Liang <yuutaw@student.cs.ubc.ca> | 2023-11-28 11:33:45 -0800 |
---|---|---|
committer | Yuuta Liang <yuutaw@student.cs.ubc.ca> | 2023-11-28 11:33:45 -0800 |
commit | ca46721f155da0e0fd4d772cdbe4d590cc2f6913 (patch) | |
tree | dfd3f87ee2bbf882c280ad9d998f4cbfa092bce4 | |
parent | 096b6866408da72db37051565f8b5b450df920d7 (diff) | |
download | jca-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>
-rw-r--r-- | src/main/model/Event.java | 66 | ||||
-rw-r--r-- | src/main/model/EventLog.java | 60 | ||||
-rw-r--r-- | src/test/model/EventLogTest.java | 56 | ||||
-rw-r--r-- | src/test/model/EventTest.java | 64 |
4 files changed, 246 insertions, 0 deletions
diff --git a/src/main/model/Event.java b/src/main/model/Event.java new file mode 100644 index 0000000..c6a4993 --- /dev/null +++ b/src/main/model/Event.java @@ -0,0 +1,66 @@ +package model; + +import java.util.Calendar; +import java.util.Date; + + +/** + * Represents an alarm system event. + */ +public class Event { + private static final int HASH_CONSTANT = 13; + private Date dateLogged; + private String description; + + /** + * Creates an event with the given description + * and the current date/time stamp. + * @param description a description of the event + */ + public Event(String description) { + dateLogged = Calendar.getInstance().getTime(); + this.description = description; + } + + /** + * Gets the date of this event (includes time). + * @return the date of the event + */ + public Date getDate() { + return dateLogged; + } + + /** + * Gets the description of this event. + * @return the description of the event + */ + public String getDescription() { + return description; + } + + @Override + public boolean equals(Object other) { + if (other == null) { + return false; + } + + if (other.getClass() != this.getClass()) { + return false; + } + + Event otherEvent = (Event) other; + + return (this.dateLogged.equals(otherEvent.dateLogged) + && this.description.equals(otherEvent.description)); + } + + @Override + public int hashCode() { + return (HASH_CONSTANT * dateLogged.hashCode() + description.hashCode()); + } + + @Override + public String toString() { + return dateLogged.toString() + "\n" + description; + } +} diff --git a/src/main/model/EventLog.java b/src/main/model/EventLog.java new file mode 100644 index 0000000..d0a29b5 --- /dev/null +++ b/src/main/model/EventLog.java @@ -0,0 +1,60 @@ +package model; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; + +/** + * Represents a log of alarm system events. + * We use the Singleton Design Pattern to ensure that there is only + * one EventLog in the system and that the system has global access + * to the single instance of the EventLog. + */ +public class EventLog implements Iterable<Event> { + /** the only EventLog in the system (Singleton Design Pattern) */ + private static EventLog theLog; + private Collection<Event> events; + + /** + * Prevent external construction. + * (Singleton Design Pattern). + */ + private EventLog() { + events = new ArrayList<Event>(); + } + + /** + * Gets instance of EventLog - creates it + * if it doesn't already exist. + * (Singleton Design Pattern) + * @return instance of EventLog + */ + public static EventLog getInstance() { + if (theLog == null) { + theLog = new EventLog(); + } + + return theLog; + } + + /** + * Adds an event to the event log. + * @param e the event to be added + */ + public void logEvent(Event e) { + events.add(e); + } + + /** + * Clears the event log and logs the event. + */ + public void clear() { + events.clear(); + logEvent(new Event("Event log cleared.")); + } + + @Override + public Iterator<Event> iterator() { + return events.iterator(); + } +} 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()); + } +} |