Skip to content

Commit be23d07

Browse files
author
Dan
committed
Fixed HandlingRepositoryTest
1 parent 1e6ed67 commit be23d07

File tree

1 file changed

+87
-34
lines changed

1 file changed

+87
-34
lines changed
Lines changed: 87 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package se.citerus.dddsample.infrastructure.persistence.hibernate;
22

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;
317
import se.citerus.dddsample.domain.model.cargo.Cargo;
418
import se.citerus.dddsample.domain.model.cargo.CargoRepository;
519
import se.citerus.dddsample.domain.model.cargo.TrackingId;
@@ -9,52 +23,91 @@
923
import se.citerus.dddsample.domain.model.location.LocationRepository;
1024
import se.citerus.dddsample.domain.model.location.UnLocode;
1125

26+
import javax.sql.DataSource;
27+
import java.lang.reflect.Field;
1228
import java.util.Date;
1329
import java.util.List;
1430
import java.util.Map;
1531

16-
/*public class HandlingEventRepositoryTest extends AbstractRepositoryTest {
32+
import static org.junit.Assert.assertEquals;
1733

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 {
2139

22-
public void setHandlingEventRepository(HandlingEventRepository handlingEventRepository) {
23-
this.handlingEventRepository = handlingEventRepository;
24-
}
40+
@Autowired
41+
HandlingEventRepository handlingEventRepository;
2542

26-
public void setCargoRepository(CargoRepository cargoRepository) {
27-
this.cargoRepository = cargoRepository;
28-
}
43+
@Autowired
44+
CargoRepository cargoRepository;
2945

30-
public void setLocationRepository(LocationRepository locationRepository) {
31-
this.locationRepository = locationRepository;
32-
}
46+
@Autowired
47+
LocationRepository locationRepository;
3348

34-
public void testSave() {
35-
Location location = locationRepository.find(new UnLocode("SESTO"));
49+
@Autowired
50+
SessionFactory sessionFactory;
3651

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;
4154

42-
handlingEventRepository.store(event);
55+
@Autowired
56+
private HibernateTransactionManager transactionManager;
4357

44-
flush();
58+
private JdbcTemplate jdbcTemplate;
4559

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

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"));
5969

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

0 commit comments

Comments
 (0)