001: package org.uispec4j;
002:
003: import junit.framework.AssertionFailedError;
004: import org.uispec4j.utils.ArrayUtils;
005: import org.uispec4j.utils.AssertionFailureNotDetectedError;
006: import org.uispec4j.utils.Functor;
007:
008: import javax.swing.*;
009: import javax.swing.table.DefaultTableCellRenderer;
010: import javax.swing.table.JTableHeader;
011: import java.awt.*;
012:
013: public class TableHeaderTest extends TableTestCase {
014: public void test() throws Exception {
015: JTableHeader tableHeader = jTable.getTableHeader();
016: tableHeader.setVisible(false);
017: jTable.setTableHeader(null);
018: assertEquals(1, table.getHeader().findColumnIndex("1"));
019: }
020:
021: public void testContent() throws Exception {
022: assertTrue(table.getHeader().contentEquals(
023: new String[] { "0", "1", "2" }));
024: try {
025: assertTrue(table.getHeader().contentEquals(new String[] {}));
026: throw new AssertionFailureNotDetectedError();
027: } catch (AssertionFailedError e) {
028: assertEquals("expected:<......> but was:<...0,1,2...>", e
029: .getMessage());
030: }
031: }
032:
033: public void testClickOnHeader() throws Exception {
034: MouseLogger mouseLogger = new MouseLogger(jTable
035: .getTableHeader());
036: table.getHeader().click(0);
037: mouseLogger.assertEquals("<log>"
038: + " <mousePressed button='1'/>"
039: + " <mouseReleased button='1'/>"
040: + " <mouseClicked button='1'/>" + "</log>");
041: }
042:
043: public void testRightClickOnHeader() throws Exception {
044: MouseLogger mouseLogger = new MouseLogger(jTable
045: .getTableHeader());
046: table.getHeader().rightClick(0);
047: mouseLogger.assertEquals("<log>"
048: + " <mousePressed button='3'/>"
049: + " <mouseReleased button='3'/>"
050: + " <mouseClicked button='3'/>" + "</log>");
051: }
052:
053: public void testHeader() throws Exception {
054: assertTrue(table.hasHeader());
055:
056: jTable.setTableHeader(null);
057: assertFalse(table.hasHeader());
058: try {
059: assertTrue(table.hasHeader());
060: throw new AssertionFailureNotDetectedError();
061: } catch (AssertionFailedError e) {
062: assertEquals("The table contains an header", e.getMessage());
063: }
064: }
065:
066: public void testNoHeaderExceptions() throws Exception {
067: jTable.setTableHeader(null);
068: checkNoHeaderException(new Functor() {
069: public void run() {
070: assertTrue(table.getHeader().backgroundEquals(null));
071: }
072: });
073: checkNoHeaderException(new Functor() {
074: public void run() {
075: assertTrue(table.getHeader().contentEquals(
076: new String[] {}));
077: }
078: });
079: checkNoHeaderException(new Functor() {
080: public void run() {
081: table.getHeader().click(0);
082: }
083: });
084: checkNoHeaderException(new Functor() {
085: public void run() {
086: table.getHeader().click("");
087: }
088: });
089: checkNoHeaderException(new Functor() {
090: public void run() throws Exception {
091: table.getHeader().triggerClick(0).run();
092: }
093: });
094: checkNoHeaderException(new Functor() {
095: public void run() throws Exception {
096: table.getHeader().triggerClick("").run();
097: }
098: });
099: checkNoHeaderException(new Functor() {
100: public void run() {
101: table.getHeader().getDefaultBackground();
102: }
103: });
104: checkNoHeaderException(new Functor() {
105: public void run() {
106: table.getHeader().rightClick(0);
107: }
108: });
109: checkNoHeaderException(new Functor() {
110: public void run() {
111: table.getHeader().rightClick("");
112: }
113: });
114: checkNoHeaderException(new Functor() {
115: public void run() throws Exception {
116: table.getHeader().triggerRightClick(0).run();
117: }
118: });
119: checkNoHeaderException(new Functor() {
120: public void run() throws Exception {
121: table.getHeader().triggerRightClick("").run();
122: }
123: });
124: }
125:
126: public void testAssertHeaderBackgroundEquals() throws Exception {
127: jTable.getTableHeader().setDefaultRenderer(
128: new DefaultTableCellRenderer() {
129: public Component getTableCellRendererComponent(
130: JTable table, Object value,
131: boolean isSelected, boolean hasFocus,
132: int row, int column) {
133: Component component = super
134: .getTableCellRendererComponent(table,
135: value, isSelected, hasFocus,
136: row, column);
137: if (column == 1) {
138: component.setBackground(Color.red);
139: } else {
140: component.setBackground(Color.blue);
141: }
142: return component;
143: }
144: });
145:
146: assertTrue(table.getHeader().backgroundEquals(
147: new Object[] { "blue", "red", "blue" }));
148:
149: try {
150: assertTrue(table.getHeader().backgroundEquals(
151: new Object[] { "blue", "black", "blue" }));
152: throw new AssertionFailureNotDetectedError();
153: } catch (AssertionFailedError e) {
154: assertEquals(
155: "Unexpected color at column 1 expected:<BLACK> but was:<FF0000>",
156: e.getMessage());
157: }
158: }
159:
160: public void testGetColumnNames() throws Exception {
161: ArrayUtils.assertEquals(new String[0], new Table(new JTable())
162: .getHeader().getColumnNames());
163: ArrayUtils.assertEquals(new String[] { "0", "1", "2" }, table
164: .getHeader().getColumnNames());
165: }
166:
167: public void testFindColumnIndex() throws Exception {
168: assertEquals(0, table.getHeader().findColumnIndex("0"));
169: assertEquals(1, table.getHeader().findColumnIndex("1"));
170: assertEquals(2, table.getHeader().findColumnIndex("2"));
171:
172: try {
173: table.getHeader().findColumnIndex("unknown");
174: throw new AssertionFailureNotDetectedError();
175: } catch (AssertionFailedError e) {
176: assertEquals("Column 'unknown' not found", e.getMessage());
177: }
178: }
179:
180: private void checkNoHeaderException(Functor functor)
181: throws Exception {
182: try {
183: functor.run();
184: throw new AssertionFailureNotDetectedError();
185: } catch (AssertionFailedError e) {
186: assertEquals("The table contains no header", e.getMessage());
187: }
188: }
189: }
|