1   package org.springframework.richclient.table;
2   
3   import java.util.ArrayList;
4   import java.util.Arrays;
5   import java.util.List;
6   
7   /**
8    * @author peter.de.bruycker
9    */
10  public class ListTableModelTests extends AbstractBaseTableModelTests {
11      
12      
13      private final ListTableModel dummyListTableModel = new ListTableModel() {
14  
15          protected Class[] createColumnClasses() {
16              return new Class[] { String.class };
17          }
18  
19          protected String[] createColumnNames() {
20              return new String[] { "column" };
21          }
22          
23          
24      };
25      
26      /**
27       * {@inheritDoc}
28       */
29      protected BaseTableModel getBaseTableModel() {
30          return this.dummyListTableModel;
31      }
32  
33      /**
34       * TestCase for bug #RCP-14
35       */
36      public void testConstructorThrowsNullPointerException() {
37          try {
38              ListTableModel model = new ListTableModel() {
39                  protected Class[] createColumnClasses() {
40                      return new Class[] { String.class };
41                  }
42  
43                  protected String[] createColumnNames() {
44                      return new String[] { "column" };
45                  }
46              };
47              model.createColumnInfo();
48              model.getColumnCount();
49          }
50          catch (NullPointerException e) {
51              fail("Should not throw NullPointerException");
52          }
53  
54          try {
55              ListTableModel model = new ListTableModel(new ArrayList()) {
56                  protected Class[] createColumnClasses() {
57                      return new Class[] { String.class };
58                  }
59  
60                  protected String[] createColumnNames() {
61                      return new String[] { "col0" };
62                  }
63              };
64              model.createColumnInfo();
65              model.getColumnCount();
66          }
67          catch (NullPointerException e) {
68              fail("Should not throw NullPointerException");
69          }
70      }
71  
72      public void testGetValueAtInternalWithOneColumnNoArray() {
73          ListTableModel model = new ListTableModel() {
74              protected Class[] createColumnClasses() {
75                  return new Class[] { String.class };
76              }
77  
78              protected String[] createColumnNames() {
79                  return new String[] { "col0" };
80              }
81          };
82          model.setRowNumbers(false);
83  
84          String row = "col0";
85  
86          assertEquals("col0", model.getValueAtInternal(row, 0));
87      }
88  
89      public void testGetValueAtInternalWithArray() {
90          ListTableModel model = new ListTableModel() {
91              protected Class[] createColumnClasses() {
92                  return new Class[] { String.class, String.class };
93              }
94  
95              protected String[] createColumnNames() {
96                  return new String[] { "col0", "col1" };
97              }
98          };
99          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 }