KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cvsgrab > AbstractTestCase


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 JavaDoc;
10
11 import java.io.File JavaDoc;
12 import java.io.FileReader JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.LineNumberReader JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import junit.framework.TestCase;
18
19
20 /**
21  * Abstract base class for test cases.
22  *
23  * @author <a HREF="jason@zenplex.com">Jason van Zyl</a>
24  */

25 public abstract class AbstractTestCase extends TestCase {
26     /**
27      * Basedir for all file I/O. Important when running tests from
28      * the reactor.
29      */

30     public String JavaDoc basedir = System.getProperty("basedir");
31
32     /**
33      * Constructor.
34      */

35     public AbstractTestCase(String JavaDoc testName) {
36         super(testName);
37     }
38
39     /**
40      * Get test input file.
41      *
42      * @param path Path to test input file.
43      */

44     public String JavaDoc getTestFile(String JavaDoc path) {
45         return new File JavaDoc(basedir, path).getAbsolutePath();
46     }
47
48     protected Document JavaDoc getDocument(String JavaDoc testFile) throws Exception JavaDoc {
49         FileReader JavaDoc fileReader = new FileReader JavaDoc(testFile);
50         LineNumberReader JavaDoc lnReader = new LineNumberReader JavaDoc(fileReader);
51         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
52         String JavaDoc 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 JavaDoc doc) {
61         Serializer serializer = new XMLSerializer(System.out, new OutputFormat());
62         try {
63             DOMSerializer domSerializer = serializer.asDOMSerializer();
64             domSerializer.serialize(doc);
65         } catch (IOException JavaDoc e) {
66             throw new RuntimeException JavaDoc(e);
67         }
68     }
69
70     protected void testXpath(Document JavaDoc doc, String JavaDoc 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 JavaDoc i = context.iteratePointers(xpath);
76         while (i.hasNext()) {
77             Pointer pointer = (Pointer) i.next();
78             JXPathContext nodeContext = context.getRelativeContext(pointer);
79             String JavaDoc text = (String JavaDoc) 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