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: AbstractDataTypeTest.java,v 1.5 2004/06/25 08:03:48 maartenc Exp $
8    */
9   
10  package org.dom4j.datatype;
11  
12  import java.util.Iterator;
13  import java.util.List;
14  
15  import org.dom4j.AbstractTestCase;
16  import org.dom4j.Attribute;
17  import org.dom4j.Element;
18  import org.dom4j.Node;
19  
20  
21  /*** Abstract base class useful for implementation inheritence
22    * for testing XML Schema Data Type integration. 
23    *
24    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
25    * @version $Revision: 1.5 $
26    */
27  public class AbstractDataTypeTest extends AbstractTestCase {
28  
29      protected boolean VERBOSE = true;
30  
31      
32      public AbstractDataTypeTest(String name) {
33          super(name);
34      }
35  
36      // Implementation methods
37      //-------------------------------------------------------------------------
38      protected void testNodes(String xpath, Class type) {
39          List list = document.selectNodes( xpath );
40          
41          log( "Searched path: " + xpath + " found: " + list.size() + " result(s)" );
42          
43          if ( VERBOSE ) {
44              log( "" );
45              log( "xpath: " + xpath );
46              log( "" );
47              log( "results: " + list );
48              log( "" );
49          }
50          
51          assertTrue( "Results are not empty", ! list.isEmpty() );
52          
53          for ( Iterator iter = list.iterator(); iter.hasNext(); ) {
54              Node node = (Node) iter.next();
55              if ( node instanceof Element ) {
56                  Element element = (Element) node;
57                  testDataType( element, element.getData(), type );
58              }
59              else if ( node instanceof Attribute ) {
60                  Attribute attribute = (Attribute) node;
61                  testDataType( attribute, attribute.getData(), type );
62              }
63              else {
64                  assertTrue( "Did not find an attribute or element: " + node, false );
65              }
66          }
67      }
68      
69      protected void testDataType(Node node, Object data, Class type) {
70          assertTrue( "Data object is not null", data != null );
71          
72          if ( VERBOSE ) {
73              log( "found: " + data + " type: " + data.getClass().getName() + " required type: " + type.getName() );
74              log( "node: " + node );
75          }
76          
77          assertTrue( 
78              "Data object is of the correct type. Expected: " 
79                  + type.getName() 
80                  + " and found: " + data.getClass().getName(), 
81              type.isAssignableFrom( data.getClass() ) 
82          );
83      }
84  }
85  
86  
87  
88  
89  /*
90   * Redistribution and use of this software and associated documentation
91   * ("Software"), with or without modification, are permitted provided
92   * that the following conditions are met:
93   *
94   * 1. Redistributions of source code must retain copyright
95   *    statements and notices.  Redistributions must also contain a
96   *    copy of this document.
97   *
98   * 2. Redistributions in binary form must reproduce the
99   *    above copyright notice, this list of conditions and the
100  *    following disclaimer in the documentation and/or other
101  *    materials provided with the distribution.
102  *
103  * 3. The name "DOM4J" must not be used to endorse or promote
104  *    products derived from this Software without prior written
105  *    permission of MetaStuff, Ltd.  For written permission,
106  *    please contact dom4j-info@metastuff.com.
107  *
108  * 4. Products derived from this Software may not be called "DOM4J"
109  *    nor may "DOM4J" appear in their names without prior written
110  *    permission of MetaStuff, Ltd. DOM4J is a registered
111  *    trademark of MetaStuff, Ltd.
112  *
113  * 5. Due credit should be given to the DOM4J Project - 
114  *    http://www.dom4j.org
115  *
116  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
117  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
118  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
119  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
120  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
121  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
122  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
123  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
124  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
125  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
126  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
127  * OF THE POSSIBILITY OF SUCH DAMAGE.
128  *
129  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
130  *
131  * $Id: AbstractDataTypeTest.java,v 1.5 2004/06/25 08:03:48 maartenc Exp $
132  */