|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.amqp.rabbit.core; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.mockito.BDDMockito.given; |
| 21 | +import static org.mockito.Mockito.mock; |
| 22 | +import static org.mockito.Mockito.verify; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import org.springframework.amqp.rabbit.connection.Connection; |
| 30 | +import org.springframework.amqp.rabbit.connection.ConnectionFactory; |
| 31 | +import org.springframework.beans.factory.annotation.Autowired; |
| 32 | +import org.springframework.context.ApplicationEventPublisher; |
| 33 | +import org.springframework.context.annotation.Bean; |
| 34 | +import org.springframework.context.annotation.Configuration; |
| 35 | +import org.springframework.test.annotation.DirtiesContext; |
| 36 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 37 | +import org.springframework.transaction.PlatformTransactionManager; |
| 38 | +import org.springframework.transaction.TransactionDefinition; |
| 39 | +import org.springframework.transaction.TransactionException; |
| 40 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 41 | +import org.springframework.transaction.annotation.Transactional; |
| 42 | +import org.springframework.transaction.event.TransactionPhase; |
| 43 | +import org.springframework.transaction.event.TransactionalEventListener; |
| 44 | +import org.springframework.transaction.support.AbstractPlatformTransactionManager; |
| 45 | +import org.springframework.transaction.support.DefaultTransactionStatus; |
| 46 | + |
| 47 | +import com.rabbitmq.client.Channel; |
| 48 | + |
| 49 | +/** |
| 50 | + * @author Gary Russell |
| 51 | + * @since 2.2.16 |
| 52 | + * |
| 53 | + */ |
| 54 | +@SpringJUnitConfig |
| 55 | +@DirtiesContext |
| 56 | +public class TransactionalEventListenerTests { |
| 57 | + |
| 58 | + @Test |
| 59 | + void txCommits(@Autowired Config config, @Autowired AtomicBoolean committed, |
| 60 | + @Autowired Channel channel) throws IOException { |
| 61 | + |
| 62 | + config.publish(); |
| 63 | + assertThat(committed.get()).isTrue(); |
| 64 | + verify(channel).txCommit(); |
| 65 | + } |
| 66 | + |
| 67 | + @Configuration(proxyBeanMethods = false) |
| 68 | + @EnableTransactionManagement |
| 69 | + public static class Config { |
| 70 | + |
| 71 | + @Autowired |
| 72 | + ApplicationEventPublisher publisher; |
| 73 | + |
| 74 | + @Autowired |
| 75 | + RabbitTemplate template; |
| 76 | + |
| 77 | + @Bean |
| 78 | + AtomicBoolean committed() { |
| 79 | + return new AtomicBoolean(); |
| 80 | + } |
| 81 | + |
| 82 | + @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) |
| 83 | + public void el(String in) { |
| 84 | + this.template.convertAndSend("test"); |
| 85 | + } |
| 86 | + |
| 87 | + @Bean |
| 88 | + RabbitTemplate template(ConnectionFactory cf) { |
| 89 | + RabbitTemplate template = new RabbitTemplate(cf); |
| 90 | + template.setChannelTransacted(true); |
| 91 | + return template; |
| 92 | + } |
| 93 | + |
| 94 | + @Bean |
| 95 | + Channel channel() { |
| 96 | + return mock(Channel.class); |
| 97 | + } |
| 98 | + |
| 99 | + @Bean |
| 100 | + ConnectionFactory cf(Channel channel) { |
| 101 | + ConnectionFactory cf = mock(ConnectionFactory.class); |
| 102 | + Connection conn = mock(Connection.class); |
| 103 | + given(conn.isOpen()).willReturn(true); |
| 104 | + given(cf.createConnection()).willReturn(conn); |
| 105 | + given(conn.createChannel(true)).willReturn(channel); |
| 106 | + given(channel.isOpen()).willReturn(true); |
| 107 | + return cf; |
| 108 | + } |
| 109 | + |
| 110 | + @Transactional |
| 111 | + public void publish() { |
| 112 | + publisher.publishEvent("test"); |
| 113 | + } |
| 114 | + |
| 115 | + @Bean |
| 116 | + PlatformTransactionManager transactionManager(AtomicBoolean committed) { |
| 117 | + return new AbstractPlatformTransactionManager() { |
| 118 | + |
| 119 | + @Override |
| 120 | + protected void doRollback(DefaultTransactionStatus status) throws TransactionException { |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + protected Object doGetTransaction() throws TransactionException { |
| 125 | + return new Object(); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + protected void doCommit(DefaultTransactionStatus status) throws TransactionException { |
| 130 | + committed.set(true); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException { |
| 135 | + } |
| 136 | + }; |
| 137 | + } |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments