001: package org.uispec4j;
002:
003: import junit.framework.AssertionFailedError;
004: import org.uispec4j.assertion.Assertion;
005: import org.uispec4j.utils.AssertionFailureNotDetectedError;
006:
007: import javax.swing.*;
008:
009: public class TableEditionTest extends TableTestCase {
010: public void testEditCellForString() throws Exception {
011: table.editCell(0, 0, "value", false);
012: assertTrue(table
013: .contentEquals(new Object[][] {
014: { "a", Boolean.TRUE, "3" },
015: { "c", Boolean.FALSE, "4" } }));
016: table.editCell(0, 0, "value", true);
017: assertTrue(table.contentEquals(new Object[][] {
018: { "value", Boolean.TRUE, "3" },
019: { "c", Boolean.FALSE, "4" } }));
020: }
021:
022: public void testEditCellForComboBox() throws Exception {
023: table.editCell(0, 2, "5", false);
024: assertTrue(table
025: .contentEquals(new Object[][] {
026: { "a", Boolean.TRUE, "3" },
027: { "c", Boolean.FALSE, "4" } }));
028: table.editCell(0, 2, "5", true);
029: assertTrue(table
030: .contentEquals(new Object[][] {
031: { "a", Boolean.TRUE, "5" },
032: { "c", Boolean.FALSE, "4" } }));
033: }
034:
035: public void testEditCellErrors() throws Exception {
036: try {
037: table.editCell(1, 0, "notEditable", true);
038: fail();
039: } catch (RuntimeException e) {
040: assertEquals("Cell (1, 0) is not editable", e.getMessage());
041: }
042:
043: try {
044: table.editCell(1, 1, "cellIsNotAJTextFieldNorAComboBox",
045: true);
046: fail();
047: } catch (Exception e) {
048: assertEquals(
049: "Unexpected editor at (1, 1): javax.swing.JCheckBox",
050: e.getMessage());
051: }
052: }
053:
054: public void testAssertEditable() throws Exception {
055: jTable.setModel(new MyModel() {
056: public boolean isCellEditable(int row, int col) {
057: return (row + col) % 2 == 0;
058: }
059: });
060: assertTrue(table.isEditable(new boolean[][] {
061: { true, false, true }, { false, true, false } }));
062:
063: try {
064: assertTrue(table.isEditable(new boolean[][] {
065: { false, false, false }, { false, false, false } }));
066: throw new AssertionFailureNotDetectedError();
067: } catch (AssertionFailedError e) {
068: assertEquals("Error at row 0:\n"
069: + "Expected: [false,false,false]\n"
070: + "Actual: [true,false,true]", e.getMessage());
071: }
072: }
073:
074: public void testAssertCellEditable() throws Exception {
075: jTable.setModel(new MyModel() {
076: public boolean isCellEditable(int row, int col) {
077: return (col == 0) || ((col == 1) && (row == 1));
078: }
079: });
080:
081: checkAssertCellEditable(new boolean[][] {
082: { true, false, false }, { true, true, false } });
083: }
084:
085: private void checkAssertCellEditable(boolean[][] values) {
086: for (int row = 0; row < values.length; row++) {
087: boolean[] rowValues = values[row];
088: for (int column = 0; column < rowValues.length; column++) {
089: boolean value = rowValues[column];
090: Assertion result = table.cellIsEditable(row, column);
091: assertTrue((value) ? result : not(result));
092: }
093: }
094: }
095:
096: public void testAssertColumnEditableWithColumnIndex()
097: throws Exception {
098: jTable.setModel(new MyModel() {
099: public boolean isCellEditable(int row, int col) {
100: return (col == 0) || ((col == 1) && (row == 1));
101: }
102: });
103:
104: assertTrue(table.columnIsEditable(0, true));
105: assertTrue(table.columnIsEditable(2, false));
106:
107: try {
108: assertTrue(table.columnIsEditable(1, true));
109: throw new AssertionFailureNotDetectedError();
110: } catch (AssertionFailedError e) {
111: assertEquals("Cell at row 0 is not editable", e
112: .getMessage());
113: }
114: }
115:
116: public void testAssertColumnEditableWithColumnName()
117: throws Exception {
118: jTable.setModel(new MyModel() {
119: public boolean isCellEditable(int row, int col) {
120: return (col == 0) || ((col == 1) && (row == 1));
121: }
122: });
123:
124: assertTrue(table.columnIsEditable("0", true));
125: assertTrue(table.columnIsEditable("2", false));
126:
127: try {
128: assertTrue(table.columnIsEditable("1", true));
129: throw new AssertionFailureNotDetectedError();
130: } catch (AssertionFailedError e) {
131: assertEquals("Cell at row 0 is not editable", e
132: .getMessage());
133: }
134:
135: try {
136: assertTrue(table.columnIsEditable("unknown", true));
137: throw new AssertionFailureNotDetectedError();
138: } catch (AssertionFailedError e) {
139: assertEquals("Column 'unknown' not found", e.getMessage());
140: }
141: }
142:
143: public void testEditingACellWithAComboBox() throws Exception {
144: String[] choices = new String[] { "a", "b", "c" };
145: jTable.setDefaultEditor(String.class, new DefaultCellEditor(
146: new JComboBox(choices)));
147: assertTrue(table
148: .contentEquals(new Object[][] {
149: { "a", Boolean.TRUE, "3" },
150: { "c", Boolean.FALSE, "4" } }));
151: ComboBox comboBox = table.editCell(0, 0).getComboBox();
152: assertTrue(comboBox.contentEquals(choices));
153: comboBox.select("b");
154: assertTrue(table
155: .contentEquals(new Object[][] {
156: { "b", Boolean.TRUE, "3" },
157: { "c", Boolean.FALSE, "4" } }));
158: }
159:
160: public void testEditingACellWithATextField() throws Exception {
161: jTable.setDefaultEditor(String.class, new DefaultCellEditor(
162: new JTextField()));
163: assertTrue(table
164: .contentEquals(new Object[][] {
165: { "a", Boolean.TRUE, "3" },
166: { "c", Boolean.FALSE, "4" } }));
167: table.editCell(0, 0).getTextBox().setText("new");
168: assertTrue(table.contentEquals(new Object[][] {
169: { "new", Boolean.TRUE, "3" },
170: { "c", Boolean.FALSE, "4" } }));
171: }
172:
173: public void testEditCellChecksThatTheCellIsEditable()
174: throws Exception {
175: jTable.setModel(new MyModel() {
176: public boolean isCellEditable(int row, int column) {
177: return false;
178: }
179: });
180: try {
181: table.editCell(0, 0);
182: throw new AssertionFailureNotDetectedError();
183: } catch (AssertionFailedError e) {
184: assertEquals("Cell (0,0) is not editable", e.getMessage());
185: }
186: }
187: }
|