1   /*
2    * Copyright 2002-2008 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
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          // the row with first name == null must be the last one after sort
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          // the row with first name == null must be the last one after sort
82          assertEquals(null, shuttleSortableTableModel.getValueAt(0, 0));
83          assertEquals("Peter", shuttleSortableTableModel.getValueAt(1, 0));
84          assertEquals("Jan", shuttleSortableTableModel.getValueAt(2, 0));
85      }
86  }