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

📄 table.java

📁 基于Junit的 功能和单元测试的的测试工具。只支持Swing.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        }
        catch (AssertionFailedError e) {
          StringBuffer buffer = new StringBuffer();
          dumpRow(jTable, rowIndex, buffer, ",");
          Assert.assertEquals(ArrayUtils.toString(expectedRow), buffer);
        }
      }
    };
  }

  public Assertion rowEquals(final int rowIndex, final String[] columnNames, final Object[] expected) {
    return new Assertion() {
      public void check() throws Exception {
        if (rowIndex < 0) {
          Assert.fail("Row index should be positive");
        }
        if (rowIndex >= jTable.getRowCount()) {
          Assert.fail("Table contains only " + jTable.getRowCount() + " rows, unable to access row " + rowIndex);
        }
        if (columnNames.length != expected.length) {
          Assert.fail("Expected array should have " + columnNames.length + " elements for each row " +
                      "- invalid row " + rowIndex + ": " + ArrayUtils.toString(expected));
        }
        Object[] actual = new Object[expected.length];
        for (int columnIndex = 0; columnIndex < columnNames.length; columnIndex++) {
          int actualIndex = findColumnIndex(columnNames[columnIndex]);
          actual[columnIndex] = getValueAt(rowIndex, actualIndex);
        }
        ArrayUtils.assertEquals("Unexpected content at row " + rowIndex, expected, actual);
      }
    };
  }

  public Assertion columnEquals(final int columnIndex, final Object[] expectedColumn) {
    return new Assertion() {
      public void check() {
        if (columnIndex < 0) {
          Assert.fail("Column index should be positive");
        }
        if (columnIndex >= jTable.getColumnCount()) {
          Assert.fail("Table contains only " + jTable.getColumnCount() + " columns, unable to access column " + columnIndex);
        }
        try {
          checkColumn(columnIndex, expectedColumn);
        }
        catch (AssertionFailedError e) {
          StringBuffer buffer = new StringBuffer();
          dumpColumn(jTable, columnIndex, buffer, ",");
          Assert.assertEquals(ArrayUtils.toString(expectedColumn), buffer);
        }
      }
    };
  }

  public Assertion isEmpty() {
    return new Assertion() {
      public void check() {
        try {
          Assert.assertEquals(0, jTable.getRowCount());
        }
        catch (AssertionFailedError e) {
          Assert.fail("Expected: empty table but was:" + getContent());
        }
      }
    };
  }

  /**
   * Checks the foreground color of the table cells
   *
   * @see <a href="http://www.uispec4j.org/usingcolors.html">Using colors</a>
   */
  public Assertion foregroundEquals(final Object[][] colors) {
    return new Assertion() {
      public void check() {
        checkColors(colors, new ComponentColorAccessor() {
          public Color getColor(Component component) {
            return component.getForeground();
          }
        });
      }
    };
  }

  /**
   * Checks the background color of the table cells
   *
   * @see <a href="http://www.uispec4j.org/usingcolors.html">Using colors</a>
   */
  public Assertion backgroundEquals(final Object[][] colors) {
    return new Assertion() {
      public void check() {
        checkColors(colors, new ComponentColorAccessor() {
          public Color getColor(Component component) {
            return component.getBackground();
          }
        });
      }
    };
  }

  private interface ComponentPropertyAccessor {
    Object getProperty(Component component);
  }

  public Assertion borderEquals(final Border[][] borders) {
    return new Assertion() {
      public void check() {
        assertCellPropertyEquals(borders, new ComponentPropertyAccessor() {
          public Object getProperty(Component component) {
            if (!JComponent.class.isAssignableFrom(component.getClass())) {
              throw new RuntimeException("Component '" + component.getClass() + "' does not support borders");
            }
            return ((JComponent) component).getBorder();
          }
        });
      }
    };
  }

  private int findColumnIndex(String columnName) {
    for (int columnIndex = 0; columnIndex < jTable.getColumnCount(); columnIndex++) {
      if (jTable.getColumnName(columnIndex).equalsIgnoreCase(columnName)) {
        return columnIndex;
      }
    }
    throw new AssertionFailedError("Column '" + columnName + "' not found");
  }

  public Assertion isEditable(final boolean[][] expected) {
    return new Assertion() {
      public void check() {
        Boolean[][] actual = new Boolean[jTable.getRowCount()][jTable.getColumnCount()];
        for (int i = 0; i < actual.length; i++) {
          Boolean[] row = actual[i];
          for (int j = 0; j < row.length; j++) {
            actual[i][j] = Boolean.valueOf(jTable.isCellEditable(i, j));
          }
        }
        ArrayUtils.assertEquals(ArrayUtils.toBooleanObjects(expected), actual);
      }
    };
  }

  public Assertion columnIsEditable(final int columnIndex, final boolean isEditable) {
    return new Assertion() {
      public void check() {
        for (int i = 0; i < jTable.getRowCount(); i++) {
          if (jTable.isCellEditable(i, columnIndex) != isEditable) {
            if (isEditable) {
              Assert.fail("Cell at row " + i + " is not editable");
            }
            else {
              Assert.fail("Cell at row " + i + " is editable");
            }
          }
        }
      }
    };
  }

  public Assertion cellIsEditable(final int rowIndex, final int columnIndex) {
    return new Assertion() {
      public void check() {
        Assert.assertTrue(jTable.isCellEditable(rowIndex, columnIndex));
      }
    };
  }

  public Assertion columnIsEditable(final String columnName, final boolean shouldBeEditable) {
    return columnIsEditable(findColumnIndex(columnName), shouldBeEditable);
  }

  private static interface ComponentColorAccessor {
    Color getColor(Component component);
  }

  public Assertion selectionIsEmpty() {
    return new Assertion() {
      public void check() {
        Assert.assertTrue("Selection is not empty", jTable.getSelectionModel().isSelectionEmpty());
      }
    };
  }

  /**
   * Checks the selection on a cell-by-cell basis.
   */
  public Assertion selectionEquals(final boolean[][] expected) {
    return new Assertion() {
      public void check() {
        int rowCount = expected.length;
        int columnCount = expected[0].length;
        Boolean[][] actual = new Boolean[rowCount][columnCount];
        if (jTable.getCellSelectionEnabled()) {
          for (int row = 0; row < rowCount; row++) {
            for (int column = 0; column < columnCount; column++) {
              actual[row][column] = Boolean.valueOf(jTable.isCellSelected(row, column));
            }
          }
        }
        else {
          for (int row = 0; row < rowCount; row++) {
            boolean isRowSelected = jTable.isRowSelected(row);
            for (int column = 0; column < columnCount; column++) {
              actual[row][column] = Boolean.valueOf(isRowSelected);
            }
          }
        }
        ArrayUtils.orderedCompare(ArrayUtils.toBooleanObjects(expected), actual);
      }
    };
  }

  public Assertion rowIsSelected(final int rowIndex) {
    return new Assertion() {
      public void check() {
        Assert.assertTrue(jTable.isRowSelected(rowIndex));
      }
    };
  }

  public Assertion cellIsSelected(final int rowIndex, final int columnIndex) {
    return new Assertion() {
      public void check() {
        if (!jTable.getCellSelectionEnabled()) {
          Assert.fail("Cell-level selection is not supported on this table");
        }
        Assert.assertTrue(jTable.isCellSelected(rowIndex, columnIndex));
      }
    };
  }

  public String toString() {
    return getContent();
  }

  private String getContent() {
    StringBuffer buffer = new StringBuffer();
    buffer.append('[');
    for (int row = 0, rowCount = jTable.getRowCount(); row < rowCount; row++) {
      if (row > 0) {
        buffer.append("\n ");
      }
      buffer.append('[');
      dumpRow(jTable, row, buffer, ",\t");
      buffer.append(']');
    }
    buffer.append(']');
    return buffer.toString();
  }

  private String getContent(String[] columnNames) {
    StringBuffer buffer = new StringBuffer();
    buffer.append('[');
    for (int row = 0, rowCount = jTable.getRowCount(); row < rowCount; row++) {
      if (row > 0) {
        buffer.append("\n ");
      }
      buffer.append('[');
      for (int col = 0, colCount = columnNames.length; col < colCount; col++) {
        buffer.append(getValueAt(row, findColumnIndex(columnNames[col])));
        if (col < (colCount - 1)) {
          buffer.append(",\t");
        }
      }
      buffer.append(']');
    }
    buffer.append(']');
    return buffer.toString();
  }

  public Component getSwingEditorComponentAt(int row, int column) {
    jTable.editCellAt(row, column);
    return jTable.getEditorComponent();
  }

  public void resizeColumn(String columnName, int width) {
    findColumn(columnName).setPreferredWidth(width);
  }

  public Assertion columnSizeEquals(final String columnName, final int expectedWidth) {
    return new Assertion() {
      public void check() {
        Assert.assertEquals(expectedWidth, findColumn(columnName).getPreferredWidth());
      }
    };
  }

  public Assertion columnSizeEquals(final int columnIndex, final int expectedWidth) {
    return new Assertion() {
      public void check() {
        Assert.assertEquals(expectedWidth,
                            jTable.getColumnModel().getColumn(columnIndex).getPreferredWidth());
      }
    };
  }

  public Assertion rowsAreSelected(final int[] rowIndexes) {
    return new Assertion() {
      public void check() {
        int[] actualSelection = jTable.getSelectedRows();
        Arrays.sort(actualSelection);
        int[] expectedSelection = (int[]) rowIndexes.clone();
        Arrays.sort(expectedSelection);
        ArrayUtils.assertEquals(expectedSelection, actualSelection);

⌨️ 快捷键说明

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