KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > compiler > TextCompiler_Test


1 /* ****************************************************************************
2  * TextCompiler_Test.java
3 * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.compiler;
11 import java.io.*;
12 import java.util.*;
13 import junit.framework.*;
14 import org.openlaszlo.utils.ChainedException;
15 import org.jdom.Element;
16 import org.jdom.Document;
17 import org.jdom.input.SAXBuilder;
18 import org.xml.sax.InputSource JavaDoc;
19
20 /*
21  * junit.awtui.TestRunner
22  * junit.swingui.TestRunner
23  * junit.textui.TestRunner
24  * java junit.textui.TestRunner org.openlaszlo.compiler.TextCompiler_Test
25  */

26
27 public class TextCompiler_Test extends TestCase {
28     public TextCompiler_Test (String JavaDoc name) {
29         super(name);
30     }
31
32     public void setUp () {
33     }
34
35
36     public void testHTMLContent () {
37         String JavaDoc[] tests = {
38             // Each case is input, expected-output (null == same as input)
39

40             // whitespace
41
"A foo <i></i>", "A foo<i></i>", // 0
42
"B foo <i>bar</i>", "B foo <i>bar</i>", // 1
43
"C foo <i> bar</i>", "C foo <i>bar</i>", // 2
44
"D <i>foo</i> bar", "D <i>foo</i> bar", // 3
45
"E <i>foo </i>bar", "E <i>foo </i>bar", // 4
46
"F <i>foo </i> bar", "F <i>foo </i>bar", // 5
47
"G <i>foo</i> <b>bar</b>", "G <i>foo</i> <b>bar</b>", // 6
48
"H <i>foo</i> <b> bar</b>", "H <i>foo</i> <b>bar</b>", // 7
49
"I <i>foo </i> <b> bar</b>", "I <i>foo </i><b>bar</b>",
50             "J <i>foo </i><b> bar</b>", "J <i>foo </i><b>bar</b>",
51             "K <b>foo </b> <i> bar </i>", "K <b>foo </b><i>bar</i>", // final trailing spaces need to be trimmed
52

53             "0 <i>foo </i><b> bar </b>", "0 <i>foo </i><b>bar</b>",
54             "1 foo <i>bar</i>", "1 foo <i>bar</i>",
55             "2 foo <i> bar</i> ", "2 foo <i>bar</i>",
56             "3 <i>foo</i> bar ", "3 <i>foo</i> bar",
57             "4 <i>foo </i>bar", "4 <i>foo </i>bar",
58             "5 <i>foo </i> bar ", "5 <i>foo </i>bar",
59             "6 <i>foo</i> <b>bar</b>", "6 <i>foo</i> <b>bar</b>",
60             "7 <i>foo</i> <b> bar </b>", "7 <i>foo</i> <b>bar</b>",
61             "8 <i>foo </i> <b> bar</b>", "8 <i>foo </i><b>bar</b>",
62             "9 <i>foo </i><b> bar</b>.", "9 <i>foo </i><b>bar</b>.",
63             "10 <i> foo </i> <b> bar </b> ", "10 <i>foo </i><b>bar</b>",
64             "11 <i> foo </i><b> bar </b> ", "11 <i>foo </i><b>bar</b>",
65             "12 <i> foo</i><b> bar </b> ", "12 <i>foo</i><b> bar</b>",
66
67             // complex test
68
"<b><i>BoldItalic</i>Bold</b><i>Italic</i><u>Underline<b>Bold Underline</b></u>", // ->
69
"<b><i>BoldItalic</i>Bold</b><i>Italic</i><u>Underline<b>Bold Underline</b></u>",
70
71             // font tags
72
"<font color=\"#FF0000\">C</font><font color=\"#FFFF00\">O</font><font color=\"#00FFCC\">L</font><font color=\"#CC00CC\">O</font><font color=\"#AABB00\">R</font><font color=\"#DDA00A\">S</font> ", "<font color=\"#FF0000\">C</font><font color=\"#FFFF00\">O</font><font color=\"#00FFCC\">L</font><font color=\"#CC00CC\">O</font><font color=\"#AABB00\">R</font><font color=\"#DDA00A\">S</font>",
73             "1 <font color=\"#ff0000\">foo <i>bar</i></font>", "1 <font color=\"#ff0000\">foo <i>bar</i></font>",
74             "2 <font color=\"#ff0000\">foo</font><font color=\"#00ffff\"> <i> bar</i></font>", "2 <font color=\"#ff0000\">foo</font><font color=\"#00ffff\"> <i>bar</i></font>",
75             "3 <font size=\"20\" color=\"#00ff00\"><i>foo</i></font><font color=\"#ff00aa\"> bar</font>", "3 <font size=\"20\" color=\"#00ff00\"><i>foo</i></font><font color=\"#ff00aa\"> bar</font>",
76
77
78             // entities
79
//"<b>x </b>&lt; y", "x &lt; y",
80
"&lt;b&gt;this text shouldn't be bold&lt;/b&gt;", "&lt;b&gt;this text shouldn&apos;t be bold&lt;/b&gt;",
81
82             // cdata
83
"<b>x </b><![CDATA[y]]> z", "<b>x </b>y z",
84             "<b>x</b> <![CDATA[y]]> z", "<b>x</b> y z",
85
86             "<b>x </b><![CDATA[a & b < c & d > e + f]]> z", "<b>x </b>a &amp; b &lt; c &amp; d &gt; e + f z",
87             "<![CDATA[<b>this text shouldn't be bold</b>]]>", "&lt;b&gt;this text shouldn&apos;t be bold&lt;/b&gt;",
88
89               " text" , "text",
90             "text " , "text",
91             " <b>x</b>" , "<b>x</b>",
92             "<b>x</b> " , "<b>x</b>",
93
94             "<![CDATA[ text]]>" , "text",
95
96             "<![CDATA[text ]]>" , "text",
97             " <b>x</b>" , "<b>x</b>",
98             "<b>x</b> " , "<b>x</b>",
99
100               "<![CDATA[ ]]><b>x</b>" , "<b>x</b>",
101             "<b>x</b><![CDATA[ ]]>" , "<b>x</b>",
102
103             // whitespace next to CDATA:
104
"<![CDATA[a]]>b" , "ab",
105             "<![CDATA[a ]]>b" , "a b",
106             "<![CDATA[a]]> b" , "a b",
107             "<![CDATA[a ]]> b" , "a b",
108             "a<![CDATA[b]]>" , "ab",
109             "a<![CDATA[ b]]>" , "a b",
110             "a <![CDATA[b]]>" , "a b",
111             "a <![CDATA[ b]]>" , "a b",
112
113             // empty cdata
114
"a<![CDATA[]]>b", "ab",
115             " a<![CDATA[]]>b", "ab",
116
117             // trim methods, and attributes
118
"some <attribute name=\"x\" value=\"10\"/> text", "some text",
119             "some <method name=\"f\" args=\"x\">return</method> text", "some text",
120             "some <state name=\"s\">state</state> text", "some text",
121             
122             // preserve attributes
123
"<a HREF=\"foo\">b</a>", null,
124             "<font face=\"face\" size=\"123\" color=\"red\">color</font>", null,
125             
126             // line breaks (TBD)
127

128             // more complex tests
129

130             "<b><i>BoldItalic</i>Bold&lt;&amp;&gt;</b><i>Italic</i><u>Underline<b>Bold Underline</b></u>", // ->
131
"<b><i>BoldItalic</i>Bold&lt;&amp;&gt;</b><i>Italic</i><u>Underline<b>Bold Underline</b></u>",
132
133         };
134         int num = 1;
135         for (Iterator iter = Arrays.asList(tests).iterator(); iter.hasNext(); num++) {
136             String JavaDoc source = (String JavaDoc) iter.next();
137             String JavaDoc result = (String JavaDoc) iter.next();
138             if (result == null) {
139                 result = source;
140             }
141             
142             try {
143                 org.jdom.input.SAXHandler handler = new org.jdom.input.SAXHandler();
144                 org.xml.sax.XMLReader JavaDoc reader =
145                     org.xml.sax.helpers.XMLReaderFactory.createXMLReader(
146                         "org.apache.xerces.parsers.SAXParser");
147                 reader.setContentHandler(handler);
148                 reader.parse(new org.xml.sax.InputSource JavaDoc(new StringReader("<text>" + source + "</text>")));
149                 Document doc = handler.getDocument();
150                 Element xml = doc.getRootElement();
151
152                 assertEquals("getHTMLContent(\"" + source + "\")",
153                              result,
154                              TextCompiler.getHTMLContent(xml));
155             } catch (IOException e) {
156                 throw new ChainedException(e);
157             } catch (org.xml.sax.SAXParseException JavaDoc e) {
158                 fail(e.getMessage());
159             } catch (org.xml.sax.SAXException JavaDoc e) {
160                 fail(e.getMessage());
161             }
162         }
163     }
164
165     /*public void testHTMLContentWithEntities () {
166         String XML2 =
167
168         try {
169             org.jdom.input.SAXHandler handler = new org.jdom.input.SAXHandler();
170             org.xml.sax.XMLReader reader =
171                 org.xml.sax.helpers.XMLReaderFactory.createXMLReader(
172                     "org.apache.xerces.parsers.SAXParser");
173             reader.setContentHandler(handler);
174             // ignore DOCTYPE declarations
175             reader.setEntityResolver(new org.xml.sax.helpers.DefaultHandler() {
176                     public InputSource resolveEntity(
177                         String publicId, String systemId)
178                     {
179                         return new InputSource(new StringReader(""));
180                     }
181                     });
182             reader.parse(new org.xml.sax.InputSource(new StringReader(XML2)));
183             Document doc = handler.getDocument();
184             Element xml = doc.getRootElement();
185
186             assertEquals("getHTMLContent",
187
188                          TextCompiler.getHTMLContent(xml)
189                          );
190
191         } catch (IOException e) {
192             throw new ChainedException(e);
193         } catch (org.xml.sax.SAXParseException e) {
194             fail(e.getMessage());
195         } catch (org.xml.sax.SAXException e) {
196             fail(e.getMessage());
197         }
198         }*/

199 }
200
201
Popular Tags