1 package net.sourceforge.cvsgrab; 2 3 import org.apache.commons.jxpath.JXPathContext; 4 import org.apache.commons.jxpath.Pointer; 5 import org.apache.xml.serialize.DOMSerializer; 6 import org.apache.xml.serialize.OutputFormat; 7 import org.apache.xml.serialize.Serializer; 8 import org.apache.xml.serialize.XMLSerializer; 9 import org.w3c.dom.Document ; 10 11 import java.io.File ; 12 import java.io.FileReader ; 13 import java.io.IOException ; 14 import java.io.LineNumberReader ; 15 import java.util.Iterator ; 16 17 import junit.framework.TestCase; 18 19 20 25 public abstract class AbstractTestCase extends TestCase { 26 30 public String basedir = System.getProperty("basedir"); 31 32 35 public AbstractTestCase(String testName) { 36 super(testName); 37 } 38 39 44 public String getTestFile(String path) { 45 return new File (basedir, path).getAbsolutePath(); 46 } 47 48 protected Document getDocument(String testFile) throws Exception { 49 FileReader fileReader = new FileReader (testFile); 50 LineNumberReader lnReader = new LineNumberReader (fileReader); 51 StringBuffer sb = new StringBuffer (); 52 String line = lnReader.readLine(); 53 while (line != null) { 54 sb.append(line); 55 line = lnReader.readLine(); 56 } 57 return WebBrowser.getInstance().getDocumentFromSource(sb.toString()); 58 } 59 60 protected void logDocument(Document doc) { 61 Serializer serializer = new XMLSerializer(System.out, new OutputFormat()); 62 try { 63 DOMSerializer domSerializer = serializer.asDOMSerializer(); 64 domSerializer.serialize(doc); 65 } catch (IOException e) { 66 throw new RuntimeException (e); 67 } 68 } 69 70 protected void testXpath(Document doc, String xpath) { 71 System.out.println(); 72 System.out.println("Found nodes for xpath " + xpath); 73 JXPathContext context = JXPathContext.newContext(doc); 74 context.setLenient(true); 75 Iterator i = context.iteratePointers(xpath); 76 while (i.hasNext()) { 77 Pointer pointer = (Pointer) i.next(); 78 JXPathContext nodeContext = context.getRelativeContext(pointer); 79 String text = (String ) nodeContext.getValue(".//text()"); 80 if (text == null) { 81 text = String.valueOf(nodeContext.getValue(".")); 82 } 83 System.out.println(pointer + " " + text); 84 } 85 System.out.println("---------"); 86 } 87 } 88 | Popular Tags |