Procházet zdrojové kódy

Tried to make the domain classes more internally consistent and created test classes for those domain classes that didn't have one.

peter_backlund před 18 roky
rodič
revize
e09a8daa7b
21 změnil soubory, kde provedl 240 přidání a 134 odebrání
  1. 12
    8
      dddsample/src/main/java/se/citerus/dddsample/domain/Cargo.java
  2. 10
    11
      dddsample/src/main/java/se/citerus/dddsample/domain/CarrierMovement.java
  3. 2
    0
      dddsample/src/main/java/se/citerus/dddsample/domain/CarrierMovementId.java
  4. 9
    7
      dddsample/src/main/java/se/citerus/dddsample/domain/DeliveryHistory.java
  5. 19
    35
      dddsample/src/main/java/se/citerus/dddsample/domain/HandlingEvent.java
  6. 6
    4
      dddsample/src/main/java/se/citerus/dddsample/domain/Itinerary.java
  7. 25
    11
      dddsample/src/main/java/se/citerus/dddsample/domain/Leg.java
  8. 4
    1
      dddsample/src/main/java/se/citerus/dddsample/domain/Location.java
  9. 2
    2
      dddsample/src/main/java/se/citerus/dddsample/domain/StatusCode.java
  10. 4
    2
      dddsample/src/main/java/se/citerus/dddsample/domain/TrackingId.java
  11. 2
    2
      dddsample/src/main/java/se/citerus/dddsample/domain/UnLocode.java
  12. 4
    4
      dddsample/src/main/resources/messages_en.properties
  13. 9
    9
      dddsample/src/test/java/se/citerus/dddsample/domain/CargoTest.java
  14. 14
    0
      dddsample/src/test/java/se/citerus/dddsample/domain/CarrierMovementIdTest.java
  15. 55
    0
      dddsample/src/test/java/se/citerus/dddsample/domain/CarrierMovementTest.java
  16. 19
    17
      dddsample/src/test/java/se/citerus/dddsample/domain/DeliveryHistoryTest.java
  17. 11
    15
      dddsample/src/test/java/se/citerus/dddsample/domain/HandlingEventTest.java
  18. 5
    5
      dddsample/src/test/java/se/citerus/dddsample/domain/ItineraryTest.java
  19. 13
    0
      dddsample/src/test/java/se/citerus/dddsample/domain/LegTest.java
  20. 14
    0
      dddsample/src/test/java/se/citerus/dddsample/domain/TrackingIdTest.java
  21. 1
    1
      dddsample/src/test/java/se/citerus/dddsample/repository/HandlingEventRepositoryTest.java

+ 12
- 8
dddsample/src/main/java/se/citerus/dddsample/domain/Cargo.java Zobrazit soubor

@@ -2,6 +2,7 @@ package se.citerus.dddsample.domain;
2 2
 
3 3
 import org.apache.commons.lang.builder.ReflectionToStringBuilder;
4 4
 import org.apache.commons.lang.builder.ToStringStyle;
5
+import org.apache.commons.lang.Validate;
5 6
 
6 7
 import javax.persistence.*;
7 8
 
@@ -33,12 +34,14 @@ public class Cargo {
33 34
 
34 35
   //TODO Remove this constructor
35 36
   public Cargo(TrackingId trackingId, Location origin, Location destination) {
37
+    Validate.noNullElements(new Object[] {trackingId, origin, destination});
36 38
     this.trackingId = trackingId;
37 39
     this.origin = origin;
38 40
     this.destination = destination;
39 41
   }
40 42
 
41 43
   public Cargo(TrackingId trackingId) {
44
+    Validate.notNull(trackingId);
42 45
     this.trackingId = trackingId;
43 46
   }
44 47
 
@@ -130,11 +133,6 @@ public class Cargo {
130 133
     return this.itinerary;
131 134
   }
132 135
 
133
-  @Override
134
-  public String toString() {
135
-    return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
136
-  }
137
-
138 136
   /**
139 137
    * Entities compare by identity, therefore the trackingId field is the only basis of comparison. For persistence we
140 138
    * have an id field, but it is not used for identiy comparison.
@@ -151,7 +149,8 @@ public class Cargo {
151 149
 
152 150
   /**
153 151
    * @param object to compare
154
-   * @return True if tracking ids are equal.
152
+   * @return True if they have the same identity
153
+   * @see #sameIdentityAs(Cargo)
155 154
    */
156 155
   @Override
157 156
   public boolean equals(Object object) {
@@ -170,7 +169,12 @@ public class Cargo {
170 169
     return trackingId.hashCode();
171 170
   }
172 171
 
173
-  // Needed by Hibernate
174
-  protected Cargo() {
172
+  @Override
173
+  public String toString() {
174
+    return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
175
+  }
176
+
177
+  Cargo() {
178
+    // Needed by Hibernate
175 179
   }
176 180
 }

+ 10
- 11
dddsample/src/main/java/se/citerus/dddsample/domain/CarrierMovement.java Zobrazit soubor

@@ -1,5 +1,8 @@
1 1
 package se.citerus.dddsample.domain;
2 2
 
3
+import org.apache.commons.lang.builder.HashCodeBuilder;
4
+import org.apache.commons.lang.Validate;
5
+
3 6
 import javax.persistence.*;
4 7
 
5 8
 
@@ -23,6 +26,7 @@ public class CarrierMovement {
23 26
   private Location to;
24 27
 
25 28
   public CarrierMovement(CarrierMovementId carrierMovementId, Location from, Location to) {
29
+    Validate.noNullElements(new Object[] {carrierMovementId, from, to});
26 30
     this.carrierMovementId = carrierMovementId;
27 31
     this.from = from;
28 32
     this.to = to;
@@ -52,14 +56,11 @@ public class CarrierMovement {
52 56
    * Compare this behavior to the value object {@link se.citerus.dddsample.domain.Leg#sameValueAs(Leg)}
53 57
    *
54 58
    * @param other The other cargo.
55
-   * @return <code>true</code> if the given carrier movement's and this carrier movement's carrierId is the same,
59
+   * @return <code>true</code> if the given carrier movement's and this carrier movement's carrier id are the same,
56 60
    *         regardles of other attributes.
57 61
    */
58 62
   public boolean sameIdentityAs(CarrierMovement other) {
59
-    if (carrierMovementId != null ? !carrierMovementId.equals(other.carrierMovementId) : other.carrierMovementId != null)
60
-      return false;
61
-
62
-    return true;
63
+    return carrierMovementId.equals(other.carrierMovementId);
63 64
   }
64 65
 
65 66
   @Override
@@ -72,13 +73,11 @@ public class CarrierMovement {
72 73
     return sameIdentityAs(that);
73 74
   }
74 75
 
76
+  /**
77
+   * @return Hashcode of carrier movement id.
78
+   */
75 79
   @Override
76 80
   public int hashCode() {
77
-    int result;
78
-    result = (id != null ? id.hashCode() : 0);
79
-    result = 31 * result + (carrierMovementId != null ? carrierMovementId.hashCode() : 0);
80
-    result = 31 * result + (from != null ? from.hashCode() : 0);
81
-    result = 31 * result + (to != null ? to.hashCode() : 0);
82
-    return result;
81
+    return carrierMovementId.hashCode();
83 82
   }
84 83
 }

+ 2
- 0
dddsample/src/main/java/se/citerus/dddsample/domain/CarrierMovementId.java Zobrazit soubor

@@ -2,6 +2,7 @@ package se.citerus.dddsample.domain;
2 2
 
3 3
 import org.apache.commons.lang.builder.EqualsBuilder;
4 4
 import org.apache.commons.lang.builder.HashCodeBuilder;
5
+import org.apache.commons.lang.Validate;
5 6
 
6 7
 import javax.persistence.Column;
7 8
 import javax.persistence.Embeddable;
@@ -17,6 +18,7 @@ public class CarrierMovementId {
17 18
   private String id;
18 19
 
19 20
   public CarrierMovementId(String id) {
21
+    Validate.notNull(id);
20 22
     this.id = id;
21 23
   }
22 24
 

+ 9
- 7
dddsample/src/main/java/se/citerus/dddsample/domain/DeliveryHistory.java Zobrazit soubor

@@ -64,27 +64,27 @@ public class DeliveryHistory {
64 64
 
65 65
   public StatusCode status() {
66 66
     if (lastEvent() == null)
67
-      return StatusCode.notReceived;
67
+      return StatusCode.NOT_RECIEVED;
68 68
 
69 69
     HandlingEvent.Type type = lastEvent().type();
70 70
     if (type == HandlingEvent.Type.LOAD)
71
-      return StatusCode.onBoardCarrier;
71
+      return StatusCode.ONBOARD_CARRIER;
72 72
 
73 73
     if (type == HandlingEvent.Type.UNLOAD)
74
-      return StatusCode.inPort;
74
+      return StatusCode.IN_PORT;
75 75
 
76 76
     if (type == HandlingEvent.Type.RECEIVE)
77
-      return StatusCode.inPort;
77
+      return StatusCode.IN_PORT;
78 78
 
79 79
     if (type == HandlingEvent.Type.CLAIM)
80
-      return StatusCode.claimed;
80
+      return StatusCode.CLAIMED;
81 81
 
82 82
     //TODO: What about Type.CUSTOMS?
83 83
     return null;
84 84
   }
85 85
 
86 86
   public Location currentLocation() {
87
-    if (status().equals(StatusCode.inPort)) {
87
+    if (status().equals(StatusCode.IN_PORT)) {
88 88
       return lastEvent().location();
89 89
     } else {
90 90
       return null;
@@ -92,11 +92,13 @@ public class DeliveryHistory {
92 92
   }
93 93
 
94 94
   public CarrierMovement currentCarrierMovement() {
95
-    if (status().equals(StatusCode.onBoardCarrier)) {
95
+    if (status().equals(StatusCode.ONBOARD_CARRIER)) {
96 96
       return lastEvent().carrierMovement();
97 97
     } else {
98 98
       return null;
99 99
     }
100 100
   }
101 101
 
102
+  // TODO: what about equals, hashCode, toString?
103
+
102 104
 }

+ 19
- 35
dddsample/src/main/java/se/citerus/dddsample/domain/HandlingEvent.java Zobrazit soubor

@@ -1,5 +1,9 @@
1 1
 package se.citerus.dddsample.domain;
2 2
 
3
+import org.apache.commons.lang.builder.EqualsBuilder;
4
+import org.apache.commons.lang.builder.HashCodeBuilder;
5
+import org.apache.commons.lang.Validate;
6
+
3 7
 import javax.persistence.*;
4 8
 import java.util.Comparator;
5 9
 import java.util.Date;
@@ -75,25 +79,6 @@ public class HandlingEvent {
75 79
 
76 80
 
77 81
   /**
78
-   * Constructor for events that do not have a carrier movement associated.
79
-   *
80
-   * @param cargo            cargo
81
-   * @param completionTime   completion time, the reported time that the event actually happened (e.g. the receive took place).
82
-   * @param registrationTime registration time, the time the message is received
83
-   * @param type             type of event. Legal values are CLAIM, RECEIVE and CUSTOMS
84
-   * @param location         where the event took place
85
-   */
86
-  public HandlingEvent(Cargo cargo, Date completionTime, Date registrationTime, Type type, Location location) {
87
-    this.registrationTime = registrationTime;
88
-    this.completionTime = completionTime;
89
-    this.type = type;
90
-    this.cargo = cargo;
91
-    this.location = location;
92
-
93
-    validateType();
94
-  }
95
-
96
-    /**
97 82
    * Constructor for events that have a carrier movement associated. The location where
98 83
    * the event took place is derived from the carrier movement: if the type of event is LOAD,
99 84
    * the location is the starting point of the movement, if the type is UNLOAD the location
@@ -107,6 +92,7 @@ public class HandlingEvent {
107 92
    * @param carrierMovement  carrier movement.
108 93
    */
109 94
   public HandlingEvent(Cargo cargo, Date completionTime, Date registrationTime, Type type, Location location, CarrierMovement carrierMovement) {
95
+    Validate.noNullElements(new Object[] {cargo, completionTime, registrationTime, type, location});
110 96
     this.registrationTime = registrationTime;
111 97
     this.completionTime = completionTime;
112 98
     this.type = type;
@@ -166,26 +152,24 @@ public class HandlingEvent {
166 152
    * @return <code>true</code> if the given handling event and this event are regarded as the same.
167 153
    */
168 154
   public boolean sameEventAs(HandlingEvent other) {
169
-    if (cargo != null ? !cargo.equals(other.cargo) : other.cargo != null) return false;
170
-    if (carrierMovement != null ? !carrierMovement.equals(other.carrierMovement) : other.carrierMovement != null)
171
-      return false;
172
-    if (completionTime != null ? !completionTime.equals(other.completionTime) : other.completionTime != null)
173
-      return false;
174
-    if (location != null ? !location.equals(other.location) : other.location != null) return false;
175
-    if (type != other.type) return false;
176
-
177
-    return true;
155
+    return other != null && new EqualsBuilder().
156
+      append(this.cargo, other.cargo).
157
+      append(this.carrierMovement, other.carrierMovement).
158
+      append(this.completionTime, other.completionTime).
159
+      append(this.location, other.location).
160
+      append(this.type, other.type).
161
+      isEquals();
178 162
   }
179 163
 
180 164
   @Override
181 165
   public int hashCode() {
182
-    int result;
183
-    result = (type != null ? type.hashCode() : 0);
184
-    result = 31 * result + (carrierMovement != null ? carrierMovement.hashCode() : 0);
185
-    result = 31 * result + (location != null ? location.hashCode() : 0);
186
-    result = 31 * result + (completionTime != null ? completionTime.hashCode() : 0);
187
-    result = 31 * result + (cargo != null ? cargo.hashCode() : 0);
188
-    return result;
166
+    return new HashCodeBuilder(13,41).
167
+      append(cargo).
168
+      append(carrierMovement).
169
+      append(completionTime).
170
+      append(location).
171
+      append(type).
172
+      toHashCode();
189 173
   }
190 174
 
191 175
   /**

+ 6
- 4
dddsample/src/main/java/se/citerus/dddsample/domain/Itinerary.java Zobrazit soubor

@@ -79,10 +79,12 @@ public class Itinerary {
79 79
     return true;
80 80
   }
81 81
 
82
-  private boolean sameValueAs(Itinerary other) {
83
-    if (!legs.equals(other.legs)) return false;
84
-
85
-    return true;
82
+  /**
83
+   * @param other itinerary to compare
84
+   * @return <code>true</code> if the legs in this and the other itinerary are all equal.
85
+   */
86
+  public boolean sameValueAs(Itinerary other) {
87
+    return legs.equals(other.legs);
86 88
   }
87 89
 
88 90
   @Override

+ 25
- 11
dddsample/src/main/java/se/citerus/dddsample/domain/Leg.java Zobrazit soubor

@@ -1,11 +1,21 @@
1 1
 package se.citerus.dddsample.domain;
2 2
 
3
+import org.apache.commons.lang.builder.HashCodeBuilder;
4
+import org.apache.commons.lang.builder.ReflectionToStringBuilder;
5
+import org.apache.commons.lang.builder.ToStringStyle;
6
+import org.apache.commons.lang.builder.EqualsBuilder;
7
+import org.apache.commons.lang.Validate;
8
+
9
+/**
10
+ * An itinerary consists of one or more legs.
11
+ */
3 12
 public class Leg {
4 13
   private CarrierMovementId carrierMovementId;
5 14
   private Location from;
6 15
   private Location to;
7 16
 
8 17
   public Leg(CarrierMovementId carrierMovementId, Location from, Location to) {
18
+    Validate.noNullElements(new Object[] {carrierMovementId, from, to});
9 19
     this.carrierMovementId = carrierMovementId;
10 20
     this.from = from;
11 21
     this.to = to;
@@ -34,12 +44,11 @@ public class Leg {
34 44
    * @return <code>true</code> if the given leg's and this leg's attributes are the same.
35 45
    */
36 46
   public boolean sameValueAs(Leg other) {
37
-    if (carrierMovementId != null ? !carrierMovementId.equals(other.carrierMovementId) : other.carrierMovementId != null)
38
-      return false;
39
-    if (from != null ? !from.equals(other.from) : other.from != null) return false;
40
-    if (to != null ? !to.equals(other.to) : other.to != null) return false;
41
-
42
-    return true;
47
+    return other != null && new EqualsBuilder().
48
+      append(this.carrierMovementId, other.carrierMovementId).
49
+      append(this.from, other.from).
50
+      append(this.to, other.to).
51
+      isEquals();
43 52
   }
44 53
 
45 54
   @Override
@@ -54,10 +63,15 @@ public class Leg {
54 63
 
55 64
   @Override
56 65
   public int hashCode() {
57
-    int result;
58
-    result = (carrierMovementId != null ? carrierMovementId.hashCode() : 0);
59
-    result = 31 * result + (from != null ? from.hashCode() : 0);
60
-    result = 31 * result + (to != null ? to.hashCode() : 0);
61
-    return result;
66
+    return HashCodeBuilder.reflectionHashCode(this);
67
+  }
68
+
69
+  @Override
70
+  public String toString() {
71
+    return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
72
+  }
73
+
74
+  Leg() {
75
+    // Needed by Hibernate
62 76
   }
63 77
 }

+ 4
- 1
dddsample/src/main/java/se/citerus/dddsample/domain/Location.java Zobrazit soubor

@@ -32,6 +32,7 @@ public class Location {
32 32
    * @throws IllegalArgumentException if the UN Locode or name is null
33 33
    */
34 34
   public Location(UnLocode unLocode, String name) {
35
+    Validate.noNullElements(new Object[] {unLocode, name});
35 36
     // TODO:
36 37
     // It shouldn't really be possible to create a new location -
37 38
     // it should only be looked up in the location repository.
@@ -101,11 +102,13 @@ public class Location {
101 102
    */
102 103
   @Override
103 104
   public String toString() {
105
+    // TODO: this is presentation logic and very inconsistent, move to DTO assembler
104 106
     return unLocode.idString() + " (" + name + ")";
105 107
   }
106 108
 
107
-  // Needed by Hibernate
109
+
108 110
   Location() {
111
+    // Needed by Hibernate
109 112
   }
110 113
 
111 114
 }

+ 2
- 2
dddsample/src/main/java/se/citerus/dddsample/domain/StatusCode.java Zobrazit soubor

@@ -1,8 +1,8 @@
1 1
 package se.citerus.dddsample.domain;
2 2
 
3 3
 /**
4
- *
4
+ * These are the different status codes for a cargo.
5 5
  */
6 6
 public enum StatusCode {
7
-  notReceived, inPort, onBoardCarrier, claimed
7
+    NOT_RECIEVED, IN_PORT, ONBOARD_CARRIER, CLAIMED
8 8
 }

+ 4
- 2
dddsample/src/main/java/se/citerus/dddsample/domain/TrackingId.java Zobrazit soubor

@@ -40,7 +40,9 @@ public class TrackingId {
40 40
     return HashCodeBuilder.reflectionHashCode(this);
41 41
   }
42 42
 
43
-  // Needed by Hibernate
44
-  protected TrackingId() {}
43
+
44
+  TrackingId() {
45
+    // Needed by Hibernate
46
+  }
45 47
 
46 48
 }

+ 2
- 2
dddsample/src/main/java/se/citerus/dddsample/domain/UnLocode.java Zobrazit soubor

@@ -32,7 +32,7 @@ public class UnLocode {
32 32
   }
33 33
 
34 34
   private void validateArgs(String countryCode, String locationCode) {
35
-    Validate.noNullElements(Arrays.asList(countryCode, locationCode),
35
+    Validate.noNullElements(new Object[] {countryCode, locationCode},
36 36
             "Neither country code nor location code may be null");
37 37
     Validate.isTrue(countryCodePattern.matcher(countryCode).matches(),
38 38
       "\"" + countryCode + "\" is not a valid country code");
@@ -57,7 +57,7 @@ public class UnLocode {
57 57
     return HashCodeBuilder.reflectionHashCode(this);
58 58
   }
59 59
 
60
-  // Needed by Hibernate
61 60
   UnLocode() {
61
+    // Needed by Hibernate
62 62
   }
63 63
 }

+ 4
- 4
dddsample/src/main/resources/messages_en.properties Zobrazit soubor

@@ -1,4 +1,4 @@
1
-cargo.status.notReceived=Not received
2
-cargo.status.inPort=In port
3
-cargo.status.onBoardCarrier=Onboard carrier
4
-cargo.status.claimed=Claimed
1
+cargo.status.NOT_RECEIVED=Not received
2
+cargo.status.IN_PORT=In port
3
+cargo.status.ONBOARD_CARRIER=Onboard carrier
4
+cargo.status.CLAIMED=Claimed

+ 9
- 9
dddsample/src/test/java/se/citerus/dddsample/domain/CargoTest.java Zobrazit soubor

@@ -86,7 +86,7 @@ public class CargoTest extends TestCase {
86 86
   private Cargo populateCargoReceivedStockholm() throws Exception {
87 87
     final Cargo cargo = new Cargo(new TrackingId("XYZ"), stockholm, melbourne);
88 88
 
89
-    HandlingEvent he = new HandlingEvent(cargo, getDate("2007-12-01"), new Date(), HandlingEvent.Type.RECEIVE, stockholm);
89
+    HandlingEvent he = new HandlingEvent(cargo, getDate("2007-12-01"), new Date(), HandlingEvent.Type.RECEIVE, stockholm, null);
90 90
     cargo.deliveryHistory().addEvent(he);
91 91
 
92 92
     return cargo;
@@ -95,7 +95,7 @@ public class CargoTest extends TestCase {
95 95
   private Cargo populateCargoClaimedMelbourne() throws Exception {
96 96
     final Cargo cargo = populateCargoOffMelbourne();
97 97
 
98
-    cargo.deliveryHistory().addEvent(new HandlingEvent(cargo, getDate("2007-12-09"), new Date(), HandlingEvent.Type.CLAIM, melbourne));
98
+    cargo.deliveryHistory().addEvent(new HandlingEvent(cargo, getDate("2007-12-09"), new Date(), HandlingEvent.Type.CLAIM, melbourne, null));
99 99
 
100 100
     return cargo;
101 101
   }
@@ -200,13 +200,13 @@ public class CargoTest extends TestCase {
200 200
     CarrierMovement ghi = new CarrierMovement(new CarrierMovementId("GHI"), rotterdam, nyc);
201 201
 
202 202
     //Happy path
203
-    handlingEvents.add(new HandlingEvent(cargo, new Date(10), new Date(20), HandlingEvent.Type.RECEIVE, shanghai));
203
+    handlingEvents.add(new HandlingEvent(cargo, new Date(10), new Date(20), HandlingEvent.Type.RECEIVE, shanghai, null));
204 204
     handlingEvents.add(new HandlingEvent(cargo, new Date(30), new Date(40), HandlingEvent.Type.LOAD, shanghai, abc));
205 205
     handlingEvents.add(new HandlingEvent(cargo, new Date(50), new Date(60), HandlingEvent.Type.UNLOAD, rotterdam, abc));
206 206
     handlingEvents.add(new HandlingEvent(cargo, new Date(70), new Date(80), HandlingEvent.Type.LOAD, rotterdam, def));
207 207
     handlingEvents.add(new HandlingEvent(cargo, new Date(90), new Date(100), HandlingEvent.Type.UNLOAD, goteborg, def));
208
-    handlingEvents.add(new HandlingEvent(cargo, new Date(110), new Date(120), HandlingEvent.Type.CLAIM, goteborg));
209
-    handlingEvents.add(new HandlingEvent(cargo, new Date(130), new Date(140), HandlingEvent.Type.CUSTOMS, goteborg));
208
+    handlingEvents.add(new HandlingEvent(cargo, new Date(110), new Date(120), HandlingEvent.Type.CLAIM, goteborg, null));
209
+    handlingEvents.add(new HandlingEvent(cargo, new Date(130), new Date(140), HandlingEvent.Type.CUSTOMS, goteborg, null));
210 210
 
211 211
     cargo.deliveryHistory().addAllEvents(handlingEvents);
212 212
     assertFalse(cargo.isMisdirected());
@@ -216,7 +216,7 @@ public class CargoTest extends TestCase {
216 216
     cargo = setUpCargoWithItinerary(shanghai, rotterdam, goteborg);
217 217
     handlingEvents = new ArrayList<HandlingEvent>();
218 218
 
219
-    handlingEvents.add(new HandlingEvent(cargo, new Date(), new Date(), HandlingEvent.Type.RECEIVE, hangzhou));
219
+    handlingEvents.add(new HandlingEvent(cargo, new Date(), new Date(), HandlingEvent.Type.RECEIVE, hangzhou, null));
220 220
     cargo.deliveryHistory().addAllEvents(handlingEvents);
221 221
     assertTrue(cargo.isMisdirected());
222 222
 
@@ -224,7 +224,7 @@ public class CargoTest extends TestCase {
224 224
     cargo = setUpCargoWithItinerary(shanghai, rotterdam, goteborg);
225 225
     handlingEvents = new ArrayList<HandlingEvent>();
226 226
 
227
-    handlingEvents.add(new HandlingEvent(cargo, new Date(10), new Date(20), HandlingEvent.Type.RECEIVE, shanghai));
227
+    handlingEvents.add(new HandlingEvent(cargo, new Date(10), new Date(20), HandlingEvent.Type.RECEIVE, shanghai, null));
228 228
     handlingEvents.add(new HandlingEvent(cargo, new Date(30), new Date(40), HandlingEvent.Type.LOAD, shanghai, abc));
229 229
     handlingEvents.add(new HandlingEvent(cargo, new Date(50), new Date(60), HandlingEvent.Type.UNLOAD, rotterdam, abc));
230 230
     handlingEvents.add(new HandlingEvent(cargo, new Date(70), new Date(80), HandlingEvent.Type.LOAD, rotterdam, ghi));
@@ -236,10 +236,10 @@ public class CargoTest extends TestCase {
236 236
     cargo = setUpCargoWithItinerary(shanghai, rotterdam, goteborg);
237 237
     handlingEvents = new ArrayList<HandlingEvent>();
238 238
 
239
-    handlingEvents.add(new HandlingEvent(cargo, new Date(10), new Date(20), HandlingEvent.Type.RECEIVE, shanghai));
239
+    handlingEvents.add(new HandlingEvent(cargo, new Date(10), new Date(20), HandlingEvent.Type.RECEIVE, shanghai, null));
240 240
     handlingEvents.add(new HandlingEvent(cargo, new Date(30), new Date(40), HandlingEvent.Type.LOAD, shanghai, abc));
241 241
     handlingEvents.add(new HandlingEvent(cargo, new Date(50), new Date(60), HandlingEvent.Type.UNLOAD, rotterdam, abc));
242
-    handlingEvents.add(new HandlingEvent(cargo, new Date(), new Date(), HandlingEvent.Type.CLAIM, rotterdam));
242
+    handlingEvents.add(new HandlingEvent(cargo, new Date(), new Date(), HandlingEvent.Type.CLAIM, rotterdam, null));
243 243
 
244 244
     cargo.deliveryHistory().addAllEvents(handlingEvents);
245 245
     assertTrue(cargo.isMisdirected());

+ 14
- 0
dddsample/src/test/java/se/citerus/dddsample/domain/CarrierMovementIdTest.java Zobrazit soubor

@@ -0,0 +1,14 @@
1
+package se.citerus.dddsample.domain;
2
+
3
+import junit.framework.TestCase;
4
+
5
+public class CarrierMovementIdTest extends TestCase {
6
+
7
+  public void testConstructor() throws Exception {
8
+    try {
9
+      new CarrierMovementId(null);
10
+      fail("Should not accept null constructor argument");
11
+    } catch (IllegalArgumentException expected) {}
12
+  }
13
+
14
+}

+ 55
- 0
dddsample/src/test/java/se/citerus/dddsample/domain/CarrierMovementTest.java Zobrazit soubor

@@ -0,0 +1,55 @@
1
+package se.citerus.dddsample.domain;
2
+
3
+import junit.framework.TestCase;
4
+
5
+public class CarrierMovementTest extends TestCase {
6
+
7
+  Location stockholm = new Location(new UnLocode("SE", "STO"), "Stockholm");
8
+  Location hamburg = new Location(new UnLocode("DE", "HAM"), "Hamburg");
9
+
10
+  public void testConstructor() throws Exception {
11
+    CarrierMovementId id = new CarrierMovementId("CAR001");
12
+
13
+    try {
14
+      new CarrierMovement(null, null, null);
15
+      fail("Should not accept null constructor arguments");
16
+    } catch (IllegalArgumentException expected) {}
17
+
18
+    try {
19
+      new CarrierMovement(id, null, null);
20
+      fail("Should not accept null constructor arguments");
21
+    } catch (IllegalArgumentException expected) {}
22
+
23
+    try {
24
+      new CarrierMovement(id, stockholm, null);
25
+      fail("Should not accept null constructor arguments");
26
+    } catch (IllegalArgumentException expected) {}
27
+
28
+    // Legal
29
+    new CarrierMovement(id, stockholm, hamburg);
30
+  }
31
+
32
+  public void testSameValueAsEqualsHashCode() throws Exception {
33
+    CarrierMovementId id1 = new CarrierMovementId("CAR1");
34
+    CarrierMovementId id2a = new CarrierMovementId("CAR2");
35
+    CarrierMovementId id2b = new CarrierMovementId("CAR2");
36
+
37
+    CarrierMovement cm1 = new CarrierMovement(id1, stockholm, hamburg);
38
+    CarrierMovement cm2 = new CarrierMovement(id1, stockholm, hamburg);
39
+    CarrierMovement cm3 = new CarrierMovement(id2a, hamburg, stockholm);
40
+    CarrierMovement cm4 = new CarrierMovement(id2b, hamburg, stockholm);
41
+
42
+    assertTrue(cm1.sameIdentityAs(cm2));
43
+    assertFalse(cm2.sameIdentityAs(cm3));
44
+    assertTrue(cm3.sameIdentityAs(cm4));
45
+    
46
+    assertTrue(cm1.equals(cm2));
47
+    assertFalse(cm2.equals(cm3));
48
+    assertTrue(cm3.equals(cm4));
49
+
50
+    assertTrue(cm1.hashCode() == cm2.hashCode());
51
+    assertFalse(cm2.hashCode() == cm3.hashCode());
52
+    assertTrue(cm3.hashCode() == cm4.hashCode());
53
+  }
54
+
55
+}

+ 19
- 17
dddsample/src/test/java/se/citerus/dddsample/domain/DeliveryHistoryTest.java Zobrazit soubor

@@ -8,19 +8,21 @@ import java.util.*;
8 8
 
9 9
 public class DeliveryHistoryTest extends TestCase {
10 10
   private Location ham = new Location(new UnLocode("DE", "HAM"), "Hamburg");
11
+  private Location from = new Location(new UnLocode("FR", "OMX"), "From");
12
+  private Location to = new Location(new UnLocode("TO", "XXX"), "To");
13
+  private Cargo cargo = new Cargo(new TrackingId("XYZ"), from, to);
14
+
11 15
 
12 16
   public void testEvensOrderedByTimeOccured() throws Exception {
13 17
     DeliveryHistory dh = new DeliveryHistory();
14 18
     assertTrue(dh.eventsOrderedByCompletionTime().isEmpty());
15 19
 
16 20
     DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
17
-    final Location from = new Location(new UnLocode("FR", "OMX"), "From");
18
-    final Location to = new Location(new UnLocode("TO", "XXX"), "To");
19 21
     CarrierMovement carrierMovement = new CarrierMovement(new CarrierMovementId("CAR_001"), from, to);
20
-    HandlingEvent he1 = new HandlingEvent(null, df.parse("2010-01-03"), new Date(), HandlingEvent.Type.RECEIVE, to);
21
-    HandlingEvent he2 = new HandlingEvent(null, df.parse("2010-01-01"), new Date(), HandlingEvent.Type.LOAD, to, carrierMovement);
22
-    HandlingEvent he3 = new HandlingEvent(null, df.parse("2010-01-04"), new Date(), HandlingEvent.Type.CLAIM, from);
23
-    HandlingEvent he4 = new HandlingEvent(null, df.parse("2010-01-02"), new Date(), HandlingEvent.Type.UNLOAD, from, carrierMovement);
22
+    HandlingEvent he1 = new HandlingEvent(cargo, df.parse("2010-01-03"), new Date(), HandlingEvent.Type.RECEIVE, to, null);
23
+    HandlingEvent he2 = new HandlingEvent(cargo, df.parse("2010-01-01"), new Date(), HandlingEvent.Type.LOAD, to, carrierMovement);
24
+    HandlingEvent he3 = new HandlingEvent(cargo, df.parse("2010-01-04"), new Date(), HandlingEvent.Type.CLAIM, from, null);
25
+    HandlingEvent he4 = new HandlingEvent(cargo, df.parse("2010-01-02"), new Date(), HandlingEvent.Type.UNLOAD, from, carrierMovement);
24 26
     dh.addAllEvents(Arrays.asList(he1, he2, he3, he4));
25 27
 
26 28
     List<HandlingEvent> orderEvents = dh.eventsOrderedByCompletionTime();
@@ -34,20 +36,20 @@ public class DeliveryHistoryTest extends TestCase {
34 36
   public void testCargoStatusFromLastHandlingEvent() {
35 37
     DeliveryHistory deliveryHistory = new DeliveryHistory();
36 38
 
37
-    assertEquals(StatusCode.notReceived, deliveryHistory.status());
39
+    assertEquals(StatusCode.NOT_RECIEVED, deliveryHistory.status());
38 40
 
39
-    deliveryHistory.addEvent(new HandlingEvent(null, new Date(10), null, HandlingEvent.Type.RECEIVE, ham));
40
-    assertEquals(StatusCode.inPort, deliveryHistory.status());
41
+    deliveryHistory.addEvent(new HandlingEvent(cargo, new Date(10), new Date(11), HandlingEvent.Type.RECEIVE, ham, null));
42
+    assertEquals(StatusCode.IN_PORT, deliveryHistory.status());
41 43
 
42 44
     CarrierMovement carrierMovement = new CarrierMovement(new CarrierMovementId("ABC"), ham, ham);
43
-    deliveryHistory.addEvent(new HandlingEvent(null, new Date(20), null, HandlingEvent.Type.LOAD, ham, carrierMovement));
44
-    assertEquals(StatusCode.onBoardCarrier, deliveryHistory.status());
45
+    deliveryHistory.addEvent(new HandlingEvent(cargo, new Date(20), new Date(21), HandlingEvent.Type.LOAD, ham, carrierMovement));
46
+    assertEquals(StatusCode.ONBOARD_CARRIER, deliveryHistory.status());
45 47
 
46
-    deliveryHistory.addEvent(new HandlingEvent(null, new Date(30), null, HandlingEvent.Type.UNLOAD, ham, carrierMovement));
47
-    assertEquals(StatusCode.inPort, deliveryHistory.status());
48
+    deliveryHistory.addEvent(new HandlingEvent(cargo, new Date(30), new Date(31), HandlingEvent.Type.UNLOAD, ham, carrierMovement));
49
+    assertEquals(StatusCode.IN_PORT, deliveryHistory.status());
48 50
 
49
-    deliveryHistory.addEvent(new HandlingEvent(null, new Date(40), null, HandlingEvent.Type.CLAIM, ham));
50
-    assertEquals(StatusCode.claimed, deliveryHistory.status());
51
+    deliveryHistory.addEvent(new HandlingEvent(cargo, new Date(40), new Date(41), HandlingEvent.Type.CLAIM, ham, null));
52
+    assertEquals(StatusCode.CLAIMED, deliveryHistory.status());
51 53
   }
52 54
 
53 55
   public void testCurrentLocation() throws Exception {
@@ -55,11 +57,11 @@ public class DeliveryHistoryTest extends TestCase {
55 57
 
56 58
     assertNull(deliveryHistory.currentLocation());
57 59
 
58
-    deliveryHistory.addEvent(new HandlingEvent(null, new Date(10), null, HandlingEvent.Type.RECEIVE, ham));
60
+    deliveryHistory.addEvent(new HandlingEvent(cargo, new Date(10), new Date(11), HandlingEvent.Type.RECEIVE, ham, null));
59 61
     assertEquals(ham, deliveryHistory.currentLocation());
60 62
 
61 63
     CarrierMovement carrierMovement = new CarrierMovement(new CarrierMovementId("ABC"), ham, ham);
62
-    deliveryHistory.addEvent(new HandlingEvent(null, new Date(20), null, HandlingEvent.Type.LOAD, ham, carrierMovement));
64
+    deliveryHistory.addEvent(new HandlingEvent(cargo, new Date(20), new Date(21), HandlingEvent.Type.LOAD, ham, carrierMovement));
63 65
     assertNull(deliveryHistory.currentLocation());
64 66
   }
65 67
 

+ 11
- 15
dddsample/src/test/java/se/citerus/dddsample/domain/HandlingEventTest.java Zobrazit soubor

@@ -12,9 +12,9 @@ public class HandlingEventTest extends TestCase {
12 12
   private final Location finalDestination = new Location(new UnLocode("TO","YYY"), "To");
13 13
   private final Location a5 = new Location(new UnLocode("AA","AAA"), "AAAAA");
14 14
   private final Location b5 = new Location(new UnLocode("BB","BBB"), "BBBBB");
15
+  private final Cargo cargo = new Cargo(new TrackingId("XYZ"), origin, finalDestination);
15 16
 
16 17
   public void testNewWithCarrierMovement() throws Exception {
17
-    Cargo cargo = new Cargo(new TrackingId("XYZ"), origin, finalDestination);
18 18
     CarrierMovement carrierMovement = new CarrierMovement(new CarrierMovementId("C01"), origin, finalDestination);
19 19
 
20 20
     HandlingEvent e1 = new HandlingEvent(cargo, new Date(), new Date(), LOAD, origin, carrierMovement);
@@ -34,27 +34,23 @@ public class HandlingEventTest extends TestCase {
34 34
       // These event types requires a carrier movement association
35 35
     for (Type type : asList(LOAD, UNLOAD)) {
36 36
         try {
37
-          new HandlingEvent(cargo, new Date(), new Date(), type, origin);
37
+          new HandlingEvent(cargo, new Date(), new Date(), type, origin, null);
38 38
             fail("Handling event type " + type + " requires carrier movement");
39 39
         } catch (IllegalArgumentException expected) {}
40 40
     }
41 41
   }
42 42
 
43 43
   public void testNewWithLocation() throws Exception {
44
-    Location origin = this.origin;
45
-    Location finalDestination = this.finalDestination;
46
-    Cargo cargo = new Cargo(new TrackingId("XYZ"), origin, finalDestination);
47
-
48 44
     Location location = new Location(new UnLocode("FO","OOO"), "Foo");
49
-    HandlingEvent e1 = new HandlingEvent(cargo, new Date(), new Date(), Type.CLAIM, location);
45
+    HandlingEvent e1 = new HandlingEvent(cargo, new Date(), new Date(), Type.CLAIM, location, null);
50 46
     assertEquals(location, e1.location());
51 47
   }
52 48
 
53 49
   public void testCurrentLocationLoadEvent() throws Exception {
54 50
     CarrierMovementId carrierMovementId = new CarrierMovementId("CAR_001");
55 51
     CarrierMovement cm = new CarrierMovement(carrierMovementId, a5, b5);
56
-    
57
-    HandlingEvent ev = new HandlingEvent(null, new Date(), new Date(), LOAD, a5, cm);
52
+
53
+    HandlingEvent ev = new HandlingEvent(cargo, new Date(), new Date(), LOAD, a5, cm);
58 54
     
59 55
     assertEquals(a5, ev.location());
60 56
   }
@@ -62,19 +58,19 @@ public class HandlingEventTest extends TestCase {
62 58
   public void testCurrentLocationUnloadEvent() throws Exception {
63 59
     CarrierMovementId carrierMovementId = new CarrierMovementId("CAR_001");
64 60
     CarrierMovement cm = new CarrierMovement(carrierMovementId, a5, b5);
65
-    
66
-    HandlingEvent ev = new HandlingEvent(null, new Date(), new Date(), UNLOAD, b5, cm);
61
+
62
+    HandlingEvent ev = new HandlingEvent(cargo, new Date(), new Date(), UNLOAD, b5, cm);
67 63
     
68 64
     assertEquals(b5, ev.location());
69 65
   }
70 66
   
71 67
   public void testCurrentLocationReceivedEvent() throws Exception {
72
-    HandlingEvent ev = new HandlingEvent(null, new Date(), new Date(), RECEIVE, a5);
68
+    HandlingEvent ev = new HandlingEvent(cargo, new Date(), new Date(), RECEIVE, a5, null);
73 69
 
74 70
     assertEquals(a5, ev.location());
75 71
   }
76 72
   public void testCurrentLocationClaimedEvent() throws Exception {
77
-    HandlingEvent ev = new HandlingEvent(null, new Date(), new Date(), CLAIM, a5);
73
+    HandlingEvent ev = new HandlingEvent(cargo, new Date(), new Date(), CLAIM, a5, null);
78 74
 
79 75
     assertEquals(a5, ev.location());
80 76
   }
@@ -101,8 +97,8 @@ public class HandlingEventTest extends TestCase {
101 97
     CarrierMovementId carrierMovementId = new CarrierMovementId("CAR_001");
102 98
     CarrierMovement cm = new CarrierMovement(carrierMovementId, a5, b5);
103 99
 
104
-    HandlingEvent ev1 = new HandlingEvent(null, timeOccured, timeRegistered, LOAD, a5, cm);
105
-    HandlingEvent ev2 = new HandlingEvent(null, timeOccured, timeRegistered, LOAD, a5, cm);
100
+    HandlingEvent ev1 = new HandlingEvent(cargo, timeOccured, timeRegistered, LOAD, a5, cm);
101
+    HandlingEvent ev2 = new HandlingEvent(cargo, timeOccured, timeRegistered, LOAD, a5, cm);
106 102
 
107 103
     // Two handling events are not equal() even if all non-uuid fields are identical
108 104
     assertTrue(ev1.equals(ev2));

+ 5
- 5
dddsample/src/test/java/se/citerus/dddsample/domain/ItineraryTest.java Zobrazit soubor

@@ -33,7 +33,7 @@ public class ItineraryTest extends TestCase {
33 33
     );
34 34
 
35 35
     //Happy path
36
-    HandlingEvent event = new HandlingEvent(cargo, new Date(), new Date(), HandlingEvent.Type.RECEIVE, shanghai);
36
+    HandlingEvent event = new HandlingEvent(cargo, new Date(), new Date(), HandlingEvent.Type.RECEIVE, shanghai,null);
37 37
     assertTrue(itinerary.isExpected(event));
38 38
 
39 39
     event = new HandlingEvent(cargo, new Date(), new Date(), HandlingEvent.Type.LOAD, shanghai, abc);
@@ -48,15 +48,15 @@ public class ItineraryTest extends TestCase {
48 48
     event = new HandlingEvent(cargo, new Date(), new Date(), HandlingEvent.Type.UNLOAD, goteborg, def);
49 49
     assertTrue(itinerary.isExpected(event));
50 50
 
51
-    event = new HandlingEvent(cargo, new Date(), new Date(), HandlingEvent.Type.CLAIM, goteborg);
51
+    event = new HandlingEvent(cargo, new Date(), new Date(), HandlingEvent.Type.CLAIM, goteborg, null);
52 52
     assertTrue(itinerary.isExpected(event));
53 53
 
54 54
     //Customs event changes nothing
55
-    event = new HandlingEvent(cargo, new Date(), new Date(), HandlingEvent.Type.CUSTOMS, goteborg);
55
+    event = new HandlingEvent(cargo, new Date(), new Date(), HandlingEvent.Type.CUSTOMS, goteborg, null);
56 56
     assertTrue(itinerary.isExpected(event));
57 57
 
58 58
     //Received at the wrong location
59
-    event = new HandlingEvent(cargo, new Date(), new Date(), HandlingEvent.Type.RECEIVE, hangzhou);
59
+    event = new HandlingEvent(cargo, new Date(), new Date(), HandlingEvent.Type.RECEIVE, hangzhou, null);
60 60
     assertFalse(itinerary.isExpected(event));
61 61
 
62 62
     //Loaded to onto the wrong ship, correct location
@@ -67,7 +67,7 @@ public class ItineraryTest extends TestCase {
67 67
     event = new HandlingEvent(cargo, new Date(), new Date(), HandlingEvent.Type.UNLOAD, longBeach, jkl);
68 68
     assertFalse(itinerary.isExpected(event));
69 69
 
70
-    event = new HandlingEvent(cargo, new Date(), new Date(), HandlingEvent.Type.CLAIM, rotterdam);
70
+    event = new HandlingEvent(cargo, new Date(), new Date(), HandlingEvent.Type.CLAIM, rotterdam, null);
71 71
     assertFalse(itinerary.isExpected(event));
72 72
 
73 73
   }

+ 13
- 0
dddsample/src/test/java/se/citerus/dddsample/domain/LegTest.java Zobrazit soubor

@@ -0,0 +1,13 @@
1
+package se.citerus.dddsample.domain;
2
+
3
+import junit.framework.TestCase;
4
+
5
+public class LegTest extends TestCase {
6
+
7
+  public void testConstructor() throws Exception {
8
+    try {
9
+      new Leg(null,null,null);
10
+      fail("Should not accept null constructor arguments");
11
+    } catch (IllegalArgumentException expected) {}
12
+  }
13
+}

+ 14
- 0
dddsample/src/test/java/se/citerus/dddsample/domain/TrackingIdTest.java Zobrazit soubor

@@ -0,0 +1,14 @@
1
+package se.citerus.dddsample.domain;
2
+
3
+import junit.framework.TestCase;
4
+
5
+public class TrackingIdTest extends TestCase {
6
+
7
+  public void testConstructor() throws Exception {
8
+    try {
9
+      new TrackingId(null);
10
+      fail("Should not accept null constructor arguments");
11
+    } catch (IllegalArgumentException expected) {}
12
+  }
13
+
14
+}

+ 1
- 1
dddsample/src/test/java/se/citerus/dddsample/repository/HandlingEventRepositoryTest.java Zobrazit soubor

@@ -30,7 +30,7 @@ public class HandlingEventRepositoryTest extends AbstractRepositoryTest {
30 30
     Cargo cargo = cargoRepository.find(new TrackingId("XYZ"));
31 31
     Date completionTime = new Date(10);
32 32
     Date registrationTime = new Date(20);
33
-    HandlingEvent event = new HandlingEvent(cargo, completionTime, registrationTime, HandlingEvent.Type.CLAIM, location);
33
+    HandlingEvent event = new HandlingEvent(cargo, completionTime, registrationTime, HandlingEvent.Type.CLAIM, location, null);
34 34
 
35 35
     handlingEventRepository.save(event);
36 36