|
37 | 37 | import com.rabbitmq.client.AMQP; |
38 | 38 | import com.rabbitmq.client.MalformedFrameException; |
39 | 39 | import com.rabbitmq.client.impl.Method; |
40 | | -import com.rabbitmq.client.impl.Frame; |
41 | 40 | import com.rabbitmq.client.impl.SocketFrameHandler; |
42 | 41 | import com.rabbitmq.client.impl.AMQCommand; |
43 | 42 | import com.rabbitmq.client.ConnectionFactory; |
|
48 | 47 | /** |
49 | 48 | * Check that protocol negotiation works |
50 | 49 | */ |
51 | | -public class ConnectionOpen extends TestCase |
52 | | -{ |
53 | | - public void testCorrectProtocolHeader() throws IOException { |
54 | | - ConnectionFactory factory = new ConnectionFactory(); |
55 | | - SocketFrameHandler fh = new SocketFrameHandler(factory.getSocketFactory().createSocket("localhost", AMQP.PROTOCOL.PORT)); |
56 | | - fh.sendHeader(); |
57 | | - AMQCommand.Assembler a = AMQCommand.newAssembler(); |
58 | | - AMQCommand command = null; |
59 | | - while (command==null) { |
60 | | - command = a.handleFrame(fh.readFrame()); |
| 50 | +public class ConnectionOpen extends TestCase { |
| 51 | + public void testCorrectProtocolHeader() throws IOException { |
| 52 | + ConnectionFactory factory = new ConnectionFactory(); |
| 53 | + SocketFrameHandler fh = new SocketFrameHandler(factory.getSocketFactory().createSocket("localhost", AMQP.PROTOCOL.PORT)); |
| 54 | + fh.sendHeader(); |
| 55 | + AMQCommand.Assembler a = AMQCommand.newAssembler(); |
| 56 | + AMQCommand command = null; |
| 57 | + while (command == null) { |
| 58 | + command = a.handleFrame(fh.readFrame()); |
| 59 | + } |
| 60 | + Method m = command.getMethod(); |
| 61 | + // System.out.println(m.getClass()); |
| 62 | + assertTrue("First command must be Connection.start", |
| 63 | + m instanceof AMQP.Connection.Start); |
| 64 | + AMQP.Connection.Start start = (AMQP.Connection.Start) m; |
| 65 | + assertTrue("Version in Connection.start is <= what we sent", |
| 66 | + start.getVersionMajor() < AMQP.PROTOCOL.MAJOR || |
| 67 | + (start.getVersionMajor() == AMQP.PROTOCOL.MAJOR && |
| 68 | + start.getVersionMinor() <= AMQP.PROTOCOL.MINOR)); |
61 | 69 | } |
62 | | - Method m = command.getMethod(); |
63 | | - // System.out.println(m.getClass()); |
64 | | - assertTrue("First command must be Connection.start", |
65 | | - m instanceof AMQP.Connection.Start); |
66 | | - AMQP.Connection.Start start = (AMQP.Connection.Start) m; |
67 | | - assertTrue("Version in Connection.start is <= what we sent", |
68 | | - start.getVersionMajor() < AMQP.PROTOCOL.MAJOR || |
69 | | - (start.getVersionMajor() == AMQP.PROTOCOL.MAJOR && |
70 | | - start.getVersionMinor() <= AMQP.PROTOCOL.MINOR)); |
71 | | - } |
72 | 70 |
|
73 | | - public void testCrazyProtocolHeader() throws IOException { |
74 | | - ConnectionFactory factory = new ConnectionFactory(); |
75 | | - SocketFrameHandler fh = new SocketFrameHandler(factory.getSocketFactory().createSocket("localhost", AMQP.PROTOCOL.PORT)); |
76 | | - fh.sendHeader(100, 3); // major, minor |
77 | | - DataInputStream in = fh._inputStream; |
78 | | - // we should get a valid protocol header back |
79 | | - byte[] header = new byte[4]; |
80 | | - in.read(header); |
81 | | - // The protocol header is "AMQP" plus a version that the server |
82 | | - // supports. We can really only test for the first bit. |
83 | | - assertEquals("AMQP", new String(header)); |
84 | | - in.read(header); |
85 | | - assertEquals(in.available(), 0); |
86 | | - // At this point the socket should have been closed. We can't |
87 | | - // directly test for this, since Socket.isClosed isn't very |
88 | | - // reliable, but we can test whether trying to read more bytes |
89 | | - // gives an error. |
90 | | - fh._socket.setSoTimeout(500); |
91 | | - // NB the frame handler will return null if the socket times out |
92 | | - try { |
93 | | - fh.readFrame(); |
94 | | - fail("Expected socket read to fail due to socket being closed"); |
| 71 | + public void testCrazyProtocolHeader() throws IOException { |
| 72 | + ConnectionFactory factory = new ConnectionFactory(); |
| 73 | + SocketFrameHandler fh = new SocketFrameHandler(factory.getSocketFactory().createSocket("localhost", AMQP.PROTOCOL.PORT)); |
| 74 | + fh.sendHeader(100, 3); // major, minor |
| 75 | + DataInputStream in = fh._inputStream; |
| 76 | + // we should get a valid protocol header back |
| 77 | + byte[] header = new byte[4]; |
| 78 | + in.read(header); |
| 79 | + // The protocol header is "AMQP" plus a version that the server |
| 80 | + // supports. We can really only test for the first bit. |
| 81 | + assertEquals("AMQP", new String(header)); |
| 82 | + in.read(header); |
| 83 | + assertEquals(in.available(), 0); |
| 84 | + // At this point the socket should have been closed. We can't |
| 85 | + // directly test for this, since Socket.isClosed isn't very |
| 86 | + // reliable, but we can test whether trying to read more bytes |
| 87 | + // gives an error. |
| 88 | + fh._socket.setSoTimeout(500); |
| 89 | + // NB the frame handler will return null if the socket times out |
| 90 | + try { |
| 91 | + fh.readFrame(); |
| 92 | + fail("Expected socket read to fail due to socket being closed"); |
| 93 | + } |
| 94 | + catch (MalformedFrameException mfe) { |
| 95 | + fail("Expected nothing, rather than a badly-formed something"); |
| 96 | + } |
| 97 | + catch (IOException ioe) { |
| 98 | + return; |
| 99 | + } |
95 | 100 | } |
96 | | - catch (MalformedFrameException mfe) { |
97 | | - fail("Expected nothing, rather than a badly-formed something"); |
98 | | - } |
99 | | - catch (IOException ioe) { |
100 | | - return; |
101 | | - } |
102 | | - } |
103 | 101 |
|
104 | | - public void testFrameMaxLessThanFrameMinSize() throws IOException { |
105 | | - ConnectionFactory factory = new ConnectionFactory(); |
106 | | - factory.setRequestedFrameMax(100); |
107 | | - try { |
108 | | - factory.newConnection(); |
109 | | - } |
110 | | - catch (IOException ioe) { |
111 | | - return; |
| 102 | + public void testFrameMaxLessThanFrameMinSize() throws IOException { |
| 103 | + ConnectionFactory factory = new ConnectionFactory(); |
| 104 | + factory.setRequestedFrameMax(100); |
| 105 | + try { |
| 106 | + factory.newConnection(); |
| 107 | + } |
| 108 | + catch (IOException ioe) { |
| 109 | + return; |
| 110 | + } |
| 111 | + fail("Broker should have closed the connection since our frame max < frame_min_size"); |
112 | 112 | } |
113 | | - fail("Broker should have closed the connection since our frame max < frame_min_size"); |
114 | | - } |
115 | | - |
116 | 113 | } |
0 commit comments