/src/test/java/com/notnoop/apns/internal/MockingUtils.java

http://github.com/notnoop/java-apns · Java · 122 lines · 74 code · 15 blank · 33 comment · 4 complexity · 482f7356889818da34e9a5c57b1ecb8b MD5 · raw file

  1. /*
  2. * Copyright 2009, Mahmood Ali.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Mahmood Ali. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. package com.notnoop.apns.internal;
  32. import java.io.IOException;
  33. import java.io.InputStream;
  34. import java.io.OutputStream;
  35. import java.net.Socket;
  36. import java.util.ArrayList;
  37. import java.util.List;
  38. import javax.net.SocketFactory;
  39. import org.mockito.invocation.InvocationOnMock;
  40. import org.mockito.stubbing.Answer;
  41. import org.mockito.stubbing.OngoingStubbing;
  42. import static org.mockito.Mockito.*;
  43. public class MockingUtils {
  44. static SocketFactory mockSocketFactory(OutputStream out, InputStream in) {
  45. try {
  46. Socket socket = mock(Socket.class);
  47. when(socket.getOutputStream()).thenReturn(out);
  48. when(socket.getInputStream()).thenReturn(in);
  49. SocketFactory factory = mock(SocketFactory.class);
  50. when(factory.createSocket()).thenReturn(socket);
  51. when(factory.createSocket(anyString(), anyInt())).thenReturn(socket);
  52. return factory;
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. throw new AssertionError("Cannot be here!");
  56. }
  57. }
  58. static SocketFactory mockClosedThenOpenSocket(OutputStream out, InputStream in, boolean isClosed, int failedTries) {
  59. try {
  60. List<Socket> socketMocks = new ArrayList<Socket>(failedTries + 1);
  61. for (int i = 0; i < failedTries; ++i) {
  62. Socket socket = mock(Socket.class);
  63. if (isClosed) {
  64. mockSocketClosed(socket);
  65. } else {
  66. when(socket.getOutputStream()).thenThrow(
  67. new IOException("simulated IOException"));
  68. doAnswer(new DynamicMockSocketClosed(socket)).when(socket).close();
  69. }
  70. socketMocks.add(socket);
  71. }
  72. Socket socket = mock(Socket.class);
  73. when(socket.getOutputStream()).thenReturn(out);
  74. when(socket.getInputStream()).thenReturn(in);
  75. when(socket.isConnected()).thenReturn(true);
  76. socketMocks.add(socket);
  77. SocketFactory factory = mock(SocketFactory.class);
  78. OngoingStubbing<Socket> stubbing = when(factory.createSocket(anyString(), anyInt()));
  79. for (Socket t : socketMocks)
  80. stubbing = stubbing.thenReturn(t);
  81. return factory;
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. throw new AssertionError("Cannot be here!");
  85. }
  86. }
  87. private static void mockSocketClosed(final Socket socket) throws IOException {
  88. when(socket.isClosed()).thenReturn(true);
  89. when(socket.isConnected()).thenReturn(false);
  90. when(socket.getOutputStream()).thenThrow(
  91. new AssertionError("Should have checked for closed connection"));
  92. }
  93. /**
  94. * Change a mock socket's behaviour to be closed. Dynamically used from close()
  95. */
  96. private static class DynamicMockSocketClosed implements Answer<Void> {
  97. private final Socket socket;
  98. public DynamicMockSocketClosed(final Socket socket) {
  99. this.socket = socket;
  100. }
  101. @Override
  102. public Void answer(final InvocationOnMock invocation) throws Throwable {
  103. mockSocketClosed(socket);
  104. return null;
  105. }
  106. }
  107. }