11package org .jooby .servlet ;
22
3+ import com .google .common .io .ByteStreams ;
34import static org .easymock .EasyMock .expect ;
5+ import org .jooby .funzy .Throwing ;
6+ import org .jooby .test .MockUnit ;
47import static org .junit .Assert .assertEquals ;
8+ import org .junit .Test ;
9+ import org .junit .runner .RunWith ;
10+ import org .powermock .core .classloader .annotations .PrepareForTest ;
11+ import org .powermock .modules .junit4 .PowerMockRunner ;
512
13+ import javax .servlet .ServletOutputStream ;
14+ import javax .servlet .http .HttpServletRequest ;
15+ import javax .servlet .http .HttpServletResponse ;
616import java .io .InputStream ;
717import java .nio .ByteBuffer ;
818import java .nio .channels .Channels ;
1222import java .util .Collections ;
1323import java .util .Optional ;
1424
15- import javax .servlet .ServletOutputStream ;
16- import javax .servlet .http .HttpServletRequest ;
17- import javax .servlet .http .HttpServletResponse ;
18-
19- import org .jooby .test .MockUnit ;
20- import org .junit .Test ;
21- import org .junit .runner .RunWith ;
22- import org .powermock .core .classloader .annotations .PrepareForTest ;
23- import org .powermock .modules .junit4 .PowerMockRunner ;
24-
25- import com .google .common .io .ByteStreams ;
26-
2725@ RunWith (PowerMockRunner .class )
2826@ PrepareForTest ({ServletServletResponse .class , Channels .class , ByteStreams .class ,
29- FileChannel .class })
27+ FileChannel .class , Throwing . class , Throwing . Runnable . class })
3028public class ServletServletResponseTest {
3129
3230 @ Test
@@ -178,7 +176,7 @@ public void sendByteBuffer() throws Exception {
178176 public void sendFileChannel () throws Exception {
179177 new MockUnit (HttpServletRequest .class , HttpServletResponse .class , ServletOutputStream .class )
180178 .expect (unit -> {
181- FileChannel channel = unit .powerMock (FileChannel .class );
179+ FileChannel channel = unit .partialMock (FileChannel .class , "transferTo" , "close" );
182180 unit .registerMock (FileChannel .class , channel );
183181 })
184182 .expect (unit -> {
@@ -192,7 +190,6 @@ public void sendFileChannel() throws Exception {
192190 expect (Channels .newChannel (output )).andReturn (channel );
193191
194192 expect (fchannel .transferTo (0L , 10L , channel )).andReturn (1L );
195-
196193 fchannel .close ();
197194 channel .close ();
198195
@@ -209,23 +206,23 @@ public void sendFileChannel() throws Exception {
209206 public void sendInputStream () throws Exception {
210207 new MockUnit (HttpServletRequest .class , HttpServletResponse .class , InputStream .class ,
211208 ServletOutputStream .class )
212- .expect (unit -> {
213- InputStream in = unit .get (InputStream .class );
214- ServletOutputStream output = unit .get (ServletOutputStream .class );
209+ .expect (unit -> {
210+ InputStream in = unit .get (InputStream .class );
211+ ServletOutputStream output = unit .get (ServletOutputStream .class );
215212
216- unit .mockStatic (ByteStreams .class );
217- expect (ByteStreams .copy (in , output )).andReturn (0L );
213+ unit .mockStatic (ByteStreams .class );
214+ expect (ByteStreams .copy (in , output )).andReturn (0L );
218215
219- output .close ();
220- in .close ();
216+ output .close ();
217+ in .close ();
221218
222- HttpServletResponse rsp = unit .get (HttpServletResponse .class );
223- expect (rsp .getOutputStream ()).andReturn (output );
224- })
225- .run (unit -> {
226- new ServletServletResponse (unit .get (HttpServletRequest .class ),
227- unit .get (HttpServletResponse .class )).send (unit .get (InputStream .class ));
228- });
219+ HttpServletResponse rsp = unit .get (HttpServletResponse .class );
220+ expect (rsp .getOutputStream ()).andReturn (output );
221+ })
222+ .run (unit -> {
223+ new ServletServletResponse (unit .get (HttpServletRequest .class ),
224+ unit .get (HttpServletResponse .class )).send (unit .get (InputStream .class ));
225+ });
229226 }
230227
231228}
0 commit comments