001    package org.springframework.richclient.table;
002    
003    import java.util.ArrayList;
004    import java.util.Arrays;
005    import java.util.List;
006    
007    /**
008     * @author peter.de.bruycker
009     */
010    public class ListTableModelTests extends AbstractBaseTableModelTests {
011        
012        
013        private final ListTableModel dummyListTableModel = new ListTableModel() {
014    
015            protected Class[] createColumnClasses() {
016                return new Class[] { String.class };
017            }
018    
019            protected String[] createColumnNames() {
020                return new String[] { "column" };
021            }
022            
023            
024        };
025        
026        /**
027         * {@inheritDoc}
028         */
029        protected BaseTableModel getBaseTableModel() {
030            return this.dummyListTableModel;
031        }
032    
033        /**
034         * TestCase for bug #RCP-14
035         */
036        public void testConstructorThrowsNullPointerException() {
037            try {
038                ListTableModel model = new ListTableModel() {
039                    protected Class[] createColumnClasses() {
040                        return new Class[] { String.class };
041                    }
042    
043                    protected String[] createColumnNames() {
044                        return new String[] { "column" };
045                    }
046                };
047                model.createColumnInfo();
048                model.getColumnCount();
049            }
050            catch (NullPointerException e) {
051                fail("Should not throw NullPointerException");
052            }
053    
054            try {
055                ListTableModel model = new ListTableModel(new ArrayList()) {
056                    protected Class[] createColumnClasses() {
057                        return new Class[] { String.class };
058                    }
059    
060                    protected String[] createColumnNames() {
061                        return new String[] { "col0" };
062                    }
063                };
064                model.createColumnInfo();
065                model.getColumnCount();
066            }
067            catch (NullPointerException e) {
068                fail("Should not throw NullPointerException");
069            }
070        }
071    
072        public void testGetValueAtInternalWithOneColumnNoArray() {
073            ListTableModel model = new ListTableModel() {
074                protected Class[] createColumnClasses() {
075                    return new Class[] { String.class };
076                }
077    
078                protected String[] createColumnNames() {
079                    return new String[] { "col0" };
080                }
081            };
082            model.setRowNumbers(false);
083    
084            String row = "col0";
085    
086            assertEquals("col0", model.getValueAtInternal(row, 0));
087        }
088    
089        public void testGetValueAtInternalWithArray() {
090            ListTableModel model = new ListTableModel() {
091                protected Class[] createColumnClasses() {
092                    return new Class[] { String.class, String.class };
093                }
094    
095                protected String[] createColumnNames() {
096                    return new String[] { "col0", "col1" };
097                }
098            };
099            model.setRowNumbers(false);
100    
101            String[] row = new String[] { "col0", "col1" };
102    
103            assertEquals("col0", model.getValueAtInternal(row, 0));
104            assertEquals("col1", model.getValueAtInternal(row, 1));
105        }
106    
107        public void testGetValueAtInternalWithInvalidObjectType() {
108            // model with two columns, but no list or array as rows
109            ListTableModel model = new ListTableModel() {
110                protected Class[] createColumnClasses() {
111                    return new Class[] { String.class, String.class };
112                }
113    
114                protected String[] createColumnNames() {
115                    return new String[] { "col0", "col1" };
116                }
117            };
118            model.setRowNumbers(false);
119    
120            String row = "col0";
121    
122            try {
123                model.getValueAtInternal(row, 0);
124                fail("Should throw IllegalArgumentException");
125            }
126            catch (IllegalArgumentException e) {
127                pass();
128            }
129        }
130    
131        private static void pass() {
132            // test passes
133        }
134    
135        public void testGetValueAtInternalWithList() {
136            ListTableModel model = new ListTableModel() {
137                protected Class[] createColumnClasses() {
138                    return new Class[] { String.class, String.class };
139                }
140    
141                protected String[] createColumnNames() {
142                    return new String[] { "col0", "col1" };
143                }
144            };
145            model.createColumnInfo();
146            List row = Arrays.asList(new String[] { "col0", "col1" });
147            assertEquals("col0", model.getValueAtInternal(row, 0));
148            assertEquals("col1", model.getValueAtInternal(row, 1));
149        }
150    
151        public void testGetValueAtInternalWithOneColumnAndArray() {
152            ListTableModel model = new ListTableModel() {
153                protected Class[] createColumnClasses() {
154                    return new Class[] { String.class };
155                }
156    
157                protected String[] createColumnNames() {
158                    return new String[] { "col0" };
159                }
160            };
161            model.setRowNumbers(false);
162    
163            String[] row = new String[] { "col0", "col1" };
164    
165            assertEquals("col0", model.getValueAtInternal(row, 0));
166        }
167    }