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());
}
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());
}
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!");
}
}
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);
}
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());
}
Aggregations