Search in sources :

Example 11 with SocketFactory

use of javax.net.SocketFactory in project mockito by mockito.

the class DeepStubbingTest method withArguments.

/**
     * Test that stubbing of methods of different arguments don't interfere
     */
@Test
public void withArguments() throws Exception {
    OutputStream out1 = new ByteArrayOutputStream();
    OutputStream out2 = new ByteArrayOutputStream();
    OutputStream out3 = new ByteArrayOutputStream();
    SocketFactory sf = mock(SocketFactory.class, RETURNS_DEEP_STUBS);
    when(sf.createSocket().getOutputStream()).thenReturn(out1);
    when(sf.createSocket("google.com", 80).getOutputStream()).thenReturn(out2);
    when(sf.createSocket("stackoverflow.com", 80).getOutputStream()).thenReturn(out3);
    assertSame(out1, sf.createSocket().getOutputStream());
    assertSame(out2, sf.createSocket("google.com", 80).getOutputStream());
    assertSame(out3, sf.createSocket("stackoverflow.com", 80).getOutputStream());
}
Also used : SocketFactory(javax.net.SocketFactory) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 12 with SocketFactory

use of javax.net.SocketFactory in project mockito by mockito.

the class DeepStubbingTest method interactions.

/**
     * Test that stubbing of two mocks stubs don't interfere
     */
@Test
public void interactions() throws Exception {
    OutputStream out1 = new ByteArrayOutputStream();
    OutputStream out2 = new ByteArrayOutputStream();
    SocketFactory sf1 = mock(SocketFactory.class, RETURNS_DEEP_STUBS);
    when(sf1.createSocket().getOutputStream()).thenReturn(out1);
    SocketFactory sf2 = mock(SocketFactory.class, RETURNS_DEEP_STUBS);
    when(sf2.createSocket().getOutputStream()).thenReturn(out2);
    assertSame(out1, sf1.createSocket().getOutputStream());
    assertSame(out2, sf2.createSocket().getOutputStream());
}
Also used : SocketFactory(javax.net.SocketFactory) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 13 with SocketFactory

use of javax.net.SocketFactory in project java-apns by notnoop.

the class MockingUtils method mockClosedThenOpenSocket.

static SocketFactory mockClosedThenOpenSocket(OutputStream out, InputStream in, boolean isClosed, int failedTries) {
    try {
        List<Socket> socketMocks = new ArrayList<Socket>(failedTries + 1);
        for (int i = 0; i < failedTries; ++i) {
            Socket socket = mock(Socket.class);
            if (isClosed) {
                mockSocketClosed(socket);
            } else {
                when(socket.getOutputStream()).thenThrow(new IOException("simulated IOException"));
                doAnswer(new DynamicMockSocketClosed(socket)).when(socket).close();
            }
            socketMocks.add(socket);
        }
        Socket socket = mock(Socket.class);
        when(socket.getOutputStream()).thenReturn(out);
        when(socket.getInputStream()).thenReturn(in);
        when(socket.isConnected()).thenReturn(true);
        socketMocks.add(socket);
        SocketFactory factory = mock(SocketFactory.class);
        OngoingStubbing<Socket> stubbing = when(factory.createSocket(anyString(), anyInt()));
        for (Socket t : socketMocks) stubbing = stubbing.thenReturn(t);
        return factory;
    } catch (Exception e) {
        e.printStackTrace();
        throw new AssertionError("Cannot be here!");
    }
}
Also used : SocketFactory(javax.net.SocketFactory) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Socket(java.net.Socket) IOException(java.io.IOException)

Example 14 with SocketFactory

use of javax.net.SocketFactory in project java-apns by notnoop.

the class ApnsConnectionTest method simpleSocket.

@Test
public void simpleSocket() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    SocketFactory factory = mockSocketFactory(baos, null);
    packetSentRegardless(factory, baos);
}
Also used : SocketFactory(javax.net.SocketFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 15 with SocketFactory

use of javax.net.SocketFactory in project java-apns by notnoop.

the class ApnsFeedbackConnectionTest method feedbackWithErrorTwice.

/**
     * Connection fails after three retries
     */
@Test(expected = Exception.class)
public void feedbackWithErrorTwice() {
    SocketFactory sf = mockClosedThenOpenSocket(null, simpleStream, true, 3);
    ApnsFeedbackConnection connection = new ApnsFeedbackConnection(sf, "localhost", 80);
    connection.DELAY_IN_MS = 0;
    checkParsedSimple(connection.getInactiveDevices());
}
Also used : SocketFactory(javax.net.SocketFactory) Test(org.junit.Test)

Aggregations

SocketFactory (javax.net.SocketFactory)66 Socket (java.net.Socket)25 Test (org.junit.Test)25 IOException (java.io.IOException)18 InetSocketAddress (java.net.InetSocketAddress)14 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 SSLSocket (javax.net.ssl.SSLSocket)10 OutputStream (java.io.OutputStream)9 ServerSocket (java.net.ServerSocket)9 SocketAddress (java.net.SocketAddress)6 Configuration (org.apache.hadoop.conf.Configuration)5 ServerSocketFactory (javax.net.ServerSocketFactory)4 InputStream (java.io.InputStream)3 InetAddress (java.net.InetAddress)3 UnknownHostException (java.net.UnknownHostException)3 ProtocolSocketFactory (org.apache.commons.httpclient.protocol.ProtocolSocketFactory)3 StandardSocketFactory (org.apache.hadoop.net.StandardSocketFactory)3 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)3 SocketException (java.net.SocketException)2