Skip to content

Commit 959f14a

Browse files
committed
Adding an event domain object to represent breaks and pomodoros
1 parent b12a0d9 commit 959f14a

File tree

2 files changed

+199
-0
lines changed
  • PomodoroTests/src/com/nullpointerengineering/android/pomodoro/persistence
  • src/com/nullpointerengineering/android/pomodoro/persistence

2 files changed

+199
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright (c) 2013 Efstratios Xakoustos.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.nullpointerengineering.android.pomodoro.persistence;
18+
19+
import android.test.AndroidTestCase;
20+
import org.joda.time.Duration;
21+
22+
import static com.nullpointerengineering.android.pomodoro.persistence.Event.Type.*;
23+
24+
/**
25+
* Created with IntelliJ IDEA.
26+
* User: Stratos
27+
* Date: 17/03/13
28+
* Time: 10:05 PM
29+
* Event domain object tests
30+
*/
31+
32+
public class EventTests extends AndroidTestCase {
33+
34+
public void testPomodoroType() {
35+
Event event = new Event(0, 0, "pomodoro", 1000, 1000);
36+
assertEquals(POMODORO, event.getType());
37+
}
38+
39+
public void testBreakType() {
40+
Event event = new Event(0, 0, "break", 1000, 1000);
41+
assertEquals(BREAK, event.getType());
42+
}
43+
44+
public void testBadType() {
45+
@SuppressWarnings("ThrowableInstanceNeverThrown")
46+
IllegalArgumentException expected = new IllegalArgumentException("Illegal typeString bad is not valid.");
47+
IllegalArgumentException caught = null;
48+
try {
49+
new Event(0, 0, "bad", 1000, 1000);
50+
} catch (IllegalArgumentException e) {
51+
caught = e;
52+
}
53+
assertNotNull(caught);
54+
assertSame(expected.getClass(), caught.getClass());
55+
assertEquals(expected.getMessage(), caught.getMessage());
56+
}
57+
58+
public void testTotalDuration() {
59+
Event event = new Event(0, 0 ,"pomodoro", 1000, 1000);
60+
Duration expected = new Duration(1000);
61+
assertEquals(expected, event.getTotalDuration());
62+
}
63+
64+
public void testActualDuration() {
65+
Event event = new Event(0, 0 ,"pomodoro", 1000, 1000);
66+
Duration expected = new Duration(1000);
67+
assertEquals(expected, event.getActualDuration());
68+
}
69+
70+
public void testNegativeDuration() {
71+
@SuppressWarnings("ThrowableInstanceNeverThrown")
72+
IllegalArgumentException expected = new IllegalArgumentException("Illegal duration argument -1000");
73+
IllegalArgumentException caught = null;
74+
try {
75+
new Event(0, 0, "pomodoro", - 1000, 1000);
76+
} catch (IllegalArgumentException e) {
77+
caught = e;
78+
}
79+
assertNotNull(caught);
80+
assertSame(expected.getClass(), caught.getClass());
81+
assertEquals(expected.getMessage(), caught.getMessage());
82+
}
83+
84+
public void testNegativeId() {
85+
@SuppressWarnings("ThrowableInstanceNeverThrown")
86+
IllegalArgumentException expected = new IllegalArgumentException("Illegal id -1");
87+
IllegalArgumentException caught = null;
88+
try {
89+
new Event(-1, 0, "pomodoro", 1000, 1000);
90+
} catch (IllegalArgumentException e) {
91+
caught = e;
92+
}
93+
assertNotNull(caught);
94+
assertSame(expected.getClass(), caught.getClass());
95+
assertEquals(expected.getMessage(), caught.getMessage());
96+
}
97+
98+
public void testNegativeCreatedOn() {
99+
@SuppressWarnings("ThrowableInstanceNeverThrown")
100+
IllegalArgumentException expected = new IllegalArgumentException("Illegal crated time -1000");
101+
IllegalArgumentException caught = null;
102+
try {
103+
new Event(0, -1000, "pomodoro", 1000, 1000);
104+
} catch (IllegalArgumentException e) {
105+
caught = e;
106+
}
107+
assertNotNull(caught);
108+
assertSame(expected.getClass(), caught.getClass());
109+
assertEquals(expected.getMessage(), caught.getMessage());
110+
}
111+
112+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2013 Efstratios Xakoustos.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.nullpointerengineering.android.pomodoro.persistence;
18+
19+
import org.joda.time.DateTime;
20+
import org.joda.time.Duration;
21+
22+
/**
23+
* Created with IntelliJ IDEA.
24+
* User: Stratos
25+
* Date: 17/03/13
26+
* Time: 12:42 PM
27+
* Event domain object.
28+
*/
29+
public class Event {
30+
31+
private final long id;
32+
private final Type type;
33+
private final Duration totalDuration;
34+
private final Duration actualDuration;
35+
private final DateTime timeCreated;
36+
37+
public enum Type {
38+
POMODORO,
39+
BREAK;
40+
41+
private static Type getType(String typeName){
42+
if ( typeName.toLowerCase().equals("pomodoro")) {
43+
return POMODORO;
44+
} else if (typeName.toLowerCase().equals("break")) {
45+
return BREAK;
46+
} else {
47+
throw new IllegalArgumentException("Illegal typeString " + typeName + " is not valid." );
48+
}
49+
}
50+
}
51+
52+
Event(long id, long timeCreatedInMillis, String typeString, long totalDurationInMillis, long actualDurationInMillis) {
53+
if (id < 0) throw new IllegalArgumentException("Illegal id " + id);
54+
if (timeCreatedInMillis < 0) throw new IllegalArgumentException("Illegal crated time " + timeCreatedInMillis);
55+
56+
this.id = id;
57+
timeCreated = new DateTime(timeCreatedInMillis * 1000);
58+
type = Type.getType(typeString);
59+
totalDuration = getDuration(totalDurationInMillis);
60+
actualDuration = getDuration(actualDurationInMillis);
61+
}
62+
63+
private Duration getDuration(long duration){
64+
if (duration > 0) return new Duration(duration);
65+
else throw new IllegalArgumentException("Illegal duration argument " + duration);
66+
}
67+
68+
public long getId() {
69+
return id;
70+
}
71+
72+
public Type getType() {
73+
return type;
74+
}
75+
76+
public Duration getTotalDuration() {
77+
return totalDuration;
78+
}
79+
80+
public Duration getActualDuration() {
81+
return actualDuration;
82+
}
83+
84+
public DateTime getTimeCreated() {
85+
return timeCreated;
86+
}
87+
}

0 commit comments

Comments
 (0)