1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2018 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.
31
31
import org .springframework .core .io .Resource ;
32
32
import org .springframework .core .io .buffer .AbstractDataBufferAllocatingTestCase ;
33
33
import org .springframework .core .io .buffer .DataBuffer ;
34
- import org .springframework .core .io .buffer .DataBufferUtils ;
35
- import org .springframework .core .io .buffer .DefaultDataBufferFactory ;
36
- import org .springframework .core .io .buffer .support .DataBufferTestUtils ;
37
34
import org .springframework .core .io .support .ResourceRegion ;
38
35
import org .springframework .util .MimeType ;
39
36
import org .springframework .util .MimeTypeUtils ;
40
- import org .springframework .util .StringUtils ;
41
37
42
- import static org .junit .Assert .assertArrayEquals ;
43
- import static org .junit .Assert .assertFalse ;
44
- import static org .junit .Assert .assertTrue ;
38
+ import static org .junit .Assert .*;
45
39
46
40
/**
47
41
* Test cases for {@link ResourceRegionEncoder} class.
@@ -55,7 +49,6 @@ public class ResourceRegionEncoderTests extends AbstractDataBufferAllocatingTest
55
49
@ Before
56
50
public void setUp () {
57
51
this .encoder = new ResourceRegionEncoder ();
58
- this .bufferFactory = new DefaultDataBufferFactory ();
59
52
}
60
53
61
54
@ Test
@@ -111,6 +104,30 @@ public void shouldEncodeMultipleResourceRegionsByteArrayResource() throws Except
111
104
new ByteArrayResource (content .getBytes (StandardCharsets .UTF_8 )));
112
105
}
113
106
107
+ @ Test
108
+ public void nonExisting () {
109
+ Resource resource = new ClassPathResource ("ResourceRegionEncoderTests.txt" , getClass ());
110
+ Resource nonExisting = new ClassPathResource ("does not exist" , getClass ());
111
+ Flux <ResourceRegion > regions = Flux .just (
112
+ new ResourceRegion (resource , 0 , 6 ),
113
+ new ResourceRegion (nonExisting , 0 , 6 ));
114
+
115
+ String boundary = MimeTypeUtils .generateMultipartBoundaryString ();
116
+
117
+ Flux <DataBuffer > result = this .encoder .encode (regions , this .bufferFactory ,
118
+ ResolvableType .forClass (ResourceRegion .class ),
119
+ MimeType .valueOf ("text/plain" ),
120
+ Collections .singletonMap (ResourceRegionEncoder .BOUNDARY_STRING_HINT , boundary ));
121
+
122
+ StepVerifier .create (result )
123
+ .consumeNextWith (stringConsumer ("\r \n --" + boundary + "\r \n " ))
124
+ .consumeNextWith (stringConsumer ("Content-Type: text/plain\r \n " ))
125
+ .consumeNextWith (stringConsumer ("Content-Range: bytes 0-5/39\r \n \r \n " ))
126
+ .consumeNextWith (stringConsumer ("Spring" ))
127
+ .expectError (EncodingException .class )
128
+ .verify ();
129
+ }
130
+
114
131
private void shouldEncodeMultipleResourceRegions (Resource resource ) {
115
132
Flux <ResourceRegion > regions = Flux .just (
116
133
new ResourceRegion (resource , 0 , 6 ),
@@ -120,45 +137,30 @@ private void shouldEncodeMultipleResourceRegions(Resource resource) {
120
137
);
121
138
String boundary = MimeTypeUtils .generateMultipartBoundaryString ();
122
139
123
- Flux <DataBuffer > result = this .encoder .encode (regions , this .bufferFactory ,
140
+ Flux <DataBuffer > result = this .encoder .encode (regions , super .bufferFactory ,
124
141
ResolvableType .forClass (ResourceRegion .class ),
125
142
MimeType .valueOf ("text/plain" ),
126
143
Collections .singletonMap (ResourceRegionEncoder .BOUNDARY_STRING_HINT , boundary )
127
144
);
128
145
129
- Mono <DataBuffer > reduced = result
130
- .reduce (bufferFactory .allocateBuffer (), (previous , current ) -> {
131
- previous .write (current );
132
- DataBufferUtils .release (current );
133
- return previous ;
134
- });
135
-
136
- StepVerifier .create (reduced )
137
- .consumeNextWith (buf -> {
138
- String content = DataBufferTestUtils .dumpString (buf , StandardCharsets .UTF_8 );
139
- String [] ranges = StringUtils .tokenizeToStringArray (content , "\r \n " ,
140
- false , true );
141
- String [] expected = new String [] {
142
- "--" + boundary ,
143
- "Content-Type: text/plain" ,
144
- "Content-Range: bytes 0-5/39" ,
145
- "Spring" ,
146
- "--" + boundary ,
147
- "Content-Type: text/plain" ,
148
- "Content-Range: bytes 7-15/39" ,
149
- "Framework" ,
150
- "--" + boundary ,
151
- "Content-Type: text/plain" ,
152
- "Content-Range: bytes 17-20/39" ,
153
- "test" ,
154
- "--" + boundary ,
155
- "Content-Type: text/plain" ,
156
- "Content-Range: bytes 22-38/39" ,
157
- "resource content." ,
158
- "--" + boundary + "--"
159
- };
160
- assertArrayEquals (expected , ranges );
161
- })
146
+ StepVerifier .create (result )
147
+ .consumeNextWith (stringConsumer ("\r \n --" + boundary + "\r \n " ))
148
+ .consumeNextWith (stringConsumer ("Content-Type: text/plain\r \n " ))
149
+ .consumeNextWith (stringConsumer ("Content-Range: bytes 0-5/39\r \n \r \n " ))
150
+ .consumeNextWith (stringConsumer ("Spring" ))
151
+ .consumeNextWith (stringConsumer ("\r \n --" + boundary + "\r \n " ))
152
+ .consumeNextWith (stringConsumer ("Content-Type: text/plain\r \n " ))
153
+ .consumeNextWith (stringConsumer ("Content-Range: bytes 7-15/39\r \n \r \n " ))
154
+ .consumeNextWith (stringConsumer ("Framework" ))
155
+ .consumeNextWith (stringConsumer ("\r \n --" + boundary + "\r \n " ))
156
+ .consumeNextWith (stringConsumer ("Content-Type: text/plain\r \n " ))
157
+ .consumeNextWith (stringConsumer ("Content-Range: bytes 17-20/39\r \n \r \n " ))
158
+ .consumeNextWith (stringConsumer ("test" ))
159
+ .consumeNextWith (stringConsumer ("\r \n --" + boundary + "\r \n " ))
160
+ .consumeNextWith (stringConsumer ("Content-Type: text/plain\r \n " ))
161
+ .consumeNextWith (stringConsumer ("Content-Range: bytes 22-38/39\r \n \r \n " ))
162
+ .consumeNextWith (stringConsumer ("resource content." ))
163
+ .consumeNextWith (stringConsumer ("\r \n --" + boundary + "--" ))
162
164
.expectComplete ()
163
165
.verify ();
164
166
}
0 commit comments