1 61 62 package org.jaxen.function; 63 64 import java.io.IOException ; 65 import java.util.List ; 66 67 import javax.xml.parsers.DocumentBuilder ; 68 import javax.xml.parsers.DocumentBuilderFactory ; 69 import javax.xml.parsers.ParserConfigurationException ; 70 71 import junit.framework.TestCase; 72 73 import org.jaxen.FunctionCallException; 74 import org.jaxen.JaxenException; 75 import org.jaxen.XPath; 76 import org.jaxen.dom.DOMXPath; 77 import org.w3c.dom.Attr ; 78 import org.w3c.dom.Comment ; 79 import org.w3c.dom.Document ; 80 import org.w3c.dom.ProcessingInstruction ; 81 import org.w3c.dom.Text ; 82 import org.xml.sax.SAXException ; 83 84 88 public class NameTest extends TestCase { 89 90 private Document doc; 91 92 public void setUp() throws ParserConfigurationException , SAXException , IOException 93 { 94 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 95 factory.setNamespaceAware(true); 96 DocumentBuilder builder = factory.newDocumentBuilder(); 97 doc = builder.parse( "xml/basic.xml" ); 98 } 99 100 101 public NameTest(String name) { 102 super(name); 103 } 104 105 public void testNameOfNumber() throws JaxenException 106 { 107 try 108 { 109 XPath xpath = new DOMXPath( "name(3)" ); 110 xpath.selectNodes( doc ); 111 fail("name of non-node-set"); 112 } 113 catch (FunctionCallException e) 114 { 115 assertEquals("The argument to the name function must be a node-set", e.getMessage()); 116 } 117 } 118 119 public void testNameWithTwoArguments() throws JaxenException 120 { 121 try 122 { 123 XPath xpath = new DOMXPath( "name(/*, //*)" ); 124 xpath.selectNodes( doc ); 125 fail("name with two arguments"); 126 } 127 catch (FunctionCallException e) 128 { 129 assertEquals("name() requires zero or one argument.", e.getMessage()); 130 } 131 } 132 133 public void testNameAllowsNoArguments() throws JaxenException 134 { 135 XPath xpath = new DOMXPath( "name()" ); 136 String result = (String ) xpath.evaluate( doc.getDocumentElement() ); 137 assertEquals("foo", result); 138 } 139 140 public void testNameOfCommentIsEmptyString() throws JaxenException 141 { 142 XPath xpath = new DOMXPath( "name(/comment())" ); 143 Comment c = doc.createComment("test"); 144 doc.appendChild(c); 145 String result = (String ) xpath.evaluate(doc); 146 assertEquals("", result); 147 } 148 149 public void testNameOfProcessingInstructionIsTarget() throws JaxenException 150 { 151 XPath xpath = new DOMXPath( "name(/processing-instruction())" ); 152 ProcessingInstruction pi = doc.createProcessingInstruction("target", "value"); 153 doc.appendChild(pi); 154 String result = (String ) xpath.evaluate(doc); 155 assertEquals("target", result); 156 } 157 158 public void testNameOfAttribute() throws JaxenException 159 { 160 XPath xpath = new DOMXPath( "name(/*/@*)" ); 161 Attr a = doc.createAttribute("name"); 162 doc.getDocumentElement().setAttributeNode(a); 163 String result = (String ) xpath.evaluate(doc); 164 assertEquals("name", result); 165 } 166 167 public void testNameOfTextIsEmptyString() throws JaxenException 168 { 169 XPath xpath = new DOMXPath( "name(/*/text())" ); 170 Text c = doc.createTextNode("test"); 171 doc.getDocumentElement().appendChild(c); 172 String result = (String ) xpath.evaluate(doc); 173 assertEquals("", result); 174 } 175 176 public void testNameOfNamespaceIsPrefix() throws JaxenException 177 { 178 XPath xpath = new DOMXPath( "name(/*/namespace::node())" ); 179 String result = (String ) xpath.evaluate(doc); 180 assertEquals("xml", result); 181 } 182 183 public void testNameNoArguments() 184 { 185 try 186 { 187 XPath xpath = new DOMXPath( "name()" ); 188 List results = xpath.selectNodes( doc ); 189 assertEquals("", results.get(0)); 190 } 191 catch (Exception e) 192 { 193 e.printStackTrace(); 194 fail( e.getMessage() ); 195 } 196 } 197 198 } 199 | Popular Tags |