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

📄 tablecontenttest.java

📁 基于Junit的 功能和单元测试的的测试工具。只支持Swing.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
              }));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertTrue(e.getMessage().startsWith("Expected 3 rows but found 2"));
    }
  }

  public void testRowEquals() throws Exception {
    assertTrue(table.rowEquals(0, new Object[]{"a", Boolean.TRUE, "3"}));
    assertTrue(table.rowEquals(1, new Object[]{"c", Boolean.FALSE, "4"}));
  }

  public void testRowEqualsFailures() throws Exception {
    try {
      assertTrue(table.rowEquals(1, new Object[]{"c", Boolean.TRUE, "4"}));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("expected:<[c,true,4]> but was:<c,false,4>", e.getMessage());
    }

    try {
      assertTrue(table.rowEquals(-1, new Object[]{"a", "b", "c"}));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("Row index should be positive", e.getMessage());
    }

    try {
      assertTrue(table.rowEquals(2, new Object[]{"a", "b", "c"}));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("Table contains only 2 rows, unable to access row 2", e.getMessage());
    }
  }

  public void testCustomRowEquals() throws Exception {
    assertTrue(table.rowEquals(0, new String[]{"0", "1", "2"}, new Object[]{"a", Boolean.TRUE, "3"}));
    assertTrue(table.rowEquals(1, new String[]{"0", "1", "2"}, new Object[]{"c", Boolean.FALSE, "4"}));

    assertTrue(table.rowEquals(0, new String[]{"0", "2", "1"}, new Object[]{"a", "3", Boolean.TRUE}));

    assertTrue(table.rowEquals(0, new String[]{"2", "0"}, new Object[]{"3", "a"}));
  }

  public void testCustomRowEqualsErrors() throws Exception {
    try {
      assertTrue(table.rowEquals(0, new String[]{"0", "2"}, new Object[]{"a", "xxxxxx"}));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("Unexpected content at row 0\n" +
                   "Expected: [a,xxxxxx]\n" +
                   "Actual:   [a,3]", e.getMessage());
    }
  }

  public void testCustomRowEqualsChecksTheNumberOfColumns() throws Exception {
    try {
      assertTrue(table.rowEquals(0, new String[]{"0", "2", "1"}, new Object[]{"a", "3"}));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("Expected array should have 3 elements for each row - invalid row 0: [a,3]", e.getMessage());
    }
  }

  public void testCustomRowEqualsChecksTheColumnNames() throws Exception {
    try {
      assertTrue(table.rowEquals(0, new String[]{"0", "2", "unknown"}, new Object[]{"a", "3", "x"}));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("Column 'unknown' not found", e.getMessage());
    }
  }

  public void testCustomRowEqualsChecksTheNumberOfRows() throws Exception {
    try {
      assertTrue(table.rowEquals(3, new String[]{"2", "0"}, new Object[]{"3", "a"}));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("Table contains only 2 rows, unable to access row 3", e.getMessage());
    }
  }

  public void testColumnEquals() throws Exception {
    assertTrue(table.columnEquals(0, new Object[]{"a", "c"}));
    assertTrue(table.columnEquals(1, new Object[]{Boolean.TRUE, Boolean.FALSE}));
    assertTrue(table.columnEquals(2, new Object[]{"3", "4"}));
  }

  public void testColumnEqualsFailures() throws Exception {
    try {
      assertTrue(table.columnEquals(1, new Object[]{Boolean.TRUE, Boolean.TRUE}));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("expected:<[true,true]> but was:<true,false>", e.getMessage());
    }

    try {
      assertTrue(table.columnEquals(-1, new Object[]{"a", "c"}));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("Column index should be positive", e.getMessage());
    }

    try {
      assertTrue(table.columnEquals(3, new Object[]{"3", "4"}));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("Table contains only 3 columns, unable to access column 3", e.getMessage());
    }
  }

  public void testGetRowAndColumnCount() throws Exception {
    assertEquals(2, table.getRowCount());
    assertEquals(3, table.getColumnCount());
  }

  public void testGetContentAt() throws Exception {
    assertEquals("a", table.getContentAt(0, 0));
    assertEquals(Boolean.TRUE, table.getContentAt(0, 1));
    assertEquals("c", table.getContentAt(1, 0));
    assertEquals(Boolean.FALSE, table.getContentAt(1, 1));

    jTable.getModel().setValueAt(null, 0, 0);
    assertEquals("", table.getContentAt(0, 0));
  }

  public void testGetContentAtWorksWhenTheRendererIsUnusable() throws Exception {
    jTable.setDefaultRenderer(String.class, new DefaultTableCellRenderer() {
      public Component getTableCellRendererComponent(JTable table, Object value,
                                                     boolean isSelected,
                                                     boolean hasFocus,
                                                     int row, int column) {
        return new JPanel();
      }
    });
    assertEquals("a", table.getContentAt(0, 0));
  }

  public void testGetContentAtWithConverter() throws Exception {
    assertEquals("-a-", table.getContentAt(0, 0, new TableCellValueConverter() {
      public Object getValue(int row, int column, Component renderedComponent, Object modelObject) {
        return "-" + modelObject + "-";
      }
    }));

    assertEquals(new Integer(3), table.getContentAt(0, 2, ModelTableCellValueConverter.INSTANCE));
  }

  public void testGetEditorComponentAt() throws Exception {
    Component firstCellComponent = table.getSwingEditorComponentAt(0, 0);
    assertTrue(firstCellComponent instanceof JTextField);

    Component secondCellComponent = table.getSwingEditorComponentAt(0, 1);
    assertTrue(secondCellComponent instanceof JCheckBox);

    Component thirdCellComponent = table.getSwingEditorComponentAt(0, 2);
    assertTrue(thirdCellComponent instanceof JComboBox);
  }

  public void testToString() throws Exception {
    assertEquals("[[a,\ttrue,\t3]\n" +
                 " [c,\tfalse,\t4]]",
                 table.toString());
  }

  public void testForegroundEquals() throws Exception {
    assertTrue(table.foregroundEquals(new String[][]{
            {"black", "black", "black"},
            {"black", "black", "black"}
    }));
    table.foregroundEquals("black");
  }

  public void testBackgrounEqualsWithDefaultValue() throws Exception {
    jTable.setBackground(Color.BLUE);
    assertTrue(table.backgroundEquals("blue"));
    try {
      assertTrue(table.backgroundEquals("green"));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("expected:<GREEN> but was:<0000FF>", e.getMessage());
    }
  }

  public void testBackgroundEquals() throws Exception {
    jTable.getColumnModel().getColumn(1).setCellRenderer(new DefaultTableCellRenderer() {
      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        if (row == 1) {
          component.setBackground(Color.red);
        }
        else {
          component.setBackground(Color.blue);
        }
        return component;
      }
    });
    assertTrue(table.backgroundEquals(new Object[][]{
            {"white", "blue", "white"},
            {Color.white, Color.red, Color.white}
    }));

    try {
      assertTrue(table.backgroundEquals(new Object[][]{
              {"white", "green", "white"},
              {Color.white, Color.red, Color.white}
      }));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("Error at (0, 1) - expected:<GREEN> but was:<0000FF>", e.getMessage());
    }
  }

  public void testBackgroundEqualsUsesTheSelectionBackgroundColor() throws Exception {
    jTable.setCellSelectionEnabled(true);
    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() {
      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        if (isSelected) {
          component.setBackground(Color.red);
        }
        else {
          component.setBackground(Color.black);
        }
        return component;
      }
    };
    jTable.setDefaultRenderer(Object.class, renderer);
    jTable.setDefaultRenderer(Boolean.class, renderer);
    jTable.setDefaultRenderer(Integer.class, renderer);
    table.selectCell(0, 1);
    table.backgroundEquals(new Object[][]{
            {Color.BLACK, "red", Color.BLACK},
            {Color.BLACK, Color.BLACK, Color.BLACK}
    });
  }
}

⌨️ 快捷键说明

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