1
1
/*
2
- * Copyright 2002-2010 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .http .converter ;
18
18
19
19
import java .io .IOException ;
20
+ import java .io .InputStream ;
20
21
import java .util .Arrays ;
21
22
22
- import org .junit .Before ;
23
23
import org .junit .Test ;
24
24
25
25
import org .springframework .core .io .ByteArrayResource ;
26
26
import org .springframework .core .io .ClassPathResource ;
27
+ import org .springframework .core .io .InputStreamResource ;
27
28
import org .springframework .core .io .Resource ;
28
29
import org .springframework .http .MediaType ;
29
30
import org .springframework .http .MockHttpInputMessage ;
30
31
import org .springframework .http .MockHttpOutputMessage ;
31
32
import org .springframework .util .FileCopyUtils ;
32
33
34
+ import static org .hamcrest .core .Is .*;
35
+ import static org .hamcrest .core .IsInstanceOf .*;
33
36
import static org .junit .Assert .*;
34
37
35
38
/**
36
39
* @author Arjen Poutsma
40
+ * @author Kazuki Shimizu
37
41
*/
38
42
public class ResourceHttpMessageConverterTests {
39
43
40
- private ResourceHttpMessageConverter converter ;
44
+ private final ResourceHttpMessageConverter converter = new ResourceHttpMessageConverter () ;
41
45
42
- @ Before
43
- public void setUp () {
44
- converter = new ResourceHttpMessageConverter ();
45
- }
46
46
47
47
@ Test
48
48
public void canRead () {
@@ -60,7 +60,19 @@ public void read() throws IOException {
60
60
byte [] body = FileCopyUtils .copyToByteArray (getClass ().getResourceAsStream ("logo.jpg" ));
61
61
MockHttpInputMessage inputMessage = new MockHttpInputMessage (body );
62
62
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
+ }
64
76
}
65
77
66
78
@ Test
@@ -73,9 +85,7 @@ public void write() throws IOException {
73
85
assertEquals ("Invalid content-length" , body .getFile ().length (), outputMessage .getHeaders ().getContentLength ());
74
86
}
75
87
76
- // SPR-10848
77
-
78
- @ Test
88
+ @ Test // SPR-10848
79
89
public void writeByteArrayNullMediaType () throws IOException {
80
90
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ();
81
91
byte [] byteArray = {1 , 2 , 3 };
0 commit comments