1 23 24 package org.enhydra.xml.io; 25 26 import java.io.File ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.lang.reflect.Method ; 30 31 import junit.framework.Test; 32 33 import org.enhydra.apache.xerces.parsers.DOMParser; 34 import org.enhydra.xml.driver.TestDiff; 35 import org.enhydra.xml.driver.TestException; 36 import org.enhydra.xml.xmlc.html.HTMLDocumentFactory; 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.html.HTMLDocument; 39 import org.w3c.dom.html.HTMLElement; 40 import org.xml.sax.SAXException ; 41 42 46 public class BasicTests extends IOTestCaseBase { 47 private final String UTF8 = "UTF-8"; 49 private final String ISO8859_1 = "ISO8859-1"; 50 51 52 private final String TEST1_WML = "xml/wml/test1.wml"; 53 private final String TEST2_WML = "xml/wml/test2.wml"; 54 55 56 public static Test suite() { 57 return createSuite(BasicTests.class, null); 58 } 59 60 61 public BasicTests(Method method) { 62 super(method); 63 } 64 65 69 private HTMLDocument createHtml1(String text) { 70 HTMLDocument doc = HTMLDocumentFactory.createBasicDocument(getTestName()); 71 HTMLElement body = doc.getBody(); 72 body.appendChild(doc.createTextNode(text)); 73 return doc; 74 } 75 76 79 private Document parseXml(File xmlFile) { 80 try { 81 DOMParser parser = new DOMParser(); 82 parser.parse(xmlFile.getPath()); 83 return parser.getDocument(); 84 } catch (IOException except) { 85 throw new TestException(except); 86 } catch (SAXException except) { 87 throw new TestException(except); 88 } 89 } 90 91 94 public void test1() { 95 String data = "One\u0096s check"; 97 HTMLDocument doc = createHtml1(data); 98 99 DOMFormatter fmt = new DOMFormatter(); File outFile = getResultFile("html"); 101 try { 102 FileOutputStream out = new FileOutputStream (outFile); 103 try { 104 out.write(data.getBytes(ISO8859_1)); 105 } finally { 106 out.close(); 107 } 108 } catch (IOException except) { 109 throw new TestException(except); 110 } 111 TestDiff differ = getDiffer(); 112 differ.diff(getExpectedFile("html"), outFile); 113 } 114 115 118 public void test2() { 119 String data = "One\u0096s check"; 121 HTMLDocument doc = createHtml1(data); 122 123 OutputOptions opts = new OutputOptions(); 124 opts.setEncoding(ISO8859_1); 125 DOMFormatter fmt = new DOMFormatter(opts); 126 File outFile = getResultFile("html"); 127 try { 128 fmt.write(doc, outFile); 129 } catch (IOException except) { 130 throw new TestException(except); 131 } 132 TestDiff differ = getDiffer(); 133 differ.diff(getExpectedFile("html"), outFile); 134 } 135 136 140 private void testWML(File xmlFile) { 141 Document doc = parseXml(xmlFile); 142 143 OutputOptions opts = new OutputOptions(); 144 opts.setOmitEncoding(true); 145 opts.setEncoding("ASCII"); 146 147 File outFile = getResultFile("wml"); 148 DOMFormatter domf = new DOMFormatter(opts); 149 try { 150 domf.write(doc, outFile); 151 } catch (IOException except) { 152 throw new TestException(except); 153 } 154 155 getDiffer().diff(getExpectedFile("wml"), outFile); 156 } 157 158 161 public void test3() { 162 testWML(getInputFile(TEST1_WML)); 163 } 164 165 168 public void test4() { 169 testWML(getInputFile(TEST2_WML)); 170 } 171 172 } 173 | Popular Tags |