瀏覽代碼

Fixed HandlingRepositoryTest

Dan 11 年之前
父節點
當前提交
be23d07d56

+ 87
- 34
src/test/java/se/citerus/dddsample/infrastructure/persistence/hibernate/HandlingEventRepositoryTest.java 查看文件

@@ -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
+}