Skip to content

Commit c3eef35

Browse files
committed
Refactor test to Java 6
1 parent 0afdae4 commit c3eef35

File tree

1 file changed

+41
-14
lines changed

1 file changed

+41
-14
lines changed

src/test/java/com/rabbitmq/client/test/TrafficListenerTest.java

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@
2020
import com.rabbitmq.client.Command;
2121
import com.rabbitmq.client.Connection;
2222
import com.rabbitmq.client.ConnectionFactory;
23+
import com.rabbitmq.client.DefaultConsumer;
24+
import com.rabbitmq.client.Envelope;
2325
import com.rabbitmq.client.TrafficListener;
2426
import org.junit.Test;
2527
import org.junit.runner.RunWith;
2628
import org.junit.runners.Parameterized;
2729

30+
import java.io.IOException;
2831
import java.util.List;
2932
import java.util.UUID;
3033
import java.util.concurrent.CopyOnWriteArrayList;
3134
import java.util.concurrent.CountDownLatch;
3235
import java.util.concurrent.TimeUnit;
33-
import java.util.function.Consumer;
3436

3537
import static org.junit.Assert.assertEquals;
3638
import static org.junit.Assert.assertTrue;
@@ -42,48 +44,73 @@
4244
public class TrafficListenerTest {
4345

4446
@Parameterized.Parameter
45-
public Consumer<ConnectionFactory> configurator;
47+
public ConnectionFactoryConfigurator configurator;
4648

4749
@Parameterized.Parameters
4850
public static Object[] data() {
4951
return new Object[] { automaticRecoveryEnabled(), automaticRecoveryDisabled() };
5052
}
5153

52-
static Consumer<ConnectionFactory> automaticRecoveryEnabled() {
53-
return cf -> cf.setAutomaticRecoveryEnabled(true);
54+
static ConnectionFactoryConfigurator automaticRecoveryEnabled() {
55+
return new ConnectionFactoryConfigurator() {
56+
57+
@Override
58+
public void configure(ConnectionFactory cf) {
59+
cf.setAutomaticRecoveryEnabled(true);
60+
}
61+
};
5462
}
5563

56-
static Consumer<ConnectionFactory> automaticRecoveryDisabled() {
57-
return cf -> cf.setAutomaticRecoveryEnabled(false);
64+
static ConnectionFactoryConfigurator automaticRecoveryDisabled() {
65+
return new ConnectionFactoryConfigurator() {
66+
67+
@Override
68+
public void configure(ConnectionFactory cf) {
69+
cf.setAutomaticRecoveryEnabled(false);
70+
}
71+
};
5872
}
5973

6074
@Test
6175
public void trafficListenerIsCalled() throws Exception {
6276
ConnectionFactory cf = TestUtils.connectionFactory();
6377
TestTrafficListener testTrafficListener = new TestTrafficListener();
6478
cf.setTrafficListener(testTrafficListener);
65-
configurator.accept(cf);
66-
try (Connection c = cf.newConnection()) {
79+
configurator.configure(cf);
80+
Connection c = cf.newConnection();
81+
try {
6782
Channel ch = c.createChannel();
6883
String queue = ch.queueDeclare().getQueue();
69-
CountDownLatch latch = new CountDownLatch(1);
70-
ch.basicConsume(queue, true,
71-
(consumerTag, message) -> latch.countDown(), consumerTag -> {
72-
});
84+
final CountDownLatch latch = new CountDownLatch(1);
85+
ch.basicConsume(queue, true, new DefaultConsumer(ch) {
86+
87+
@Override
88+
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
89+
latch.countDown();
90+
}
91+
}
92+
);
7393
String messageContent = UUID.randomUUID().toString();
7494
ch.basicPublish("", queue, null, messageContent.getBytes());
7595
assertTrue(latch.await(5, TimeUnit.SECONDS));
7696
assertEquals(1, testTrafficListener.outboundContent.size());
7797
assertEquals(messageContent, testTrafficListener.outboundContent.get(0));
7898
assertEquals(1, testTrafficListener.inboundContent.size());
7999
assertEquals(messageContent, testTrafficListener.inboundContent.get(0));
100+
} finally {
101+
TestUtils.close(c);
80102
}
81103
}
82104

105+
interface ConnectionFactoryConfigurator {
106+
107+
void configure(ConnectionFactory connectionFactory);
108+
}
109+
83110
private static class TestTrafficListener implements TrafficListener {
84111

85-
final List<String> outboundContent = new CopyOnWriteArrayList<>();
86-
final List<String> inboundContent = new CopyOnWriteArrayList<>();
112+
final List<String> outboundContent = new CopyOnWriteArrayList<String>();
113+
final List<String> inboundContent = new CopyOnWriteArrayList<String>();
87114

88115
@Override
89116
public void write(Command outboundCommand) {

0 commit comments

Comments
 (0)