Home | History | Annotate | Download | only in dom
      1 package tests.org.w3c.dom;
      2 
      3 import dalvik.annotation.TestTargetClass;
      4 
      5 import java.net.MalformedURLException;
      6 import java.net.URL;
      7 
      8 import javax.xml.parsers.DocumentBuilder;
      9 
     10 import org.w3c.dom.Document;
     11 import org.xml.sax.SAXException;
     12 import org.xml.sax.SAXParseException;
     13 
     14 import junit.framework.TestCase;
     15 
     16 @TestTargetClass(Document.class)
     17 public class DOMTestCase extends TestCase {
     18 
     19     public Document load(String docURI, DocumentBuilder builder) {
     20         Document doc = load(resolveURI(docURI), builder);
     21         return doc;
     22     }
     23 
     24     public Document load(URL url, DocumentBuilder builder) {
     25         Document doc = null;
     26         Exception parseException = null;
     27         try {
     28             LoadErrorHandler errorHandler = new LoadErrorHandler();
     29             builder.setErrorHandler(errorHandler);
     30             doc = builder.parse(url.openStream());
     31             parseException = errorHandler.getFirstException();
     32         } catch (Exception ex) {
     33             parseException = ex;
     34         }
     35         builder.setErrorHandler(null);
     36         if (parseException != null) {
     37             // fail("Unexpected exception " + parseException.getMessage());
     38             throw new RuntimeException("Unexpected exception " + parseException.getMessage(), parseException);
     39         }
     40         return doc;
     41     }
     42 
     43     public void preload(String contentType, String docURI,
     44             boolean willBeModified) {
     45         if ("text/html".equals(contentType)
     46                 || "application/xhtml+xml".equals(contentType)) {
     47             if (docURI.startsWith("staff")
     48                     || docURI.equals("datatype_normalization")) {
     49 
     50             }
     51         }
     52     }
     53 
     54     private URL resolveURI(String baseURI) {
     55         String docURI = baseURI + ".xml";
     56 
     57         URL resolvedURI = null;
     58         try {
     59             resolvedURI = new URL(docURI);
     60             if (resolvedURI.getProtocol() != null) {
     61                 return resolvedURI;
     62             }
     63         } catch (MalformedURLException ex) {
     64             // fail("Unexpected exception " + ex.getMessage());
     65         }
     66         //
     67         // build a URL for a test file in the JAR
     68         //
     69         resolvedURI = getClass().getResource("/" + docURI);
     70         if (resolvedURI == null) {
     71             //
     72             // see if it is an absolute URI
     73             //
     74             int firstSlash = docURI.indexOf('/');
     75             try {
     76                 if (firstSlash == 0
     77                         || (firstSlash >= 1 && docURI.charAt(firstSlash - 1) == ':')) {
     78                     resolvedURI = new URL(docURI);
     79                 } else {
     80                     //
     81                     // try the files/level?/spec directory
     82                     //
     83                     String filename = getClass().getPackage().getName();
     84                     filename = "tests/"
     85                             + filename.substring(14).replace('.', '/')
     86                             + "/files/" + docURI;
     87                     resolvedURI = new java.io.File(filename).toURL();
     88                 }
     89             } catch (MalformedURLException ex) {
     90                 fail("Unexpected exception " + ex.getMessage());
     91             }
     92         }
     93 
     94         if (resolvedURI == null) {
     95             fail("resolvedURI is null ");
     96         }
     97         return resolvedURI;
     98     }
     99 
    100 
    101     public String getContentType() {
    102         return "xml";
    103     }
    104 
    105     public void assertURIEquals(String assertID, String scheme, String path,
    106             String host, String file, String name, String query,
    107             String fragment, Boolean isAbsolute, String actual) {
    108         //
    109         // URI must be non-null
    110         assertNotNull(assertID, actual);
    111 
    112         String uri = actual;
    113 
    114         int lastPound = actual.lastIndexOf("#");
    115         String actualFragment = "";
    116         if (lastPound != -1) {
    117             //
    118             // substring before pound
    119             //
    120             uri = actual.substring(0, lastPound);
    121             actualFragment = actual.substring(lastPound + 1);
    122         }
    123         if (fragment != null) {
    124             assertEquals(assertID, fragment, actualFragment);
    125 
    126         }
    127         int lastQuestion = uri.lastIndexOf("?");
    128         String actualQuery = "";
    129         if (lastQuestion != -1) {
    130             //
    131             // substring before pound
    132             //
    133             uri = actual.substring(0, lastQuestion);
    134             actualQuery = actual.substring(lastQuestion + 1);
    135         }
    136         if (query != null) {
    137             assertEquals(assertID, query, actualQuery);
    138 
    139         }
    140         int firstColon = uri.indexOf(":");
    141         int firstSlash = uri.indexOf("/");
    142         String actualPath = uri;
    143         String actualScheme = "";
    144         if (firstColon != -1 && firstColon < firstSlash) {
    145             actualScheme = uri.substring(0, firstColon);
    146             actualPath = uri.substring(firstColon + 1);
    147         }
    148 
    149         if (scheme != null) {
    150             assertEquals(assertID, scheme, actualScheme);
    151         }
    152 
    153         if (path != null) {
    154             assertEquals(assertID, path, actualPath);
    155         }
    156 
    157         if (host != null) {
    158             String actualHost = "";
    159             if (actualPath.startsWith("//")) {
    160                 int termSlash = actualPath.indexOf("/", 2);
    161                 actualHost = actualPath.substring(0, termSlash);
    162             }
    163             assertEquals(assertID, host, actualHost);
    164         }
    165 
    166         String actualFile = actualPath;
    167         if (file != null || name != null) {
    168             int finalSlash = actualPath.lastIndexOf("/");
    169             if (finalSlash != -1) {
    170                 actualFile = actualPath.substring(finalSlash + 1);
    171             }
    172             if (file != null) {
    173                 assertEquals(assertID, file, actualFile);
    174             }
    175         }
    176 
    177         if (name != null) {
    178             String actualName = actualFile;
    179             int finalPeriod = actualFile.lastIndexOf(".");
    180             if (finalPeriod != -1) {
    181                 actualName = actualFile.substring(0, finalPeriod);
    182             }
    183             assertEquals(assertID, name, actualName);
    184         }
    185 
    186         if (isAbsolute != null) {
    187             //
    188             // Jar URL's will have any actual path like file:/c:/somedrive...
    189             assertEquals(assertID, isAbsolute.booleanValue(), actualPath
    190                     .startsWith("/")
    191                     || actualPath.startsWith("file:/"));
    192         }
    193     }
    194 
    195 
    196     private class LoadErrorHandler implements org.xml.sax.ErrorHandler {
    197         private SAXException parseException;
    198 
    199         private int errorCount;
    200 
    201         private int warningCount;
    202 
    203         public LoadErrorHandler() {
    204             parseException = null;
    205             errorCount = 0;
    206             warningCount = 0;
    207         }
    208 
    209         public void error(SAXParseException ex) {
    210             errorCount++;
    211             if (parseException == null) {
    212                 parseException = ex;
    213             }
    214         }
    215 
    216         public void warning(SAXParseException ex) {
    217             warningCount++;
    218         }
    219 
    220         public void fatalError(SAXParseException ex) {
    221             if (parseException == null) {
    222                 parseException = ex;
    223             }
    224         }
    225 
    226         public SAXException getFirstException() {
    227             return parseException;
    228         }
    229     }
    230 }
    231