Esempio n. 1
0
  @Test
  public void testSimple() {
    Table table = this.getTableWithHeaders();
    table.startRow();
    table.addCell("foo1");
    table.addCell("bar1");
    table.endRow();
    table.startRow();
    table.addCell("foo2");
    table.addCell("bar2");
    table.endRow();

    // Check headers
    List<Table.Cell> headers = table.getHeaders();
    assertEquals(2, headers.size());
    assertEquals("foo", headers.get(0).value.toString());
    assertEquals(2, headers.get(0).attr.size());
    assertEquals("f", headers.get(0).attr.get("alias"));
    assertEquals("foo", headers.get(0).attr.get("desc"));
    assertEquals("bar", headers.get(1).value.toString());
    assertEquals(2, headers.get(1).attr.size());
    assertEquals("b", headers.get(1).attr.get("alias"));
    assertEquals("bar", headers.get(1).attr.get("desc"));

    // Check rows
    List<List<Table.Cell>> rows = table.getRows();
    assertEquals(2, rows.size());
    List<Table.Cell> row = rows.get(0);
    assertEquals("foo1", row.get(0).value.toString());
    assertEquals("bar1", row.get(1).value.toString());
    row = rows.get(1);
    assertEquals("foo2", row.get(0).value.toString());
    assertEquals("bar2", row.get(1).value.toString());

    // Check getAsMap
    Map<String, List<Table.Cell>> map = table.getAsMap();
    assertEquals(2, map.size());
    row = map.get("foo");
    assertEquals("foo1", row.get(0).value.toString());
    assertEquals("foo2", row.get(1).value.toString());
    row = map.get("bar");
    assertEquals("bar1", row.get(0).value.toString());
    assertEquals("bar2", row.get(1).value.toString());

    // Check getHeaderMap
    Map<String, Table.Cell> headerMap = table.getHeaderMap();
    assertEquals(2, headerMap.size());
    Table.Cell cell = headerMap.get("foo");
    assertEquals("foo", cell.value.toString());
    cell = headerMap.get("bar");
    assertEquals("bar", cell.value.toString());

    // Check findHeaderByName
    cell = table.findHeaderByName("foo");
    assertEquals("foo", cell.value.toString());
    cell = table.findHeaderByName("missing");
    assertNull(cell);
  }
Esempio n. 2
0
  public static RestResponse buildTextPlainResponse(
      Table table, RestRequest request, RestChannel channel) {
    boolean verbose = request.paramAsBoolean("v", false);

    List<DisplayHeader> headers = buildDisplayHeaders(table, request);
    int[] width = buildWidths(table, request, verbose, headers);

    StringBuilder out = new StringBuilder();
    if (verbose) {
      for (int col = 0; col < headers.size(); col++) {
        DisplayHeader header = headers.get(col);
        pad(
            new Table.Cell(header.display, table.findHeaderByName(header.name)),
            width[col],
            request,
            out);
        out.append(" ");
      }
      out.append("\n");
    }

    for (int row = 0; row < table.getRows().size(); row++) {
      for (int col = 0; col < headers.size(); col++) {
        DisplayHeader header = headers.get(col);
        pad(table.getAsMap().get(header.name).get(row), width[col], request, out);
        out.append(" ");
      }
      out.append("\n");
    }

    return new StringRestResponse(RestStatus.OK, out.toString());
  }
Esempio n. 3
0
  public static RestResponse buildTextPlainResponse(Table table, RestChannel channel)
      throws IOException {
    RestRequest request = channel.request();
    boolean verbose = request.paramAsBoolean("v", false);

    List<DisplayHeader> headers = buildDisplayHeaders(table, request);
    int[] width = buildWidths(table, request, verbose, headers);

    BytesStreamOutput bytesOut = channel.bytesOutput();
    UTF8StreamWriter out = new UTF8StreamWriter().setOutput(bytesOut);
    int lastHeader = headers.size() - 1;
    if (verbose) {
      for (int col = 0; col < headers.size(); col++) {
        DisplayHeader header = headers.get(col);
        boolean isLastColumn = col == lastHeader;
        pad(
            new Table.Cell(header.display, table.findHeaderByName(header.name)),
            width[col],
            request,
            out,
            isLastColumn);
        if (!isLastColumn) {
          out.append(" ");
        }
      }
      out.append("\n");
    }
    for (int row = 0; row < table.getRows().size(); row++) {
      for (int col = 0; col < headers.size(); col++) {
        DisplayHeader header = headers.get(col);
        boolean isLastColumn = col == lastHeader;
        pad(table.getAsMap().get(header.name).get(row), width[col], request, out, isLastColumn);
        if (!isLastColumn) {
          out.append(" ");
        }
      }
      out.append("\n");
    }
    out.close();
    return new BytesRestResponse(
        RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, bytesOut.bytes());
  }