Selaa lähdekoodia

Merge branch 'bump-java-and-spring' of https://github.com/citerus/dddsample-core into bump-java-and-spring

marlin 11 vuotta sitten
vanhempi
commit
86a8f5c2d4

+ 1
- 1
pom.xml Näytä tiedosto

@@ -127,7 +127,7 @@
127 127
     <dependency>
128 128
       <groupId>junit</groupId>
129 129
       <artifactId>junit</artifactId>
130
-      <version>4.4</version>
130
+      <version>4.12</version>
131 131
       <scope>test</scope>
132 132
     </dependency>
133 133
     <dependency>

+ 0
- 76
src/test/java/se/citerus/dddsample/infrastructure/persistence/hibernate/AbstractRepositoryTest.java Näytä tiedosto

@@ -1,76 +0,0 @@
1
-package se.citerus.dddsample.infrastructure.persistence.hibernate;
2
-
3
-import org.hibernate.SessionFactory;
4
-import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
5
-import org.springframework.transaction.support.TransactionTemplate;
6
-import se.citerus.dddsample.application.util.SampleDataGenerator;
7
-import se.citerus.dddsample.domain.model.handling.HandlingEventFactory;
8
-import se.citerus.dddsample.domain.model.handling.HandlingEventRepository;
9
-
10
-import java.lang.reflect.Field;
11
-
12
-/*public abstract class AbstractRepositoryTest extends AbstractTransactionalDataSourceSpringContextTests {
13
-
14
-  SessionFactory sessionFactory;
15
-  SimpleJdbcTemplate sjt;
16
-  HandlingEventFactory handlingEventFactory;
17
-  HandlingEventRepository handlingEventRepository;
18
-
19
-  public void setHandlingEventFactory(HandlingEventFactory handlingEventFactory) {
20
-    this.handlingEventFactory = handlingEventFactory;
21
-  }
22
-
23
-  public void setHandlingEventRepository(HandlingEventRepository handlingEventRepository) {
24
-    this.handlingEventRepository = handlingEventRepository;
25
-  }
26
-
27
-  protected AbstractRepositoryTest() {
28
-    setAutowireMode(AUTOWIRE_BY_NAME);
29
-    setDependencyCheck(false);
30
-  }
31
-
32
-  public void setSessionFactory(SessionFactory sessionFactory) {
33
-    this.sessionFactory = sessionFactory;
34
-    transactionManager = new HibernateTransactionManager(sessionFactory);
35
-  }
36
-
37
-  public SessionFactory getSessionFactory() {
38
-    return sessionFactory;
39
-  }
40
-
41
-  protected void flush() {
42
-    sessionFactory.getCurrentSession().flush();
43
-  }
44
-
45
-  @Override
46
-  protected String[] getConfigLocations() {
47
-    return new String[] {"/context-infrastructure-persistence.xml", "context-domain.xml"};
48
-  }
49
-
50
-  @Override
51
-  protected void onSetUpInTransaction() throws Exception {
52
-    // TODO store Sample* and object instances here instead of handwritten SQL
53
-    SampleDataGenerator.loadSampleData(jdbcTemplate, new TransactionTemplate(transactionManager));
54
-    sjt = new SimpleJdbcTemplate(jdbcTemplate);
55
-  }
56
-
57
-  protected Session getSession() {
58
-    return sessionFactory.getCurrentSession();
59
-  }
60
-
61
-  // Instead of exposing a getId() on persistent classes
62
-  protected Long getLongId(Object o) {
63
-    if (getSession().contains(o)) {
64
-      return (Long) getSession().getIdentifier(o);
65
-    } else {
66
-      try {
67
-        Field id = o.getClass().getDeclaredField("id");
68
-        id.setAccessible(true);
69
-        return (Long) id.get(o);
70
-      } catch (Exception e) {
71
-        throw new RuntimeException(e);
72
-      }
73
-    }
74
-  }
75
-}
76
-*/

+ 175
- 115
src/test/java/se/citerus/dddsample/infrastructure/persistence/hibernate/CargoRepositoryTest.java Näytä tiedosto

@@ -1,171 +1,231 @@
1 1
 package se.citerus.dddsample.infrastructure.persistence.hibernate;
2 2
 
3
+import org.hibernate.SessionFactory;
4
+import org.hibernate.classic.Session;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+import org.junit.runner.RunWith;
8
+import org.springframework.beans.factory.annotation.Autowired;
9
+import org.springframework.jdbc.core.JdbcTemplate;
10
+import org.springframework.orm.hibernate3.HibernateTransactionManager;
11
+import org.springframework.test.context.ContextConfiguration;
12
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
13
+import org.springframework.test.context.transaction.TransactionConfiguration;
14
+import org.springframework.transaction.annotation.Transactional;
15
+import org.springframework.transaction.support.TransactionTemplate;
3 16
 import se.citerus.dddsample.application.util.SampleDataGenerator;
4 17
 import se.citerus.dddsample.domain.model.cargo.*;
5 18
 import se.citerus.dddsample.domain.model.handling.HandlingEvent;
6
-import static se.citerus.dddsample.domain.model.handling.HandlingEvent.Type.LOAD;
7
-import static se.citerus.dddsample.domain.model.handling.HandlingEvent.Type.RECEIVE;
19
+import se.citerus.dddsample.domain.model.handling.HandlingEventRepository;
8 20
 import se.citerus.dddsample.domain.model.location.Location;
9 21
 import se.citerus.dddsample.domain.model.location.LocationRepository;
10
-import static se.citerus.dddsample.domain.model.location.SampleLocations.*;
11 22
 import se.citerus.dddsample.domain.model.location.UnLocode;
12
-import static se.citerus.dddsample.domain.model.voyage.SampleVoyages.CM004;
13 23
 import se.citerus.dddsample.domain.model.voyage.Voyage;
14 24
 import se.citerus.dddsample.domain.model.voyage.VoyageNumber;
15 25
 import se.citerus.dddsample.domain.model.voyage.VoyageRepository;
26
+import static se.citerus.dddsample.domain.model.location.SampleLocations.*;
27
+import static se.citerus.dddsample.domain.model.voyage.SampleVoyages.*;
28
+import static se.citerus.dddsample.domain.model.handling.HandlingEvent.Type.*;
29
+
30
+import javax.sql.DataSource;
16 31
 
32
+import java.lang.reflect.Field;
17 33
 import java.util.Arrays;
18 34
 import java.util.Date;
19 35
 import java.util.List;
20 36
 import java.util.Map;
21 37
 
22
-/*public class CargoRepositoryTest extends AbstractRepositoryTest {
38
+import static org.junit.Assert.*;
23 39
 
24
-  CargoRepository cargoRepository;
25
-  LocationRepository locationRepository;
26
-  VoyageRepository voyageRepository;
40
+@RunWith(SpringJUnit4ClassRunner.class)
41
+@ContextConfiguration(value = {"/context-infrastructure-persistence.xml", "/context-domain.xml"})
42
+@TransactionConfiguration(transactionManager = "transactionManager")
43
+@Transactional
44
+public class CargoRepositoryTest {
27 45
 
28
-  public void setCargoRepository(CargoRepository cargoRepository) {
29
-    this.cargoRepository = cargoRepository;
30
-  }
46
+    @Autowired
47
+    CargoRepository cargoRepository;
31 48
 
32
-  public void setLocationRepository(LocationRepository locationRepository) {
33
-    this.locationRepository = locationRepository;
34
-  }
49
+    @Autowired
50
+    LocationRepository locationRepository;
35 51
 
36
-  public void setVoyageRepository(VoyageRepository voyageRepository) {
37
-    this.voyageRepository = voyageRepository;
38
-  }
52
+    @Autowired
53
+    VoyageRepository voyageRepository;
39 54
 
40
-  public void testFindByCargoId() {
41
-    final TrackingId trackingId = new TrackingId("FGH");
42
-    final Cargo cargo = cargoRepository.find(trackingId);
43
-    assertEquals(STOCKHOLM, cargo.origin());
44
-    assertEquals(HONGKONG, cargo.routeSpecification().origin());
45
-    assertEquals(HELSINKI, cargo.routeSpecification().destination());
55
+    @Autowired
56
+    HandlingEventRepository handlingEventRepository;
46 57
 
47
-    assertNotNull(cargo.delivery());
58
+    @Autowired
59
+    SessionFactory sessionFactory;
48 60
 
49
-    final List<HandlingEvent> events = handlingEventRepository.lookupHandlingHistoryOfCargo(trackingId).distinctEventsByCompletionTime();
50
-    assertEquals(2, events.size());
61
+    @Autowired
62
+    private DataSource dataSource;
51 63
 
52
-    HandlingEvent firstEvent = events.get(0);
53
-    assertHandlingEvent(cargo, firstEvent, RECEIVE, HONGKONG, 100, 160, Voyage.NONE);
64
+    @Autowired
65
+    private HibernateTransactionManager transactionManager;
54 66
 
55
-    HandlingEvent secondEvent = events.get(1);
67
+    private JdbcTemplate jdbcTemplate;
56 68
 
57
-    Voyage hongkongMelbourneTokyoAndBack = new Voyage.Builder(
58
-      new VoyageNumber("0303"), HONGKONG).
59
-      addMovement(MELBOURNE, new Date(), new Date()).
60
-      addMovement(TOKYO, new Date(), new Date()).
61
-      addMovement(HONGKONG, new Date(), new Date()).
62
-      build();
63
-    
64
-    assertHandlingEvent(cargo, secondEvent, LOAD, HONGKONG, 150, 110, hongkongMelbourneTokyoAndBack);
69
+    @Before
70
+    public void setup() {
71
+        jdbcTemplate = new JdbcTemplate(dataSource);
72
+        SampleDataGenerator.loadSampleData(jdbcTemplate, new TransactionTemplate(transactionManager));
73
+    }
65 74
 
66
-    List<Leg> legs = cargo.itinerary().legs();
67
-    assertEquals(3, legs.size());
75
+    @Test
76
+    public void testFindByCargoId() {
77
+        final TrackingId trackingId = new TrackingId("FGH");
78
+        final Cargo cargo = cargoRepository.find(trackingId);
79
+        assertEquals(STOCKHOLM, cargo.origin());
80
+        assertEquals(HONGKONG, cargo.routeSpecification().origin());
81
+        assertEquals(HELSINKI, cargo.routeSpecification().destination());
68 82
 
69
-    Leg firstLeg = legs.get(0);
70
-    assertLeg(firstLeg, "0101", HONGKONG, MELBOURNE);
83
+        assertNotNull(cargo.delivery());
71 84
 
72
-    Leg secondLeg = legs.get(1);
73
-    assertLeg(secondLeg, "0101", MELBOURNE, STOCKHOLM);
85
+        final List<HandlingEvent> events = handlingEventRepository.lookupHandlingHistoryOfCargo(trackingId).distinctEventsByCompletionTime();
86
+        assertEquals(2, events.size());
74 87
 
75
-    Leg thirdLeg = legs.get(2);
76
-    assertLeg(thirdLeg, "0101", STOCKHOLM, HELSINKI);
77
-  }
88
+        HandlingEvent firstEvent = events.get(0);
89
+        assertHandlingEvent(cargo, firstEvent, RECEIVE, HONGKONG, 100, 160, Voyage.NONE);
78 90
 
79
-  private void assertHandlingEvent(Cargo cargo, HandlingEvent event, HandlingEvent.Type expectedEventType, Location expectedLocation, int completionTimeMs, int registrationTimeMs, Voyage voyage) {
80
-    assertEquals(expectedEventType, event.type());
81
-    assertEquals(expectedLocation, event.location());
91
+        HandlingEvent secondEvent = events.get(1);
82 92
 
83
-    Date expectedCompletionTime = SampleDataGenerator.offset(completionTimeMs);
84
-    assertEquals(expectedCompletionTime, event.completionTime());
93
+        Voyage hongkongMelbourneTokyoAndBack = new Voyage.Builder(
94
+                new VoyageNumber("0303"), HONGKONG).
95
+                addMovement(MELBOURNE, new Date(), new Date()).
96
+                addMovement(TOKYO, new Date(), new Date()).
97
+                addMovement(HONGKONG, new Date(), new Date()).
98
+                build();
85 99
 
86
-    Date expectedRegistrationTime = SampleDataGenerator.offset(registrationTimeMs);
87
-    assertEquals(expectedRegistrationTime, event.registrationTime());
100
+        assertHandlingEvent(cargo, secondEvent, LOAD, HONGKONG, 150, 110, hongkongMelbourneTokyoAndBack);
88 101
 
89
-    assertEquals(voyage, event.voyage());
90
-    assertEquals(cargo, event.cargo());
91
-  }
102
+        List<Leg> legs = cargo.itinerary().legs();
103
+        assertEquals(3, legs.size());
92 104
 
93
-  public void testFindByCargoIdUnknownId() {
94
-    assertNull(cargoRepository.find(new TrackingId("UNKNOWN")));
95
-  }
105
+        Leg firstLeg = legs.get(0);
106
+        assertLeg(firstLeg, "0101", HONGKONG, MELBOURNE);
96 107
 
97
-  private void assertLeg(Leg firstLeg, String vn, Location expectedFrom, Location expectedTo) {
98
-    assertEquals(new VoyageNumber(vn), firstLeg.voyage().voyageNumber());
99
-    assertEquals(expectedFrom, firstLeg.loadLocation());
100
-    assertEquals(expectedTo, firstLeg.unloadLocation());
101
-  }
108
+        Leg secondLeg = legs.get(1);
109
+        assertLeg(secondLeg, "0101", MELBOURNE, STOCKHOLM);
102 110
 
103
-  public void testSave() {
104
-    TrackingId trackingId = new TrackingId("AAA");
105
-    Location origin = locationRepository.find(STOCKHOLM.unLocode());
106
-    Location destination = locationRepository.find(MELBOURNE.unLocode());
111
+        Leg thirdLeg = legs.get(2);
112
+        assertLeg(thirdLeg, "0101", STOCKHOLM, HELSINKI);
113
+    }
107 114
 
108
-    Cargo cargo = new Cargo(trackingId, new RouteSpecification(origin, destination, new Date()));
109
-    cargoRepository.store(cargo);
115
+    private void assertHandlingEvent(Cargo cargo, HandlingEvent event, HandlingEvent.Type expectedEventType, Location expectedLocation, int completionTimeMs, int registrationTimeMs, Voyage voyage) {
116
+        assertEquals(expectedEventType, event.type());
117
+        assertEquals(expectedLocation, event.location());
110 118
 
111
-    cargo.assignToRoute(new Itinerary(Arrays.asList(
112
-      new Leg(
113
-        voyageRepository.find(new VoyageNumber("0101")),
114
-        locationRepository.find(STOCKHOLM.unLocode()),
115
-        locationRepository.find(MELBOURNE.unLocode()),
116
-        new Date(), new Date())
117
-    )));
118
-    
119
-    flush();
119
+        Date expectedCompletionTime = SampleDataGenerator.offset(completionTimeMs);
120
+        assertEquals(expectedCompletionTime, event.completionTime());
120 121
 
121
-    Map<String, Object> map = sjt.queryForMap(
122
-      "select * from Cargo where tracking_id = ?", trackingId.idString());
122
+        Date expectedRegistrationTime = SampleDataGenerator.offset(registrationTimeMs);
123
+        assertEquals(expectedRegistrationTime, event.registrationTime());
123 124
 
124
-    assertEquals("AAA", map.get("TRACKING_ID"));
125
+        assertEquals(voyage, event.voyage());
126
+        assertEquals(cargo, event.cargo());
127
+    }
125 128
 
126
-    Long originId = getLongId(origin);
127
-    assertEquals(originId, map.get("SPEC_ORIGIN_ID"));
129
+    @Test
130
+    public void testFindByCargoIdUnknownId() {
131
+        assertNull(cargoRepository.find(new TrackingId("UNKNOWN")));
132
+    }
128 133
 
129
-    Long destinationId = getLongId(destination);
130
-    assertEquals(destinationId, map.get("SPEC_DESTINATION_ID"));
134
+    private void assertLeg(Leg firstLeg, String vn, Location expectedFrom, Location expectedTo) {
135
+        assertEquals(new VoyageNumber(vn), firstLeg.voyage().voyageNumber());
136
+        assertEquals(expectedFrom, firstLeg.loadLocation());
137
+        assertEquals(expectedTo, firstLeg.unloadLocation());
138
+    }
131 139
 
132
-    getSession().clear();
140
+    @Test
141
+    public void testSave() {
142
+        TrackingId trackingId = new TrackingId("AAA");
143
+        Location origin = locationRepository.find(STOCKHOLM.unLocode());
144
+        Location destination = locationRepository.find(MELBOURNE.unLocode());
133 145
 
134
-    final Cargo loadedCargo = cargoRepository.find(trackingId);
135
-    assertEquals(1, loadedCargo.itinerary().legs().size());
136
-  }
146
+        Cargo cargo = new Cargo(trackingId, new RouteSpecification(origin, destination, new Date()));
147
+        cargoRepository.store(cargo);
137 148
 
138
-  public void testReplaceItinerary() {
139
-    Cargo cargo = cargoRepository.find(new TrackingId("FGH"));
140
-    Long cargoId = getLongId(cargo);
141
-    assertEquals(3, sjt.queryForInt("select count(*) from Leg where cargo_id = ?", cargoId));
149
+        cargo.assignToRoute(new Itinerary(Arrays.asList(
150
+                new Leg(
151
+                        voyageRepository.find(new VoyageNumber("0101")),
152
+                        locationRepository.find(STOCKHOLM.unLocode()),
153
+                        locationRepository.find(MELBOURNE.unLocode()),
154
+                        new Date(), new Date())
155
+        )));
142 156
 
143
-    Location legFrom = locationRepository.find(new UnLocode("FIHEL"));
144
-    Location legTo = locationRepository.find(new UnLocode("DEHAM"));
145
-    Itinerary newItinerary = new Itinerary(Arrays.asList(new Leg(CM004, legFrom, legTo, new Date(), new Date())));
157
+        flush();
146 158
 
147
-    cargo.assignToRoute(newItinerary);
159
+        Map<String, Object> map = jdbcTemplate.queryForMap(
160
+                "select * from Cargo where tracking_id = ?", trackingId.idString());
148 161
 
149
-    cargoRepository.store(cargo);
150
-    flush();
162
+        assertEquals("AAA", map.get("TRACKING_ID"));
151 163
 
152
-    assertEquals(1, sjt.queryForInt("select count(*) from Leg where cargo_id = ?", cargoId));
153
-  }
164
+        Long originId = getLongId(origin);
165
+        assertEquals(originId, map.get("SPEC_ORIGIN_ID"));
154 166
 
167
+        Long destinationId = getLongId(destination);
168
+        assertEquals(destinationId, map.get("SPEC_DESTINATION_ID"));
169
+
170
+        sessionFactory.getCurrentSession().clear();
171
+
172
+        final Cargo loadedCargo = cargoRepository.find(trackingId);
173
+        assertEquals(1, loadedCargo.itinerary().legs().size());
174
+    }
175
+
176
+    @Test
177
+    public void testReplaceItinerary() {
178
+        Cargo cargo = cargoRepository.find(new TrackingId("FGH"));
179
+        Long cargoId = getLongId(cargo);
180
+        assertEquals(3, jdbcTemplate.queryForInt("select count(*) from Leg where cargo_id = ?", cargoId));
155 181
 
156
-  public void testFindAll() {
157
-    List<Cargo> all = cargoRepository.findAll();
158
-    assertNotNull(all);
159
-    assertEquals(6, all.size());
160
-  }
182
+        Location legFrom = locationRepository.find(new UnLocode("FIHEL"));
183
+        Location legTo = locationRepository.find(new UnLocode("DEHAM"));
184
+        Itinerary newItinerary = new Itinerary(Arrays.asList(new Leg(CM004, legFrom, legTo, new Date(), new Date())));
161 185
 
162
-  public void testNextTrackingId() {
163
-    TrackingId trackingId = cargoRepository.nextTrackingId();
164
-    assertNotNull(trackingId);
186
+        cargo.assignToRoute(newItinerary);
165 187
 
166
-    TrackingId trackingId2 = cargoRepository.nextTrackingId();
167
-    assertNotNull(trackingId2);
168
-    assertFalse(trackingId.equals(trackingId2));
169
-  }
188
+        cargoRepository.store(cargo);
189
+        flush();
170 190
 
171
-}*/
191
+        assertEquals(1, jdbcTemplate.queryForInt("select count(*) from Leg where cargo_id = ?", cargoId));
192
+    }
193
+
194
+    @Test
195
+    public void testFindAll() {
196
+        List<Cargo> all = cargoRepository.findAll();
197
+        assertNotNull(all);
198
+        assertEquals(6, all.size());
199
+    }
200
+
201
+    @Test
202
+    public void testNextTrackingId() {
203
+        TrackingId trackingId = cargoRepository.nextTrackingId();
204
+        assertNotNull(trackingId);
205
+
206
+        TrackingId trackingId2 = cargoRepository.nextTrackingId();
207
+        assertNotNull(trackingId2);
208
+        assertFalse(trackingId.equals(trackingId2));
209
+    }
210
+
211
+
212
+    private void flush() {
213
+        sessionFactory.getCurrentSession().flush();
214
+    }
215
+
216
+    private Long getLongId(Object o) {
217
+        final Session session = sessionFactory.getCurrentSession();
218
+        if (session.contains(o)) {
219
+            return (Long) session.getIdentifier(o);
220
+        } else {
221
+            try {
222
+                Field id = o.getClass().getDeclaredField("id");
223
+                id.setAccessible(true);
224
+                return (Long) id.get(o);
225
+            } catch (Exception e) {
226
+                throw new RuntimeException();
227
+            }
228
+        }
229
+    }
230
+
231
+}

+ 64
- 27
src/test/java/se/citerus/dddsample/infrastructure/persistence/hibernate/CarrierMovementRepositoryTest.java Näytä tiedosto

@@ -1,27 +1,64 @@
1
-package se.citerus.dddsample.infrastructure.persistence.hibernate;
2
-
3
-import se.citerus.dddsample.domain.model.voyage.Voyage;
4
-import se.citerus.dddsample.domain.model.voyage.VoyageNumber;
5
-import se.citerus.dddsample.domain.model.voyage.VoyageRepository;
6
-
7
-//public class CarrierMovementRepositoryTest extends AbstractRepositoryTest {
8
-//
9
-//  VoyageRepository voyageRepository;
10
-//
11
-//  public void setVoyageRepository(VoyageRepository voyageRepository) {
12
-//    this.voyageRepository = voyageRepository;
13
-//  }
14
-//
15
-//  public void testFind() throws Exception {
16
-//    Voyage voyage = voyageRepository.find(new VoyageNumber("0101"));
17
-//    assertNotNull(voyage);
18
-//    assertEquals("0101", voyage.voyageNumber().idString());
19
-//    /* TODO adapt
20
-//    assertEquals(STOCKHOLM, carrierMovement.departureLocation());
21
-//    assertEquals(HELSINKI, carrierMovement.arrivalLocation());
22
-//    assertEquals(DateTestUtil.toDate("2007-09-23", "02:00"), carrierMovement.departureTime());
23
-//    assertEquals(DateTestUtil.toDate("2007-09-23", "03:00"), carrierMovement.arrivalTime());
24
-//    */
25
-//  }
26
-//
27
-//}
1
+package se.citerus.dddsample.infrastructure.persistence.hibernate;
2
+
3
+import org.hibernate.SessionFactory;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+import org.junit.runner.RunWith;
7
+import org.springframework.beans.factory.annotation.Autowired;
8
+import org.springframework.jdbc.core.JdbcTemplate;
9
+import org.springframework.orm.hibernate3.HibernateTransactionManager;
10
+import org.springframework.test.context.ContextConfiguration;
11
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12
+import org.springframework.test.context.transaction.TransactionConfiguration;
13
+import org.springframework.transaction.annotation.Transactional;
14
+import org.springframework.transaction.support.TransactionTemplate;
15
+import se.citerus.dddsample.application.util.SampleDataGenerator;
16
+import se.citerus.dddsample.domain.model.voyage.Voyage;
17
+import se.citerus.dddsample.domain.model.voyage.VoyageNumber;
18
+import se.citerus.dddsample.domain.model.voyage.VoyageRepository;
19
+
20
+import javax.sql.DataSource;
21
+
22
+import static org.junit.Assert.assertEquals;
23
+import static org.junit.Assert.assertNotNull;
24
+
25
+@RunWith(SpringJUnit4ClassRunner.class)
26
+@ContextConfiguration(value = {"/context-infrastructure-persistence.xml", "/context-domain.xml"})
27
+@TransactionConfiguration(transactionManager = "transactionManager")
28
+@Transactional
29
+public class CarrierMovementRepositoryTest {
30
+
31
+    @Autowired
32
+    VoyageRepository voyageRepository;
33
+
34
+    @Autowired
35
+    SessionFactory sessionFactory;
36
+
37
+    @Autowired
38
+    private DataSource dataSource;
39
+
40
+    @Autowired
41
+    private HibernateTransactionManager transactionManager;
42
+
43
+    private JdbcTemplate jdbcTemplate;
44
+
45
+    @Before
46
+    public void setup() {
47
+        jdbcTemplate = new JdbcTemplate(dataSource);
48
+        SampleDataGenerator.loadSampleData(jdbcTemplate, new TransactionTemplate(transactionManager));
49
+    }
50
+
51
+    @Test
52
+    public void testFind() throws Exception {
53
+        Voyage voyage = voyageRepository.find(new VoyageNumber("0101"));
54
+        assertNotNull(voyage);
55
+        assertEquals("0101", voyage.voyageNumber().idString());
56
+    /* TODO adapt
57
+    assertEquals(STOCKHOLM, carrierMovement.departureLocation());
58
+    assertEquals(HELSINKI, carrierMovement.arrivalLocation());
59
+    assertEquals(DateTestUtil.toDate("2007-09-23", "02:00"), carrierMovement.departureTime());
60
+    assertEquals(DateTestUtil.toDate("2007-09-23", "03:00"), carrierMovement.arrivalTime());
61
+    */
62
+    }
63
+
64
+}

+ 87
- 34
src/test/java/se/citerus/dddsample/infrastructure/persistence/hibernate/HandlingEventRepositoryTest.java Näytä tiedosto

@@ -1,5 +1,19 @@
1 1
 package se.citerus.dddsample.infrastructure.persistence.hibernate;
2 2
 
3
+import org.hibernate.SessionFactory;
4
+import org.hibernate.classic.Session;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+import org.junit.runner.RunWith;
8
+import org.springframework.beans.factory.annotation.Autowired;
9
+import org.springframework.jdbc.core.JdbcTemplate;
10
+import org.springframework.orm.hibernate3.HibernateTransactionManager;
11
+import org.springframework.test.context.ContextConfiguration;
12
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
13
+import org.springframework.test.context.transaction.TransactionConfiguration;
14
+import org.springframework.transaction.annotation.Transactional;
15
+import org.springframework.transaction.support.TransactionTemplate;
16
+import se.citerus.dddsample.application.util.SampleDataGenerator;
3 17
 import se.citerus.dddsample.domain.model.cargo.Cargo;
4 18
 import se.citerus.dddsample.domain.model.cargo.CargoRepository;
5 19
 import se.citerus.dddsample.domain.model.cargo.TrackingId;
@@ -9,52 +23,91 @@ import se.citerus.dddsample.domain.model.location.Location;
9 23
 import se.citerus.dddsample.domain.model.location.LocationRepository;
10 24
 import se.citerus.dddsample.domain.model.location.UnLocode;
11 25
 
26
+import javax.sql.DataSource;
27
+import java.lang.reflect.Field;
12 28
 import java.util.Date;
13 29
 import java.util.List;
14 30
 import java.util.Map;
15 31
 
16
-/*public class HandlingEventRepositoryTest extends AbstractRepositoryTest {
32
+import static org.junit.Assert.assertEquals;
17 33
 
18
-  HandlingEventRepository handlingEventRepository;
19
-  CargoRepository cargoRepository;
20
-  LocationRepository locationRepository;
34
+@RunWith(SpringJUnit4ClassRunner.class)
35
+@ContextConfiguration(value = {"/context-infrastructure-persistence.xml", "/context-domain.xml"})
36
+@TransactionConfiguration(transactionManager = "transactionManager")
37
+@Transactional
38
+public class HandlingEventRepositoryTest {
21 39
 
22
-  public void setHandlingEventRepository(HandlingEventRepository handlingEventRepository) {
23
-    this.handlingEventRepository = handlingEventRepository;
24
-  }
40
+    @Autowired
41
+    HandlingEventRepository handlingEventRepository;
25 42
 
26
-  public void setCargoRepository(CargoRepository cargoRepository) {
27
-    this.cargoRepository = cargoRepository;
28
-  }
43
+    @Autowired
44
+    CargoRepository cargoRepository;
29 45
 
30
-  public void setLocationRepository(LocationRepository locationRepository) {
31
-    this.locationRepository = locationRepository;
32
-  }
46
+    @Autowired
47
+    LocationRepository locationRepository;
33 48
 
34
-  public void testSave() {
35
-    Location location = locationRepository.find(new UnLocode("SESTO"));
49
+    @Autowired
50
+    SessionFactory sessionFactory;
36 51
 
37
-    Cargo cargo = cargoRepository.find(new TrackingId("XYZ"));
38
-    Date completionTime = new Date(10);
39
-    Date registrationTime = new Date(20);
40
-    HandlingEvent event = new HandlingEvent(cargo, completionTime, registrationTime, HandlingEvent.Type.CLAIM, location);
52
+    @Autowired
53
+    private DataSource dataSource;
41 54
 
42
-    handlingEventRepository.store(event);
55
+    @Autowired
56
+    private HibernateTransactionManager transactionManager;
43 57
 
44
-    flush();
58
+    private JdbcTemplate jdbcTemplate;
45 59
 
46
-    Map<String,Object> result = sjt.queryForMap("select * from HandlingEvent where id = ?", getLongId(event));
47
-    assertEquals(1L, result.get("CARGO_ID"));
48
-    assertEquals(new Date(10), result.get("COMPLETIONTIME"));
49
-    assertEquals(new Date(20), result.get("REGISTRATIONTIME"));
50
-    assertEquals("CLAIM", result.get("TYPE"));
51
-    // TODO: the rest of the columns
52
-  }
60
+    @Before
61
+    public void setup() {
62
+        jdbcTemplate = new JdbcTemplate(dataSource);
63
+        SampleDataGenerator.loadSampleData(jdbcTemplate, new TransactionTemplate(transactionManager));
64
+    }
53 65
 
54
-  public void testFindEventsForCargo() throws Exception {
55
-    TrackingId trackingId = new TrackingId("XYZ");
56
-    List<HandlingEvent> handlingEvents = handlingEventRepository.lookupHandlingHistoryOfCargo(trackingId).distinctEventsByCompletionTime();
57
-    assertEquals(12, handlingEvents.size());
58
-  }
66
+    @Test
67
+    public void testSave() {
68
+        Location location = locationRepository.find(new UnLocode("SESTO"));
59 69
 
60
-}*/
70
+        Cargo cargo = cargoRepository.find(new TrackingId("XYZ"));
71
+        Date completionTime = new Date(10);
72
+        Date registrationTime = new Date(20);
73
+        HandlingEvent event = new HandlingEvent(cargo, completionTime, registrationTime, HandlingEvent.Type.CLAIM, location);
74
+
75
+        handlingEventRepository.store(event);
76
+
77
+        flush();
78
+
79
+        Map<String, Object> result = jdbcTemplate.queryForMap("select * from HandlingEvent where id = ?", getLongId(event));
80
+        assertEquals(1L, result.get("CARGO_ID"));
81
+        assertEquals(new Date(10), result.get("COMPLETIONTIME"));
82
+        assertEquals(new Date(20), result.get("REGISTRATIONTIME"));
83
+        assertEquals("CLAIM", result.get("TYPE"));
84
+        // TODO: the rest of the columns
85
+    }
86
+
87
+    private void flush() {
88
+        sessionFactory.getCurrentSession().flush();
89
+    }
90
+
91
+    private Long getLongId(Object o) {
92
+        final Session session = sessionFactory.getCurrentSession();
93
+        if (session.contains(o)) {
94
+            return (Long) session.getIdentifier(o);
95
+        } else {
96
+            try {
97
+                Field id = o.getClass().getDeclaredField("id");
98
+                id.setAccessible(true);
99
+                return (Long) id.get(o);
100
+            } catch (Exception e) {
101
+                throw new RuntimeException();
102
+            }
103
+        }
104
+    }
105
+
106
+    @Test
107
+    public void testFindEventsForCargo() throws Exception {
108
+        TrackingId trackingId = new TrackingId("XYZ");
109
+        List<HandlingEvent> handlingEvents = handlingEventRepository.lookupHandlingHistoryOfCargo(trackingId).distinctEventsByCompletionTime();
110
+        assertEquals(12, handlingEvents.size());
111
+    }
112
+
113
+}

+ 54
- 23
src/test/java/se/citerus/dddsample/infrastructure/persistence/hibernate/LocationRepositoryTest.java Näytä tiedosto

@@ -1,31 +1,62 @@
1 1
 package se.citerus.dddsample.infrastructure.persistence.hibernate;
2 2
 
3
+import org.junit.Before;
4
+import org.junit.Test;
5
+import org.junit.runner.RunWith;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.jdbc.core.JdbcTemplate;
8
+import org.springframework.orm.hibernate3.HibernateTransactionManager;
9
+import org.springframework.test.context.ContextConfiguration;
10
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11
+import org.springframework.test.context.transaction.TransactionConfiguration;
12
+import org.springframework.transaction.annotation.Transactional;
13
+import org.springframework.transaction.support.TransactionTemplate;
14
+import se.citerus.dddsample.application.util.SampleDataGenerator;
3 15
 import se.citerus.dddsample.domain.model.location.Location;
4 16
 import se.citerus.dddsample.domain.model.location.LocationRepository;
5 17
 import se.citerus.dddsample.domain.model.location.UnLocode;
6 18
 
19
+import javax.sql.DataSource;
7 20
 import java.util.List;
8 21
 
9
-/*public class LocationRepositoryTest extends AbstractRepositoryTest {
10
-  private LocationRepository locationRepository;
11
-  
12
-  public void testFind() throws Exception {
13
-    final UnLocode melbourne = new UnLocode("AUMEL");
14
-    Location location = locationRepository.find(melbourne);
15
-    assertNotNull(location);
16
-    assertEquals(melbourne, location.unLocode());
17
-
18
-    assertNull(locationRepository.find(new UnLocode("NOLOC")));
19
-  }
20
-
21
-  public void testFindAll() throws Exception {
22
-    List<Location> allLocations = locationRepository.findAll();
23
-
24
-    assertNotNull(allLocations);
25
-    assertEquals(7, allLocations.size());
26
-  }
27
-
28
-  public void setLocationRepository(LocationRepository locationRepository) {
29
-    this.locationRepository = locationRepository;
30
-  }
31
-}*/
22
+import static org.junit.Assert.*;
23
+
24
+@RunWith(SpringJUnit4ClassRunner.class)
25
+@ContextConfiguration(value = {"/context-infrastructure-persistence.xml", "/context-domain.xml"})
26
+@TransactionConfiguration(transactionManager = "transactionManager")
27
+@Transactional
28
+public class LocationRepositoryTest {
29
+    @Autowired
30
+    private LocationRepository locationRepository;
31
+
32
+    @Autowired
33
+    private DataSource dataSource;
34
+
35
+    @Autowired
36
+    private HibernateTransactionManager transactionManager;
37
+
38
+    @Before
39
+    public void setup() {
40
+        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
41
+        SampleDataGenerator.loadSampleData(jdbcTemplate, new TransactionTemplate(transactionManager));
42
+    }
43
+
44
+    @Test
45
+    public void testFind() throws Exception {
46
+        final UnLocode melbourne = new UnLocode("AUMEL");
47
+        Location location = locationRepository.find(melbourne);
48
+        assertNotNull(location);
49
+        assertEquals(melbourne, location.unLocode());
50
+
51
+        assertNull(locationRepository.find(new UnLocode("NOLOC")));
52
+    }
53
+
54
+    @Test
55
+    public void testFindAll() throws Exception {
56
+        List<Location> allLocations = locationRepository.findAll();
57
+
58
+        assertNotNull(allLocations);
59
+        assertEquals(7, allLocations.size());
60
+    }
61
+
62
+}