KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > io > BasicTests


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: BasicTests.java,v 1.3 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.io;
25
26 import java.io.File JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
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 JavaDoc;
38 import org.w3c.dom.html.HTMLDocument;
39 import org.w3c.dom.html.HTMLElement;
40 import org.xml.sax.SAXException JavaDoc;
41
42 /**
43  * Basic DOMFormatter tests, mostly of special cases. More extensive
44  * tests are needed, however its well excerised by the XMLC tests.
45  */

46 public class BasicTests extends IOTestCaseBase {
47     // Character encodings.
48
private final String JavaDoc UTF8 = "UTF-8";
49     private final String JavaDoc ISO8859_1 = "ISO8859-1";
50
51     /** Test input files */
52     private final String JavaDoc TEST1_WML = "xml/wml/test1.wml";
53     private final String JavaDoc TEST2_WML = "xml/wml/test2.wml";
54
55     /** Factory method to create suite of these tests */
56     public static Test suite() {
57         return createSuite(BasicTests.class, null);
58     }
59     
60     /** Required constructor */
61     public BasicTests(Method JavaDoc method) {
62         super(method);
63     }
64
65     /**
66      * Create skeleton html object with the specified text
67      * in the body.
68      */

69     private HTMLDocument createHtml1(String JavaDoc text) {
70         HTMLDocument doc = HTMLDocumentFactory.createBasicDocument(getTestName());
71         HTMLElement body = doc.getBody();
72         body.appendChild(doc.createTextNode(text));
73         return doc;
74     }
75
76     /**
77      * Parse an XML document
78      */

79     private Document JavaDoc parseXml(File JavaDoc xmlFile) {
80         try {
81             DOMParser parser = new DOMParser();
82             parser.parse(xmlFile.getPath());
83             return parser.getDocument();
84         } catch (IOException JavaDoc except) {
85             throw new TestException(except);
86         } catch (SAXException JavaDoc except) {
87             throw new TestException(except);
88         }
89     }
90
91     /**
92      * Test 1: HTML formatting tests with special characters to a string.
93      */

94     public void test1() {
95         // ’ = 0122 = 0x96
96
String JavaDoc data = "One\u0096s check";
97         HTMLDocument doc = createHtml1(data);
98
99         DOMFormatter fmt = new DOMFormatter(); // Defaults
100
File JavaDoc outFile = getResultFile("html");
101         try {
102             FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(outFile);
103             try {
104                 out.write(data.getBytes(ISO8859_1));
105             } finally {
106                 out.close();
107             }
108         } catch (IOException JavaDoc except) {
109             throw new TestException(except);
110         }
111         TestDiff differ = getDiffer();
112         differ.diff(getExpectedFile("html"), outFile);
113     }
114
115     /**
116      * Test 2: HTML formatting tests with special characters to a file
117      */

118     public void test2() {
119         // ’ = 0122 = 0x96
120
String JavaDoc 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 JavaDoc outFile = getResultFile("html");
127         try {
128             fmt.write(doc, outFile);
129         } catch (IOException JavaDoc except) {
130             throw new TestException(except);
131         }
132         TestDiff differ = getDiffer();
133         differ.diff(getExpectedFile("html"), outFile);
134     }
135
136     /**
137      * Test various output options useful for WML. ASCII and omitting
138      * the encoding.
139      */

140     private void testWML(File JavaDoc xmlFile) {
141         Document JavaDoc doc = parseXml(xmlFile);
142
143         OutputOptions opts = new OutputOptions();
144         opts.setOmitEncoding(true);
145         opts.setEncoding("ASCII");
146
147         File JavaDoc outFile = getResultFile("wml");
148         DOMFormatter domf = new DOMFormatter(opts);
149         try {
150             domf.write(doc, outFile);
151         } catch (IOException JavaDoc except) {
152             throw new TestException(except);
153         }
154
155         getDiffer().diff(getExpectedFile("wml"), outFile);
156     }
157
158     /**
159      * Test 3: WML formatting options
160      */

161     public void test3() {
162         testWML(getInputFile(TEST1_WML));
163     }
164
165     /**
166      * Test 4: WML formatting options
167      */

168     public void test4() {
169         testWML(getInputFile(TEST2_WML));
170     }
171
172 }
173
Popular Tags