/java/src/test/java/com/googlecode/protobuf/socketrpc/SocketRpcConnectionFactoryTest.java

http://protobuf-socket-rpc.googlecode.com/ · Java · 112 lines · 71 code · 17 blank · 24 comment · 0 complexity · a6c821d9e15f637875ad427f0aae38e5 MD5 · raw file

  1. // Copyright (c) 2010 Shardul Deo
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package com.googlecode.protobuf.socketrpc;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.IOException;
  24. import java.net.UnknownHostException;
  25. import com.googlecode.protobuf.socketrpc.RpcConnectionFactory.Connection;
  26. import com.googlecode.protobuf.socketrpc.TestProtos.Request;
  27. import com.googlecode.protobuf.socketrpc.TestProtos.Request.Builder;
  28. import junit.framework.TestCase;
  29. /**
  30. * Tests for {@link SocketRpcConnectionFactory}.
  31. *
  32. * @author Shardul Deo
  33. */
  34. public class SocketRpcConnectionFactoryTest extends TestCase {
  35. private static final Request MESSAGE = Request.newBuilder()
  36. .setStrData("test data")
  37. .build();
  38. private FakeSocketFactory socketFactory;
  39. private RpcConnectionFactory connectionFactory;
  40. @Override
  41. protected void setUp() throws Exception {
  42. super.setUp();
  43. socketFactory = new FakeSocketFactory();
  44. connectionFactory = new SocketRpcConnectionFactory("host", 8080,
  45. socketFactory, true /* delimited */);
  46. }
  47. public void testCreateConnection_unknownHost() throws IOException {
  48. UnknownHostException uhe = new UnknownHostException();
  49. socketFactory.throwsException(uhe);
  50. try {
  51. connectionFactory.createConnection();
  52. } catch (UnknownHostException e) {
  53. assertSame(uhe, e);
  54. }
  55. }
  56. public void testCreateConnection_ioError() {
  57. IOException ioe = new IOException();
  58. socketFactory.throwsException(ioe);
  59. try {
  60. connectionFactory.createConnection();
  61. } catch (IOException e) {
  62. assertSame(ioe, e);
  63. }
  64. }
  65. private Connection connectionForSocket(FakeSocket socket) throws IOException {
  66. socketFactory.returnsSocket(socket);
  67. return connectionFactory.createConnection();
  68. }
  69. public void testConnectionOutputInput() throws IOException {
  70. FakeSocket socket = new FakeSocket(true);
  71. ByteArrayOutputStream os = new ByteArrayOutputStream();
  72. MESSAGE.writeDelimitedTo(os);
  73. socket.withInputBytes(os.toByteArray());
  74. Connection connection = connectionForSocket(socket);
  75. connection.sendProtoMessage(MESSAGE);
  76. ByteArrayInputStream is = new ByteArrayInputStream(socket.getOutputBytes());
  77. assertEquals(MESSAGE, Request.parseDelimitedFrom(is));
  78. Builder builder = Request.newBuilder();
  79. connection.receiveProtoMessage(builder);
  80. assertEquals(MESSAGE, builder.build());
  81. }
  82. public void testConnectionInputOutput() throws IOException {
  83. FakeSocket socket = new FakeSocket(true);
  84. ByteArrayOutputStream os = new ByteArrayOutputStream();
  85. MESSAGE.writeDelimitedTo(os);
  86. socket.withInputBytes(os.toByteArray());
  87. Connection connection = connectionForSocket(socket);
  88. Builder builder = Request.newBuilder();
  89. connection.receiveProtoMessage(builder);
  90. assertEquals(MESSAGE, builder.build());
  91. connection.sendProtoMessage(MESSAGE);
  92. ByteArrayInputStream is = new ByteArrayInputStream(socket.getOutputBytes());
  93. assertEquals(MESSAGE, Request.parseDelimitedFrom(is));
  94. }
  95. }