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

http://protobuf-socket-rpc.googlecode.com/ · Java · 89 lines · 52 code · 13 blank · 24 comment · 4 complexity · f441c8a8e0b5a9fc735685f35e583435 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.IOException;
  22. import java.net.InetAddress;
  23. import java.net.Socket;
  24. import java.net.UnknownHostException;
  25. import javax.net.SocketFactory;
  26. /**
  27. * Fake {@link SocketFactory} used for unit tests.
  28. *
  29. * @author Shardul Deo
  30. */
  31. public class FakeSocketFactory extends SocketFactory {
  32. private Socket socket = null;
  33. private IOException ioException = null;
  34. private UnknownHostException unknownHostException = null;
  35. public FakeSocketFactory returnsSocket(Socket socket) {
  36. this.socket = socket;
  37. return this;
  38. }
  39. public FakeSocketFactory throwsException(IOException e) {
  40. ioException = e;
  41. return this;
  42. }
  43. public FakeSocketFactory throwsException(UnknownHostException e) {
  44. unknownHostException = e;
  45. return this;
  46. }
  47. @Override
  48. public Socket createSocket() throws IOException {
  49. if (unknownHostException != null) {
  50. throw unknownHostException;
  51. }
  52. if (ioException != null) {
  53. throw ioException;
  54. }
  55. return socket;
  56. }
  57. @Override
  58. public Socket createSocket(String host, int port) throws IOException,
  59. UnknownHostException {
  60. return createSocket();
  61. }
  62. @Override
  63. public Socket createSocket(InetAddress host, int port) throws IOException {
  64. return createSocket();
  65. }
  66. @Override
  67. public Socket createSocket(String host, int port, InetAddress localHost,
  68. int localPort) throws IOException, UnknownHostException {
  69. return createSocket();
  70. }
  71. @Override
  72. public Socket createSocket(InetAddress address, int port,
  73. InetAddress localAddress, int localPort) throws IOException {
  74. return createSocket();
  75. }
  76. }