Kaynağa Gözat

Inlined cargo factory.

peter_backlund 17 yıl önce
ebeveyn
işleme
2a39902612

+ 12
- 6
dddsample/tracking/core/src/main/java/se/citerus/dddsample/tracking/core/application/booking/BookingServiceImpl.java Dosyayı Görüntüle

@@ -3,9 +3,9 @@ package se.citerus.dddsample.tracking.core.application.booking;
3 3
 import org.apache.commons.lang.Validate;
4 4
 import org.apache.commons.logging.Log;
5 5
 import org.apache.commons.logging.LogFactory;
6
-import org.springframework.transaction.annotation.Transactional;
7
-import org.springframework.stereotype.Service;
8 6
 import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.stereotype.Service;
8
+import org.springframework.transaction.annotation.Transactional;
9 9
 import se.citerus.dddsample.tracking.core.domain.model.cargo.*;
10 10
 import se.citerus.dddsample.tracking.core.domain.model.location.Location;
11 11
 import se.citerus.dddsample.tracking.core.domain.model.location.LocationRepository;
@@ -20,18 +20,18 @@ import java.util.List;
20 20
 public final class BookingServiceImpl implements BookingService {
21 21
 
22 22
   private final RoutingService routingService;
23
-  private final CargoFactory cargoFactory;
24 23
   private final CargoRepository cargoRepository;
25 24
   private final LocationRepository locationRepository;
26 25
   private final Log logger = LogFactory.getLog(getClass());
26
+  private final TrackingIdFactory trackingIdFactory;
27 27
 
28 28
   @Autowired
29 29
   public BookingServiceImpl(final RoutingService routingService,
30
-                            final CargoFactory cargoFactory,
30
+                            final TrackingIdFactory trackingIdFactory,
31 31
                             final CargoRepository cargoRepository,
32 32
                             final LocationRepository locationRepository) {
33 33
     this.routingService = routingService;
34
-    this.cargoFactory = cargoFactory;
34
+    this.trackingIdFactory = trackingIdFactory;
35 35
     this.cargoRepository = cargoRepository;
36 36
     this.locationRepository = locationRepository;
37 37
   }
@@ -41,8 +41,14 @@ public final class BookingServiceImpl implements BookingService {
41 41
   public TrackingId bookNewCargo(final UnLocode originUnLocode,
42 42
                                  final UnLocode destinationUnLocode,
43 43
                                  final Date arrivalDeadline) {
44
-    final Cargo cargo = cargoFactory.newCargo(originUnLocode, destinationUnLocode, arrivalDeadline);
44
+    final TrackingId trackingId = trackingIdFactory.nextTrackingId();
45
+    final Location origin = locationRepository.find(originUnLocode);
46
+    final Location destination = locationRepository.find(destinationUnLocode);
47
+    final RouteSpecification routeSpecification = new RouteSpecification(origin, destination, arrivalDeadline);
48
+
49
+    final Cargo cargo = new Cargo(trackingId, routeSpecification);
45 50
     cargoRepository.store(cargo);
51
+    
46 52
     logger.info("Booked new cargo with tracking id " + cargo.trackingId().stringValue());
47 53
 
48 54
     return cargo.trackingId();

+ 0
- 28
dddsample/tracking/core/src/main/java/se/citerus/dddsample/tracking/core/domain/model/cargo/CargoFactory.java Dosyayı Görüntüle

@@ -1,28 +0,0 @@
1
-package se.citerus.dddsample.tracking.core.domain.model.cargo;
2
-
3
-import se.citerus.dddsample.tracking.core.domain.model.location.Location;
4
-import se.citerus.dddsample.tracking.core.domain.model.location.LocationRepository;
5
-import se.citerus.dddsample.tracking.core.domain.model.location.UnLocode;
6
-
7
-import java.util.Date;
8
-
9
-public class CargoFactory {
10
-
11
-  private final LocationRepository locationRepository;
12
-  private final TrackingIdFactory trackingIdFactory;
13
-
14
-  public CargoFactory(LocationRepository locationRepository, TrackingIdFactory trackingIdFactory) {
15
-    this.locationRepository = locationRepository;
16
-    this.trackingIdFactory = trackingIdFactory;
17
-  }
18
-
19
-  public Cargo newCargo(UnLocode originUnLocode, UnLocode destinationUnLocode, Date arrivalDeadline) {
20
-    final TrackingId trackingId = trackingIdFactory.nextTrackingId();
21
-    final Location origin = locationRepository.find(originUnLocode);
22
-    final Location destination = locationRepository.find(destinationUnLocode);
23
-    final RouteSpecification routeSpecification = new RouteSpecification(origin, destination, arrivalDeadline);
24
-
25
-    return new Cargo(trackingId, routeSpecification);
26
-  }
27
-
28
-}

+ 0
- 2
dddsample/tracking/core/src/main/resources/contexts/context-domain.xml Dosyayı Görüntüle

@@ -8,8 +8,6 @@
8 8
 
9 9
   <context:component-scan base-package="se.citerus.dddsample.tracking.core.domain"/>
10 10
 
11
-  <bean class="se.citerus.dddsample.tracking.core.domain.model.cargo.CargoFactory" autowire="constructor"/>
12
-
13 11
   <bean class="se.citerus.dddsample.tracking.core.domain.model.handling.HandlingEventFactory" autowire="constructor"/>
14 12
   
15 13
 </beans>

+ 5
- 3
dddsample/tracking/core/src/test/java/se/citerus/dddsample/tracking/core/application/booking/BookingServiceTest.java Dosyayı Görüntüle

@@ -2,7 +2,10 @@ package se.citerus.dddsample.tracking.core.application.booking;
2 2
 
3 3
 import junit.framework.TestCase;
4 4
 import static org.easymock.EasyMock.*;
5
-import se.citerus.dddsample.tracking.core.domain.model.cargo.*;
5
+import se.citerus.dddsample.tracking.core.domain.model.cargo.Cargo;
6
+import se.citerus.dddsample.tracking.core.domain.model.cargo.CargoRepository;
7
+import se.citerus.dddsample.tracking.core.domain.model.cargo.TrackingId;
8
+import se.citerus.dddsample.tracking.core.domain.model.cargo.TrackingIdFactory;
6 9
 import se.citerus.dddsample.tracking.core.domain.model.location.LocationRepository;
7 10
 import static se.citerus.dddsample.tracking.core.domain.model.location.SampleLocations.CHICAGO;
8 11
 import static se.citerus.dddsample.tracking.core.domain.model.location.SampleLocations.STOCKHOLM;
@@ -24,8 +27,7 @@ public class BookingServiceTest extends TestCase {
24 27
     locationRepository = createMock(LocationRepository.class);
25 28
     routingService = createMock(RoutingService.class);
26 29
     trackingIdFactory = createMock(TrackingIdFactory.class);
27
-    CargoFactory cargoFactory = new CargoFactory(locationRepository, trackingIdFactory);
28
-    bookingService = new BookingServiceImpl(routingService, cargoFactory, cargoRepository, locationRepository);
30
+    bookingService = new BookingServiceImpl(routingService, trackingIdFactory, cargoRepository, locationRepository);
29 31
   }
30 32
 
31 33
   public void testRegisterNew() {

+ 9
- 5
dddsample/tracking/core/src/test/java/se/citerus/dddsample/tracking/core/application/event/CargoUpdaterTest.java Dosyayı Görüntüle

@@ -8,8 +8,9 @@ import org.junit.Before;
8 8
 import org.junit.Test;
9 9
 import static se.citerus.dddsample.tracking.core.application.util.DateTestUtil.toDate;
10 10
 import se.citerus.dddsample.tracking.core.domain.model.cargo.Cargo;
11
-import se.citerus.dddsample.tracking.core.domain.model.cargo.CargoFactory;
12 11
 import se.citerus.dddsample.tracking.core.domain.model.cargo.CargoRepository;
12
+import se.citerus.dddsample.tracking.core.domain.model.cargo.RouteSpecification;
13
+import se.citerus.dddsample.tracking.core.domain.model.cargo.TrackingId;
13 14
 import se.citerus.dddsample.tracking.core.domain.model.handling.*;
14 15
 import static se.citerus.dddsample.tracking.core.domain.model.handling.HandlingEvent.Type.LOAD;
15 16
 import se.citerus.dddsample.tracking.core.domain.model.location.LocationRepository;
@@ -23,28 +24,31 @@ public class CargoUpdaterTest {
23 24
 
24 25
   SystemEvents systemEvents;
25 26
   CargoUpdater cargoUpdater;
26
-  CargoFactory cargoFactory;
27 27
   HandlingEventFactory handlingEventFactory;
28 28
   CargoRepository cargoRepository;
29 29
   HandlingEventRepository handlingEventRepository;
30 30
   LocationRepository locationRepository;
31 31
   VoyageRepository voyageRepository;
32
+  private TrackingIdFactoryInMem trackingIdFactory;
32 33
 
33 34
   @Before
34
-  public void setUp() throws CannotCreateHandlingEventException {
35
+  public void setUp() {
35 36
     systemEvents = createMock(SystemEvents.class);
36 37
     cargoRepository = new CargoRepositoryInMem();
37 38
     handlingEventRepository = new HandlingEventRepositoryInMem();
38 39
     locationRepository = new LocationRepositoryInMem();
39 40
     voyageRepository = new VoyageRepositoryInMem();
40
-    cargoFactory = new CargoFactory(locationRepository, new TrackingIdFactoryInMem());
41
+    trackingIdFactory = new TrackingIdFactoryInMem();
41 42
     handlingEventFactory = new HandlingEventFactory(cargoRepository, voyageRepository, locationRepository);
42 43
     cargoUpdater = new CargoUpdater(systemEvents, cargoRepository, handlingEventRepository);
43 44
   }
44 45
 
45 46
   @Test
46 47
   public void updateCargo() throws CannotCreateHandlingEventException {
47
-    Cargo cargo = cargoFactory.newCargo(HONGKONG.unLocode(), GOTHENBURG.unLocode(), toDate("2009-10-15"));
48
+    TrackingId trackingId = trackingIdFactory.nextTrackingId();
49
+    RouteSpecification routeSpecification = new RouteSpecification(HONGKONG, GOTHENBURG, toDate("2009-10-15"));
50
+
51
+    Cargo cargo = new Cargo(trackingId, routeSpecification);
48 52
     cargoRepository.store(cargo);
49 53
 
50 54
     HandlingEvent handlingEvent = handlingEventFactory.createHandlingEvent(

+ 0
- 42
dddsample/tracking/core/src/test/java/se/citerus/dddsample/tracking/core/domain/model/cargo/CargoFactoryTest.java Dosyayı Görüntüle

@@ -1,42 +0,0 @@
1
-/**
2
- * Purpose
3
- * @author peter
4
- * @created 2009-aug-08
5
- * $Id$
6
- */
7
-package se.citerus.dddsample.tracking.core.domain.model.cargo;
8
-
9
-import static org.junit.Assert.assertEquals;
10
-import static org.junit.Assert.assertNotNull;
11
-import org.junit.Before;
12
-import org.junit.Test;
13
-import static se.citerus.dddsample.tracking.core.application.util.DateTestUtil.toDate;
14
-import static se.citerus.dddsample.tracking.core.domain.model.location.SampleLocations.HONGKONG;
15
-import static se.citerus.dddsample.tracking.core.domain.model.location.SampleLocations.ROTTERDAM;
16
-import se.citerus.dddsample.tracking.core.infrastructure.persistence.inmemory.LocationRepositoryInMem;
17
-
18
-public class CargoFactoryTest {
19
-
20
-  CargoFactory cargoFactory;
21
-
22
-  @Before
23
-  public void setup() {
24
-    TrackingIdFactory stubFactory = new TrackingIdFactory() {
25
-      @Override
26
-      public TrackingId nextTrackingId() {
27
-        return new TrackingId("ABC");
28
-      }
29
-    };
30
-    cargoFactory = new CargoFactory(new LocationRepositoryInMem(), stubFactory);
31
-  }
32
-
33
-  @Test
34
-  public void createNewCargo() {
35
-    Cargo cargo = cargoFactory.newCargo(HONGKONG.unLocode(), ROTTERDAM.unLocode(), toDate("2009-07-01"));
36
-
37
-    assertNotNull(cargo);
38
-    assertEquals(cargo.trackingId(), new TrackingId("ABC"));
39
-    assertEquals(cargo.routeSpecification(), new RouteSpecification(HONGKONG, ROTTERDAM, toDate("2009-07-01")));
40
-  }
41
-
42
-}

+ 5
- 2
dddsample/tracking/core/src/test/java/se/citerus/dddsample/tracking/core/scenario/CargoLifecycleScenarioTest.java Dosyayı Görüntüle

@@ -38,7 +38,7 @@ public class CargoLifecycleScenarioTest {
38 38
   VoyageRepository voyageRepository = new VoyageRepositoryInMem();
39 39
 
40 40
   HandlingEventFactory handlingEventFactory = new HandlingEventFactory(cargoRepository, voyageRepository, locationRepository);
41
-  CargoFactory cargoFactory = new CargoFactory(locationRepository, new TrackingIdFactoryInMem());
41
+  TrackingIdFactory trackingIdFactory = new TrackingIdFactoryInMem();
42 42
 
43 43
   /**
44 44
    * This is a domain service interface, whose implementation
@@ -188,7 +188,10 @@ public class CargoLifecycleScenarioTest {
188 188
 
189 189
   private void bookCargoFromHongkongToStockholm() {
190 190
     Date arrivalDeadline = toDate("2009-03-18");
191
-    Cargo cargo = cargoFactory.newCargo(HONGKONG.unLocode(), STOCKHOLM.unLocode(), arrivalDeadline);
191
+    final TrackingId trackingId1 = trackingIdFactory.nextTrackingId();
192
+    final RouteSpecification routeSpecification = new RouteSpecification(HONGKONG, STOCKHOLM, arrivalDeadline);
193
+
194
+    Cargo cargo = new Cargo(trackingId1, routeSpecification);
192 195
     cargoRepository.store(cargo);
193 196
 
194 197
     trackingId = cargo.trackingId();

+ 8
- 7
dddsample/tracking/core/src/test/java/se/citerus/dddsample/tracking/core/scenario/VoyageRescheduledScenarioTest.java Dosyayı Görüntüle

@@ -11,17 +11,13 @@ import static org.hamcrest.Matchers.is;
11 11
 import org.junit.Before;
12 12
 import org.junit.Test;
13 13
 import static se.citerus.dddsample.tracking.core.application.util.DateTestUtil.toDate;
14
-import se.citerus.dddsample.tracking.core.domain.model.cargo.Cargo;
15
-import se.citerus.dddsample.tracking.core.domain.model.cargo.CargoFactory;
16
-import se.citerus.dddsample.tracking.core.domain.model.cargo.Itinerary;
17
-import se.citerus.dddsample.tracking.core.domain.model.cargo.Leg;
14
+import se.citerus.dddsample.tracking.core.domain.model.cargo.*;
18 15
 import static se.citerus.dddsample.tracking.core.domain.model.cargo.RoutingStatus.MISROUTED;
19 16
 import static se.citerus.dddsample.tracking.core.domain.model.cargo.RoutingStatus.ROUTED;
20 17
 import static se.citerus.dddsample.tracking.core.domain.model.location.SampleLocations.*;
21 18
 import static se.citerus.dddsample.tracking.core.domain.model.voyage.SampleVoyages.*;
22 19
 import se.citerus.dddsample.tracking.core.domain.model.voyage.Voyage;
23 20
 import se.citerus.dddsample.tracking.core.domain.model.voyage.VoyageNumber;
24
-import se.citerus.dddsample.tracking.core.infrastructure.persistence.inmemory.LocationRepositoryInMem;
25 21
 import se.citerus.dddsample.tracking.core.infrastructure.persistence.inmemory.TrackingIdFactoryInMem;
26 22
 
27 23
 import java.util.Date;
@@ -35,12 +31,17 @@ public class VoyageRescheduledScenarioTest {
35 31
 
36 32
   @Before
37 33
   public void setupCargo() {
34
+    TrackingIdFactoryInMem trackingIdFactory = new TrackingIdFactoryInMem();
35
+
38 36
     // Creating new voyages to avoid rescheduling shared ones, breaking other tests
39 37
     voyage1 = new Voyage(new VoyageNumber("V1"), HONGKONG_TO_NEW_YORK.schedule());
40 38
     voyage2 = new Voyage(new VoyageNumber("V2"), NEW_YORK_TO_DALLAS.schedule());
41 39
     voyage3 = new Voyage(new VoyageNumber("V3"), DALLAS_TO_HELSINKI.schedule());
42
-    CargoFactory cargoFactory = new CargoFactory(new LocationRepositoryInMem(), new TrackingIdFactoryInMem());
43
-    cargo = cargoFactory.newCargo(HANGZOU.unLocode(), STOCKHOLM.unLocode(), toDate("2008-12-23"));
40
+
41
+    TrackingId trackingId = trackingIdFactory.nextTrackingId();
42
+    RouteSpecification routeSpecification = new RouteSpecification(HANGZOU, STOCKHOLM, toDate("2008-12-23"));
43
+
44
+    cargo = new Cargo(trackingId, routeSpecification);
44 45
     Itinerary itinerary = new Itinerary(
45 46
       Leg.deriveLeg(voyage1, HANGZOU, NEWYORK),
46 47
       Leg.deriveLeg(voyage2, NEWYORK, DALLAS),