@Test
  public void whenResultIsAnInputStreamShouldCreateAByteArrayDownload() throws Exception {
    byte[] bytes = "abc".getBytes();
    when(info.getResult()).thenReturn(bytes);

    interceptor.intercept(stack, controllerMethod, null);

    verify(outputStream).write(argThat(is(arrayStartingWith(bytes))), eq(0), eq(3));
  }
  @Test
  public void whenResultIsADownloadShouldUseIt() throws Exception {
    Download download = mock(Download.class);

    when(info.getResult()).thenReturn(download);

    interceptor.intercept(stack, controllerMethod, null);

    verify(download).write(response);
  }
  @Test
  public void whenResultIsNullAndResultWasUsedShouldDoNothing() throws Exception {
    when(info.getResult()).thenReturn(null);
    when(result.used()).thenReturn(true);

    interceptor.intercept(stack, controllerMethod, null);

    verify(stack).next(controllerMethod, null);
    verifyZeroInteractions(response);
  }
Exemplo n.º 4
0
  public void outject(@Observes MethodExecuted event, Result result, MethodInfo methodInfo) {

    Type returnType = event.getMethodReturnType();

    if (!returnType.equals(Void.TYPE)) {
      String name = extractor.nameFor(returnType);
      Object value = methodInfo.getResult();
      logger.debug("outjecting {}={}", name, value);
      result.include(name, value);
    }
  }
  @Test
  public void whenResultIsNullAndResultWasNotUsedShouldThrowNPE() throws Exception {
    when(info.getResult()).thenReturn(null);
    when(result.used()).thenReturn(false);

    try {
      interceptor.intercept(stack, controllerMethod, null);
      fail("expected NullPointerException");
    } catch (NullPointerException e) {
      verifyZeroInteractions(response);
    }
  }
  @Test
  public void whenResultIsAFileShouldCreateAFileDownload() throws Exception {
    File tmp = File.createTempFile("test", "test");
    Files.write(tmp.toPath(), "abc".getBytes());

    when(info.getResult()).thenReturn(tmp);

    interceptor.intercept(stack, controllerMethod, null);
    verify(outputStream).write(argThat(is(arrayStartingWith("abc".getBytes()))), eq(0), eq(3));

    tmp.delete();
  }
  @Test
  public void shouldThrowInterceptionExceptionIfIOExceptionOccurs() throws Exception {
    Download download = mock(Download.class);

    when(info.getResult()).thenReturn(download);
    when(result.used()).thenReturn(false);
    doThrow(new IOException()).when(download).write(any(HttpServletResponse.class));

    try {
      interceptor.intercept(stack, controllerMethod, null);
      fail("expected InterceptionException");
    } catch (InterceptionException e) {

    }
  }