aboutsummaryrefslogtreecommitdiff
path: root/src/test/model/EventTest.java
blob: a74dcd4bdffa99f29556f90a520f2bb5ccd6ba63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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());
    }
}