@Test public void copyRange() throws Exception { ByteArrayOutputStream out = spy(new ByteArrayOutputStream()); StreamUtils.copyRange(new ByteArrayInputStream(bytes), out, 0, 100); byte[] range = Arrays.copyOfRange(bytes, 0, 101); assertThat(out.toByteArray(), equalTo(range)); verify(out, never()).close(); }
@Test public void copyString() throws Exception { Charset charset = Charset.defaultCharset(); ByteArrayOutputStream out = spy(new ByteArrayOutputStream()); StreamUtils.copy(string, charset, out); assertThat(out.toByteArray(), equalTo(string.getBytes(charset))); verify(out, never()).close(); }
@Test public void copyStream() throws Exception { ByteArrayOutputStream out = spy(new ByteArrayOutputStream()); StreamUtils.copy(new ByteArrayInputStream(bytes), out); assertThat(out.toByteArray(), equalTo(bytes)); verify(out, never()).close(); }
@Test public void copyToString() throws Exception { Charset charset = Charset.defaultCharset(); InputStream inputStream = spy(new ByteArrayInputStream(string.getBytes(charset))); String actual = StreamUtils.copyToString(inputStream, charset); assertThat(actual, equalTo(string)); verify(inputStream, never()).close(); }
@Test public void copyBytes() throws Exception { ByteArrayOutputStream out = spy(new ByteArrayOutputStream()); StreamUtils.copy(bytes, out); assertThat(out.toByteArray(), equalTo(bytes)); verify(out, never()).close(); }
@Test public void copyToByteArray() throws Exception { InputStream inputStream = spy(new ByteArrayInputStream(bytes)); byte[] actual = StreamUtils.copyToByteArray(inputStream); assertThat(actual, equalTo(bytes)); verify(inputStream, never()).close(); }
@Test // SPR-17181 public void shouldDrainResponseEvenIfResponseNotRead() throws Exception { TestByteArrayInputStream is = new TestByteArrayInputStream("SpringSpring".getBytes(StandardCharsets.UTF_8)); given(this.connection.getErrorStream()).willReturn(null); given(this.connection.getInputStream()).willReturn(is); this.response.close(); assertThat(is.available(), is(0)); assertTrue(is.isClosed()); verify(this.connection, never()).disconnect(); }
@Test // SPR-14040 public void shouldNotCloseConnectionWhenResponseClosed() throws Exception { TestByteArrayInputStream is = new TestByteArrayInputStream("Spring".getBytes(StandardCharsets.UTF_8)); given(this.connection.getErrorStream()).willReturn(null); given(this.connection.getInputStream()).willReturn(is); InputStream responseStream = this.response.getBody(); assertThat(StreamUtils.copyToString(responseStream, StandardCharsets.UTF_8), is("Spring")); this.response.close(); assertTrue(is.isClosed()); verify(this.connection, never()).disconnect(); }
@Test public void replyJacksonPojoAndJsonView() throws JMSException { TextMessage reply = testReplyWithJackson("replyJacksonPojoAndJsonView", "{\"name\":\"Response\"}"); verify(reply, never()).setObjectProperty("foo", "bar"); }
@Test public void unsubscribeTwice() { this.channel.subscribe(this.handler); assertThat(this.channel.unsubscribe(this.handler), equalTo(true)); assertThat(this.channel.unsubscribe(this.handler), equalTo(false)); this.channel.send(this.message); verify(this.handler, never()).handleMessage(this.message); }
@Test public void resolveUrlExistingNotInJarFile() { this.locations = Collections.singletonList(new ClassPathResource("/META-INF/resources/webjars/", getClass())); String file = "foo/foo.txt"; given(this.chain.resolveUrlPath(file, this.locations)).willReturn(null); String actual = this.resolver.resolveUrlPath(file, this.locations, this.chain); assertNull(actual); verify(this.chain, times(1)).resolveUrlPath(file, this.locations); verify(this.chain, never()).resolveUrlPath("foo/2.3/foo.txt", this.locations); }
@Test public void resolveResourceExisting() throws Exception { String file = "bar.css"; Resource expected = new ClassPathResource("test/" + file, getClass()); given(this.chain.resolveResource(null, file, this.locations)).willReturn(expected); this.resolver.setStrategyMap(Collections.singletonMap("/**", this.versionStrategy)); Resource actual = this.resolver.resolveResourceInternal(null, file, this.locations, this.chain); assertEquals(expected, actual); verify(this.chain, times(1)).resolveResource(null, file, this.locations); verify(this.versionStrategy, never()).extractVersion(file); }
@Test public void resolveResourceNotFound() { String file = "something/something.js"; given(this.chain.resolveResource(this.exchange, file, this.locations)).willReturn(Mono.empty()); Resource actual = this.resolver .resolveResource(this.exchange, file, this.locations, this.chain) .block(TIMEOUT); assertNull(actual); verify(this.chain, times(1)).resolveResource(this.exchange, file, this.locations); verify(this.chain, never()).resolveResource(this.exchange, null, this.locations); }
@Test public void testSetParameterValueWithNullAndGetParameterTypeWorking() throws SQLException { ParameterMetaData pmd = mock(ParameterMetaData.class); given(preparedStatement.getParameterMetaData()).willReturn(pmd); given(pmd.getParameterType(1)).willReturn(Types.SMALLINT); StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, null); verify(pmd).getParameterType(1); verify(preparedStatement, never()).getConnection(); verify(preparedStatement).setNull(1, Types.SMALLINT); }
@Test public void payloadConversionLazilyInvoked() throws JMSException { javax.jms.Message jmsMessage = mock(javax.jms.Message.class); MessageConverter messageConverter = mock(MessageConverter.class); given(messageConverter.fromMessage(jmsMessage)).willReturn("FooBar"); MessagingMessageListenerAdapter listener = getSimpleInstance("simple", Message.class); listener.setMessageConverter(messageConverter); Message<?> message = listener.toMessagingMessage(jmsMessage); verify(messageConverter, never()).fromMessage(jmsMessage); assertEquals("FooBar", message.getPayload()); verify(messageConverter, times(1)).fromMessage(jmsMessage); }
@Test public void resolveResourceNotFound() { String file = "something/something.js"; given(this.chain.resolveUrlPath(file, this.locations)).willReturn(null); Resource actual = this.resolver.resolveResource(this.request, file, this.locations, this.chain); assertNull(actual); verify(this.chain, times(1)).resolveResource(this.request, file, this.locations); verify(this.chain, never()).resolveResource(this.request, null, this.locations); }
@Test public void resolveUrlWebJarResourceNotFound() { String file = "something/something.js"; given(this.chain.resolveUrlPath(file, this.locations)).willReturn(null); String actual = this.resolver.resolveUrlPath(file, this.locations, this.chain); assertNull(actual); verify(this.chain, times(1)).resolveUrlPath(file, this.locations); verify(this.chain, never()).resolveUrlPath(null, this.locations); }
@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 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()); }