AsynchronousProcessListWriter implements MessageBodyWriter<Iterable<AsynchronousProcess>> { @Override public void writeTo(Iterable<AsynchronousProcess> asynchronousProcesses, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { PrintWriter writer = new PrintWriter(new OutputStreamWriter(entityStream)); try { writer.format(OUTPUT_FORMAT, "USER", "ID", "STAT", "PATH"); for (AsynchronousProcess process : asynchronousProcesses) { writer.format(OUTPUT_FORMAT, process.getOwner() == null ? "unknown" : process.getOwner(), process.getId(), process.getStatus(), process.getPath()); } } finally { writer.flush(); } }  @Override boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override long getSize(Iterable<AsynchronousProcess> asynchronousProcesses,
                        Class<?> type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType); @Override void writeTo(Iterable<AsynchronousProcess> asynchronousProcesses,
                        Class<?> type, Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType,
                        MultivaluedMap<String, Object> httpHeaders,
                        OutputStream entityStream);  }
@Test public void writesListOfAsynchronousProcessAsPlainText() throws Exception { List<AsynchronousProcess> processes = newArrayList(new AsynchronousProcess("andrew", 1L, "/a", "running"), new AsynchronousProcess("user", 2L, "/b", "done")); List<List<String>> expectedProcessesTable = newArrayList(newArrayList("USER", "ID", "STAT", "PATH"), newArrayList("andrew", "1", "running", "/a"), newArrayList("user", "2", "done", "/b")); ByteArrayOutputStream bout = new ByteArrayOutputStream(); processListWriter.writeTo(processes, List.class, newParameterizedType(List.class, AsynchronousProcess.class), new Annotation[0], MediaType.TEXT_PLAIN_TYPE, new MultivaluedHashMap<>(), bout); List<String> lines = CharStreams.readLines(new StringReader(bout.toString())); assertEquals(3, lines.size()); Pattern pattern = Pattern.compile("(\\w+)\\s+(\\w+)\\s+(\\w+)\\s+(/?\\w+)"); List<List<String>> processesTable = newArrayList(); for (String line : lines) { Matcher matcher = pattern.matcher(line); assertTrue(String.format("String '%s' is not matched to pattern", line), matcher.matches()); processesTable.add(getAllGroups(matcher)); } assertEquals(expectedProcessesTable, processesTable); }