001    /*
002     * Copyright 2002-2008 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.table;
017    
018    import java.util.Comparator;
019    
020    import javax.swing.table.DefaultTableModel;
021    import javax.swing.table.TableModel;
022    
023    import junit.framework.TestCase;
024    
025    public class ShuttleSortableTableModelTests extends TestCase {
026    
027        public void testNullComparisonWithComparator() {
028            Object[] columnNames = new Object[] { "first name", "last name" };
029            Object[][] data = new Object[][] { { "Peter", "De Bruycker" },
030                    { "Jan", "Hoskens" }, { null, "test" } };
031    
032            DefaultTableModel tableModel = new DefaultTableModel(data, columnNames);
033    
034            ShuttleSortableTableModel shuttleSortableTableModel = new ShuttleSortableTableModel(tableModel);
035            shuttleSortableTableModel.setComparator(0, new Comparator() {
036                public int compare(Object o1, Object o2) {
037                    String s1 = (String) o1;
038                    String s2 = (String) o2;
039    
040                    if (s1 == null && s2 == null) {
041                        return 0;
042                    }
043    
044                    if (s1 == null) {
045                        return 1;
046                    }
047                    if (s2 == null) {
048                        return -1;
049                    }
050    
051                    return s1.compareTo(s2);
052                }
053            });
054    
055            shuttleSortableTableModel.sortByColumn(new ColumnToSort(1, 0));
056    
057            // the row with first name == null must be the last one after sort
058            assertEquals("Jan", shuttleSortableTableModel.getValueAt(0, 0));
059            assertEquals("Peter", shuttleSortableTableModel.getValueAt(1, 0));
060            assertEquals(null, shuttleSortableTableModel.getValueAt(2, 0));
061        }
062    
063        public void testNullComparisonWithoutComparator() {
064            Object[] columnNames = new Object[] { "first name", "last name", "test bean" };
065            Object[][] data = new Object[][] { { "Peter", "De Bruycker", new TestBean("1") },
066                    { "Jan", "Hoskens", new TestBean("2") }, { null, "test", null } };
067    
068            TableModel tableModel = new DefaultTableModel(data, columnNames) {
069                public Class getColumnClass(int columnIndex) {
070                    if (columnIndex == 2) {
071                        return TestBean.class;
072                    }
073                    return super.getColumnClass(columnIndex);
074                }
075            };
076    
077            ShuttleSortableTableModel shuttleSortableTableModel = new ShuttleSortableTableModel(tableModel);
078    
079            shuttleSortableTableModel.sortByColumn(new ColumnToSort(1, 2));
080    
081            // the row with first name == null must be the last one after sort
082            assertEquals(null, shuttleSortableTableModel.getValueAt(0, 0));
083            assertEquals("Peter", shuttleSortableTableModel.getValueAt(1, 0));
084            assertEquals("Jan", shuttleSortableTableModel.getValueAt(2, 0));
085        }
086    }