PageRenderTime 42ms CodeModel.GetById 14ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
 31package com.notnoop.apns.internal;
 32
 33import java.io.IOException;
 34import java.io.InputStream;
 35import java.io.OutputStream;
 36import java.net.Socket;
 37import java.util.ArrayList;
 38import java.util.List;
 39import javax.net.SocketFactory;
 40import org.mockito.invocation.InvocationOnMock;
 41import org.mockito.stubbing.Answer;
 42import org.mockito.stubbing.OngoingStubbing;
 43
 44import static org.mockito.Mockito.*;
 45
 46public class MockingUtils {
 47
 48    static SocketFactory mockSocketFactory(OutputStream out, InputStream in) {
 49        try {
 50            Socket socket = mock(Socket.class);
 51            when(socket.getOutputStream()).thenReturn(out);
 52            when(socket.getInputStream()).thenReturn(in);
 53
 54            SocketFactory factory = mock(SocketFactory.class);
 55            when(factory.createSocket()).thenReturn(socket);
 56            when(factory.createSocket(anyString(), anyInt())).thenReturn(socket);
 57
 58            return factory;
 59        } catch (Exception e) {
 60            e.printStackTrace();
 61            throw new AssertionError("Cannot be here!");
 62        }
 63    }
 64
 65    static SocketFactory mockClosedThenOpenSocket(OutputStream out, InputStream in, boolean isClosed, int failedTries) {
 66        try {
 67            List<Socket> socketMocks = new ArrayList<Socket>(failedTries + 1);
 68
 69            for (int i = 0; i < failedTries; ++i) {
 70                Socket socket = mock(Socket.class);
 71                if (isClosed) {
 72                    mockSocketClosed(socket);
 73                } else {
 74                    when(socket.getOutputStream()).thenThrow(
 75                            new IOException("simulated IOException"));
 76                    doAnswer(new DynamicMockSocketClosed(socket)).when(socket).close();
 77                }
 78                socketMocks.add(socket);
 79            }
 80
 81            Socket socket = mock(Socket.class);
 82            when(socket.getOutputStream()).thenReturn(out);
 83            when(socket.getInputStream()).thenReturn(in);
 84            when(socket.isConnected()).thenReturn(true);
 85            socketMocks.add(socket);
 86
 87            SocketFactory factory = mock(SocketFactory.class);
 88            OngoingStubbing<Socket> stubbing = when(factory.createSocket(anyString(), anyInt()));
 89            for (Socket t : socketMocks)
 90                stubbing = stubbing.thenReturn(t);
 91
 92            return factory;
 93        } catch (Exception e) {
 94            e.printStackTrace();
 95            throw new AssertionError("Cannot be here!");
 96        }
 97    }
 98
 99    private static void mockSocketClosed(final Socket socket) throws IOException {
100        when(socket.isClosed()).thenReturn(true);
101        when(socket.isConnected()).thenReturn(false);
102        when(socket.getOutputStream()).thenThrow(
103                new AssertionError("Should have checked for closed connection"));
104    }
105
106    /**
107     * Change a mock socket's behaviour to be closed. Dynamically used from close()
108     */
109    private static class DynamicMockSocketClosed implements Answer<Void> {
110        private final Socket socket;
111
112        public DynamicMockSocketClosed(final Socket socket) {
113            this.socket = socket;
114        }
115
116        @Override
117        public Void answer(final InvocationOnMock invocation) throws Throwable {
118            mockSocketClosed(socket);
119            return null;
120        }
121    }
122}