1 package test.prefuse.data; 2 3 import java.awt.geom.GeneralPath ; 4 import java.util.Comparator ; 5 import java.util.Iterator ; 6 7 import junit.framework.TestCase; 8 import prefuse.data.Table; 9 import prefuse.data.Tuple; 10 import prefuse.data.column.Column; 11 import prefuse.data.util.Sort; 12 import prefuse.util.collections.DefaultLiteralComparator; 13 import test.prefuse.TestConfig; 14 15 public class TableTest extends TestCase implements TableTestData { 16 17 int rows[] = { -1, NROWS+1, 2*NROWS }; 18 int cols[] = { -1, NCOLS+1, 2*NCOLS }; 19 20 Table t; 21 22 public static Table getTestCaseTable() { 23 Table t = new Table(NROWS, NCOLS); 24 for ( int c=0; c<NCOLS; ++c ) { 25 t.addColumn(HEADERS[c], TYPES[c]); 26 for ( int r=0; r<NROWS; ++r ) { 27 t.set(r, HEADERS[c], TABLE[c][r]); 28 } 29 } 30 return t; 31 } 32 33 36 protected void setUp() throws Exception { 37 super.setUp(); 38 t = getTestCaseTable(); 39 } 40 41 44 protected void tearDown() throws Exception { 45 super.tearDown(); 46 t = null; 47 } 48 49 52 public void testGetColumnCount() { 53 assertEquals(NCOLS, t.getColumnCount()); 54 } 55 56 59 public void testGetColumnClassInt() { 60 for ( int c=0; c<NCOLS; ++c ) { 61 assertEquals(TYPES[c], t.getColumnType(c)); 62 } 63 } 64 65 68 public void testGetColumnClassString() { 69 for ( int c=0; c<NCOLS; ++c ) { 70 assertEquals(TYPES[c], t.getColumnType(HEADERS[c])); 71 } 72 } 73 74 77 public void testGetRowCount() { 78 assertEquals(NROWS, t.getRowCount()); 79 } 80 81 84 public void testAddRow() { 85 int r = t.addRow(); 86 assertEquals(NROWS+1, t.getRowCount()); 87 assertEquals(NROWS, r); 88 t.set(r,HEADERS[0],TABLE[0][0]); 89 assertEquals(TABLE[0][0], t.get(r,HEADERS[0])); 90 } 91 92 95 public void testAddRows() { 96 int nrows = 3; 97 t.addRows(nrows); 98 assertEquals(NROWS+nrows, t.getRowCount()); 99 } 100 101 104 public void testRemoveRow() { 105 for ( int i=0; i<NROWS; ++i ) { 106 t.removeRow(i); 107 assertEquals(NROWS-1, t.getRowCount()); 108 int r = t.addRow(); 109 assertEquals(i, r); 110 assertEquals(NROWS, t.getRowCount()); 111 } 112 113 try { 114 tearDown(); setUp(); 115 } catch ( Exception e ) {} 116 117 for ( int i=0; i<NROWS; ++i ) { 118 t.removeRow(i); 119 assertEquals(NROWS-i-1, t.getRowCount()); 120 try { 121 t.get(i, HEADERS[0]); 122 fail("Allowed access to invalid row"); 123 } catch ( Exception e ) {} 124 try { 125 t.get(i, HEADERS[2]); 126 fail("Allowed access to invalid row"); 127 } catch ( Exception e ) {} 128 } 129 } 130 131 134 public void testGetColumnName() { 135 for ( int c=0; c<NCOLS; ++c ) { 136 assertEquals(HEADERS[c], t.getColumnName(c)); 137 } 138 } 139 140 143 public void testGetColumnIndex() { 144 for ( int c=0; c<NCOLS; ++c ) { 145 assertEquals(c, t.getColumnNumber(HEADERS[c])); 146 } 147 } 148 149 152 public void testGetColumn() { 153 for ( int c=0; c<NCOLS; ++c ) { 154 Column col1 = t.getColumn(c); 155 Column col2 = t.getColumn(HEADERS[c]); 156 assertEquals(col1, col2); 157 } 158 } 159 160 163 public void testAddColumn() { 164 String [] names = { "polygon", "boolean" }; 165 Class [] types = { GeneralPath .class, boolean.class }; 166 167 for ( int i=0; i < names.length; ++i ) { 168 t.addColumn(names[i], types[i]); 169 Column col = t.getColumn(names[i]); 170 assertTrue(col.getRowCount() >= t.getRowCount()); 171 assertTrue(col.canSet(types[i])); 172 assertFalse(col.canSet(Math .class)); 173 174 assertEquals(NCOLS+i+1, t.getColumnCount()); 175 assertEquals(types[i], t.getColumnType(names[i])); 176 } 177 } 178 179 182 public void testRemoveColumnString() { 183 t.removeColumn(HEADERS[0]); 184 assertEquals(NCOLS-1, t.getColumnCount()); 185 assertEquals(-1, t.getColumnNumber(HEADERS[0])); 186 try { 187 t.get(0,HEADERS[0]); 188 fail("Allowed access to removed column"); 189 } catch ( Exception success ) {} 190 try { 191 t.set(0,HEADERS[0],TABLE[0][0]); 192 fail("Allowed access to removed column"); 193 } catch ( Exception success ) {} 194 } 195 196 199 public void testCanGet() { 200 for ( int c=0; c<NCOLS; ++c ) { 201 assertTrue(t.canGet(HEADERS[c], TYPES[c])); 202 assertFalse(t.canGet(HEADERS[c], Math .class)); 203 } 204 } 205 206 209 public void testCanSet() { 210 for ( int c=0; c<NCOLS; ++c ) { 211 assertTrue(t.canSet(HEADERS[c], TYPES[c])); 212 assertFalse(t.canSet(HEADERS[c], Math .class)); 213 } 214 } 215 216 219 public void testGet() { 220 for ( int r=0; r<NROWS; ++r ) { 221 for ( int c=0; c<NCOLS; ++c ) { 222 assertEquals(TABLE[c][r], t.get(r, HEADERS[c])); 223 } 224 } 225 226 for ( int i=0; i<rows.length; ++i ) { 227 try { 228 t.get(rows[i],HEADERS[0]); 229 fail("Allowed illegal access"); 230 } catch ( Exception success ) { 231 } 232 } 233 234 for ( int i=0; i<cols.length; ++i ) { 235 try { 236 t.get(0,cols[i]); 237 fail("Allowed illegal access"); 238 } catch ( Exception success ) { 239 } 240 } 241 } 242 243 246 public void testSet() { 247 for ( int c=0; c<NCOLS; ++c ) { 248 t.set(0, HEADERS[c], TABLE[c][1]); 249 assertEquals(TABLE[c][1], t.get(0, HEADERS[c])); 250 } 251 252 for ( int i=0; i<rows.length; ++i ) { 253 try { 254 t.set(rows[i],HEADERS[0],TABLE[0][i]); 255 fail("Allowed illegal access"); 256 } catch ( Exception success ) { 257 } 258 } 259 260 for ( int i=0; i<cols.length; ++i ) { 261 try { 262 t.set(0,cols[i],TABLE[i][0]); 263 fail("Allowed illegal access"); 264 } catch ( Exception success ) { 265 } 266 } 267 } 268 269 public void testSort() { 270 String h1 = HEADERS[2]; 271 String h2 = HEADERS[1]; 272 Iterator iter = t.tuples(null, Sort.parse(h1+", "+h2+" desc")); 273 Tuple[] tpls = new Tuple[t.getRowCount()]; 274 for ( int i=0; iter.hasNext(); ++i ) { 275 tpls[i] = (Tuple)iter.next(); 276 if ( TestConfig.verbose() ) 277 System.err.println(tpls[i]); 278 } 279 Comparator cmp = DefaultLiteralComparator.getInstance(); 280 for ( int i=0; i<tpls.length-1; ++i ) { 281 Tuple t1 = tpls[i], t2 = tpls[i+1]; 282 int c = cmp.compare(t1.get(h1), t2.get(h1)); 283 assertTrue(c<=0); 284 if ( c == 0 ) { 285 c = cmp.compare(t1.get(h2), t2.get(h2)); 286 assertTrue(c>=0); 287 } 288 289 } 290 } 291 292 } 293 | Popular Tags |