001: /* ****************************************************************************
002: * TextCompiler_Test.java
003: * ****************************************************************************/
004:
005: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
006: * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
007: * Use is subject to license terms. *
008: * J_LZ_COPYRIGHT_END *********************************************************/
009:
010: package org.openlaszlo.compiler;
011:
012: import java.io.*;
013: import java.util.*;
014: import junit.framework.*;
015: import org.openlaszlo.utils.ChainedException;
016: import org.jdom.Element;
017: import org.jdom.Document;
018: import org.jdom.input.SAXBuilder;
019: import org.xml.sax.InputSource;
020:
021: /*
022: * junit.awtui.TestRunner
023: * junit.swingui.TestRunner
024: * junit.textui.TestRunner
025: * java junit.textui.TestRunner org.openlaszlo.compiler.TextCompiler_Test
026: */
027:
028: public class TextCompiler_Test extends TestCase {
029: public TextCompiler_Test(String name) {
030: super (name);
031: }
032:
033: public void setUp() {
034: }
035:
036: public void testHTMLContent() {
037: String[] tests = {
038: // Each case is input, expected-output (null == same as input)
039:
040: // whitespace
041: "A foo <i></i>",
042: "A foo<i></i>", // 0
043: "B foo <i>bar</i>",
044: "B foo <i>bar</i>", // 1
045: "C foo <i> bar</i>",
046: "C foo <i>bar</i>", // 2
047: "D <i>foo</i> bar",
048: "D <i>foo</i> bar", // 3
049: "E <i>foo </i>bar",
050: "E <i>foo </i>bar", // 4
051: "F <i>foo </i> bar",
052: "F <i>foo </i>bar", // 5
053: "G <i>foo</i> <b>bar</b>",
054: "G <i>foo</i> <b>bar</b>", // 6
055: "H <i>foo</i> <b> bar</b>",
056: "H <i>foo</i> <b>bar</b>", // 7
057: "I <i>foo </i> <b> bar</b>",
058: "I <i>foo </i><b>bar</b>",
059: "J <i>foo </i><b> bar</b>",
060: "J <i>foo </i><b>bar</b>",
061: "K <b>foo </b> <i> bar </i>",
062: "K <b>foo </b><i>bar</i>", // final trailing spaces need to be trimmed
063:
064: "0 <i>foo </i><b> bar </b>",
065: "0 <i>foo </i><b>bar</b>",
066: "1 foo <i>bar</i>",
067: "1 foo <i>bar</i>",
068: "2 foo <i> bar</i> ",
069: "2 foo <i>bar</i>",
070: "3 <i>foo</i> bar ",
071: "3 <i>foo</i> bar",
072: "4 <i>foo </i>bar",
073: "4 <i>foo </i>bar",
074: "5 <i>foo </i> bar ",
075: "5 <i>foo </i>bar",
076: "6 <i>foo</i> <b>bar</b>",
077: "6 <i>foo</i> <b>bar</b>",
078: "7 <i>foo</i> <b> bar </b>",
079: "7 <i>foo</i> <b>bar</b>",
080: "8 <i>foo </i> <b> bar</b>",
081: "8 <i>foo </i><b>bar</b>",
082: "9 <i>foo </i><b> bar</b>.",
083: "9 <i>foo </i><b>bar</b>.",
084: "10 <i> foo </i> <b> bar </b> ",
085: "10 <i>foo </i><b>bar</b>",
086: "11 <i> foo </i><b> bar </b> ",
087: "11 <i>foo </i><b>bar</b>",
088: "12 <i> foo</i><b> bar </b> ",
089: "12 <i>foo</i><b> bar</b>",
090:
091: // complex test
092: "<b><i>BoldItalic</i>Bold</b><i>Italic</i><u>Underline<b>Bold Underline</b></u>", // ->
093: "<b><i>BoldItalic</i>Bold</b><i>Italic</i><u>Underline<b>Bold Underline</b></u>",
094:
095: // font tags
096: "<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> ",
097: "<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>",
098: "1 <font color=\"#ff0000\">foo <i>bar</i></font>",
099: "1 <font color=\"#ff0000\">foo <i>bar</i></font>",
100: "2 <font color=\"#ff0000\">foo</font><font color=\"#00ffff\"> <i> bar</i></font>",
101: "2 <font color=\"#ff0000\">foo</font><font color=\"#00ffff\"> <i>bar</i></font>",
102: "3 <font size=\"20\" color=\"#00ff00\"><i>foo</i></font><font color=\"#ff00aa\"> bar</font>",
103: "3 <font size=\"20\" color=\"#00ff00\"><i>foo</i></font><font color=\"#ff00aa\"> bar</font>",
104:
105: // entities
106: //"<b>x </b>< y", "x < y",
107: "<b>this text shouldn't be bold</b>",
108: "<b>this text shouldn't be bold</b>",
109:
110: // cdata
111: "<b>x </b><![CDATA[y]]> z",
112: "<b>x </b>y z",
113: "<b>x</b> <![CDATA[y]]> z",
114: "<b>x</b> y z",
115:
116: "<b>x </b><![CDATA[a & b < c & d > e + f]]> z",
117: "<b>x </b>a & b < c & d > e + f z",
118: "<![CDATA[<b>this text shouldn't be bold</b>]]>",
119: "<b>this text shouldn't be bold</b>",
120:
121: " text",
122: "text",
123: "text ",
124: "text",
125: " <b>x</b>",
126: "<b>x</b>",
127: "<b>x</b> ",
128: "<b>x</b>",
129:
130: "<![CDATA[ text]]>",
131: "text",
132:
133: "<![CDATA[text ]]>",
134: "text",
135: " <b>x</b>",
136: "<b>x</b>",
137: "<b>x</b> ",
138: "<b>x</b>",
139:
140: "<![CDATA[ ]]><b>x</b>",
141: "<b>x</b>",
142: "<b>x</b><![CDATA[ ]]>",
143: "<b>x</b>",
144:
145: // whitespace next to CDATA:
146: "<![CDATA[a]]>b",
147: "ab",
148: "<![CDATA[a ]]>b",
149: "a b",
150: "<![CDATA[a]]> b",
151: "a b",
152: "<![CDATA[a ]]> b",
153: "a b",
154: "a<![CDATA[b]]>",
155: "ab",
156: "a<![CDATA[ b]]>",
157: "a b",
158: "a <![CDATA[b]]>",
159: "a b",
160: "a <![CDATA[ b]]>",
161: "a b",
162:
163: // empty cdata
164: "a<![CDATA[]]>b",
165: "ab",
166: " a<![CDATA[]]>b",
167: "ab",
168:
169: // trim methods, and attributes
170: "some <attribute name=\"x\" value=\"10\"/> text",
171: "some text",
172: "some <method name=\"f\" args=\"x\">return</method> text",
173: "some text",
174: "some <state name=\"s\">state</state> text",
175: "some text",
176:
177: // preserve attributes
178: "<a href=\"foo\">b</a>",
179: null,
180: "<font face=\"face\" size=\"123\" color=\"red\">color</font>",
181: null,
182:
183: // line breaks (TBD)
184:
185: // more complex tests
186:
187: "<b><i>BoldItalic</i>Bold<&></b><i>Italic</i><u>Underline<b>Bold Underline</b></u>", // ->
188: "<b><i>BoldItalic</i>Bold<&></b><i>Italic</i><u>Underline<b>Bold Underline</b></u>",
189:
190: };
191: int num = 1;
192: for (Iterator iter = Arrays.asList(tests).iterator(); iter
193: .hasNext(); num++) {
194: String source = (String) iter.next();
195: String result = (String) iter.next();
196: if (result == null) {
197: result = source;
198: }
199:
200: try {
201: org.jdom.input.SAXHandler handler = new org.jdom.input.SAXHandler();
202: org.xml.sax.XMLReader reader = org.xml.sax.helpers.XMLReaderFactory
203: .createXMLReader("org.apache.xerces.parsers.SAXParser");
204: reader.setContentHandler(handler);
205: reader
206: .parse(new org.xml.sax.InputSource(
207: new StringReader("<text>" + source
208: + "</text>")));
209: Document doc = handler.getDocument();
210: Element xml = doc.getRootElement();
211:
212: assertEquals("getHTMLContent(\"" + source + "\")",
213: result, TextCompiler.getHTMLContent(xml));
214: } catch (IOException e) {
215: throw new ChainedException(e);
216: } catch (org.xml.sax.SAXParseException e) {
217: fail(e.getMessage());
218: } catch (org.xml.sax.SAXException e) {
219: fail(e.getMessage());
220: }
221: }
222: }
223:
224: /*public void testHTMLContentWithEntities () {
225: String XML2 =
226:
227: try {
228: org.jdom.input.SAXHandler handler = new org.jdom.input.SAXHandler();
229: org.xml.sax.XMLReader reader =
230: org.xml.sax.helpers.XMLReaderFactory.createXMLReader(
231: "org.apache.xerces.parsers.SAXParser");
232: reader.setContentHandler(handler);
233: // ignore DOCTYPE declarations
234: reader.setEntityResolver(new org.xml.sax.helpers.DefaultHandler() {
235: public InputSource resolveEntity(
236: String publicId, String systemId)
237: {
238: return new InputSource(new StringReader(""));
239: }
240: });
241: reader.parse(new org.xml.sax.InputSource(new StringReader(XML2)));
242: Document doc = handler.getDocument();
243: Element xml = doc.getRootElement();
244:
245: assertEquals("getHTMLContent",
246:
247: TextCompiler.getHTMLContent(xml)
248: );
249:
250: } catch (IOException e) {
251: throw new ChainedException(e);
252: } catch (org.xml.sax.SAXParseException e) {
253: fail(e.getMessage());
254: } catch (org.xml.sax.SAXException e) {
255: fail(e.getMessage());
256: }
257: }*/
258: }
|