Bladeren bron

Fixed bug, cargo without any handling events is not misdirected (thi scase previously caused NPE).

Patrik Fredriksson 18 jaren geleden
bovenliggende
commit
497da164a5

+ 16
- 5
dddsample/src/main/java/se/citerus/dddsample/domain/Cargo.java Bestand weergeven

@@ -108,11 +108,22 @@ public class Cargo {
108 108
   }
109 109
 
110 110
   /**
111
-   * @return True if the cargo has been misdirected,
112
-   *         that is if the cargo is in a location that's not in the itinerary.
111
+   * Check if cargo is misdirected.
112
+   * <p/>
113
+   * <ul>
114
+   * <li>A cargo is misdirected if it is in a location that's not in the itinerary.
115
+   * <li>A cargo with no itinerary can not be misdirected.
116
+   * <li>A cargo that has received no handling events can not be misdirected.
117
+   * </ul>
118
+   *
119
+   * @return <code>true</code> if the cargo has been misdirected,
113 120
    */
114 121
   public boolean isMisdirected() {
115
-    return itinerary != null && !itinerary.isExpected(deliveryHistory.lastEvent());
122
+    final HandlingEvent lastEvent = deliveryHistory.lastEvent();
123
+    if (itinerary == null || lastEvent == null)
124
+      return false;
125
+
126
+    return !itinerary.isExpected(lastEvent);
116 127
   }
117 128
 
118 129
   public Itinerary itinerary() {
@@ -127,12 +138,12 @@ public class Cargo {
127 138
   /**
128 139
    * Entities compare by identity, therefore the trackingId field is the only basis of comparison. For persistence we
129 140
    * have an id field, but it is not used for identiy comparison.
130
-   *
141
+   * <p/>
131 142
    * Compare this behavior to the value object {@link se.citerus.dddsample.domain.Leg#sameValueAs(Leg)}
132 143
    *
133 144
    * @param other The other cargo.
134 145
    * @return <code>true</code> if the given cargo's and this cargos's trackingId is the same, regardles of other
135
-   * attributes.
146
+   *         attributes.
136 147
    */
137 148
   private boolean sameIdentityAs(Cargo other) {
138 149
     return trackingId.equals(other.trackingId);

+ 8
- 1
dddsample/src/test/java/se/citerus/dddsample/domain/CargoTest.java Bestand weergeven

@@ -184,7 +184,14 @@ public class CargoTest extends TestCase {
184 184
 
185 185
   public void testIsMisdirected() throws Exception {
186 186
 
187
-    Cargo cargo = setUpCargoWithItinerary(shanghai, rotterdam, goteborg);
187
+    //A cargo with no itinerary is not misdirected
188
+    Cargo cargo = new Cargo(new TrackingId("TRKID"));
189
+    assertFalse(cargo.isMisdirected());
190
+
191
+    cargo = setUpCargoWithItinerary(shanghai, rotterdam, goteborg);
192
+
193
+    //A cargo with no handling events is not misdirected
194
+    assertFalse(cargo.isMisdirected());
188 195
 
189 196
     Collection<HandlingEvent> handlingEvents = new ArrayList<HandlingEvent>();
190 197