1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.richclient.table;
17
18 import java.util.Comparator;
19
20 import javax.swing.table.DefaultTableModel;
21 import javax.swing.table.TableModel;
22
23 import junit.framework.TestCase;
24
25 public class ShuttleSortableTableModelTests extends TestCase {
26
27 public void testNullComparisonWithComparator() {
28 Object[] columnNames = new Object[] { "first name", "last name" };
29 Object[][] data = new Object[][] { { "Peter", "De Bruycker" },
30 { "Jan", "Hoskens" }, { null, "test" } };
31
32 DefaultTableModel tableModel = new DefaultTableModel(data, columnNames);
33
34 ShuttleSortableTableModel shuttleSortableTableModel = new ShuttleSortableTableModel(tableModel);
35 shuttleSortableTableModel.setComparator(0, new Comparator() {
36 public int compare(Object o1, Object o2) {
37 String s1 = (String) o1;
38 String s2 = (String) o2;
39
40 if (s1 == null && s2 == null) {
41 return 0;
42 }
43
44 if (s1 == null) {
45 return 1;
46 }
47 if (s2 == null) {
48 return -1;
49 }
50
51 return s1.compareTo(s2);
52 }
53 });
54
55 shuttleSortableTableModel.sortByColumn(new ColumnToSort(1, 0));
56
57
58 assertEquals("Jan", shuttleSortableTableModel.getValueAt(0, 0));
59 assertEquals("Peter", shuttleSortableTableModel.getValueAt(1, 0));
60 assertEquals(null, shuttleSortableTableModel.getValueAt(2, 0));
61 }
62
63 public void testNullComparisonWithoutComparator() {
64 Object[] columnNames = new Object[] { "first name", "last name", "test bean" };
65 Object[][] data = new Object[][] { { "Peter", "De Bruycker", new TestBean("1") },
66 { "Jan", "Hoskens", new TestBean("2") }, { null, "test", null } };
67
68 TableModel tableModel = new DefaultTableModel(data, columnNames) {
69 public Class getColumnClass(int columnIndex) {
70 if (columnIndex == 2) {
71 return TestBean.class;
72 }
73 return super.getColumnClass(columnIndex);
74 }
75 };
76
77 ShuttleSortableTableModel shuttleSortableTableModel = new ShuttleSortableTableModel(tableModel);
78
79 shuttleSortableTableModel.sortByColumn(new ColumnToSort(1, 2));
80
81
82 assertEquals(null, shuttleSortableTableModel.getValueAt(0, 0));
83 assertEquals("Peter", shuttleSortableTableModel.getValueAt(1, 0));
84 assertEquals("Jan", shuttleSortableTableModel.getValueAt(2, 0));
85 }
86 }