|
17 | 17 |
|
18 | 18 | import static org.junit.Assert.assertEquals;
|
19 | 19 | import static org.junit.Assert.assertNotNull;
|
20 |
| -import static org.junit.Assert.assertNull; |
21 | 20 | import static org.junit.Assert.assertSame;
|
22 |
| -import static org.junit.Assert.assertTrue; |
23 |
| - |
24 |
| -import java.util.concurrent.CountDownLatch; |
25 |
| -import java.util.concurrent.TimeUnit; |
26 | 21 |
|
| 22 | +import org.junit.Before; |
27 | 23 | import org.junit.Test;
|
28 | 24 | import org.junit.runner.RunWith;
|
| 25 | + |
29 | 26 | import org.springframework.beans.factory.annotation.Autowired;
|
| 27 | +import org.springframework.integration.support.MessageBuilder; |
30 | 28 | import org.springframework.messaging.Message;
|
31 |
| -import org.springframework.messaging.PollableChannel; |
32 | 29 | import org.springframework.messaging.support.GenericMessage;
|
33 |
| -import org.springframework.integration.support.MessageBuilder; |
| 30 | +import org.springframework.test.annotation.DirtiesContext; |
34 | 31 | import org.springframework.test.context.ContextConfiguration;
|
35 | 32 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
36 | 33 |
|
|
41 | 38 | */
|
42 | 39 | @ContextConfiguration
|
43 | 40 | @RunWith(SpringJUnit4ClassRunner.class)
|
| 41 | +@DirtiesContext |
44 | 42 | public class TransactionSynchronizationQueueChannelTests {
|
45 | 43 |
|
46 | 44 | @Autowired
|
47 |
| - private PollableChannel queueChannel; |
| 45 | + private QueueChannel queueChannel; |
48 | 46 |
|
49 | 47 | @Autowired
|
50 |
| - private PollableChannel good; |
| 48 | + private QueueChannel good; |
51 | 49 |
|
52 | 50 | @Autowired
|
53 |
| - private Service service; |
| 51 | + private QueueChannel queueChannel2; |
54 | 52 |
|
55 |
| - @Autowired |
56 |
| - private PollableChannel queueChannel2; |
| 53 | + @Before |
| 54 | + public void setup() { |
| 55 | + this.good.purge(null); |
| 56 | + this.queueChannel.purge(null); |
| 57 | + this.queueChannel2.purge(null); |
| 58 | + } |
57 | 59 |
|
58 | 60 | @Test
|
59 | 61 | public void testCommit() throws Exception {
|
60 |
| - service.latch = new CountDownLatch(1); |
61 |
| - GenericMessage<String> sentMessage = new GenericMessage<String>("hello"); |
62 |
| - queueChannel.send(sentMessage); |
63 |
| - assertTrue(service.latch.await(10, TimeUnit.SECONDS)); |
64 |
| - Message<?> message = good.receive(1000); |
| 62 | + GenericMessage<String> sentMessage = new GenericMessage<>("hello"); |
| 63 | + this.queueChannel.send(sentMessage); |
| 64 | + Message<?> message = this.good.receive(10000); |
65 | 65 | assertNotNull(message);
|
66 | 66 | assertEquals("hello", message.getPayload());
|
67 | 67 | assertSame(message, sentMessage);
|
68 | 68 | }
|
69 | 69 |
|
70 | 70 | @Test
|
71 | 71 | public void testRollback() throws Exception {
|
72 |
| - service.latch = new CountDownLatch(1); |
73 |
| - queueChannel.send(new GenericMessage<String>("fail")); |
74 |
| - assertTrue(service.latch.await(10, TimeUnit.SECONDS)); |
75 |
| - Message<?> message = queueChannel.receive(1000); |
| 72 | + this.queueChannel.send(new GenericMessage<>("fail")); |
| 73 | + Message<?> message = this.good.receive(10000); |
76 | 74 | assertNotNull(message);
|
77 | 75 | assertEquals("retry:fail", message.getPayload());
|
78 |
| - assertNull(good.receive(0)); |
79 | 76 | }
|
80 | 77 |
|
81 | 78 | @Test
|
82 | 79 | public void testIncludeChannelName() throws Exception {
|
83 |
| - service.latch = new CountDownLatch(1); |
84 | 80 | Message<String> sentMessage = MessageBuilder.withPayload("hello")
|
85 | 81 | .setHeader("foo", "bar").build();
|
86 | 82 | queueChannel2.send(sentMessage);
|
87 |
| - assertTrue(service.latch.await(10, TimeUnit.SECONDS)); |
88 |
| - Message<?> message = good.receive(1000); |
| 83 | + Message<?> message = good.receive(10000); |
89 | 84 | assertNotNull(message);
|
90 | 85 | assertEquals("hello processed ok from queueChannel2", message.getPayload());
|
91 | 86 | assertNotNull(message.getHeaders().get("foo"));
|
92 | 87 | assertEquals("bar", message.getHeaders().get("foo"));
|
93 |
| - } |
| 88 | + } |
94 | 89 |
|
95 | 90 | public static class Service {
|
96 |
| - private CountDownLatch latch; |
97 | 91 |
|
98 | 92 | public void handle(String foo) {
|
99 |
| - latch.countDown(); |
100 | 93 | if (foo.startsWith("fail")) {
|
101 | 94 | throw new RuntimeException("planned failure");
|
102 | 95 | }
|
103 | 96 | }
|
| 97 | + |
104 | 98 | }
|
105 | 99 |
|
106 | 100 | }
|
0 commit comments