|
|
@@ -1,38 +1,56 @@
|
|
1
|
1
|
package se.citerus.dddsample.service;
|
|
2
|
2
|
|
|
3
|
|
-import org.aopalliance.aop.Advice;
|
|
4
|
|
-import org.aopalliance.intercept.MethodInvocation;
|
|
5
|
|
-import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;
|
|
|
3
|
+import static org.easymock.EasyMock.*;
|
|
6
|
4
|
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
|
|
7
|
|
-import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
|
|
5
|
+import org.springframework.transaction.PlatformTransactionManager;
|
|
|
6
|
+import org.springframework.transaction.TransactionDefinition;
|
|
|
7
|
+import org.springframework.transaction.TransactionStatus;
|
|
8
|
8
|
import se.citerus.dddsample.domain.Cargo;
|
|
|
9
|
+import se.citerus.dddsample.domain.Location;
|
|
9
|
10
|
import se.citerus.dddsample.domain.TrackingId;
|
|
10
|
|
-import se.citerus.dddsample.repository.CargoRepositoryInMem;
|
|
|
11
|
+import se.citerus.dddsample.repository.CargoRepository;
|
|
11
|
12
|
|
|
12
|
13
|
|
|
13
|
14
|
public class CargoServiceTest extends AbstractDependencyInjectionSpringContextTests {
|
|
14
|
15
|
|
|
15
|
|
- // TODO:
|
|
16
|
|
- // find nice way to test context configuration and
|
|
17
|
|
- // transaction existence (and attributes),
|
|
18
|
|
- // with repository layer stubbed out. Sketchy atm.
|
|
19
|
|
-
|
|
20
|
16
|
CargoService cargoService;
|
|
21
|
|
- CargoRepositoryInMem cargoRepository;
|
|
|
17
|
+ CargoRepository cargoRepository;
|
|
|
18
|
+ PlatformTransactionManager transactionManager;
|
|
22
|
19
|
|
|
23
|
20
|
public CargoServiceTest() {
|
|
24
|
21
|
setDependencyCheck(false);
|
|
25
|
22
|
}
|
|
26
|
23
|
|
|
27
|
24
|
public void setCargoService(CargoService cargoService) {
|
|
28
|
|
- AspectJProxyFactory factory = new AspectJProxyFactory(cargoRepository);
|
|
29
|
|
- factory.addAdvice(new TransactionVerifier());
|
|
30
|
|
- this.cargoRepository = factory.getProxy();
|
|
31
|
25
|
this.cargoService = cargoService;
|
|
32
|
26
|
}
|
|
33
|
27
|
|
|
|
28
|
+ public void setCargoRepository(CargoRepository cargoRepository) {
|
|
|
29
|
+ this.cargoRepository = cargoRepository;
|
|
|
30
|
+ }
|
|
|
31
|
+
|
|
|
32
|
+ public void setTransactionManager(PlatformTransactionManager transactionManager) {
|
|
|
33
|
+ this.transactionManager = transactionManager;
|
|
|
34
|
+ }
|
|
|
35
|
+
|
|
|
36
|
+ @Override
|
|
|
37
|
+ protected void prepareTestInstance() throws Exception {
|
|
|
38
|
+ // TODO: verify an existing transaction inside the repository mock instead, this smells like we're testing framework code...
|
|
|
39
|
+ super.prepareTestInstance();
|
|
|
40
|
+ expect(cargoRepository.find(new TrackingId("XYZ"))).
|
|
|
41
|
+ andReturn(new Cargo(new TrackingId("XYZ"), new Location("ORIG"), new Location("DEST")));
|
|
|
42
|
+ replay(cargoRepository);
|
|
|
43
|
+
|
|
|
44
|
+ TransactionStatus ts = createMock(TransactionStatus.class);
|
|
|
45
|
+ replay(ts);
|
|
|
46
|
+ expect(transactionManager.getTransaction(isA(TransactionDefinition.class))).
|
|
|
47
|
+ andReturn(ts);
|
|
|
48
|
+ transactionManager.commit(isA(TransactionStatus.class));
|
|
|
49
|
+ replay(transactionManager);
|
|
|
50
|
+ }
|
|
|
51
|
+
|
|
34
|
52
|
protected String[] getConfigLocations() {
|
|
35
|
|
- return new String[] { "context-service.xml" };
|
|
|
53
|
+ return new String[] { "context-service.xml", "mock-context-persistence.xml" };
|
|
36
|
54
|
}
|
|
37
|
55
|
|
|
38
|
56
|
public void testCargoServiceFindByTrackingIdScenario() throws Exception {
|
|
|
@@ -41,13 +59,7 @@ public class CargoServiceTest extends AbstractDependencyInjectionSpringContextTe
|
|
41
|
59
|
assertEquals(new TrackingId("XYZ"), cargo.trackingId());
|
|
42
|
60
|
}
|
|
43
|
61
|
|
|
44
|
|
- private static class TransactionVerifier implements Advice {
|
|
45
|
|
- public Object invoke(MethodInvocation methodInvocation) throws Throwable {
|
|
46
|
|
- if (TransactionAspectSupport.currentTransactionStatus() == null) {
|
|
47
|
|
- fail("Transaction is manadatory");
|
|
48
|
|
- }
|
|
49
|
|
- return methodInvocation.proceed();
|
|
50
|
|
- }
|
|
|
62
|
+ protected void onTearDown() throws Exception {
|
|
|
63
|
+ verify(cargoRepository, transactionManager);
|
|
51
|
64
|
}
|
|
52
|
|
-
|
|
53
|
65
|
}
|