001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Vadim L. Bogdanov
020: * @version $Revision$
021: */package javax.swing.text.html;
022:
023: import java.io.IOException;
024: import java.io.StringReader;
025: import java.io.StringWriter;
026: import java.io.Writer;
027:
028: import javax.swing.SwingTestCase;
029: import javax.swing.text.AbstractDocument;
030: import javax.swing.text.Element;
031: import javax.swing.text.SimpleAttributeSet;
032: import javax.swing.text.StyleConstants;
033: import javax.swing.text.StyledEditorKit;
034:
035: public class HTMLWriterTest extends SwingTestCase {
036: private static final String HTML_TEXT = "normal <b>bold</b>";
037:
038: private static class TestHTMLDocument extends HTMLDocument {
039: public void callWriteLock() {
040: writeLock();
041: }
042:
043: public void callWriteUnlock() {
044: writeUnlock();
045: }
046: }
047:
048: private static class TestHTMLWriter extends HTMLWriter {
049: public TestHTMLWriter(final Writer w, final HTMLDocument doc) {
050: super (w, doc);
051: }
052:
053: public TestHTMLWriter(final Writer w, final HTMLDocument doc,
054: final int pos, final int len) {
055: super (w, doc, pos, len);
056: }
057:
058: protected void incrIndent() {
059: super .incrIndent();
060: }
061:
062: protected int getIndentLevel() {
063: return super .getIndentLevel();
064: }
065:
066: protected void setLineLength(final int len) {
067: super .setLineLength(len);
068: }
069:
070: protected boolean getCanWrapLines() {
071: return super .getCanWrapLines();
072: }
073:
074: protected int getLineLength() {
075: return super .getLineLength();
076: }
077: }
078:
079: private TestHTMLDocument doc;
080: private Element root;
081: private Element body;
082: private StringWriter out;
083: private TestHTMLWriter writer;
084:
085: public HTMLWriterTest(final String name) {
086: super (name);
087: }
088:
089: protected void setUp() throws Exception {
090: super .setUp();
091: setIgnoreNotImplemented(true);
092:
093: createDocument(HTML_TEXT);
094: out = new StringWriter();
095: writer = new TestHTMLWriter(out, doc);
096: writer.setLineSeparator("~");
097: }
098:
099: protected void tearDown() throws Exception {
100: super .tearDown();
101: }
102:
103: public void testWrite() throws Exception {
104: final String content = "<html>\n" + " <head>\n" + " \n"
105: + " </head>\n" + " <body>\n" + " <p>\n"
106: + " Body text. <i>Italic text</i>\n"
107: + " </p>\n" + " Text outside paragraphs.\n"
108: + " </body>\n" + "</html>\n" + "<!-- mycomment1 -->\n"
109: + "<!-- mycomment2 -->\n";
110: createDocument(content);
111: writer = new TestHTMLWriter(out, doc);
112: writer.write();
113: assertEquals(content, out.toString());
114: }
115:
116: public void testWriteLineSeparator() throws Exception {
117: doc.putProperty(StyledEditorKit.EndOfLineStringProperty, "`");
118: writer = new TestHTMLWriter(out, doc);
119: writer.writeLineSeparator();
120: assertEquals("`", out.toString());
121:
122: writer.setLineSeparator("~");
123: writer.writeLineSeparator();
124: assertEquals("`~", out.toString());
125: }
126:
127: public void testOutput() throws Exception {
128: String content = "abc<def";
129: writer.output(content.toCharArray(), 0, content.length());
130: assertEquals(content, out.toString());
131:
132: content = "abc<>def";
133: out = new StringWriter();
134: createDocument(content);
135: createWriter();
136: writer.text(body);
137: assertEquals(content, out.toString());
138: }
139:
140: public void testHTMLWriterWriterHTMLDocument() {
141: assertEquals(80, writer.getLineLength());
142: }
143:
144: public void testHTMLWriterWriterHTMLDocumentIntInt() {
145: assertEquals(80, writer.getLineLength());
146: }
147:
148: public void testComment() throws Exception {
149: SimpleAttributeSet attrs = new SimpleAttributeSet();
150: attrs.addAttribute(HTML.Tag.I, HTML.Tag.I);
151: attrs.addAttribute(HTML.Attribute.COMMENT, "comment body");
152:
153: doc.callWriteLock();
154: Element elem = doc.createBranchElement(body, attrs);
155: setTag(elem, HTML.Tag.COMMENT);
156: doc.callWriteUnlock();
157:
158: writer.incrIndent();
159: writer.writeEmbeddedTags(attrs);
160: writer.comment(elem);
161: assertEquals("<i><!--comment body-->~", out.toString());
162: }
163:
164: public void testEmptyTag() throws Exception {
165: SimpleAttributeSet attrs = new SimpleAttributeSet();
166: attrs.addAttribute(HTML.Tag.I, HTML.Tag.I);
167:
168: writer.incrIndent();
169: writer.writeEmbeddedTags(attrs);
170: setTag(body, HTML.Tag.CONTENT);
171: writer.emptyTag(body);
172: writer.emptyTag(body);
173: assertEquals("<i> </i>normal boldnormal bold", out.toString());
174:
175: out = new StringWriter();
176: writer = new TestHTMLWriter(out, doc);
177: writer.setLineSeparator("~");
178: writer.incrIndent();
179: writer.writeEmbeddedTags(attrs);
180: setTag(body, HTML.Tag.COMMENT);
181: writer.emptyTag(body);
182: assertEquals("<i> </i><!---->~", out.toString());
183:
184: setTag(body, HTML.Tag.P);
185: out = new StringWriter();
186: writer = new TestHTMLWriter(out, doc);
187: writer.setLineSeparator("~");
188: writer.incrIndent();
189: writer.writeEmbeddedTags(attrs);
190: writer.emptyTag(body);
191: assertEquals("<i> </i><p>~", out.toString());
192: }
193:
194: public void testIsBlockTag() {
195: SimpleAttributeSet attrs = new SimpleAttributeSet();
196: assertFalse(writer.isBlockTag(attrs));
197:
198: attrs.addAttribute(StyleConstants.NameAttribute, HTML.Tag.BODY);
199: assertTrue(writer.isBlockTag(attrs));
200:
201: attrs.addAttribute(StyleConstants.NameAttribute, HTML.Tag.I);
202: assertFalse(writer.isBlockTag(attrs));
203: }
204:
205: public void testMatchNameAttribute() {
206: SimpleAttributeSet attrs = new SimpleAttributeSet();
207: assertFalse(writer.matchNameAttribute(attrs, null));
208: assertFalse(writer.matchNameAttribute(attrs, HTML.Tag.BODY));
209:
210: attrs.addAttribute(StyleConstants.NameAttribute, HTML.Tag.I);
211: assertTrue(writer.matchNameAttribute(attrs, HTML.Tag.I));
212: assertFalse(writer.matchNameAttribute(attrs, HTML.Tag.BODY));
213: assertFalse(writer.matchNameAttribute(attrs, null));
214: }
215:
216: public void testStartTag() throws Exception {
217: SimpleAttributeSet attrs = new SimpleAttributeSet();
218: attrs.addAttribute(HTML.Tag.I, HTML.Tag.I);
219: writer.writeEmbeddedTags(attrs);
220: setTag(body, HTML.Tag.P);
221: writer.incrIndent();
222: writer.startTag(body);
223: assertEquals("<i></i> <p>~", out.toString());
224:
225: out = new StringWriter();
226: writer = new TestHTMLWriter(out, doc);
227: writer.setLineSeparator("~");
228: writer.writeEmbeddedTags(attrs);
229: setTag(body, HTML.Tag.IMPLIED);
230: writer.incrIndent();
231: writer.startTag(body);
232: assertEquals("<i>", out.toString());
233:
234: out = new StringWriter();
235: writer = new TestHTMLWriter(out, doc);
236: writer.setLineSeparator("~");
237: writer.writeEmbeddedTags(attrs);
238: setTag(body, HTML.Tag.BODY);
239: writer.incrIndent();
240: writer.setLineLength(5);
241: writer.startTag(body);
242: assertEquals("<i></i> <head>~~ </head>~ <body>~", out
243: .toString());
244: }
245:
246: public void testEndTag() throws Exception {
247: SimpleAttributeSet attrs = new SimpleAttributeSet();
248: attrs.addAttribute(HTML.Tag.I, HTML.Tag.I);
249: writer.writeEmbeddedTags(attrs);
250: writer.incrIndent();
251: writer.endTag(body);
252: assertEquals("<i></i> </body>~", out.toString());
253:
254: out = new StringWriter();
255: writer = new TestHTMLWriter(out, doc);
256: writer.setLineSeparator("~");
257: writer.writeEmbeddedTags(attrs);
258: setTag(body, HTML.Tag.IMPLIED);
259: writer.incrIndent();
260: writer.endTag(body);
261: assertEquals("<i>", out.toString());
262: }
263:
264: public void testSynthesizedElement() {
265: assertFalse(writer.synthesizedElement(root));
266:
267: setTag(root, HTML.Tag.BODY);
268: assertFalse(writer.synthesizedElement(root));
269:
270: setTag(root, HTML.Tag.IMPLIED);
271: assertTrue(writer.synthesizedElement(root));
272: }
273:
274: public void testText() throws Exception {
275: String content = "abc<> def";
276: createDocument(content);
277: createWriter();
278: writer.setLineLength(7);
279: writer.incrIndent();
280: writer.text(body);
281: assertEquals("abc<> ~ def", out.toString());
282:
283: out = new StringWriter();
284: createWriter();
285:
286: writer.setLineLength(7);
287: writer.incrIndent();
288: setTag(body, HTML.Tag.PRE);
289: writer.startTag(body);
290: writer.text(body);
291: assertEquals(" <pre>abc<> def", out.toString());
292: }
293:
294: public void testSelectContent() throws Exception {
295: String content = "<select>\n <option selected>Component1</option>\n"
296: + " <option>Component2</option>\n</select>";
297: createDocument(content);
298: createWriter();
299: Element elem = doc.getElement(body,
300: StyleConstants.NameAttribute, HTML.Tag.SELECT);
301: writer.selectContent(elem.getAttributes());
302: assertEquals(
303: " <option selected>Component1~ <option>Component2~",
304: out.toString());
305:
306: content = "<select multiple>\n <option selected>Component1</option>\n"
307: + " <option>Component2</option>\n</select>";
308: createDocument(content);
309: out = new StringWriter();
310: createWriter();
311: elem = doc.getElement(body, StyleConstants.NameAttribute,
312: HTML.Tag.SELECT);
313: writer.selectContent(elem.getAttributes());
314: assertEquals(
315: " <option selected>Component1~ <option>Component2~",
316: out.toString());
317:
318: if (isHarmony()) {
319: content = "<select>\n <optgroup label=optgr>\n"
320: + " <option selected>Comp1</option>\n"
321: + " <option>Comp2</option>\n </optgroup>\n</select>";
322: createDocument(content);
323: out = new StringWriter();
324: createWriter();
325: elem = doc.getElement(body, StyleConstants.NameAttribute,
326: HTML.Tag.SELECT);
327: writer.selectContent(elem.getAttributes());
328: assertEquals(" <optgroup label=\"optgr\">~"
329: + " <option selected>Comp1~ <option>Comp2~"
330: + " </optgroup>", out.toString());
331: }
332: }
333:
334: public void testTextAreaContent() throws Exception {
335: String content = "<textarea>\n First line<.\n"
336: + " Second line.\n </textarea>";
337: createDocument(content);
338: createWriter();
339:
340: Element elem = doc.getElement(body,
341: StyleConstants.NameAttribute, HTML.Tag.TEXTAREA);
342: writer.textAreaContent(elem.getAttributes());
343: assertEquals(" First line<.~ Second line.~ ~",
344: out.toString());
345: }
346:
347: public void testWriteAttributes() throws IOException {
348: SimpleAttributeSet attrs = new SimpleAttributeSet();
349: attrs.addAttribute(HTML.Tag.H1, HTML.Tag.H2);
350: attrs.addAttribute(StyleConstants.Bold, StyleConstants.Bold);
351: attrs
352: .addAttribute(HTML.Attribute.ENDTAG,
353: HTML.Attribute.ENDTAG);
354: attrs.addAttribute(HTML.Attribute.COLOR, "red");
355: attrs.addAttribute(CSS.Attribute.MARGIN, new Integer(10));
356:
357: writer.writeAttributes(attrs);
358: assertEquals(" style=\"margin: 10\" color=\"red\"", out
359: .toString());
360: }
361:
362: public void testWriteEmbeddedTags() throws IOException {
363: SimpleAttributeSet attrs = new SimpleAttributeSet();
364: attrs.addAttribute(HTML.Tag.I, HTML.Tag.B);
365: attrs.addAttribute(HTML.Tag.H1, HTML.Tag.H2);
366:
367: writer.writeEmbeddedTags(attrs);
368: writer.writeEmbeddedTags(attrs);
369: assertTrue("<i><h1>".equals(out.toString())
370: || "<h1><i>".equals(out.toString()));
371: }
372:
373: public void testCloseOutUnwantedEmbeddedTags() throws IOException {
374: SimpleAttributeSet attrs = new SimpleAttributeSet();
375: attrs.addAttribute(HTML.Tag.I, HTML.Tag.B);
376: attrs.addAttribute(HTML.Tag.H1, HTML.Tag.H2);
377: writer.writeEmbeddedTags(attrs);
378:
379: SimpleAttributeSet attrs2 = new SimpleAttributeSet();
380: attrs2.addAttribute(HTML.Tag.I, HTML.Tag.B);
381: writer.closeOutUnwantedEmbeddedTags(attrs2);
382: assertTrue("<i><h1></h1>".equals(out.toString())
383: || "<h1><i></i></h1>".equals(out.toString()));
384:
385: out = new StringWriter();
386: createWriter();
387: writer.writeEmbeddedTags(attrs);
388: attrs2.addAttribute(HTML.Tag.H1, HTML.Tag.H2);
389: writer.closeOutUnwantedEmbeddedTags(attrs2);
390: assertTrue("<i><h1>".equals(out.toString())
391: || "<h1><i>".equals(out.toString()));
392: }
393:
394: public void testWriteOption() throws IOException {
395: writer.incrIndent();
396: SimpleAttributeSet attrs = new SimpleAttributeSet();
397: Option option = new Option(attrs);
398: writer.writeOption(option);
399: assertEquals(" <option>~", out.toString());
400:
401: out = new StringWriter();
402: createWriter();
403: writer.incrIndent();
404: attrs.addAttribute(HTML.Attribute.SELECTED, Boolean
405: .valueOf(true));
406: option = new Option(attrs);
407: writer.writeOption(option);
408: assertEquals(" <option selected>~", out.toString());
409:
410: out = new StringWriter();
411: createWriter();
412: writer.incrIndent();
413: attrs.addAttribute(HTML.Attribute.VALUE, "option_value");
414: option = new Option(attrs);
415: writer.writeOption(option);
416: assertEquals(" <option value=option_value selected>~", out
417: .toString());
418:
419: out = new StringWriter();
420: createWriter();
421: attrs.addAttribute(HTML.Attribute.VALUE, "");
422: option = new Option(attrs);
423: option.setLabel("option_label");
424: writer.incrIndent();
425: writer.writeOption(option);
426: assertEquals(" <option value= selected>option_label~", out
427: .toString());
428: }
429:
430: public void testWriteDocumentBase() throws Exception {
431: if (!isHarmony()) {
432: return;
433: }
434:
435: String content = "<html><head><base href=\"http://my.site.com/index.html\""
436: + "</head></html>";
437: createDocument(content);
438: createWriter();
439:
440: writer.write();
441:
442: assertEquals(
443: "<html>~ <head>~ <base href=\"http://my.site.com/index.html\"> ~"
444: + " </head>~</html>~", out.toString());
445: }
446:
447: private void createDocument(final String content) throws Exception {
448: doc = new TestHTMLDocument();
449: doc.setAsynchronousLoadPriority(-1); // synchronous loading
450:
451: new HTMLEditorKit().read(new StringReader(content), doc, 0);
452:
453: root = doc.getDefaultRootElement();
454: body = doc.getElement(root, StyleConstants.NameAttribute,
455: HTML.Tag.BODY);
456: }
457:
458: private void createWriter() {
459: writer = new TestHTMLWriter(out, doc);
460: writer.setLineSeparator("~");
461: }
462:
463: private void setTag(final Element elem, final HTML.Tag tag) {
464: doc.callWriteLock();
465: ((AbstractDocument.AbstractElement) elem).addAttribute(
466: StyleConstants.NameAttribute, tag);
467: doc.callWriteUnlock();
468: }
469: }
|