@Test
  public void testImportStudents() throws Exception {

    // Given
    StudentDirectoryServiceImpl studentDirectoryService = new StudentDirectoryServiceImpl();

    StudentDAO studentDAO = mock(StudentDAO.class);
    studentDirectoryService.setStudentDAO(studentDAO);

    MultipartFile multipartFile = mock(MultipartFile.class);

    StringBuffer buffer = new StringBuffer();
    buffer.append("Bart, Simpson\n");
    buffer.append("Lisa, Simpson");
    InputStream stream =
        new ByteArrayInputStream(buffer.toString().getBytes(StandardCharsets.UTF_8));
    when(multipartFile.getInputStream()).thenReturn(stream);

    // When
    studentDirectoryService.importStudents(multipartFile);

    // Then
    ArgumentCaptor<Student> studentCaptor = ArgumentCaptor.forClass(Student.class);
    verify(studentDAO, times(2)).createStudent(studentCaptor.capture());

    assertEquals("Bart", studentCaptor.getAllValues().get(0).getFirstName());
    assertEquals("Simpson", studentCaptor.getAllValues().get(0).getLastName());

    assertEquals("Lisa", studentCaptor.getAllValues().get(1).getFirstName());
    assertEquals("Simpson", studentCaptor.getAllValues().get(1).getLastName());
  }
    @Override
    public String toString(
        boolean qOption, boolean hOption, boolean tOption, List<StorageType> types) {
      if (tOption) {
        StringBuffer result = new StringBuffer();
        result.append(hOption ? HUMAN : BYTES);

        for (StorageType type : types) {
          result.append(type.toString());
          result.append(" ");
        }
        return result.toString();
      }

      if (qOption) {
        if (hOption) {
          return (HUMAN + WITH_QUOTAS);
        } else {
          return (BYTES + WITH_QUOTAS);
        }
      } else {
        if (hOption) {
          return (HUMAN + NO_QUOTAS);
        } else {
          return (BYTES + NO_QUOTAS);
        }
      }
    }
 @Nonnull
 protected String generateCookieString(final Cookie cookie) {
   final StringBuffer sb = new StringBuffer();
   ServerCookie.appendCookieValue(
       sb,
       cookie.getVersion(),
       cookie.getName(),
       cookie.getValue(),
       cookie.getPath(),
       cookie.getDomain(),
       cookie.getComment(),
       cookie.getMaxAge(),
       cookie.getSecure(),
       true);
   final String setSessionCookieHeader = sb.toString();
   return setSessionCookieHeader;
 }
Exemplo n.º 4
0
  @Test
  public void testPrintMethods() throws IOException {
    JspWriter out = mock(JspWriter.class);
    HttpServletRequest req = mock(HttpServletRequest.class);

    final StringBuffer buffer = new StringBuffer();

    ArgumentCaptor<String> arg = ArgumentCaptor.forClass(String.class);
    doAnswer(
            new Answer<String>() {
              @Override
              public String answer(InvocationOnMock invok) {
                Object[] args = invok.getArguments();
                buffer.append(args[0]);
                return null;
              }
            })
        .when(out)
        .print(arg.capture());

    JspHelper.createTitle(out, req, "testfile.txt");
    verify(out, times(1)).print(Mockito.anyString());

    JspHelper.addTableHeader(out);
    verify(out, times(1 + 2)).print(anyString());

    JspHelper.addTableRow(out, new String[] {" row11", "row12 "});
    verify(out, times(1 + 2 + 4)).print(anyString());

    JspHelper.addTableRow(out, new String[] {" row11", "row12 "}, 3);
    verify(out, times(1 + 2 + 4 + 4)).print(Mockito.anyString());

    JspHelper.addTableRow(out, new String[] {" row21", "row22"});
    verify(out, times(1 + 2 + 4 + 4 + 4)).print(anyString());

    JspHelper.addTableFooter(out);
    verify(out, times(1 + 2 + 4 + 4 + 4 + 1)).print(anyString());

    assertFalse(isNullOrEmpty(buffer.toString()));
  }