@Before public void setUp() throws Exception { // we have 3 appenders all connecting to the same ConnectionFactory then(connection).should(times(3)).start(); }
@Test public void testAppendToQueue() throws Exception { final JmsAppender appender = (JmsAppender) ctx.getRequiredAppender("JmsAppender"); final LogEvent event = createLogEvent(); appender.append(event); then(session).should().createTextMessage(eq(LOG_MESSAGE)); then(textMessage).should().setJMSTimestamp(anyLong()); then(messageProducer).should().send(textMessage); appender.stop(); then(session).should().close(); then(connection).should().close(); }
@Test public void testJmsQueueAppenderCompatibility() throws Exception { final JmsAppender appender = (JmsAppender) ctx.getRequiredAppender("JmsQueueAppender"); final LogEvent expected = createLogEvent(); appender.append(expected); then(session).should().createObjectMessage(eq(expected)); then(objectMessage).should().setJMSTimestamp(anyLong()); then(messageProducer).should().send(objectMessage); appender.stop(); then(session).should().close(); then(connection).should().close(); }
@Test public void testJmsTopicAppenderCompatibility() throws Exception { final JmsAppender appender = (JmsAppender) ctx.getRequiredAppender("JmsTopicAppender"); final LogEvent expected = createLogEvent(); appender.append(expected); then(session).should().createObjectMessage(eq(expected)); then(objectMessage).should().setJMSTimestamp(anyLong()); then(messageProducer).should().send(objectMessage); appender.stop(); then(session).should().close(); then(connection).should().close(); }
@Test public void testReplaceManager() throws Exception { setUp("name"); final LocalAbstractDatabaseManager oldManager = appender.getManager(); assertSame("The manager should be the same.", manager, oldManager); final LocalAbstractDatabaseManager newManager = mock(LocalAbstractDatabaseManager.class); appender.replaceManager(newManager); then(manager).should().close(); then(newManager).should().startupInternal(); appender.stop(); then(newManager).should().stop(0L, TimeUnit.MILLISECONDS); }
@Test public void shouldHandleResource() throws Exception { ResponseEntity<Resource> returnValue = ResponseEntity .ok(new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8))); given(resourceMessageConverter.canWrite(ByteArrayResource.class, null)).willReturn(true); given(resourceMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL)); given(resourceMessageConverter.canWrite(ByteArrayResource.class, APPLICATION_OCTET_STREAM)).willReturn(true); processor.handleReturnValue(returnValue, returnTypeResponseEntityResource, mavContainer, webRequest); then(resourceMessageConverter).should(times(1)).write( any(ByteArrayResource.class), eq(APPLICATION_OCTET_STREAM), any(HttpOutputMessage.class)); assertEquals(200, servletResponse.getStatus()); }
@Test public void handleReturnTypeResource() throws Exception { Resource returnValue = new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8)); given(resourceMessageConverter.canWrite(ByteArrayResource.class, null)).willReturn(true); given(resourceMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL)); given(resourceMessageConverter.canWrite(ByteArrayResource.class, MediaType.APPLICATION_OCTET_STREAM)) .willReturn(true); processor.handleReturnValue(returnValue, returnTypeResource, mavContainer, webRequest); then(resourceMessageConverter).should(times(1)).write(any(ByteArrayResource.class), eq(MediaType.APPLICATION_OCTET_STREAM), any(HttpOutputMessage.class)); assertEquals(200, servletResponse.getStatus()); }
@Test public void preHandle() throws Exception { HandlerInterceptor interceptor = mock(HandlerInterceptor.class); MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { "/**" }, interceptor); mappedInterceptor.preHandle(mock(HttpServletRequest.class), mock(HttpServletResponse.class), null); then(interceptor).should().preHandle(any(HttpServletRequest.class), any(HttpServletResponse.class), any()); }
@Test public void shouldHandleResourceByteRange() throws Exception { ResponseEntity<Resource> returnValue = ResponseEntity .ok(new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8))); servletRequest.addHeader("Range", "bytes=0-5"); given(resourceRegionMessageConverter.canWrite(any(), eq(null))).willReturn(true); given(resourceRegionMessageConverter.canWrite(any(), eq(APPLICATION_OCTET_STREAM))).willReturn(true); processor.handleReturnValue(returnValue, returnTypeResponseEntityResource, mavContainer, webRequest); then(resourceRegionMessageConverter).should(times(1)).write( anyCollection(), eq(APPLICATION_OCTET_STREAM), argThat(outputMessage -> "bytes".equals(outputMessage.getHeaders().getFirst(HttpHeaders.ACCEPT_RANGES)))); assertEquals(206, servletResponse.getStatus()); }
@Test public void testConnection() { try (final NoSqlDatabaseManager<?> manager = NoSqlDatabaseManager.getNoSqlDatabaseManager("name", 0, provider)) { assertNotNull("The manager should not be null.", manager); manager.connectAndStart(); then(provider).should().getConnection(); manager.commitAndClose(); } }
@Test //SPR-16754 public void disableRangeSupportForStreamingResponses() throws Exception { InputStream is = new ByteArrayInputStream("Content".getBytes(StandardCharsets.UTF_8)); InputStreamResource resource = new InputStreamResource(is, "test"); ResponseEntity<Resource> returnValue = ResponseEntity.ok(resource); servletRequest.addHeader("Range", "bytes=0-5"); given(resourceMessageConverter.canWrite(any(), eq(null))).willReturn(true); given(resourceMessageConverter.canWrite(any(), eq(APPLICATION_OCTET_STREAM))).willReturn(true); processor.handleReturnValue(returnValue, returnTypeResponseEntityResource, mavContainer, webRequest); then(resourceMessageConverter).should(times(1)).write( any(InputStreamResource.class), eq(APPLICATION_OCTET_STREAM), any(HttpOutputMessage.class)); assertEquals(200, servletResponse.getStatus()); assertThat(servletResponse.getHeader(HttpHeaders.ACCEPT_RANGES), Matchers.isEmptyOrNullString()); }
@Test public void handleReturnTypeResourceIllegalByteRange() throws Exception { ResponseEntity<Resource> returnValue = ResponseEntity .ok(new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8))); servletRequest.addHeader("Range", "illegal"); given(resourceRegionMessageConverter.canWrite(any(), eq(null))).willReturn(true); given(resourceRegionMessageConverter.canWrite(any(), eq(APPLICATION_OCTET_STREAM))).willReturn(true); processor.handleReturnValue(returnValue, returnTypeResponseEntityResource, mavContainer, webRequest); then(resourceRegionMessageConverter).should(never()).write( anyCollection(), eq(APPLICATION_OCTET_STREAM), any(HttpOutputMessage.class)); assertEquals(416, servletResponse.getStatus()); }
@Test //SPR-16921 public void disableRangeSupportIfContentRangePresent() throws Exception { ResponseEntity<Resource> returnValue = ResponseEntity .status(HttpStatus.PARTIAL_CONTENT) .header(HttpHeaders.RANGE, "bytes=0-5") .body(new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8))); given(resourceRegionMessageConverter.canWrite(any(), eq(null))).willReturn(true); given(resourceRegionMessageConverter.canWrite(any(), eq(APPLICATION_OCTET_STREAM))).willReturn(true); processor.handleReturnValue(returnValue, returnTypeResponseEntityResource, mavContainer, webRequest); then(resourceRegionMessageConverter).should(never()).write(anyCollection(), any(), any()); assertEquals(206, servletResponse.getStatus()); }
@Test public void testWriteInternalNotConnected02() { given(connection.isClosed()).willReturn(true); try (final NoSqlDatabaseManager<?> manager = NoSqlDatabaseManager.getNoSqlDatabaseManager("name", 0, provider)) { manager.startup(); manager.connectAndStart(); then(provider).should().getConnection(); expectedException.expect(AppenderLoggingException.class); manager.writeInternal(mock(LogEvent.class), null); } }
@Test public void handleReturnTypeResourceByteRange() throws Exception { Resource returnValue = new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8)); servletRequest.addHeader("Range", "bytes=0-5"); given(resourceRegionMessageConverter.canWrite(any(), eq(null))).willReturn(true); given(resourceRegionMessageConverter.canWrite(any(), eq(MediaType.APPLICATION_OCTET_STREAM))).willReturn(true); processor.handleReturnValue(returnValue, returnTypeResource, mavContainer, webRequest); then(resourceRegionMessageConverter).should(times(1)).write( anyCollection(), eq(MediaType.APPLICATION_OCTET_STREAM), argThat(outputMessage -> "bytes".equals(outputMessage.getHeaders().getFirst(HttpHeaders.ACCEPT_RANGES)))); assertEquals(206, servletResponse.getStatus()); }
@Test public void testStartupShutdown02() throws Exception { setUp("anotherName02", 0); assertEquals("The name is not correct.", "anotherName02", manager.getName()); assertFalse("The manager should not have started.", manager.isRunning()); manager.startup(); then(manager).should().startupInternal(); assertTrue("The manager should be running now.", manager.isRunning()); manager.releaseSub(-1, null); then(manager).should().shutdownInternal(); assertFalse("The manager should not be running anymore.", manager.isRunning()); }
@Test public void postHandle() throws Exception { HandlerInterceptor interceptor = mock(HandlerInterceptor.class); MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { "/**" }, interceptor); mappedInterceptor.postHandle(mock(HttpServletRequest.class), mock(HttpServletResponse.class), null, mock(ModelAndView.class)); then(interceptor).should().postHandle(any(), any(), any(), any()); }
@Test public void afterCompletion() throws Exception { HandlerInterceptor interceptor = mock(HandlerInterceptor.class); MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { "/**" }, interceptor); mappedInterceptor.afterCompletion(mock(HttpServletRequest.class), mock(HttpServletResponse.class), null, mock(Exception.class)); then(interceptor).should().afterCompletion(any(), any(), any(), any()); }
@Test public void handleReturnTypeResourceIllegalByteRange() throws Exception { Resource returnValue = new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8)); servletRequest.addHeader("Range", "illegal"); given(resourceRegionMessageConverter.canWrite(any(), eq(null))).willReturn(true); given(resourceRegionMessageConverter.canWrite(any(), eq(MediaType.APPLICATION_OCTET_STREAM))).willReturn(true); processor.handleReturnValue(returnValue, returnTypeResource, mavContainer, webRequest); then(resourceRegionMessageConverter).should(never()).write( anyCollection(), eq(MediaType.APPLICATION_OCTET_STREAM), any(HttpOutputMessage.class)); assertEquals(416, servletResponse.getStatus()); }