1   /*
2    * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3    *
4    * This software is open source.
5    * See the bottom of this file for the licence.
6    */
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  /***
18   * Tests the Swing TableModel using a dom4j document.
19   * 
20   * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
21   * @version $Revision: 1.4 $
22   */
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          // now lets test the values come out
42          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          // now lets test the values come out
64          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  /*
93   * Redistribution and use of this software and associated documentation
94   * ("Software"), with or without modification, are permitted provided that the
95   * following conditions are met:
96   * 
97   * 1. Redistributions of source code must retain copyright statements and
98   * notices. Redistributions must also contain a copy of this document.
99   * 
100  * 2. Redistributions in binary form must reproduce the above copyright notice,
101  * this list of conditions and the following disclaimer in the documentation
102  * and/or other materials provided with the distribution.
103  * 
104  * 3. The name "DOM4J" must not be used to endorse or promote products derived
105  * from this Software without prior written permission of MetaStuff, Ltd. For
106  * written permission, please contact dom4j-info@metastuff.com.
107  * 
108  * 4. Products derived from this Software may not be called "DOM4J" nor may
109  * "DOM4J" appear in their names without prior written permission of MetaStuff,
110  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
111  * 
112  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
113  * 
114  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
115  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
116  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
117  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
118  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
119  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
120  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
121  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
122  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
123  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
124  * POSSIBILITY OF SUCH DAMAGE.
125  * 
126  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
127  */