⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 table.java

📁 基于Junit的 功能和单元测试的的测试工具。只支持Swing.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
      }
    };
  }

  public void selectCell(int row, int column) {
    if (!jTable.getCellSelectionEnabled()) {
      Assert.fail("Individual cell selection is not allowed on this table");
    }
    jTable.setRowSelectionInterval(row, row);
    jTable.setColumnSelectionInterval(column, column);
  }

  public void selectRow(int row) {
    jTable.setRowSelectionInterval(row, row);
    if (jTable.getCellSelectionEnabled()) {
      jTable.setColumnSelectionInterval(0, jTable.getColumnCount() - 1);
    }
  }

  public void selectRows(int[] rowIndexes) {
    jTable.getSelectionModel().setValueIsAdjusting(true);
    try {
      jTable.clearSelection();
      for (int i = 0; i < rowIndexes.length; i++) {
        int row = rowIndexes[i];
        jTable.addRowSelectionInterval(row, row);
      }
      if (jTable.getCellSelectionEnabled()) {
        jTable.setColumnSelectionInterval(0, jTable.getColumnCount() - 1);
      }
    }
    finally {
      jTable.getSelectionModel().setValueIsAdjusting(false);
    }
  }

  public void selectRows(int start, int end) {
    if (start > end) {
      throw new IllegalArgumentException("Invalid indexes: " + start + " > " + end);
    }
    jTable.setRowSelectionInterval(start, end);
  }

  public void selectBlock(int top, int left, int bottom, int right) {
    Assert.assertTrue("Only row-level selection is allowed on this table",
                      jTable.getCellSelectionEnabled());
    if ((top > bottom) && (left > right)) {
      throw new IllegalArgumentException("Invalid block definition - expected top <= bottom and left <= right");
    }
    jTable.setRowSelectionInterval(top, bottom);
    jTable.setColumnSelectionInterval(left, right);
  }

  public void addRowToSelection(int row) {
    jTable.addRowSelectionInterval(row, row);
    if (jTable.getCellSelectionEnabled()) {
      jTable.setColumnSelectionInterval(0, jTable.getColumnCount() - 1);
    }
  }

  public void removeRowFromSelection(int row) {
    Assert.assertTrue("Row " + row + " is not selected", jTable.isRowSelected(row));
    jTable.removeRowSelectionInterval(row, row);
  }

  public void clearSelection() {
    jTable.clearSelection();
  }

  /**
   * Helper interface for manipulating the table header, if any
   */
  public class Header {
    private Header() {
    }

    public String[] getColumnNames() {
      String[] columnNames = new String[jTable.getColumnCount()];
      for (int columnIndex = 0; columnIndex < jTable.getColumnCount(); columnIndex++) {
        columnNames[columnIndex] = jTable.getColumnName(columnIndex);
      }
      return columnNames;
    }

    public int findColumnIndex(String columnName) {
      return Table.this.findColumnIndex(columnName);
    }

    /**
     * Checks the column names.
     */
    public Assertion contentEquals(final String[] expectedHeaders) {
      return new Assertion() {
        public void check() {
          checkHeader();
          try {
            Assert.assertEquals(expectedHeaders.length, jTable.getColumnCount());
            for (int i = 0; i < expectedHeaders.length; i++) {
              Assert.assertEquals(expectedHeaders[i], jTable.getColumnName(i));
            }
          }
          catch (AssertionFailedError e) {
            Assert.assertEquals(ArrayUtils.toString(expectedHeaders), ArrayUtils.toString(getColumnNames()));
            throw e;
          }
        }
      };
    }

    /**
     * Returns a string description of the default background color.
     */
    public String getDefaultBackground() {
      checkHeader();
      return ColorUtils.getColorDescription(jTable.getTableHeader().getBackground());
    }

    /**
     * Checks the background color on each column of the table header.
     */
    public Assertion backgroundEquals(final Object[] expectedColors) {
      return new Assertion() {
        public void check() {
          checkHeader();
          TableModel model = jTable.getModel();
          TableCellRenderer headerRenderer = jTable.getTableHeader().getDefaultRenderer();
          for (int columnIndex = 0; columnIndex < model.getColumnCount(); columnIndex++) {
            Component component =
                    headerRenderer.getTableCellRendererComponent(jTable,
                                                                 model.getColumnName(columnIndex),
                                                                 false,
                                                                 false,
                                                                 -1,
                                                                 columnIndex);
            ColorUtils.assertEquals("Unexpected color at column " + columnIndex,
                                    expectedColors[columnIndex], component.getBackground());
          }
        }
      };
    }

    public void click(int columnIndex) {
      checkHeader();
      JTableHeader tableHeader = jTable.getTableHeader();
      Mouse.doClickInRectangle(tableHeader, tableHeader.getHeaderRect(columnIndex), false, Key.Modifier.NONE);
    }

    public void click(String columnName) {
      checkHeader();
      JTableHeader tableHeader = jTable.getTableHeader();
      int columnIndex = findColumnIndex(columnName);
      Mouse.doClickInRectangle(tableHeader, tableHeader.getHeaderRect(columnIndex), false, Key.Modifier.NONE);
    }

    public Trigger triggerClick(final String columnName) {
      return new Trigger() {
        public void run() throws Exception {
          click(columnName);
        }
      };
    }

    public Trigger triggerClick(final int columnIndex) {
      return new Trigger() {
        public void run() throws Exception {
          click(columnIndex);
        }
      };
    }

    public void rightClick(int columnIndex) {
      checkHeader();
      JTableHeader tableHeader = jTable.getTableHeader();
      Mouse.doClickInRectangle(tableHeader, tableHeader.getHeaderRect(columnIndex), true, Key.Modifier.NONE);
    }

    public void rightClick(String columnName) {
      checkHeader();
      JTableHeader tableHeader = jTable.getTableHeader();
      int columnIndex = findColumnIndex(columnName);
      Mouse.doClickInRectangle(tableHeader, tableHeader.getHeaderRect(columnIndex), true, Key.Modifier.NONE);
    }

    public Trigger triggerRightClick(final int columnIndex) {
      return new Trigger() {
        public void run() throws Exception {
          rightClick(columnIndex);
        }
      };
    }

    public Trigger triggerRightClick(final String columnName) {
      return new Trigger() {
        public void run() throws Exception {
          rightClick(columnName);
        }
      };
    }

    private void checkHeader() {
      Assert.assertNotNull("The table contains no header", jTable.getTableHeader());
    }
  }

  private void checkColors(Object[][] colors, ComponentColorAccessor accessor) {
    Assert.assertEquals(colors.length, jTable.getRowCount());
    for (int row = 0; row < colors.length; row++) {
      for (int col = 0; col < colors[row].length; col++) {
        TableCellRenderer cellRenderer = jTable.getCellRenderer(row, col);
        Component component =
                cellRenderer.getTableCellRendererComponent(jTable,
                                                           jTable.getModel().getValueAt(row, col),
                                                           jTable.isCellSelected(row, col),
                                                           false, row, col);
        ColorUtils.assertEquals("Error at (" + row + ", " + col + ") -",
                                colors[row][col], accessor.getColor(component));
      }
    }
  }

  private TableColumn findColumn(String columnName) {
    int columnIndex = findColumnIndex(columnName);
    if (columnIndex == -1) {
      throw new RuntimeException("Column '" + columnName + "' not found");
    }
    return jTable.getColumnModel().getColumn(columnIndex);
  }

  private void dumpRow(JTable table, int row, StringBuffer buffer, String separator) {
    for (int col = 0, colCount = table.getColumnCount(); col < colCount; col++) {
      buffer.append(getValueAt(row, col));
      if (col < (colCount - 1)) {
        buffer.append(separator);
      }
    }
  }

  private void dumpColumn(JTable table, int col, StringBuffer buffer, String separator) {
    for (int row = 0, rowCount = table.getRowCount(); row < rowCount; row++) {
      buffer.append(getValueAt(row, col));
      if (row < (rowCount - 1)) {
        buffer.append(separator);
      }
    }
  }

  private void checkRow(int rowIndex, Object[] expectedRow) {
    Assert.assertEquals(expectedRow.length, jTable.getColumnCount());
    for (int columnIndex = 0; columnIndex < expectedRow.length; columnIndex++) {
      checkValueAt(rowIndex, columnIndex, expectedRow[columnIndex]);
    }
  }

  private void checkColumn(int columnIndex, Object[] expectedColumn) {
    Assert.assertEquals(expectedColumn.length, jTable.getRowCount());
    for (int rowIndex = 0; rowIndex < expectedColumn.length; rowIndex++) {
      checkValueAt(rowIndex, columnIndex, expectedColumn[rowIndex]);
    }
  }

  private void checkValueAt(int rowIndex, int columnIndex, Object expectedValue) {
    Assert.assertEquals("Element at (" + rowIndex + ", " + columnIndex + ") does not match",
                        expectedValue,
                        getValueAt(rowIndex, columnIndex));
  }

  private Object getValueAt(int rowIndex, int columnIndex) {
    return getCellValueConverter(columnIndex).getValue(rowIndex, columnIndex,
                                                       getComponent(rowIndex, columnIndex),
                                                       jTable.getValueAt(rowIndex, columnIndex));
  }

  private TableCellValueConverter getCellValueConverter(int columnIndex) {
    TableCellValueConverter specificConverter = (TableCellValueConverter) cellValuesConvertersByColumn.get(new Integer(columnIndex));
    if (specificConverter != null) {
      return specificConverter;
    }
    return defaultCellValueConverter;
  }

  private void assertCellPropertyEquals(Object[][] properties, ComponentPropertyAccessor accessor) {
    Assert.assertEquals(properties.length, jTable.getRowCount());
    for (int row = 0; row < properties.length; row++) {
      for (int col = 0; col < properties[row].length; col++) {
        TableCellRenderer cellRenderer = jTable.getCellRenderer(row, col);
        Component component =
                cellRenderer.getTableCellRendererComponent(jTable,
                                                           jTable.getModel().getValueAt(row, col),
                                                           jTable.isCellSelected(row, col), false, row, col);
        Assert.assertEquals("Error at (" + row + ", " + col + ") -",
                            properties[row][col], accessor.getProperty(component));
      }
    }
  }

  private Component getComponent(int row, int column) {
    return jTable
            .getCellRenderer(row, column)
            .getTableCellRendererComponent(jTable,
                                           jTable.getValueAt(row, column),
                                           jTable.isCellSelected(row, column),
                                           false,
                                           row, column);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -