📄 tablecontenttest.java
字号:
package org.uispec4j;
import junit.framework.AssertionFailedError;
import org.uispec4j.utils.AssertionFailureNotDetectedError;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
public class TableContentTest extends TableTestCase {
public void testAssertContentEquals() throws Exception {
assertTrue(table.contentEquals(new Object[][]{
{"a", Boolean.TRUE, "3"},
{"c", Boolean.FALSE, "4"}
}));
}
public void testAssertContentEqualsFailure() throws Exception {
try {
assertTrue(table.contentEquals(new Object[][]{
{"a", Boolean.TRUE, "3"},
{"c", Boolean.FALSE, "ERROR!"}
}));
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
}
}
public void testAssertEmptyFailure() throws Exception {
try {
assertTrue(table.isEmpty());
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
assertEquals("Expected: empty table but was:[[a,\ttrue,\t3]\n [c,\tfalse,\t4]]", e.getMessage());
}
}
public void testContentEqualsUsesTheModelWhenAnUnknownRendererComponentIsUsed() throws Exception {
installJPanelRenderer();
assertTrue(table.contentEquals(new Object[][]{
{"a", "true", "3"},
{"c", "false", "4"}
}));
try {
assertTrue(table.contentEquals(new Object[][]{
{"a", Boolean.TRUE, "3"},
{"c", Boolean.FALSE, "4"}
}));
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
}
}
public void testContentEqualsWithUnknownRendererComponentAndNullModelValue() throws Exception {
installJPanelRenderer();
jTable.getModel().setValueAt(null, 0, 1);
assertTrue(table.contentEquals(new Object[][]{
{"a", "", "3"},
{"c", "false", "4"}
}));
try {
assertTrue(table.contentEquals(new Object[][]{
{"a", "", "3"},
{"c", Boolean.FALSE, "4"}
}));
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
}
}
public void testContentEqualsWhenColumnMoved() throws Exception {
jTable.moveColumn(0, 1);
assertTrue(table.contentEquals(new Object[][]{
{Boolean.TRUE, "a", "3"},
{Boolean.FALSE, "c", "4"}
}));
try {
assertTrue(table.contentEquals(new Object[][]{
{"a", Boolean.TRUE, "3"},
{"c", Boolean.FALSE, "4"}
}));
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
}
}
public void testContentEqualsAfterSettingACustomCellValueConverterAsDefault() throws Exception {
table.setDefaultCellValueConverter(new TableCellValueConverter() {
public Object getValue(int row, int column, Component renderedComponent, Object modelObject) {
if ((row == 1) && (column == 1)) {
return "toto";
}
return modelObject.toString();
}
});
assertTrue(table.contentEquals(new Object[][]{
{"a", "true", "3"},
{"c", "toto", "4"}
}));
try {
assertTrue(table.contentEquals(new Object[][]{
{"a", Boolean.TRUE, "3"},
{"c", Boolean.FALSE, "4"}
}));
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
}
}
public void testContentEqualsAfterSettingACustomCellValueConverterOnAColumn() throws Exception {
table.setCellValueConverter(0, new TableCellValueConverter() {
public Object getValue(int row, int column, Component renderedComponent, Object modelObject) {
return "custom " + modelObject;
}
});
assertTrue(table.contentEquals(new Object[][]{
{"custom a", Boolean.TRUE, "3"},
{"custom c", Boolean.FALSE, "4"}
}));
try {
assertTrue(table.contentEquals(new Object[][]{
{"a", Boolean.TRUE, "3"},
{"c", Boolean.FALSE, "4"}
}));
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
}
}
public void testContentEqualsAfterSettingACustomCellValueConverterOnAColumnThatHasBeenMoved() throws Exception {
jTable.moveColumn(0, 1);
table.setCellValueConverter(0, new TableCellValueConverter() {
public Object getValue(int row, int column, Component renderedComponent, Object modelObject) {
return "custom " + modelObject;
}
});
assertTrue(table.contentEquals(new Object[][]{
{"custom true", "a", "3"},
{"custom false", "c", "4"}
}));
try {
assertTrue(table.contentEquals(new Object[][]{
{Boolean.TRUE, "a", "3"},
{Boolean.FALSE, "c", "4"}
}));
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
}
}
public void testContentEqualsAfterSettingACustomCellValueConverterThatHandlesSelection() throws Exception {
jTable.setDefaultRenderer(Boolean.class, new DefaultTableCellRenderer() {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
value = isSelected ? "selected " + value : value;
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
});
assertTrue(table.contentEquals(new Object[][]{
{"a", "true", "3"},
{"c", "false", "4"}
}));
table.selectRow(0);
assertTrue(table.contentEquals(new Object[][]{
{"a", "selected true", "3"},
{"c", "false", "4"}
}));
}
public void testCustomContentEquals() throws Exception {
assertTrue(table.contentEquals(
new String[]{"0", "1", "2"},
new Object[][]{
{"a", Boolean.TRUE, "3"},
{"c", Boolean.FALSE, "4"}
}));
assertTrue(table.contentEquals(
new String[]{"0", "2", "1"},
new Object[][]{
{"a", "3", Boolean.TRUE},
{"c", "4", Boolean.FALSE}
}));
assertTrue(table.contentEquals(
new String[]{"2", "0"},
new Object[][]{
{"3", "a"},
{"4", "c"}
}));
}
public void testCustomContentEqualsErrors() throws Exception {
try {
assertTrue(table.contentEquals(
new String[]{"0", "2"},
new Object[][]{
{"a", "3"},
{"c", "ERROR!"}
}));
throw new AssertionFailureNotDetectedError();
}
catch (AssertionFailedError e) {
assertTrue(e.getMessage().startsWith("Error at (1, 1)"));
}
}
public void testCustomContentEqualsChecksTheNumberOfColumns() throws Exception {
try {
assertTrue(table.contentEquals(
new String[]{"0", "2", "1"},
new Object[][]{
{"a", "3"},
{"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 testCustomContentEqualsChecksTheColumnNames() throws Exception {
try {
assertTrue(table.contentEquals(
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 testCustomContentEqualsChecksTheNumberOfRows() throws Exception {
try {
assertTrue(table.contentEquals(
new String[]{"2", "0"},
new Object[][]{
{"3", "a"},
{"4", "c"},
{"5", "e"}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -