8000 Refactor non-broker ClientTests to JUnit 4 · CassOnMars/rabbitmq-java-client@0c0c4f4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c0c4f4

Browse files
committed
Refactor non-broker ClientTests to JUnit 4
References rabbitmq#174
1 parent a4e9669 commit 0c0c4f4

13 files changed

+167
-234
lines changed

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

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,78 +15,55 @@
1515

1616
package com.rabbitmq.client.test;
1717

18+
import com.rabbitmq.client.*;
19+
import com.rabbitmq.client.impl.AMQConnection;
20+
import com.rabbitmq.client.impl.ConnectionParams;
21+
import com.rabbitmq.client.impl.Frame;
22+
import com.rabbitmq.client.impl.FrameHandler;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+ ED4F
1827
import java.io.IOException;
1928
import java.net.InetAddress;
2029
import java.net.SocketException;
2130
import java.net.SocketTimeoutException;
2231
import java.util.ArrayList;
32+
import java.util.Arrays;
2333
import java.util.Collections;
2434
import java.util.List;
35+
import java.util.concurrent.ExecutorService;
2536
import java.util.concurrent.Executors;
2637
import java.util.concurrent.TimeoutException;
27-
import java.util.concurrent.ExecutorService;
28-
import com.rabbitmq.client.Address;
29-
import java.util.Arrays;
3038

31-
import com.rabbitmq.client.impl.ConnectionParams;
32-
import com.rabbitmq.client.TopologyRecoveryException;
33-
import junit.framework.TestCase;
34-
import junit.framework.TestSuite;
35-
36-
import com.rabbitmq.client.Channel;
37-
import com.rabbitmq.client.Connection;
38-
import com.rabbitmq.client.ConnectionFactory;
39-
import com.rabbitmq.client.Consumer;
40-
import com.rabbitmq.client.impl.AMQConnection;
41-
import com.rabbitmq.client.ExceptionHandler;
42-
import com.rabbitmq.client.impl.Frame;
43-
import com.rabbitmq.client.impl.FrameHandler;
39+
import static org.junit.Assert.assertEquals;
40+
import static org.junit.Assert.fail;
4441

4542
/**
4643
* Test suite for AMQConnection.
4744
*/
4845

49-
public class AMQConnectionTest extends TestCase {
46+
public class AMQConnectionTest {
5047
// private static final String CLOSE_MESSAGE = "terminated by test";
5148

52-
/**
53-
* Build a suite of tests
54-
* @ 1E79 return the test suite for this class
55-
*/
56-
public static TestSuite suite() {
57-
TestSuite suite = new TestSuite("connection");
58-
suite.addTestSuite(AMQConnectionTest.class);
59-
return suite;
60-
}
61-
6249
/** The mock frame handler used to test connection behaviour. */
6350
private MockFrameHandler _mockFrameHandler;
6451
private ConnectionFactory factory;
6552
private MyExceptionHandler exceptionHandler;
6653

67-
/** Setup the environment for this test
68-
* @see junit.framework.TestCase#setUp()
69-
* @throws Exception if anything goes wrong
70-
*/
71-
@Override protected void setUp() throws Exception {
72-
super.setUp();
54+
@Before public void setUp() throws Exception {
7355
_mockFrameHandler = new MockFrameHandler();
7456
factory = new ConnectionFactory();
7557
exceptionHandler = new MyExceptionHandler();
7658
factory.setExceptionHandler(exceptionHandler);
7759
}
7860

79-
/** Tear down the environment for this test
80-
* @see junit.framework.TestCase#tearDown()
81-
* @throws Exception if anything goes wrong
82-
*/
83-
@Override protected void tearDown() throws Exception {
61+
@After public void tearDown() throws Exception {
8462
factory = null;
8563
_mockFrameHandler = null;
86-
super.tearDown();
8764
}
8865

89-
public void testNegativeTCPConnectionTimeout() {
66+
@Test public void negativeTCPConnectionTimeout() {
9067
ConnectionFactory cf = new ConnectionFactory();
9168
try {
9269
cf.setConnectionTimeout(-10);
@@ -96,7 +73,7 @@ public void testNegativeTCPConnectionTimeout() {
9673
}
9774
}
9875

99-
public void testNegativeProtocolHandshakeTimeout() {
76+
@Test public void negativeProtocolHandshakeTimeout() {
10077
ConnectionFactory cf = new ConnectionFactory();
10178
try {
10279
cf.setHandshakeTimeout(-10);
@@ -106,13 +83,13 @@ public void testNegativeProtocolHandshakeTimeout() {
10683
}
10784
}
10885

109-
public void testTCPConnectionTimeoutGreaterThanHandShakeTimeout() {
86+
@Test public void tcpConnectionTimeoutGreaterThanHandShakeTimeout() {
11087
ConnectionFactory cf = new ConnectionFactory();
11188
cf.setHandshakeTimeout(3000);
11289
cf.setConnectionTimeout(5000);
11390
}
11491

115-
public void testProtocolHandshakeTimeoutGreaterThanTCPConnectionTimeout() {
92+
@Test public void protocolHandshakeTimeoutGreaterThanTCPConnectionTimeout() {
11693
ConnectionFactory cf = new ConnectionFactory();
11794

11895
cf.setConnectionTimeout(5000);
@@ -125,7 +102,7 @@ public void testProtocolHandshakeTimeoutGreaterThanTCPConnectionTimeout() {
125102
/** Check the AMQConnection does send exactly 1 initial header, and deal correctly with
126103
* the frame handler throwing an exception when we try to read data
127104
*/
128-
public void testConnectionSendsSingleHeaderAndTimesOut() throws TimeoutException {
105+
@Test public void connectionSendsSingleHeaderAndTimesOut() throws TimeoutException {
129106
IOException exception = new SocketTimeoutException();
130107
_mockFrameHandler.setExceptionOnReadingFrames(exception);
131108
assertEquals(0, _mockFrameHandler.countHeadersSent());
@@ -158,7 +135,7 @@ public void testConnectionSendsSingleHeaderAndTimesOut() throws TimeoutException
158135
/**
159136
* Test that we catch timeout between connect and negotiation of the connection being finished.
160137
*/
161-
public void testConnectionHangInNegotiation() {
138+
@Test public void connectionHangInNegotiation() {
162139
this._mockFrameHandler.setTimeoutCount(10); // to limit hang
163140
assertEquals(0, this._mockFrameHandler.countHeadersSent());
164141
try {
@@ -176,7 +153,7 @@ public void testConnectionHangInNegotiation() {
176153
assertEquals("Wrong type of exception returned.", SocketTimeoutException.class, exceptionList.get(0).getClass());
177154
}
178155

179-
public void testClientProvidedConnectionName() throws IOException, TimeoutException {
156+
@Test public void clientProvidedConnectionName() throws IOException, TimeoutException {
180157
String providedName = "event consumers connection";
181158
Connection connection = factory.newConnection(providedName);
182159
assertEquals(providedName, connection.getClientProvidedName());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
import com.rabbitmq.tools.Host;
2828

29-
public abstract class AbstractRMQTestSuite extends TestSuite {
29+
public abstract class AbstractRMQTestSuite { //extends TestSuite {
3030
private static final String DEFAULT_SSL_HOSTNAME = "localhost";
3131
private static final int DEFAULT_SSL_PORT = 5671;
3232

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

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,19 @@
1616

1717
package com.rabbitmq.client.test;
1818

19+
import com.rabbitmq.utility.BlockingCell;
20+
import org.junit.Test;
21+
1922
import java.util.concurrent.atomic.AtomicBoolean;
2023
import java.util.concurrent.atomic.AtomicReference;
2124

22-
import junit.framework.TestCase;
23-
import junit.framework.TestSuite;
24-
25-
import com.rabbitmq.utility.BlockingCell;
25+
import static org.junit.Assert.*;
2626

2727
public class BlockingCellTest
28-
extends TestCase
2928
{
30-
public static TestSuite suite()
31-
{
32-
TestSuite suite = new TestSuite("blockingCells");
33-
suite.addTestSuite(BlockingCellTest.class);
34-
return suite;
35-
}
3629

37-
public void testDoubleSet() throws InterruptedException
30+
@Test
31+
public void doubleSet() throws InterruptedException
3832
{
3933
BlockingCell<String> cell = new BlockingCell<String>();
4034
cell.set("one");
@@ -47,7 +41,7 @@ public void testDoubleSet() throws InterruptedException
4741
fail("Expected AssertionError");
4842
}
4943

50-
public void testMultiGet()
44+
@Test public void multiGet()
5145
throws InterruptedException
5246
{
5347
final BlockingCell<String> cell = new BlockingCell<String>();
@@ -56,15 +50,15 @@ public void testMultiGet()
5650
assertEquals("one", cell.get());
5751
}
5852

59-
public void testNullSet()
53+
@Test public void nullSet()
6054
throws InterruptedException
6155
{
6256
BlockingCell<Integer> c = new BlockingCell<Integer>();
6357
c.set(null);
6458
assertNull(c.get());
6559
}
6660

67-
public void testEarlySet()
61+
@Test public void earlySet()
6862
throws InterruptedException
6963
{
7064
final BlockingCell<String> cell = new BlockingCell<String>();
@@ -95,7 +89,7 @@ public void run() {
9589
assertEquals("hello", holder.get());
9690
}
9791

98-
public void testLateSet()
92+
@Test public void lateSet()
9993
throws InterruptedException
10094
{
10195
final BlockingCell<String> cell = new BlockingCell<String>();
@@ -129,8 +123,8 @@ public void run() {
129123

130124
assertEquals("hello", holder.get());
131125
}
132-
133-
public void testGetWaitsUntilSet() throws InterruptedException {
126+
127+
@Test public void getWaitsUntilSet() throws InterruptedException {
134128
final BlockingCell<String> cell = new BlockingCell<String>();
135129
final String value = "foo";
136130
final AtomicReference<Object> valueHolder = new AtomicReference<Object>();
@@ -155,7 +149,7 @@ public void testGetWaitsUntilSet() throws InterruptedException {
155149
assertTrue(value == valueHolder.get());
156150
}
157151

158-
public void testSetIfUnset() throws InterruptedException {
152+
@Test public void setIfUnset() throws InterruptedException {
159153
final BlockingCell<String> cell = new BlockingCell<String>();
160154
assertTrue(cell.setIfUnset("foo"));
161155
assertEquals("foo", cell.get());

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

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@
1616

1717
package com.rabbitmq.client.test;
1818

19+
import com.rabbitmq.client.AMQP;
20+
import com.rabbitmq.client.ConnectionFactory;
21+
import com.rabbitmq.client.UnexpectedFrameError;
22+
import com.rabbitmq.client.impl.AMQConnection;
23+
import com.rabbitmq.client.impl.AMQImpl.Basic.Publish;
24+
import com.rabbitmq.client.impl.Frame;
25+
import com.rabbitmq.client.impl.FrameHandler;
26+
import org.junit.After;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
1930
import java.io.IOException;
2031
import java.net.InetAddress;
2132
import java.net.SocketException;
@@ -24,42 +35,24 @@
2435
import java.util.List;
2536
import java.util.concurrent.Executors;
2637

27-
import junit.framework.TestCase;
28-
import junit.framework.TestSuite;
29-
30-
import com.rabbitmq.client.AMQP;
31-
import com.rabbitmq.client.ConnectionFactory;
32-
import com.rabbitmq.client.UnexpectedFrameError;
33-
import com.rabbitmq.client.impl.AMQConnection;
34-
import com.rabbitmq.client.impl.Frame;
35-
import com.rabbitmq.client.impl.FrameHandler;
36-
import com.rabbitmq.client.impl.AMQImpl.Basic.Publish;
38+
import static org.junit.Assert.*;
3739

38-
public class BrokenFramesTest extends TestCase {
39-
public static TestSuite suite() {
40-
TestSuite suite = new TestSuite("connection");
41-
suite.addTestSuite(BrokenFramesTest.class);
42-
return suite;
43-
}
40+
public class BrokenFramesTest {
4441

4542
private MyFrameHandler myFrameHandler;
4643
private ConnectionFactory factory;
4744

48-
@Override
49-
protected void setUp() throws Exception {
50-
super.setUp();
45+
@Before public void setUp() throws Exception {
5146
myFrameHandler = new MyFrameHandler();
5247
factory = new ConnectionFactory();
5348
}
5449

55-
@Override
56-
protected void tearDown() throws Exception {
50+
@After public void tearDown() throws Exception {
5751
factory = null;
5852
myFrameHandler = null;
59-
super.tearDown();
6053
}
6154

62-
public void testNoMethod() throws Exception {
55+
@Test public void noMethod() throws Exception {
6356
List<Frame> frames = new ArrayList<Frame>();
6457
frames.add(new Frame(AMQP.FRAME_HEADER, 0));
6558
myFrameHandler.setFrames(frames.iterator());
@@ -77,7 +70,7 @@ public void testNoMethod() throws Exception {
7770
fail("No UnexpectedFrameError thrown");
7871
}
7972

80-
public void testMethodThenBody() throws Exception {
73+
@Test public void methodThenBody() throws Exception {
8174
List<Frame> frames = new ArrayList<Frame>();
8275

8376
byte[] contentBody = new byte[10];
@@ -116,14 +109,14 @@ private UnexpectedFrameError findUnexpectedFrameError(Exception e) {
116109
}
117110

118111
private static class MyFrameHandler implements FrameHandler {
119-
private Iterator<Frame> frames;
112+
private Iterator<Frame> frames;
120113

121-
public void setFrames(Iterator<Frame> frames) {
122-
this.frames = frames;
123-
}
114+
public void setFrames(Iterator<Frame> frames) {
115+
this.frames = frames;
116+
}
124117

125-
public Frame readFrame() throws IOException {
126-
return frames.next();
118+
public Frame readFrame() throws IOException {
119+
return frames.next();
127120
}
128121

129122
public void sendHeader() throws IOException {

0 commit comments

Comments
 (0)
0