1 23 24 package org.enhydra.xml.io; 25 26 import java.io.File ; 27 import java.io.FileWriter ; 28 import java.io.IOException ; 29 import java.io.PrintWriter ; 30 import java.lang.reflect.Method ; 31 32 import junit.framework.Test; 33 34 import org.enhydra.xml.dom.DOMInfo; 35 import org.enhydra.xml.dom.DOMStats; 36 import org.enhydra.xml.driver.TestDomOps; 37 import org.enhydra.xml.driver.TestException; 38 import org.enhydra.xml.driver.TestFileOps; 39 import org.enhydra.xml.xmlc.XMLObject; 40 import org.enhydra.xml.xmlc.driver.ExecXmlc; 41 import org.enhydra.xml.xmlc.driver.TestDOMSource; 42 import org.enhydra.xml.xmlc.driver.XmlcTestParams; 43 import org.w3c.dom.Node ; 44 45 48 public class URLRewriteTests extends IOTestCaseBase { 49 50 private static final int OMIT_ATTR_CHAR_ENTITY_REFS = 0x01; 51 52 53 private static final int EXPAND = 0x02; 54 55 56 private static final int START_AT_NODE = 0x04; 57 58 59 private static final String TEST_DOC_HTML = "html/TestDoc1.html"; 60 private static final String TEST_DOC_XML = "xml/wml/test1.wml"; 61 62 66 private TestDOMSource fXercesDomTestHtml; 67 private TestDOMSource fXercesDomKestrelHtml; 68 private TestDOMSource fLazyDomTestHtml; 69 private TestDOMSource fLazyDomKestrelHtml; 70 private TestDOMSource fXercesDomTestXml; 71 private TestDOMSource fLazyDomTestXml; 72 73 74 public static Test suite() { 75 return createSuite(URLRewriteTests.class, null); 76 } 77 78 79 public URLRewriteTests(Method method) { 80 super(method); 81 82 XmlcTestParams xercesHtmlParams 85 = new XmlcTestParams(false, ExecXmlc.TIDY_PARSER, 86 ExecXmlc.XERCES_DOM, false); 87 fXercesDomTestHtml 88 = new TestDOMSource(this, xercesHtmlParams, 89 getInputFile(TEST_DOC_HTML), 90 "testHtml", null); 91 fXercesDomKestrelHtml 92 = new TestDOMSource(this, xercesHtmlParams, 93 getInputFile(TEST_DOC_HTML), 94 "kestrelHtml", Kestrel.class); 95 96 XmlcTestParams lazyHtmlParams 97 = new XmlcTestParams(false, ExecXmlc.TIDY_PARSER, 98 ExecXmlc.LAZY_DOM, false); 99 fLazyDomTestHtml 100 = new TestDOMSource(this, lazyHtmlParams, 101 getInputFile(TEST_DOC_HTML), 102 "testHtml", null); 103 fLazyDomKestrelHtml 104 = new TestDOMSource(this, lazyHtmlParams, 105 getInputFile(TEST_DOC_HTML), 106 "kestrelHtml", Kestrel.class); 107 108 XmlcTestParams xercesXmlParams 109 = new XmlcTestParams(true, ExecXmlc.XERCES_PARSER, 110 ExecXmlc.XERCES_DOM, false); 111 fXercesDomTestXml 112 = new TestDOMSource(this, xercesXmlParams, 113 getInputFile(TEST_DOC_XML), 114 "testXml", null); 115 116 XmlcTestParams lazyXmlParams 117 = new XmlcTestParams(true, ExecXmlc.XERCES_PARSER, 118 ExecXmlc.LAZY_DOM, false); 119 fLazyDomTestXml 120 = new TestDOMSource(this, lazyXmlParams, 121 getInputFile(TEST_DOC_XML), 122 "testXml", null); 123 124 } 125 126 129 private void outputWithRewritting(Node node, 130 int flags, 131 File outFile) throws IOException { 132 OutputOptions options = new OutputOptions(); 133 if ((flags & OMIT_ATTR_CHAR_ENTITY_REFS) != 0) { 134 options.setOmitAttributeCharEntityRefs(true); 135 } 136 137 options.setURLRewriter(new URLRewriter() { 138 public String rewriteURL(String urlAttrValue) { 139 return urlAttrValue + "#REWRITTEN"; 140 } 141 }); 142 DOMFormatter formatter = new DOMFormatter(options); 143 formatter.write(node, outFile); 144 } 145 146 149 private void testStep(TestDOMSource domSource, 150 int flags) { 151 String docFileExt = TestFileOps.getFileExt(domSource.getInputFile()); 152 XMLObject xmlcDoc = domSource.create(); 153 if ((flags & EXPAND) != 0) { 154 TestDomOps.expandTree(xmlcDoc); 155 } 156 Node startingNode = ((flags & START_AT_NODE) != 0) 157 ? (Node )xmlcDoc.getDocumentElement() : (Node )xmlcDoc; 158 159 if (xmlcDoc instanceof Kestrel) { 161 Kestrel kestrel = (Kestrel)xmlcDoc; 162 kestrel.getElementKestrel().setHref("javascript:submitForm(\"\",\"folder\",\"/INBOX/\")"); 163 } 164 165 try { 166 File outFile = getResultFile(docFileExt); 168 outputWithRewritting(startingNode, flags, outFile); 169 getDiffer().diff(getExpectedFile(docFileExt), outFile); 170 171 File outDom = getResultFile("dom"); 173 File expectDom = getExpectedFile("dom"); 174 PrintWriter writer = new PrintWriter (new FileWriter (outDom)); 175 DOMInfo.printTree("DOM after URL rewritting", xmlcDoc, 176 DOMInfo.PRINT_ATTR_DETAILS, writer); 177 DOMStats.printStats("DOM after URL rewritting", 178 xmlcDoc, 0, writer); 179 writer.close(); 180 getDiffer().diff(expectDom, outDom); 181 } catch (IOException except) { 182 throw new TestException(except); 183 } 184 } 185 186 189 public void test1() { 190 testStep(fXercesDomTestHtml, 0); 191 } 192 193 196 public void test2() { 197 testStep(fLazyDomTestHtml, 0); 198 } 199 200 203 public void test3() { 204 testStep(fLazyDomTestHtml, EXPAND); 205 } 206 207 210 public void test4() { 211 testStep(fXercesDomTestXml, 0); 212 } 213 217 public void test5() { 218 testStep(fXercesDomTestHtml, START_AT_NODE); 219 } 220 221 225 public void test6() { 226 testStep(fLazyDomTestHtml, START_AT_NODE); 227 } 228 229 232 public void test7() { 233 testStep(fXercesDomTestXml, START_AT_NODE); 234 } 235 236 239 public void test8() { 240 testStep(fLazyDomTestHtml, START_AT_NODE); 241 } 242 243 247 public void test9() { 248 testStep(fXercesDomTestHtml, OMIT_ATTR_CHAR_ENTITY_REFS); 249 } 250 251 254 public void test10() { 255 testStep(fLazyDomTestHtml, OMIT_ATTR_CHAR_ENTITY_REFS); 256 } 257 } 258 | Popular Tags |