1 9 10 package test.dom4j.datatype; 11 12 import org.dom4j.Attribute; 13 import org.dom4j.Element; 14 import org.dom4j.Node; 15 import test.dom4j.AbstractTestCase; 16 17 import java.util.Iterator ; 18 import java.util.List ; 19 20 21 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 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 133 | Popular Tags |