1   /*
2    * Copyright 2001-2004 (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    * $Id: TestTableModel.java,v 1.4 2004/06/25 08:03:50 maartenc Exp $
8    */
9   
10  package org.dom4j.swing;
11  
12  import javax.swing.table.TableModel;
13  
14  import junit.framework.Test;
15  import junit.framework.TestCase;
16  import junit.framework.TestSuite;
17  import junit.textui.TestRunner;
18  
19  import org.dom4j.Document;
20  import org.dom4j.io.SAXReader;
21  
22  /*** Tests the Swing TableModel using a dom4j document.
23    *
24    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
25    * @version $Revision: 1.4 $
26    */
27  public class TestTableModel extends TestCase
28  {
29      public TestTableModel(String name) {
30          super( name );
31      }
32      
33      public static void main(String[] args) {
34          TestRunner.run( suite() );
35      }
36      
37      public static Test suite() {
38          return new TestSuite( TestTableModel.class );
39      }
40      
41      public void testServletTable() throws Exception {
42          SAXReader reader = new SAXReader();
43          Document document = reader.read(getClass().getResource("/xml/web.xml"));
44          
45          XMLTableDefinition tableDefinition = new XMLTableDefinition();
46          tableDefinition.setRowExpression( "/web-app/servlet" );
47          tableDefinition.addStringColumn( "Name", "servlet-name" );
48          tableDefinition.addStringColumn( "Class", "servlet-class" );
49          tableDefinition.addStringColumn( "Mapping", "../servlet-mapping[servlet-name=$Name]/url-pattern" );
50          
51          XMLTableModel tableModel = new XMLTableModel( tableDefinition, document );
52  
53          // now lets test the values come out
54          assertEquals( "correct row count", tableModel.getRowCount(), 2 );
55          assertEquals( "correct column count", tableModel.getColumnCount(), 3 );
56          
57          assertColumnNameEquals( tableModel, 0, "Name" );
58          assertColumnNameEquals( tableModel, 1, "Class" );
59          assertColumnNameEquals( tableModel, 2, "Mapping" );
60          
61          assertCellEquals( tableModel, 0, 0, "snoop" );
62          assertCellEquals( tableModel, 1, 0, "file" );
63          assertCellEquals( tableModel, 0, 1, "SnoopServlet" );
64          assertCellEquals( tableModel, 1, 1, "ViewFile" );        
65          assertCellEquals( tableModel, 0, 2, "/foo/snoop" );
66          assertCellEquals( tableModel, 1, 2, "" );        
67      }
68      
69      public void testServletTableViaXMLDescription() throws Exception {
70          SAXReader reader = new SAXReader();
71          Document definition = reader.read(getClass().getResource("/xml/swing/tableForWeb.xml"));
72          Document document = reader.read(getClass().getResource("/xml/web.xml"));
73          
74          XMLTableModel tableModel = new XMLTableModel( definition, document );
75          
76          // now lets test the values come out
77          assertEquals( "correct row count", tableModel.getRowCount(), 2 );
78          assertEquals( "correct column count", tableModel.getColumnCount(), 3 );
79          
80          assertColumnNameEquals( tableModel, 0, "Name" );
81          assertColumnNameEquals( tableModel, 1, "Class" );
82          assertColumnNameEquals( tableModel, 2, "Mapping" );
83          
84          assertCellEquals( tableModel, 0, 0, "snoop" );
85          assertCellEquals( tableModel, 1, 0, "file" );
86          assertCellEquals( tableModel, 0, 1, "SnoopServlet" );
87          assertCellEquals( tableModel, 1, 1, "ViewFile" );        
88          assertCellEquals( tableModel, 0, 2, "/foo/snoop" );
89          assertCellEquals( tableModel, 1, 2, "" );        
90      }
91      
92      protected void assertColumnNameEquals(TableModel tableModel, int columnIndex, String name) {
93          assertEquals( "Column name correct for index: " + columnIndex, 
94              name, tableModel.getColumnName( columnIndex ) 
95          );
96      }
97      
98      protected void assertCellEquals(TableModel tableModel, int rowIndex, int columnIndex, Object value) {
99          assertEquals( "Cell value at row: " + rowIndex + " col: " + columnIndex, 
100             value, tableModel.getValueAt( rowIndex, columnIndex ) 
101         );
102     }
103 }
104 
105 
106 
107 
108 /*
109  * Redistribution and use of this software and associated documentation
110  * ("Software"), with or without modification, are permitted provided
111  * that the following conditions are met:
112  *
113  * 1. Redistributions of source code must retain copyright
114  *    statements and notices.  Redistributions must also contain a
115  *    copy of this document.
116  *
117  * 2. Redistributions in binary form must reproduce the
118  *    above copyright notice, this list of conditions and the
119  *    following disclaimer in the documentation and/or other
120  *    materials provided with the distribution.
121  *
122  * 3. The name "DOM4J" must not be used to endorse or promote
123  *    products derived from this Software without prior written
124  *    permission of MetaStuff, Ltd.  For written permission,
125  *    please contact dom4j-info@metastuff.com.
126  *
127  * 4. Products derived from this Software may not be called "DOM4J"
128  *    nor may "DOM4J" appear in their names without prior written
129  *    permission of MetaStuff, Ltd. DOM4J is a registered
130  *    trademark of MetaStuff, Ltd.
131  *
132  * 5. Due credit should be given to the DOM4J Project - 
133  *    http://www.dom4j.org
134  *
135  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
136  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
137  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
138  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
139  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
140  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
141  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
142  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
143  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
144  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
145  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
146  * OF THE POSSIBILITY OF SUCH DAMAGE.
147  *
148  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
149  *
150  * $Id: TestTableModel.java,v 1.4 2004/06/25 08:03:50 maartenc Exp $
151  */