Skip to content
Snippets Groups Projects
Commit fb382417 authored by Manuel Leduc's avatar Manuel Leduc
Browse files

[Misc] Fix AbstractServletResourceReferenceHandlerTest after commons-io upgrade to 2.9.0

- Provides a mock instead of null for IOUtils.copy second parameter
- Fix a typo, steam replaced by stReam
- Code style improvements
parent fc6d3e3f
No related branches found
No related tags found
No related merge requests found
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
package org.xwiki.resource.servlet; package org.xwiki.resource.servlet;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.util.List; import java.util.List;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
...@@ -67,6 +68,12 @@ class AbstractServletResourceReferenceHandlerTest ...@@ -67,6 +68,12 @@ class AbstractServletResourceReferenceHandlerTest
@Mock @Mock
private InputStream filterStream; private InputStream filterStream;
@Mock
private Response response;
@Mock
private OutputStream outputStream;
@RegisterExtension @RegisterExtension
LogCaptureExtension logCapture = new LogCaptureExtension(DEBUG); LogCaptureExtension logCapture = new LogCaptureExtension(DEBUG);
...@@ -90,7 +97,7 @@ public List<ResourceType> getSupportedResourceReferences() ...@@ -90,7 +97,7 @@ public List<ResourceType> getSupportedResourceReferences()
@Override @Override
protected InputStream getResourceStream(ResourceReference resourceReference) protected InputStream getResourceStream(ResourceReference resourceReference)
{ {
return inputStream; return this.inputStream;
} }
@Override @Override
...@@ -102,40 +109,41 @@ protected String getResourceName(ResourceReference resourceReference) ...@@ -102,40 +109,41 @@ protected String getResourceName(ResourceReference resourceReference)
@Override @Override
protected InputStream filterResource(ResourceReference resourceReference, InputStream resourceStream) protected InputStream filterResource(ResourceReference resourceReference, InputStream resourceStream)
{ {
return filterStream; return this.filterStream;
} }
} }
@BeforeEach @BeforeEach
void setUp() void setUp() throws Exception
{ {
when(this.container.getResponse()).thenReturn(this.response);
when(this.response.getOutputStream()).thenReturn(this.outputStream);
// Initialize the reference handler with input stream mocks in order to be able to verify them in the tests. // Initialize the reference handler with input stream mocks in order to be able to verify them in the tests.
this.referenceHandler.inputStream = this.inputStream; this.referenceHandler.inputStream = this.inputStream;
this.referenceHandler.filterStream = this.filterStream; this.referenceHandler.filterStream = this.filterStream;
} }
/** /**
* Verify that the input and filter steams are closed after use. * Verify that the input and filter streams are closed after use.
*/ */
@Test @Test
void handleSteamClosed() throws Exception void handleStreamClosed() throws Exception
{ {
when(this.container.getResponse()).thenReturn(mock(Response.class));
this.referenceHandler.handle(mock(ResourceReference.class), mock(ResourceReferenceHandlerChain.class)); this.referenceHandler.handle(mock(ResourceReference.class), mock(ResourceReferenceHandlerChain.class));
verify(inputStream).close(); verify(this.inputStream).close();
verify(filterStream).close(); verify(this.filterStream).close();
} }
/** /**
* Verify that the resources are closed even if an error occurs during the execution. * Verify that the resources are closed even if an error occurs during the execution.
*/ */
@Test @Test
void handleSteamClosedWhenErrorOnServeResource() throws Exception void handleStreamClosedWhenErrorOnServeResource() throws Exception
{ {
// Fail at first call in serveResource, then return a result in sendError // Fail at first call in serveResource, then return a result in sendError
when(this.container.getResponse()).thenThrow(new RuntimeException()).thenReturn(mock(Response.class)); when(this.container.getResponse()).thenThrow(new RuntimeException()).thenReturn(this.response);
this.referenceHandler this.referenceHandler
.handle(mock(ResourceReference.class), mock(ResourceReferenceHandlerChain.class)); .handle(mock(ResourceReference.class), mock(ResourceReferenceHandlerChain.class));
...@@ -152,14 +160,12 @@ void handleSteamClosedWhenErrorOnServeResource() throws Exception ...@@ -152,14 +160,12 @@ void handleSteamClosedWhenErrorOnServeResource() throws Exception
* Verify the resources are closed even when filterResource returns the same stream as getResourceStream. * Verify the resources are closed even when filterResource returns the same stream as getResourceStream.
*/ */
@Test @Test
void handleFilteredSteamUnchanged() throws Exception void handleFilteredStreamUnchanged() throws Exception
{ {
this.referenceHandler.filterStream = this.inputStream; this.referenceHandler.filterStream = this.inputStream;
when(this.container.getResponse()).thenReturn(mock(Response.class));
this.referenceHandler.handle(mock(ResourceReference.class), mock(ResourceReferenceHandlerChain.class)); this.referenceHandler.handle(mock(ResourceReference.class), mock(ResourceReferenceHandlerChain.class));
verify(this.inputStream, times(2)).close(); verify(this.inputStream, times(2)).close();
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment