1   package net.sourceforge.mavenplugins.sourceforge;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   
6   import junit.framework.TestCase;
7   
8   import org.apache.xerces.xni.parser.XMLInputSource;
9   import org.cyberneko.html.parsers.DOMParser;
10  import org.w3c.dom.Document;
11  
12  
13  /***
14   * Abstract base class for test cases.
15   *
16   * @author <a href="jason@zenplex.com">Jason van Zyl</a>
17   */
18  public abstract class AbstractTestCase extends TestCase {
19      /***
20       * Basedir for all file I/O. Important when running tests from
21       * the reactor.
22       */
23      public String basedir = System.getProperty("basedir", ".");
24  
25      /***
26       * Constructor.
27       */
28      public AbstractTestCase(String testName) {
29          super(testName);
30      }
31  
32      /***
33       * Get test input file.
34       *
35       * @param path Path to test input file.
36       */
37      public String getTestFile(String path) throws Exception {
38          File file = new File(basedir);
39          file = new File(file, path);
40          file = file.getCanonicalFile();
41          if (!file.exists()) {
42              throw new Exception("File " + file + " doesn't exist");
43          }
44          return file.getCanonicalPath();
45      }
46      
47      protected Document getDocument(String testFile) throws Exception {
48          try {
49              DOMParser parser = new DOMParser();
50              XMLInputSource source = new XMLInputSource(null, null, null, new FileInputStream(getTestFile(testFile)), null);
51      
52              parser.setProperty("http://cyberneko.org/html/properties/names/elems", "lower");
53              parser.parse(source);
54      
55              Document doc = parser.getDocument();
56              return doc;
57          } catch (Exception ex) {
58              System.err.println("Could not read document " + testFile);
59              ex.printStackTrace();
60              throw ex;
61          }
62      }
63      
64  }