@Test @SuppressWarnings({"rawtypes", "unchecked"}) public void cancelInactivityTasks() throws Exception { TcpConnection<byte[]> tcpConnection = getTcpConnection(); ScheduledFuture future = mock(ScheduledFuture.class); when(this.taskScheduler.scheduleWithFixedDelay(any(), eq(1L))).thenReturn(future); tcpConnection.onReadInactivity(mock(Runnable.class), 2L); tcpConnection.onWriteInactivity(mock(Runnable.class), 2L); this.webSocketHandlerCaptor.getValue().afterConnectionClosed(this.webSocketSession, CloseStatus.NORMAL); verify(future, times(2)).cancel(true); verifyNoMoreInteractions(future); }
@Test public void handlerExceptionResolver() throws Exception { delegatingConfig.setConfigurers(Collections.singletonList(webMvcConfigurer)); delegatingConfig.handlerExceptionResolver(); verify(webMvcConfigurer).configureMessageConverters(converters.capture()); verify(webMvcConfigurer).configureContentNegotiation(contentNegotiationConfigurer.capture()); verify(webMvcConfigurer).configureHandlerExceptionResolvers(exceptionResolvers.capture()); assertEquals(3, exceptionResolvers.getValue().size()); assertTrue(exceptionResolvers.getValue().get(0) instanceof ExceptionHandlerExceptionResolver); assertTrue(exceptionResolvers.getValue().get(1) instanceof ResponseStatusExceptionResolver); assertTrue(exceptionResolvers.getValue().get(2) instanceof DefaultHandlerExceptionResolver); assertTrue(converters.getValue().size() > 0); }
private void testDefaultMediaType(MediaType negotiatedMediaType) { this.mediaTypeCaptor = ArgumentCaptor.forClass(MediaType.class); MimeType defaultContentType = MimeTypeUtils.TEXT_XML; HttpMessageWriter<String> writer = getWriter(defaultContentType); writer.write(Mono.just("body"), forClass(String.class), negotiatedMediaType, this.response, NO_HINTS); assertEquals(defaultContentType, this.response.getHeaders().getContentType()); assertEquals(defaultContentType, this.mediaTypeCaptor.getValue()); }
@Test public void onTimeoutBeforeHandlerInitialized() throws Exception { Runnable runnable = mock(Runnable.class); this.emitter.onTimeout(runnable); this.emitter.initialize(this.handler); ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class); verify(this.handler).onTimeout(captor.capture()); verify(this.handler).onCompletion(any()); assertNotNull(captor.getValue()); captor.getValue().run(); verify(runnable).run(); }
@Test public void convertAndSendPayloadAndHeaders() throws JMSException { Destination destination = new Destination() {}; Map<String, Object> headers = new HashMap<>(); headers.put("foo", "bar"); this.messagingTemplate.convertAndSend(destination, "Hello", headers); verify(this.jmsTemplate).send(eq(destination), this.messageCreator.capture()); assertTextMessage(this.messageCreator.getValue()); // see createTextMessage }
private WebSocketHandler connect() { this.stompClient.connect("/foo", mock(StompSessionHandler.class)); verify(this.stompSession).getSessionFuture(); verifyNoMoreInteractions(this.stompSession); WebSocketHandler webSocketHandler = this.webSocketHandlerCaptor.getValue(); assertNotNull(webSocketHandler); return webSocketHandler; }
@Test public void onTimeoutAfterHandlerInitialized() throws Exception { this.emitter.initialize(this.handler); ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class); verify(this.handler).onTimeout(captor.capture()); verify(this.handler).onCompletion(any()); Runnable runnable = mock(Runnable.class); this.emitter.onTimeout(runnable); assertNotNull(captor.getValue()); captor.getValue().run(); verify(runnable).run(); }
@Test public void convertAndSendPayloadAndHeadersName() throws JMSException { Map<String, Object> headers = new HashMap<>(); headers.put("foo", "bar"); this.messagingTemplate.convertAndSend("myQueue", "Hello", headers); verify(this.jmsTemplate).send(eq("myQueue"), this.messageCreator.capture()); assertTextMessage(this.messageCreator.getValue()); // see createTextMessage }
@Test public void onCompletionBeforeHandlerInitialized() throws Exception { Runnable runnable = mock(Runnable.class); this.emitter.onCompletion(runnable); this.emitter.initialize(this.handler); ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class); verify(this.handler).onTimeout(any()); verify(this.handler).onCompletion(captor.capture()); assertNotNull(captor.getValue()); captor.getValue().run(); verify(runnable).run(); }
@Test public void test5() throws Exception { byte[] testContent = "Bla".getBytes(); SqlLobValue lob = new SqlLobValue(new ByteArrayInputStream(testContent), 3, handler); lob.setTypeValue(preparedStatement, 1, Types.CLOB, "test"); verify(creator).setClobAsAsciiStream(eq(preparedStatement), eq(1), inputStreamCaptor.capture(), eq(3)); byte[] bytes = new byte[3]; inputStreamCaptor.getValue().read(bytes); assertThat(bytes, equalTo(testContent)); }
@Test public void onCompletionAfterHandlerInitialized() throws Exception { this.emitter.initialize(this.handler); ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class); verify(this.handler).onTimeout(any()); verify(this.handler).onCompletion(captor.capture()); Runnable runnable = mock(Runnable.class); this.emitter.onCompletion(runnable); assertNotNull(captor.getValue()); captor.getValue().run(); verify(runnable).run(); }
@Test public void jsonView() throws Exception { given(this.messageChannel.send(any(Message.class))).willReturn(true); String sessionId = "sess1"; Message<?> inputMessage = createMessage(sessionId, "sub1", "/app", "/dest", null); this.jsonHandler.handleReturnValue(handleAndSendToJsonView(), this.jsonViewReturnType, inputMessage); verify(this.messageChannel).send(this.messageCaptor.capture()); Message<?> message = this.messageCaptor.getValue(); assertNotNull(message); String bytes = new String((byte[]) message.getPayload(), StandardCharsets.UTF_8); assertEquals("{\"withView1\":\"with\"}", bytes); }
@Test public void testLoadBitmap_asDrawable() { Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); requestManager .load(bitmap) .into(target); ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); verify(target).onResourceReady(captor.capture(), any(Transition.class)); BitmapDrawable drawable = (BitmapDrawable) captor.getValue(); assertThat(drawable.getBitmap()).isEqualTo(bitmap); }
@Test public void convertAndSendPayloadName() throws JMSException { this.messagingTemplate.convertAndSend("myQueue", "my Payload"); verify(this.jmsTemplate).send(eq("myQueue"), this.messageCreator.capture()); TextMessage textMessage = createTextMessage(this.messageCreator.getValue()); assertEquals("my Payload", textMessage.getText()); }
@Test public void testLoadDrawable() { Drawable drawable = new ColorDrawable(Color.RED); requestManager .load(drawable) .into(target); ArgumentCaptor<Drawable> drawableCaptor = ArgumentCaptor.forClass(Drawable.class); verify(target).onResourceReady(drawableCaptor.capture(), any(Transition.class)); assertThat(((ColorDrawable) drawableCaptor.getValue()).getColor()).isEqualTo(Color.RED); }
@Test public void convertAndSendPayload() throws JMSException { Destination destination = new Destination() {}; this.messagingTemplate.convertAndSend(destination, "my Payload"); verify(this.jmsTemplate).send(eq(destination), this.messageCreator.capture()); TextMessage textMessage = createTextMessage(this.messageCreator.getValue()); assertEquals("my Payload", textMessage.getText()); }
private void testInactivityTaskScheduling(Runnable runnable, long delay, long sleepTime) throws InterruptedException { ArgumentCaptor<Runnable> inactivityTaskCaptor = ArgumentCaptor.forClass(Runnable.class); verify(this.taskScheduler).scheduleWithFixedDelay(inactivityTaskCaptor.capture(), eq(delay/2)); verifyNoMoreInteractions(this.taskScheduler); if (sleepTime > 0) { Thread.sleep(sleepTime); } Runnable inactivityTask = inactivityTaskCaptor.getValue(); assertNotNull(inactivityTask); inactivityTask.run(); if (sleepTime > 0) { verify(runnable).run(); } else { verifyNoMoreInteractions(runnable); } }
@Test public void testJsonView() throws Exception { given(this.messageChannel.send(any(Message.class))).willReturn(true); String sessionId = "sess1"; String subscriptionId = "subs1"; String destination = "/dest"; Message<?> inputMessage = createInputMessage(sessionId, subscriptionId, destination, null); this.jsonHandler.handleReturnValue(getJsonView(), this.subscribeEventJsonViewReturnType, inputMessage); verify(this.messageChannel).send(this.messageCaptor.capture()); Message<?> message = this.messageCaptor.getValue(); assertNotNull(message); assertEquals("{\"withView1\":\"with\"}", new String((byte[]) message.getPayload(), StandardCharsets.UTF_8)); }