KickJava   Java API By Example, From Geeks To Geeks.

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


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: URLRewriteTests.java,v 1.2 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.FileWriter JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
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 JavaDoc;
44
45 /**
46  * Test DOMFormatter URL rewriting funtionality.
47  */

48 public class URLRewriteTests extends IOTestCaseBase {
49     /** Flag indicating test of omitAttributeCharEntityRefs */
50     private static final int OMIT_ATTR_CHAR_ENTITY_REFS = 0x01;
51
52     /** Traverse to expand Lazy DOM */
53     private static final int EXPAND = 0x02;
54
55     /** Optionally start at an element to test the XMLObjectLink stuff */
56     private static final int START_AT_NODE = 0x04;
57
58     /** Input file */
59     private static final String JavaDoc TEST_DOC_HTML = "html/TestDoc1.html";
60     private static final String JavaDoc TEST_DOC_XML = "xml/wml/test1.wml";
61
62     /**
63      * DOM source objects used to create test documents. These repersent
64      * documents compiled with different options.
65      */

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     /** Factory method to create suite of these tests */
74     public static Test suite() {
75         return createSuite(URLRewriteTests.class, null);
76     }
77     
78     /** Required constructor, params is ignored */
79     public URLRewriteTests(Method JavaDoc method) {
80         super(method);
81
82         /// FIXME:make these static...
83
// Initialize test DOM sources; actual compile happens on demand.
84
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     /**
127      * Output a document with URL rewritting
128      */

129     private void outputWithRewritting(Node JavaDoc node,
130                                       int flags,
131                                       File JavaDoc outFile) throws IOException JavaDoc {
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 JavaDoc rewriteURL(String JavaDoc urlAttrValue) {
139                     return urlAttrValue + "#REWRITTEN";
140                 }
141             });
142         DOMFormatter formatter = new DOMFormatter(options);
143         formatter.write(node, outFile);
144     }
145     
146     /**
147      * Basic test step. Output and compare with expected.
148      */

149     private void testStep(TestDOMSource domSource,
150                           int flags) {
151         String JavaDoc docFileExt = TestFileOps.getFileExt(domSource.getInputFile());
152         XMLObject xmlcDoc = domSource.create();
153         if ((flags & EXPAND) != 0) {
154             TestDomOps.expandTree(xmlcDoc);
155         }
156         Node JavaDoc startingNode = ((flags & START_AT_NODE) != 0)
157             ? (Node JavaDoc)xmlcDoc.getDocumentElement() : (Node JavaDoc)xmlcDoc;
158
159         // Insert a attribute value with quotes:
160
if (xmlcDoc instanceof Kestrel) {
161             Kestrel kestrel = (Kestrel)xmlcDoc;
162             kestrel.getElementKestrel().setHref("javascript:submitForm(\"\",\"folder\",\"/INBOX/\")");
163         }
164
165         try {
166             // Output and verify
167
File JavaDoc outFile = getResultFile(docFileExt);
168             outputWithRewritting(startingNode, flags, outFile);
169             getDiffer().diff(getExpectedFile(docFileExt), outFile);
170
171             // Print DOM info and verify that DOM is not modified
172
File JavaDoc outDom = getResultFile("dom");
173             File JavaDoc expectDom = getExpectedFile("dom");
174             PrintWriter JavaDoc writer = new PrintWriter JavaDoc(new FileWriter JavaDoc(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 JavaDoc except) {
182             throw new TestException(except);
183         }
184     }
185
186     /**
187      * Test 1: Xerces DOM URL rewriting
188      */

189     public void test1() {
190         testStep(fXercesDomTestHtml, 0);
191     }
192
193     /**
194      * Test 2: Lazy DOM URL rewriting
195      */

196     public void test2() {
197         testStep(fLazyDomTestHtml, 0);
198     }
199
200     /**
201      * Test 3: Expanded Lazy DOM URL rewriting
202      */

203     public void test3() {
204         testStep(fLazyDomTestHtml, EXPAND);
205     }
206
207     /**
208      * Test 4: Xerces XML URL rewriting
209      */

210     public void test4() {
211         testStep(fXercesDomTestXml, 0);
212     }
213     /**
214      * Test 5: Xerces DOM URL rewriting starting below the document
215      * (test XMLObjectLink).
216      */

217     public void test5() {
218         testStep(fXercesDomTestHtml, START_AT_NODE);
219     }
220
221     /**
222      * Test 6: Lazy DOM URL rewriting starting below the document
223      * (test XMLObjectLink)
224      */

225     public void test6() {
226         testStep(fLazyDomTestHtml, START_AT_NODE);
227     }
228
229     /**
230      * Test 7: Xerces XML URL rewriting starting below the document.
231      */

232     public void test7() {
233         testStep(fXercesDomTestXml, START_AT_NODE);
234     }
235
236     /**
237      * Test 8: LazyDOM XML URL rewriting starting below the document.
238      */

239     public void test8() {
240         testStep(fLazyDomTestHtml, START_AT_NODE);
241     }
242
243     /**
244      * Test 9: Bug with not substituting URL character entity references, URL
245      * rewriting and attributes that contain double quites
246      */

247     public void test9() {
248         testStep(fXercesDomTestHtml, OMIT_ATTR_CHAR_ENTITY_REFS);
249     }
250
251     /**
252      * Test 10: Above test with LazyDOM
253      */

254     public void test10() {
255         testStep(fLazyDomTestHtml, OMIT_ATTR_CHAR_ENTITY_REFS);
256     }
257 }
258
Popular Tags