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

Introduced CargoTestHelper for creating test cargo instances while preserving package level visibility where desired.

peter_backlund 18 лет назад
Родитель
Сommit
cb906b9c4a

+ 25
- 0
dddsample/src/main/java/se/citerus/dddsample/domain/CargoTestHelper.java Просмотреть файл

@@ -0,0 +1,25 @@
1
+package se.citerus.dddsample.domain;
2
+
3
+import java.util.Collection;
4
+
5
+/**
6
+ * For easy testdata creation.
7
+ * 
8
+ */
9
+public class CargoTestHelper {
10
+
11
+  public static Cargo createCargoWithDeliveryHistory(
12
+    TrackingId trackingId, Location origin, Location destination,
13
+    Collection<HandlingEvent> events) {
14
+
15
+    final Cargo cargo = new Cargo(trackingId, origin, destination);
16
+    setDeliveryHistory(cargo, events);
17
+
18
+    return cargo;
19
+  }
20
+
21
+  public static void setDeliveryHistory(Cargo cargo, Collection<HandlingEvent> events) {
22
+    cargo.setDeliveryHistory(new DeliveryHistory(events));
23
+  }
24
+  
25
+}

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

@@ -2,8 +2,7 @@ package se.citerus.dddsample.repository;
2 2
 
3 3
 import org.springframework.dao.DataRetrievalFailureException;
4 4
 import se.citerus.dddsample.domain.Cargo;
5
-import se.citerus.dddsample.domain.DeliveryHistory;
6
-import se.citerus.dddsample.domain.Itinerary;
5
+import se.citerus.dddsample.domain.CargoTestHelper;
7 6
 import static se.citerus.dddsample.domain.SampleLocations.*;
8 7
 import se.citerus.dddsample.domain.TrackingId;
9 8
 
@@ -49,34 +48,30 @@ public class CargoRepositoryInMem implements CargoRepository {
49 48
     );
50 49
   }
51 50
 
52
-  public void deleteItinerary(Itinerary itinerary) {
53
-  }
54
-
55 51
   public List<Cargo> findAll() {
56 52
     return new ArrayList(cargoDb.values());
57 53
   }
58 54
 
59 55
   public void init() throws Exception {
60
-    String trackIdXYZ = "XYZ";
61
-    final Cargo cargoXYZ = new Cargo(new TrackingId(trackIdXYZ), STOCKHOLM, MELBOURNE);
62
-    cargoDb.put(trackIdXYZ, cargoXYZ);
63
-    
64
-    String trackIdZYX = "ZYX";
65
-    final Cargo cargoZYX = new Cargo(new TrackingId(trackIdZYX), MELBOURNE, STOCKHOLM);
66
-    cargoDb.put(trackIdZYX, cargoZYX);
67
-    
68
-    String trackIdABC = "ABC";
69
-    final Cargo cargoABC = new Cargo(new TrackingId(trackIdABC), STOCKHOLM, HELSINKI);
70
-    cargoDb.put(trackIdABC, cargoABC);
71
-    
72
-    String trackIdCBA = "CBA";
73
-    final Cargo cargoCBA = new Cargo(new TrackingId(trackIdCBA), HELSINKI, STOCKHOLM);
74
-    cargoDb.put(trackIdCBA, cargoCBA);
56
+    final TrackingId xyz = new TrackingId("XYZ");
57
+    final Cargo cargoXYZ = CargoTestHelper.createCargoWithDeliveryHistory(
58
+      xyz, STOCKHOLM, MELBOURNE, handlingEventRepository.findEventsForCargo(xyz));
59
+    cargoDb.put(xyz.idString(), cargoXYZ);
75 60
 
76
-    for (Cargo cargo : cargoDb.values()) {
77
-      DeliveryHistory dh = new DeliveryHistory(handlingEventRepository.findEventsForCargo(cargo.trackingId()));
78
-      cargo.setDeliveryHistory(dh);
79
-    }
61
+    final TrackingId zyx = new TrackingId("ZYX");
62
+    final Cargo cargoZYX = CargoTestHelper.createCargoWithDeliveryHistory(
63
+      zyx, MELBOURNE, STOCKHOLM, handlingEventRepository.findEventsForCargo(zyx));
64
+    cargoDb.put(zyx.idString(), cargoZYX);
65
+
66
+    final TrackingId abc = new TrackingId("ABC");
67
+    final Cargo cargoABC = CargoTestHelper.createCargoWithDeliveryHistory(
68
+      abc, STOCKHOLM, HELSINKI, handlingEventRepository.findEventsForCargo(abc));
69
+    cargoDb.put(abc.idString(), cargoABC);
70
+
71
+    final TrackingId cba = new TrackingId("CBA");
72
+    final Cargo cargoCBA = CargoTestHelper.createCargoWithDeliveryHistory(
73
+      cba, HELSINKI, STOCKHOLM, handlingEventRepository.findEventsForCargo(cba));
74
+    cargoDb.put(cba.idString(), cargoCBA);
80 75
   }
81 76
 
82 77
   public void setHandlingEventRepository(final HandlingEventRepository handlingEventRepository) {

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

@@ -25,6 +25,6 @@ public interface HandlingEventService {
25 25
    * @throws UnknownLocationException   if there's no location with this UN Locode
26 26
    */
27 27
   void register(Date completionTime, TrackingId trackingId, CarrierMovementId carrierMovementId, UnLocode unlocode, HandlingEvent.Type type)
28
-    throws UnknownCarrierMovementIdException, UnknownTrackingIdException, UnknownLocationException;
28
+  throws UnknownCarrierMovementIdException, UnknownTrackingIdException, UnknownLocationException;
29 29
 
30 30
 }

+ 1
- 1
dddsample/src/test/java/se/citerus/dddsample/repository/CargoRepositoryTest.java Просмотреть файл

@@ -155,7 +155,7 @@ public class CargoRepositoryTest extends AbstractRepositoryTest {
155 155
     HandlingEvent event = new HandlingEvent(cargo, new Date(), new Date(), HandlingEvent.Type.RECEIVE, origin, null);
156 156
     assertFalse(cargo.deliveryHistory().eventsOrderedByCompletionTime().contains(event));
157 157
 
158
-    cargo.setDeliveryHistory(new DeliveryHistory(Arrays.asList(event)));
158
+    CargoTestHelper.setDeliveryHistory(cargo, Arrays.asList(event));
159 159
     assertTrue(cargo.deliveryHistory().eventsOrderedByCompletionTime().contains(event));
160 160
 
161 161
     // Save cargo, evict from session and then re-load it - should not pick up the added event,

+ 2
- 1
dddsample/src/test/java/se/citerus/dddsample/service/TrackingServiceTest.java Просмотреть файл

@@ -33,7 +33,8 @@ public class TrackingServiceTest extends TestCase {
33 33
     HandlingEvent unloaded = new HandlingEvent(cargo, new Date(100), new Date(110), HandlingEvent.Type.UNLOAD, CHICAGO, carrierMovement);
34 34
     // Add out of order to verify ordering in DTO
35 35
     List<HandlingEvent> eventList = Arrays.asList(loaded, unloaded, claimed);
36
-    cargo.setDeliveryHistory(new DeliveryHistory(eventList));
36
+
37
+    CargoTestHelper.setDeliveryHistory(cargo, eventList);
37 38
 
38 39
     expect(cargoRepository.find(new TrackingId("XYZ"))).andReturn(cargo);
39 40
 

+ 2
- 2
dddsample/src/test/java/se/citerus/dddsample/web/CargoTrackingControllerTest.java Просмотреть файл

@@ -10,7 +10,7 @@ import org.springframework.validation.Errors;
10 10
 import org.springframework.validation.FieldError;
11 11
 import org.springframework.web.servlet.ModelAndView;
12 12
 import se.citerus.dddsample.domain.Cargo;
13
-import se.citerus.dddsample.domain.DeliveryHistory;
13
+import se.citerus.dddsample.domain.CargoTestHelper;
14 14
 import se.citerus.dddsample.domain.HandlingEvent;
15 15
 import static se.citerus.dddsample.domain.SampleLocations.HONGKONG;
16 16
 import static se.citerus.dddsample.domain.SampleLocations.TOKYO;
@@ -48,7 +48,7 @@ public class CargoTrackingControllerTest extends TestCase {
48 48
       public Cargo track(TrackingId trackingId) {
49 49
         final Cargo cargo = new Cargo(trackingId, HONGKONG, TOKYO);
50 50
         final HandlingEvent event = new HandlingEvent(cargo, new Date(10L), new Date(20L), HandlingEvent.Type.RECEIVE, HONGKONG, null);
51
-        cargo.setDeliveryHistory(new DeliveryHistory(Arrays.asList(event)));
51
+        CargoTestHelper.setDeliveryHistory(cargo, Arrays.asList(event));
52 52
         
53 53
         return cargo;
54 54
       }