Просмотр исходного кода

Added check for adding HandlingEvents with the same timestamp. This will now cause a IllegalArgumentExcpetion to be thrown

jorgen_falk 18 лет назад
Родитель
Сommit
4a90b167df

+ 13
- 1
dddsample/src/main/java/se/citerus/dddsample/domain/DeliveryHistory.java Просмотреть файл

@@ -6,6 +6,10 @@ import java.util.TreeSet;
6 6
 import org.apache.commons.lang.builder.ReflectionToStringBuilder;
7 7
 import org.apache.commons.lang.builder.ToStringStyle;
8 8
 
9
+/**
10
+ * A wrapper class that holds a sorted set of HandlingEvents. The set can not contain events with the same timestamp.
11
+ * 
12
+ */
9 13
 public class DeliveryHistory {
10 14
 
11 15
   private final SortedSet<HandlingEvent> events = new TreeSet<HandlingEvent>();
@@ -14,8 +18,16 @@ public class DeliveryHistory {
14 18
     return events;
15 19
   }
16 20
 
21
+  /**
22
+   * Adds the HandlingEvent to the sorted set.
23
+   * 
24
+   * @throws IllegalArgumentException if an event is not unique. Uniquness are evaluated by checking that compareTo() not returns 0.
25
+   * @param event
26
+   */
17 27
   public void addEvent(HandlingEvent event) {
18
-    events.add(event);
28
+    if (!events.add(event)){
29
+      throw new IllegalArgumentException("HandlingEvent are not evaluated to be unique");
30
+    }
19 31
   }
20 32
 
21 33
   public HandlingEvent last() {

+ 6
- 3
dddsample/src/main/java/se/citerus/dddsample/repository/CargoRepositoryInMem.java Просмотреть файл

@@ -1,7 +1,5 @@
1 1
 package se.citerus.dddsample.repository;
2 2
 
3
-import se.citerus.dddsample.domain.*;
4
-
5 3
 import java.text.DateFormat;
6 4
 import java.text.ParseException;
7 5
 import java.text.SimpleDateFormat;
@@ -9,9 +7,14 @@ import java.util.Date;
9 7
 import java.util.HashMap;
10 8
 import java.util.Map;
11 9
 
12
-import org.springframework.dao.DataAccessException;
13 10
 import org.springframework.dao.DataRetrievalFailureException;
14 11
 
12
+import se.citerus.dddsample.domain.Cargo;
13
+import se.citerus.dddsample.domain.CarrierMovement;
14
+import se.citerus.dddsample.domain.HandlingEvent;
15
+import se.citerus.dddsample.domain.Location;
16
+import se.citerus.dddsample.domain.TrackingId;
17
+
15 18
 /**
16 19
  * CargoRepositoryInMem implement the CargoRepository interface but is a test
17 20
  * class not intended for usage in real application.

+ 24
- 0
dddsample/src/test/java/se/citerus/dddsample/domain/DeliveryHistoryTest.java Просмотреть файл

@@ -0,0 +1,24 @@
1
+package se.citerus.dddsample.domain;
2
+
3
+import java.text.DateFormat;
4
+import java.text.SimpleDateFormat;
5
+
6
+import junit.framework.TestCase;
7
+
8
+public class DeliveryHistoryTest extends TestCase {
9
+  public void testAddEventUnique() throws Exception {
10
+    DeliveryHistory dh = new DeliveryHistory();
11
+    
12
+    DateFormat f = new SimpleDateFormat("yyyy-MM-dd");
13
+
14
+    dh.addEvent(new HandlingEvent(f.parse("2010-01-01"), HandlingEvent.Type.RECEIVE, null));
15
+    
16
+    // Expect exception to be thrown when unique events are added (e.g. same timestamp)
17
+    try {
18
+      dh.addEvent(new HandlingEvent(f.parse("2010-01-01"), HandlingEvent.Type.LOAD, null));
19
+      assertFalse(true);
20
+    } catch (RuntimeException e) {
21
+      assertTrue(true);
22
+    }
23
+  }
24
+}