1 7 8 package org.dom4j.swing; 9 10 import junit.textui.TestRunner; 11 12 import javax.swing.table.TableModel ; 13 14 import org.dom4j.AbstractTestCase; 15 import org.dom4j.Document; 16 17 23 public class TableModelTest extends AbstractTestCase { 24 public static void main(String [] args) { 25 TestRunner.run(TableModelTest.class); 26 } 27 28 public void testServletTable() throws Exception { 29 Document document = getDocument("/xml/web.xml"); 30 31 XMLTableDefinition tableDefinition = new XMLTableDefinition(); 32 tableDefinition.setRowExpression("/web-app/servlet"); 33 tableDefinition.addStringColumn("Name", "servlet-name"); 34 tableDefinition.addStringColumn("Class", "servlet-class"); 35 36 String mapping = "../servlet-mapping[servlet-name=$Name]/url-pattern"; 37 tableDefinition.addStringColumn("Mapping", mapping); 38 39 XMLTableModel tableModel = new XMLTableModel(tableDefinition, document); 40 41 assertEquals("correct row count", tableModel.getRowCount(), 2); 43 assertEquals("correct column count", tableModel.getColumnCount(), 3); 44 45 assertColumnNameEquals(tableModel, 0, "Name"); 46 assertColumnNameEquals(tableModel, 1, "Class"); 47 assertColumnNameEquals(tableModel, 2, "Mapping"); 48 49 assertCellEquals(tableModel, 0, 0, "snoop"); 50 assertCellEquals(tableModel, 1, 0, "file"); 51 assertCellEquals(tableModel, 0, 1, "SnoopServlet"); 52 assertCellEquals(tableModel, 1, 1, "ViewFile"); 53 assertCellEquals(tableModel, 0, 2, "/foo/snoop"); 54 assertCellEquals(tableModel, 1, 2, ""); 55 } 56 57 public void testServletTableViaXMLDescription() throws Exception { 58 Document definition = getDocument("/xml/swing/tableForWeb.xml"); 59 Document document = getDocument("/xml/web.xml"); 60 61 XMLTableModel tableModel = new XMLTableModel(definition, document); 62 63 assertEquals("correct row count", tableModel.getRowCount(), 2); 65 assertEquals("correct column count", tableModel.getColumnCount(), 3); 66 67 assertColumnNameEquals(tableModel, 0, "Name"); 68 assertColumnNameEquals(tableModel, 1, "Class"); 69 assertColumnNameEquals(tableModel, 2, "Mapping"); 70 71 assertCellEquals(tableModel, 0, 0, "snoop"); 72 assertCellEquals(tableModel, 1, 0, "file"); 73 assertCellEquals(tableModel, 0, 1, "SnoopServlet"); 74 assertCellEquals(tableModel, 1, 1, "ViewFile"); 75 assertCellEquals(tableModel, 0, 2, "/foo/snoop"); 76 assertCellEquals(tableModel, 1, 2, ""); 77 } 78 79 protected void assertColumnNameEquals(TableModel tableModel, 80 int columnIndex, String name) { 81 assertEquals("Column name correct for index: " + columnIndex, name, 82 tableModel.getColumnName(columnIndex)); 83 } 84 85 protected void assertCellEquals(TableModel tableModel, int rowIndex, 86 int columnIndex, Object value) { 87 assertEquals("Cell value at row: " + rowIndex + " col: " + columnIndex, 88 value, tableModel.getValueAt(rowIndex, columnIndex)); 89 } 90 } 91 92 128 | Popular Tags |