001: package org.uispec4j;
002:
003: import junit.framework.AssertionFailedError;
004: import org.uispec4j.utils.AssertionFailureNotDetectedError;
005:
006: import javax.swing.*;
007: import javax.swing.table.DefaultTableCellRenderer;
008: import java.awt.*;
009:
010: public class TableContentTest extends TableTestCase {
011: public void testAssertContentEquals() throws Exception {
012: assertTrue(table
013: .contentEquals(new Object[][] {
014: { "a", Boolean.TRUE, "3" },
015: { "c", Boolean.FALSE, "4" } }));
016: }
017:
018: public void testAssertContentEqualsFailure() throws Exception {
019: try {
020: assertTrue(table.contentEquals(new Object[][] {
021: { "a", Boolean.TRUE, "3" },
022: { "c", Boolean.FALSE, "ERROR!" } }));
023: throw new AssertionFailureNotDetectedError();
024: } catch (AssertionFailedError e) {
025: }
026: }
027:
028: public void testAssertEmptyFailure() throws Exception {
029: try {
030: assertTrue(table.isEmpty());
031: throw new AssertionFailureNotDetectedError();
032: } catch (AssertionFailedError e) {
033: assertEquals(
034: "Expected: empty table but was:[[a,\ttrue,\t3]\n [c,\tfalse,\t4]]",
035: e.getMessage());
036: }
037: }
038:
039: public void testContentEqualsUsesTheModelWhenAnUnknownRendererComponentIsUsed()
040: throws Exception {
041: installJPanelRenderer();
042: assertTrue(table.contentEquals(new Object[][] {
043: { "a", "true", "3" }, { "c", "false", "4" } }));
044: try {
045: assertTrue(table.contentEquals(new Object[][] {
046: { "a", Boolean.TRUE, "3" },
047: { "c", Boolean.FALSE, "4" } }));
048: throw new AssertionFailureNotDetectedError();
049: } catch (AssertionFailedError e) {
050: }
051: }
052:
053: public void testContentEqualsWithUnknownRendererComponentAndNullModelValue()
054: throws Exception {
055: installJPanelRenderer();
056: jTable.getModel().setValueAt(null, 0, 1);
057: assertTrue(table.contentEquals(new Object[][] {
058: { "a", "", "3" }, { "c", "false", "4" } }));
059: try {
060: assertTrue(table.contentEquals(new Object[][] {
061: { "a", "", "3" }, { "c", Boolean.FALSE, "4" } }));
062: throw new AssertionFailureNotDetectedError();
063: } catch (AssertionFailedError e) {
064: }
065: }
066:
067: public void testContentEqualsWhenColumnMoved() throws Exception {
068: jTable.moveColumn(0, 1);
069: assertTrue(table
070: .contentEquals(new Object[][] {
071: { Boolean.TRUE, "a", "3" },
072: { Boolean.FALSE, "c", "4" } }));
073: try {
074: assertTrue(table.contentEquals(new Object[][] {
075: { "a", Boolean.TRUE, "3" },
076: { "c", Boolean.FALSE, "4" } }));
077: throw new AssertionFailureNotDetectedError();
078: } catch (AssertionFailedError e) {
079: }
080: }
081:
082: public void testContentEqualsAfterSettingACustomCellValueConverterAsDefault()
083: throws Exception {
084: table
085: .setDefaultCellValueConverter(new TableCellValueConverter() {
086: public Object getValue(int row, int column,
087: Component renderedComponent,
088: Object modelObject) {
089: if ((row == 1) && (column == 1)) {
090: return "toto";
091: }
092: return modelObject.toString();
093: }
094: });
095: assertTrue(table.contentEquals(new Object[][] {
096: { "a", "true", "3" }, { "c", "toto", "4" } }));
097: try {
098: assertTrue(table.contentEquals(new Object[][] {
099: { "a", Boolean.TRUE, "3" },
100: { "c", Boolean.FALSE, "4" } }));
101: throw new AssertionFailureNotDetectedError();
102: } catch (AssertionFailedError e) {
103: }
104: }
105:
106: public void testContentEqualsAfterSettingACustomCellValueConverterOnAColumn()
107: throws Exception {
108: table.setCellValueConverter(0, new TableCellValueConverter() {
109: public Object getValue(int row, int column,
110: Component renderedComponent, Object modelObject) {
111: return "custom " + modelObject;
112: }
113: });
114: assertTrue(table.contentEquals(new Object[][] {
115: { "custom a", Boolean.TRUE, "3" },
116: { "custom c", Boolean.FALSE, "4" } }));
117: try {
118: assertTrue(table.contentEquals(new Object[][] {
119: { "a", Boolean.TRUE, "3" },
120: { "c", Boolean.FALSE, "4" } }));
121: throw new AssertionFailureNotDetectedError();
122: } catch (AssertionFailedError e) {
123: }
124: }
125:
126: public void testContentEqualsAfterSettingACustomCellValueConverterOnAColumnThatHasBeenMoved()
127: throws Exception {
128: jTable.moveColumn(0, 1);
129: table.setCellValueConverter(0, new TableCellValueConverter() {
130: public Object getValue(int row, int column,
131: Component renderedComponent, Object modelObject) {
132: return "custom " + modelObject;
133: }
134: });
135: assertTrue(table.contentEquals(new Object[][] {
136: { "custom true", "a", "3" },
137: { "custom false", "c", "4" } }));
138: try {
139: assertTrue(table.contentEquals(new Object[][] {
140: { Boolean.TRUE, "a", "3" },
141: { Boolean.FALSE, "c", "4" } }));
142: throw new AssertionFailureNotDetectedError();
143: } catch (AssertionFailedError e) {
144: }
145: }
146:
147: public void testContentEqualsAfterSettingACustomCellValueConverterThatHandlesSelection()
148: throws Exception {
149: jTable.setDefaultRenderer(Boolean.class,
150: new DefaultTableCellRenderer() {
151: public Component getTableCellRendererComponent(
152: JTable table, Object value,
153: boolean isSelected, boolean hasFocus,
154: int row, int column) {
155: value = isSelected ? "selected " + value
156: : value;
157: return super .getTableCellRendererComponent(
158: table, value, isSelected, hasFocus,
159: row, column);
160: }
161: });
162: assertTrue(table.contentEquals(new Object[][] {
163: { "a", "true", "3" }, { "c", "false", "4" } }));
164:
165: table.selectRow(0);
166: assertTrue(table.contentEquals(new Object[][] {
167: { "a", "selected true", "3" }, { "c", "false", "4" } }));
168: }
169:
170: public void testCustomContentEquals() throws Exception {
171: assertTrue(table.contentEquals(new String[] { "0", "1", "2" },
172: new Object[][] { { "a", Boolean.TRUE, "3" },
173: { "c", Boolean.FALSE, "4" } }));
174:
175: assertTrue(table.contentEquals(new String[] { "0", "2", "1" },
176: new Object[][] { { "a", "3", Boolean.TRUE },
177: { "c", "4", Boolean.FALSE } }));
178:
179: assertTrue(table.contentEquals(new String[] { "2", "0" },
180: new Object[][] { { "3", "a" }, { "4", "c" } }));
181: }
182:
183: public void testCustomContentEqualsErrors() throws Exception {
184: try {
185: assertTrue(table.contentEquals(new String[] { "0", "2" },
186: new Object[][] { { "a", "3" }, { "c", "ERROR!" } }));
187: throw new AssertionFailureNotDetectedError();
188: } catch (AssertionFailedError e) {
189: assertTrue(e.getMessage().startsWith("Error at (1, 1)"));
190: }
191: }
192:
193: public void testCustomContentEqualsChecksTheNumberOfColumns()
194: throws Exception {
195: try {
196: assertTrue(table.contentEquals(
197: new String[] { "0", "2", "1" }, new Object[][] {
198: { "a", "3" }, { "a", "3" }, }));
199: throw new AssertionFailureNotDetectedError();
200: } catch (AssertionFailedError e) {
201: assertEquals(
202: "Expected array should have 3 elements for each row - invalid row 0: [a,3]",
203: e.getMessage());
204: }
205: }
206:
207: public void testCustomContentEqualsChecksTheColumnNames()
208: throws Exception {
209: try {
210: assertTrue(table.contentEquals(new String[] { "0", "2",
211: "unknown" }, new Object[][] { { "a", "3", "x" } }));
212: throw new AssertionFailureNotDetectedError();
213: } catch (AssertionFailedError e) {
214: assertEquals("Column 'unknown' not found", e.getMessage());
215: }
216: }
217:
218: public void testCustomContentEqualsChecksTheNumberOfRows()
219: throws Exception {
220: try {
221: assertTrue(table.contentEquals(new String[] { "2", "0" },
222: new Object[][] { { "3", "a" }, { "4", "c" },
223: { "5", "e" } }));
224: throw new AssertionFailureNotDetectedError();
225: } catch (AssertionFailedError e) {
226: assertTrue(e.getMessage().startsWith(
227: "Expected 3 rows but found 2"));
228: }
229: }
230:
231: public void testRowEquals() throws Exception {
232: assertTrue(table.rowEquals(0, new Object[] { "a", Boolean.TRUE,
233: "3" }));
234: assertTrue(table.rowEquals(1, new Object[] { "c",
235: Boolean.FALSE, "4" }));
236: }
237:
238: public void testRowEqualsFailures() throws Exception {
239: try {
240: assertTrue(table.rowEquals(1, new Object[] { "c",
241: Boolean.TRUE, "4" }));
242: throw new AssertionFailureNotDetectedError();
243: } catch (AssertionFailedError e) {
244: assertEquals("expected:<[c,true,4]> but was:<c,false,4>", e
245: .getMessage());
246: }
247:
248: try {
249: assertTrue(table.rowEquals(-1,
250: new Object[] { "a", "b", "c" }));
251: throw new AssertionFailureNotDetectedError();
252: } catch (AssertionFailedError e) {
253: assertEquals("Row index should be positive", e.getMessage());
254: }
255:
256: try {
257: assertTrue(table.rowEquals(2,
258: new Object[] { "a", "b", "c" }));
259: throw new AssertionFailureNotDetectedError();
260: } catch (AssertionFailedError e) {
261: assertEquals(
262: "Table contains only 2 rows, unable to access row 2",
263: e.getMessage());
264: }
265: }
266:
267: public void testCustomRowEquals() throws Exception {
268: assertTrue(table.rowEquals(0, new String[] { "0", "1", "2" },
269: new Object[] { "a", Boolean.TRUE, "3" }));
270: assertTrue(table.rowEquals(1, new String[] { "0", "1", "2" },
271: new Object[] { "c", Boolean.FALSE, "4" }));
272:
273: assertTrue(table.rowEquals(0, new String[] { "0", "2", "1" },
274: new Object[] { "a", "3", Boolean.TRUE }));
275:
276: assertTrue(table.rowEquals(0, new String[] { "2", "0" },
277: new Object[] { "3", "a" }));
278: }
279:
280: public void testCustomRowEqualsErrors() throws Exception {
281: try {
282: assertTrue(table.rowEquals(0, new String[] { "0", "2" },
283: new Object[] { "a", "xxxxxx" }));
284: throw new AssertionFailureNotDetectedError();
285: } catch (AssertionFailedError e) {
286: assertEquals("Unexpected content at row 0\n"
287: + "Expected: [a,xxxxxx]\n" + "Actual: [a,3]", e
288: .getMessage());
289: }
290: }
291:
292: public void testCustomRowEqualsChecksTheNumberOfColumns()
293: throws Exception {
294: try {
295: assertTrue(table.rowEquals(0,
296: new String[] { "0", "2", "1" }, new Object[] { "a",
297: "3" }));
298: throw new AssertionFailureNotDetectedError();
299: } catch (AssertionFailedError e) {
300: assertEquals(
301: "Expected array should have 3 elements for each row - invalid row 0: [a,3]",
302: e.getMessage());
303: }
304: }
305:
306: public void testCustomRowEqualsChecksTheColumnNames()
307: throws Exception {
308: try {
309: assertTrue(table.rowEquals(0, new String[] { "0", "2",
310: "unknown" }, new Object[] { "a", "3", "x" }));
311: throw new AssertionFailureNotDetectedError();
312: } catch (AssertionFailedError e) {
313: assertEquals("Column 'unknown' not found", e.getMessage());
314: }
315: }
316:
317: public void testCustomRowEqualsChecksTheNumberOfRows()
318: throws Exception {
319: try {
320: assertTrue(table.rowEquals(3, new String[] { "2", "0" },
321: new Object[] { "3", "a" }));
322: throw new AssertionFailureNotDetectedError();
323: } catch (AssertionFailedError e) {
324: assertEquals(
325: "Table contains only 2 rows, unable to access row 3",
326: e.getMessage());
327: }
328: }
329:
330: public void testColumnEquals() throws Exception {
331: assertTrue(table.columnEquals(0, new Object[] { "a", "c" }));
332: assertTrue(table.columnEquals(1, new Object[] { Boolean.TRUE,
333: Boolean.FALSE }));
334: assertTrue(table.columnEquals(2, new Object[] { "3", "4" }));
335: }
336:
337: public void testColumnEqualsFailures() throws Exception {
338: try {
339: assertTrue(table.columnEquals(1, new Object[] {
340: Boolean.TRUE, Boolean.TRUE }));
341: throw new AssertionFailureNotDetectedError();
342: } catch (AssertionFailedError e) {
343: assertEquals("expected:<[true,true]> but was:<true,false>",
344: e.getMessage());
345: }
346:
347: try {
348: assertTrue(table
349: .columnEquals(-1, new Object[] { "a", "c" }));
350: throw new AssertionFailureNotDetectedError();
351: } catch (AssertionFailedError e) {
352: assertEquals("Column index should be positive", e
353: .getMessage());
354: }
355:
356: try {
357: assertTrue(table.columnEquals(3, new Object[] { "3", "4" }));
358: throw new AssertionFailureNotDetectedError();
359: } catch (AssertionFailedError e) {
360: assertEquals(
361: "Table contains only 3 columns, unable to access column 3",
362: e.getMessage());
363: }
364: }
365:
366: public void testGetRowAndColumnCount() throws Exception {
367: assertEquals(2, table.getRowCount());
368: assertEquals(3, table.getColumnCount());
369: }
370:
371: public void testGetContentAt() throws Exception {
372: assertEquals("a", table.getContentAt(0, 0));
373: assertEquals(Boolean.TRUE, table.getContentAt(0, 1));
374: assertEquals("c", table.getContentAt(1, 0));
375: assertEquals(Boolean.FALSE, table.getContentAt(1, 1));
376:
377: jTable.getModel().setValueAt(null, 0, 0);
378: assertEquals("", table.getContentAt(0, 0));
379: }
380:
381: public void testGetContentAtWorksWhenTheRendererIsUnusable()
382: throws Exception {
383: jTable.setDefaultRenderer(String.class,
384: new DefaultTableCellRenderer() {
385: public Component getTableCellRendererComponent(
386: JTable table, Object value,
387: boolean isSelected, boolean hasFocus,
388: int row, int column) {
389: return new JPanel();
390: }
391: });
392: assertEquals("a", table.getContentAt(0, 0));
393: }
394:
395: public void testGetContentAtWithConverter() throws Exception {
396: assertEquals("-a-", table.getContentAt(0, 0,
397: new TableCellValueConverter() {
398: public Object getValue(int row, int column,
399: Component renderedComponent,
400: Object modelObject) {
401: return "-" + modelObject + "-";
402: }
403: }));
404:
405: assertEquals(new Integer(3), table.getContentAt(0, 2,
406: ModelTableCellValueConverter.INSTANCE));
407: }
408:
409: public void testGetEditorComponentAt() throws Exception {
410: Component firstCellComponent = table.getSwingEditorComponentAt(
411: 0, 0);
412: assertTrue(firstCellComponent instanceof JTextField);
413:
414: Component secondCellComponent = table
415: .getSwingEditorComponentAt(0, 1);
416: assertTrue(secondCellComponent instanceof JCheckBox);
417:
418: Component thirdCellComponent = table.getSwingEditorComponentAt(
419: 0, 2);
420: assertTrue(thirdCellComponent instanceof JComboBox);
421: }
422:
423: public void testToString() throws Exception {
424: assertEquals("[[a,\ttrue,\t3]\n" + " [c,\tfalse,\t4]]", table
425: .toString());
426: }
427:
428: public void testForegroundEquals() throws Exception {
429: assertTrue(table.foregroundEquals(new String[][] {
430: { "black", "black", "black" },
431: { "black", "black", "black" } }));
432: table.foregroundEquals("black");
433: }
434:
435: public void testBackgrounEqualsWithDefaultValue() throws Exception {
436: jTable.setBackground(Color.BLUE);
437: assertTrue(table.backgroundEquals("blue"));
438: try {
439: assertTrue(table.backgroundEquals("green"));
440: throw new AssertionFailureNotDetectedError();
441: } catch (AssertionFailedError e) {
442: assertEquals("expected:<GREEN> but was:<0000FF>", e
443: .getMessage());
444: }
445: }
446:
447: public void testBackgroundEquals() throws Exception {
448: jTable.getColumnModel().getColumn(1).setCellRenderer(
449: new DefaultTableCellRenderer() {
450: public Component getTableCellRendererComponent(
451: JTable table, Object value,
452: boolean isSelected, boolean hasFocus,
453: int row, int column) {
454: Component component = super
455: .getTableCellRendererComponent(table,
456: value, isSelected, hasFocus,
457: row, column);
458: if (row == 1) {
459: component.setBackground(Color.red);
460: } else {
461: component.setBackground(Color.blue);
462: }
463: return component;
464: }
465: });
466: assertTrue(table.backgroundEquals(new Object[][] {
467: { "white", "blue", "white" },
468: { Color.white, Color.red, Color.white } }));
469:
470: try {
471: assertTrue(table.backgroundEquals(new Object[][] {
472: { "white", "green", "white" },
473: { Color.white, Color.red, Color.white } }));
474: throw new AssertionFailureNotDetectedError();
475: } catch (AssertionFailedError e) {
476: assertEquals(
477: "Error at (0, 1) - expected:<GREEN> but was:<0000FF>",
478: e.getMessage());
479: }
480: }
481:
482: public void testBackgroundEqualsUsesTheSelectionBackgroundColor()
483: throws Exception {
484: jTable.setCellSelectionEnabled(true);
485: DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() {
486: public Component getTableCellRendererComponent(
487: JTable table, Object value, boolean isSelected,
488: boolean hasFocus, int row, int column) {
489: Component component = super
490: .getTableCellRendererComponent(table, value,
491: isSelected, hasFocus, row, column);
492: if (isSelected) {
493: component.setBackground(Color.red);
494: } else {
495: component.setBackground(Color.black);
496: }
497: return component;
498: }
499: };
500: jTable.setDefaultRenderer(Object.class, renderer);
501: jTable.setDefaultRenderer(Boolean.class, renderer);
502: jTable.setDefaultRenderer(Integer.class, renderer);
503: table.selectCell(0, 1);
504: table.backgroundEquals(new Object[][] {
505: { Color.BLACK, "red", Color.BLACK },
506: { Color.BLACK, Color.BLACK, Color.BLACK } });
507: }
508: }
|