コード例 #1
0
 @Test(expected = IOException.class)
 public void testDispatchIncludeHasNoValidResource() throws Exception {
   when(mockRequest.getRequestDispatcher(Mockito.anyString())).thenReturn(mockDispatcher);
   doAnswer(
           new Answer<Void>() {
             public Void answer(final InvocationOnMock invocation) throws Throwable {
               throw new IOException("Include doesn't work... nothing found");
             }
           })
       .when(mockDispatcher)
       .include(Mockito.any(HttpServletRequest.class), Mockito.any(HttpServletResponse.class));
   victim.getInputStream(mockRequest, mockResponse, "/static/*.js");
 }
コード例 #2
0
 @Test
 public void shouldReturnsResourceIncludedByDispatcher() throws Exception {
   final String content = "SomeNonEmptyContent";
   when(mockRequest.getRequestDispatcher(Mockito.anyString())).thenReturn(mockDispatcher);
   doAnswer(
           new Answer<Void>() {
             public Void answer(final InvocationOnMock invocation) throws Throwable {
               HttpServletResponse response = (HttpServletResponse) invocation.getArguments()[1];
               response.getOutputStream().write(content.getBytes());
               return null;
             }
           })
       .when(mockDispatcher)
       .include(Mockito.any(HttpServletRequest.class), Mockito.any(HttpServletResponse.class));
   assertEquals(
       content,
       IOUtils.toString(victim.getInputStream(mockRequest, mockResponse, "/static/*.js")));
   verify(mockUriLocator, never()).locate(Mockito.anyString());
 }
コード例 #3
0
  @Test
  public void shouldFallbackToExternalResourceLocatorWhenDispatcherReturns404() throws Exception {
    victim =
        new DispatcherStreamLocator() {
          @Override
          UriLocator createExternalResourceLocator() {
            return mockUriLocator;
          }
        };
    final String location = "/some/location.js";
    when(mockRequest.getRequestDispatcher(location)).thenReturn(mockDispatcher);
    Mockito.doAnswer(
            new Answer<Void>() {
              public Void answer(final InvocationOnMock invocation) throws Throwable {
                // simulate the dispatched response is empty
                return null;
              }
            })
        .when(mockDispatcher)
        .include(Mockito.any(HttpServletRequest.class), Mockito.any(HttpServletResponse.class));
    victim.getInputStream(mockRequest, mockResponse, location);

    verify(mockUriLocator).locate(Mockito.anyString());
  }
コード例 #4
0
 @Test
 public void canLocateValidResource() throws Exception {
   when(mockRequest.getRequestURL()).thenReturn(new StringBuffer(""));
   assertNotNull(victim.getInputStream(mockRequest, mockResponse, "http://www.google.com"));
 }
コード例 #5
0
 @Test(expected = IOException.class)
 public void cannotLocateInvalidLocation() throws Exception {
   victim.getInputStream(mockRequest, mockResponse, "/INVALID");
 }
コード例 #6
0
 @Test(expected = IOException.class)
 public void cannotLocateNullLocation() throws Exception {
   victim.getInputStream(mockRequest, mockResponse, null);
 }
コード例 #7
0
 @Test(expected = NullPointerException.class)
 public void shouldNotAcceptNullRequestOrResponse() throws Exception {
   victim.getInputStream(null, null, null);
 }