11/*
2- * Copyright 2002-2010 the original author or authors.
2+ * Copyright 2002-2015 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1717package org .springframework .http .converter ;
1818
1919import java .io .IOException ;
20+ import java .io .InputStream ;
2021import java .util .Arrays ;
2122
22- import org .junit .Before ;
2323import org .junit .Test ;
2424
2525import org .springframework .core .io .ByteArrayResource ;
2626import org .springframework .core .io .ClassPathResource ;
27+ import org .springframework .core .io .InputStreamResource ;
2728import org .springframework .core .io .Resource ;
2829import org .springframework .http .MediaType ;
2930import org .springframework .http .MockHttpInputMessage ;
3031import org .springframework .http .MockHttpOutputMessage ;
3132import org .springframework .util .FileCopyUtils ;
3233
34+ import static org .hamcrest .core .Is .*;
35+ import static org .hamcrest .core .IsInstanceOf .*;
3336import static org .junit .Assert .*;
3437
3538/**
3639 * @author Arjen Poutsma
40+ * @author Kazuki Shimizu
3741 */
3842public class ResourceHttpMessageConverterTests {
3943
40- private ResourceHttpMessageConverter converter ;
44+ private final ResourceHttpMessageConverter converter = new ResourceHttpMessageConverter () ;
4145
42- @ Before
43- public void setUp () {
44- converter = new ResourceHttpMessageConverter ();
45- }
4646
4747 @ Test
4848 public void canRead () {
@@ -60,7 +60,19 @@ public void read() throws IOException {
6060 byte [] body = FileCopyUtils .copyToByteArray (getClass ().getResourceAsStream ("logo.jpg" ));
6161 MockHttpInputMessage inputMessage = new MockHttpInputMessage (body );
6262 inputMessage .getHeaders ().setContentType (MediaType .IMAGE_JPEG );
63- converter .read (Resource .class , inputMessage );
63+ Resource actualResource = converter .read (Resource .class , inputMessage );
64+ assertThat (FileCopyUtils .copyToByteArray (actualResource .getInputStream ()), is (body ));
65+ }
66+
67+ @ Test // SPR-13443
68+ public void readWithInputStreamResource () throws IOException {
69+ try (InputStream body = getClass ().getResourceAsStream ("logo.jpg" ) ) {
70+ MockHttpInputMessage inputMessage = new MockHttpInputMessage (body );
71+ inputMessage .getHeaders ().setContentType (MediaType .IMAGE_JPEG );
72+ Resource actualResource = converter .read (InputStreamResource .class , inputMessage );
73+ assertThat (actualResource , instanceOf (InputStreamResource .class ));
74+ assertThat (actualResource .getInputStream (), is (body ));
75+ }
6476 }
6577
6678 @ Test
@@ -73,9 +85,7 @@ public void write() throws IOException {
7385 assertEquals ("Invalid content-length" , body .getFile ().length (), outputMessage .getHeaders ().getContentLength ());
7486 }
7587
76- // SPR-10848
77-
78- @ Test
88+ @ Test // SPR-10848
7989 public void writeByteArrayNullMediaType () throws IOException {
8090 MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ();
8191 byte [] byteArray = {1 , 2 , 3 };
0 commit comments