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: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: */package javax.swing.text.html;
021:
022: import java.net.MalformedURLException;
023: import java.net.URL;
024: import java.util.ArrayList;
025: import java.util.Enumeration;
026:
027: import javax.swing.event.DocumentEvent;
028: import javax.swing.event.HyperlinkEvent;
029: import javax.swing.text.AbstractDocument;
030: import javax.swing.text.AttributeSet;
031: import javax.swing.text.BadLocationException;
032: import javax.swing.text.DefaultStyledDocumentTest;
033: import javax.swing.text.Element;
034: import javax.swing.text.GapContent;
035: import javax.swing.text.SimpleAttributeSet;
036: import javax.swing.text.Style;
037: import javax.swing.text.StyleConstants;
038: import javax.swing.text.AbstractDocument.AttributeContext;
039: import javax.swing.text.AbstractDocument.Content;
040: import javax.swing.text.DefaultStyledDocument.ElementSpec;
041: import javax.swing.text.html.HTML.Tag;
042: import javax.swing.text.html.HTMLDocument.BlockElement;
043: import javax.swing.text.html.HTMLDocument.Iterator;
044: import javax.swing.text.html.HTMLDocument.RunElement;
045: import javax.swing.text.html.HTMLDocumentTestCase.DocumentController;
046: import javax.swing.text.html.HTMLDocumentTestCase.PublicHTMLDocument;
047: import javax.swing.text.html.parser.ParserDelegator;
048:
049: public class HTMLDocumentTest extends DefaultStyledDocumentTest {
050:
051: protected PublicHTMLDocument htmlDoc;
052: private Marker insertMarker;
053:
054: protected void setUp() throws Exception {
055: super .setUp();
056: setIgnoreNotImplemented(true);
057: htmlDoc = new PublicHTMLDocument();
058: timeoutDelay = Integer.MAX_VALUE;
059: insertMarker = htmlDoc.getInsertMarker();
060: doc = htmlDoc;
061: }
062:
063: protected void tearDown() throws Exception {
064: htmlDoc = null;
065: super .tearDown();
066: }
067:
068: public void testHTMLDocumentContentStyleSheet()
069: throws MalformedURLException {
070: StyleSheet styles = new StyleSheet();
071: final GapContent gapContent = new GapContent(10);
072: htmlDoc = new PublicHTMLDocument(gapContent, styles);
073: assertSame(styles, htmlDoc.getAttributeContextPublicly());
074: assertSame(gapContent, htmlDoc.getContentPublicly());
075:
076: URL u1 = new URL("http://www.apache.org");
077: styles.setBase(u1);
078: htmlDoc = new PublicHTMLDocument(gapContent, styles);
079: assertNull(htmlDoc.getBase());
080: }
081:
082: public void testHTMLDocumentStyleSheet()
083: throws BadLocationException, MalformedURLException {
084: StyleSheet styles = new StyleSheet();
085: htmlDoc = new PublicHTMLDocument(styles);
086: assertSame(styles, htmlDoc.getAttributeContextPublicly());
087: final Content content = htmlDoc.getContentPublicly();
088: assertTrue(content instanceof GapContent);
089:
090: URL u1 = new URL("http://www.apache.org");
091: styles.setBase(u1);
092: htmlDoc = new PublicHTMLDocument(styles);
093: assertNull(htmlDoc.getBase());
094: }
095:
096: public void testHTMLDocument() {
097: htmlDoc = new PublicHTMLDocument();
098: assertTrue(htmlDoc.getContentPublicly() instanceof GapContent);
099: AttributeContext styleSheet = htmlDoc
100: .getAttributeContextPublicly();
101: assertTrue(styleSheet instanceof StyleSheet);
102: final Enumeration styleNames = ((StyleSheet) styleSheet)
103: .getStyleNames();
104: assertTrue(styleNames.hasMoreElements());
105: assertEquals("default", styleNames.nextElement());
106: assertFalse(styleNames.hasMoreElements());
107: final Style style = ((StyleSheet) styleSheet)
108: .getStyle("default");
109: assertEquals(1, style.getAttributeCount());
110: assertEquals("default", style
111: .getAttribute(StyleConstants.NameAttribute));
112: }
113:
114: public void testCreateLeafElement() throws BadLocationException {
115: Element leaf = htmlDoc.createLeafElement(null, null, 0, 1);
116: assertTrue(leaf instanceof HTMLDocument.RunElement);
117: assertNull(leaf.getParentElement());
118: assertEquals(0, leaf.getStartOffset());
119: assertEquals(1, leaf.getEndOffset());
120: assertNotSame(htmlDoc.createLeafElement(null, null, 0, 1),
121: htmlDoc.createLeafElement(null, null, 0, 1));
122:
123: htmlDoc.insertString(0, "01234", null);
124:
125: Element leaf2 = htmlDoc.createLeafElement(leaf, null, 1, 3);
126: assertTrue(leaf2 instanceof HTMLDocument.RunElement);
127: assertSame(leaf, leaf2.getParentElement());
128: assertEquals(1, leaf2.getStartOffset());
129: assertEquals(3, leaf2.getEndOffset());
130:
131: htmlDoc.remove(0, 5);
132: assertEquals(0, leaf2.getStartOffset());
133: assertEquals(0, leaf2.getEndOffset());
134: }
135:
136: public void testCreateBranchElement() {
137: Element branch = htmlDoc.createBranchElement(null, null);
138: assertTrue(branch instanceof HTMLDocument.BlockElement);
139: assertNull(branch.getParentElement());
140:
141: assertNull(branch.getElement(0));
142: assertNull(branch.getElement(1));
143:
144: assertEquals(0, branch.getElementCount());
145:
146: // Since this branch element has no children yet, it has no start and
147: // end offsets. Thus calling get{Start,End}Offset on an empty branch
148: // element causes the exception being thrown.
149: if (isHarmony()) {
150: try {
151: assertEquals(0, branch.getStartOffset());
152:
153: fail("getStartOffset on an empty BranchElement "
154: + "causes exception");
155: } catch (ArrayIndexOutOfBoundsException e) {
156: }
157: try {
158: assertEquals(1, branch.getEndOffset());
159:
160: fail("getEndOffset on an empty BranchElement causes exception");
161: } catch (ArrayIndexOutOfBoundsException e) {
162: }
163: } else {
164: try {
165: assertEquals(0, branch.getStartOffset());
166:
167: fail("getStartOffset on an empty BranchElement "
168: + "causes exception");
169: } catch (NullPointerException e) {
170: }
171: try {
172: assertEquals(1, branch.getEndOffset());
173:
174: fail("getEndOffset on an empty BranchElement causes exception");
175: } catch (NullPointerException e) {
176: }
177: }
178: }
179:
180: public void testCreateDefaultRoot() {
181: try {
182: htmlDoc.insertString(0, "123", null);
183: } catch (BadLocationException e) {
184: }
185:
186: final Element root = htmlDoc.createDefaultRoot();
187: assertSame(htmlDoc, root.getDocument());
188: assertTrue(root instanceof BlockElement);
189: AttributeSet attributes = root.getAttributes();
190: assertNotNull(attributes);
191: assertEquals(1, attributes.getAttributeCount());
192: assertEquals(StyleConstants.NameAttribute, attributes
193: .getAttributeNames().nextElement());
194: assertEquals(Tag.HTML, attributes
195: .getAttribute(StyleConstants.NameAttribute));
196: assertEquals("html", root.getName());
197: assertNull(root.getParentElement());
198: assertNull(((BlockElement) root).getResolveParent());
199:
200: assertEquals(1, root.getElementCount());
201: assertTrue(root.getElement(0) instanceof BlockElement);
202: assertSame(root, root.getElement(0).getParentElement());
203: Element child = root.getElement(0);
204: attributes = child.getAttributes();
205: assertNotNull(attributes);
206: assertEquals(1, attributes.getAttributeCount());
207: assertEquals(StyleConstants.NameAttribute, child
208: .getAttributes().getAttributeNames().nextElement());
209: assertEquals(Tag.BODY, attributes
210: .getAttribute(StyleConstants.NameAttribute));
211: assertEquals("body", child.getName());
212: assertNull(((BlockElement) child).getResolveParent());
213:
214: assertEquals(1, child.getElementCount());
215: assertTrue(child.getElement(0) instanceof BlockElement);
216: assertSame(child, child.getElement(0).getParentElement());
217: child = child.getElement(0);
218: attributes = child.getAttributes();
219: assertNotNull(attributes);
220: assertEquals(2, attributes.getAttributeCount());
221: checkAttributes(attributes, StyleConstants.NameAttribute, Tag.P);
222: checkAttributes(attributes, CSS.Attribute.MARGIN_TOP, "0");
223: assertEquals("p", child.getName());
224: assertNull(((BlockElement) child).getResolveParent());
225:
226: assertEquals(1, child.getElementCount());
227: assertTrue(child.getElement(0) instanceof RunElement);
228: assertSame(child, child.getElement(0).getParentElement());
229: child = child.getElement(0);
230: attributes = child.getAttributes();
231: assertNotNull(attributes);
232: assertEquals(1, attributes.getAttributeCount());
233: assertEquals(StyleConstants.NameAttribute, child
234: .getAttributes().getAttributeNames().nextElement());
235: assertEquals(Tag.CONTENT, attributes
236: .getAttribute(StyleConstants.NameAttribute));
237: assertEquals("content", child.getName());
238: assertNull(((RunElement) child).getResolveParent());
239: if (!isHarmony()) {
240: assertEquals(0, child.getStartOffset());
241: assertEquals(1, child.getEndOffset());
242: } else {
243: assertEquals(0, child.getStartOffset());
244: assertEquals(4, child.getEndOffset());
245: }
246: }
247:
248: public void testGetElementElementObjectObject()
249: throws BadLocationException {
250: final Element root = htmlDoc.getDefaultRootElement();
251: final String value = "ASD";
252: assertNull(htmlDoc.getElement(root, HTML.Attribute.ID, value));
253: final SimpleAttributeSet attr = new SimpleAttributeSet();
254: attr.addAttribute(HTML.Attribute.NAME, Tag.B.toString());
255: attr.addAttribute(HTML.Attribute.ID, value);
256:
257: htmlDoc.insertString(0, "0000", attr);
258: Element child1 = root.getElement(0).getElement(0).getElement(0);
259: assertSame(child1, htmlDoc.getElement(root, HTML.Attribute.ID,
260: value));
261: assertSame(child1, htmlDoc.getElement(root,
262: HTML.Attribute.NAME, Tag.B.toString()));
263: assertNull(htmlDoc.getElement(root, HTML.Attribute.ID, "AAA"));
264: assertNull(htmlDoc.getElement(htmlDoc.getRootElements()[1],
265: HTML.Attribute.ID, "AAA"));
266:
267: attr.addAttribute(HTML.Attribute.NAME, Tag.I.toString());
268: htmlDoc.insertString(4, "0000", attr);
269: child1 = root.getElement(0).getElement(0).getElement(0);
270: Element child2 = root.getElement(0).getElement(0).getElement(1);
271: assertSame(child1, htmlDoc.getElement(root, HTML.Attribute.ID,
272: value));
273: assertSame(child1, htmlDoc.getElement(root,
274: HTML.Attribute.NAME, Tag.B.toString()));
275: assertSame(child2, htmlDoc.getElement(root,
276: HTML.Attribute.NAME, Tag.I.toString()));
277: assertNull(htmlDoc.getElement(root, HTML.Attribute.ID, "AAA"));
278: assertNull(htmlDoc.getElement(htmlDoc.getRootElements()[1],
279: HTML.Attribute.ID, "AAA"));
280: }
281:
282: public void testGetElementString() throws Exception {
283: final Element root = htmlDoc.getDefaultRootElement();
284: final String value = "B";
285: assertNull(htmlDoc.getElement(value));
286: SimpleAttributeSet attr = new SimpleAttributeSet();
287: attr.addAttribute(StyleConstants.NameAttribute, value);
288: htmlDoc.insertString(0, "0000", attr);
289: attr = new SimpleAttributeSet();
290: attr.addAttribute(StyleConstants.NameAttribute, Tag.P
291: .toString());
292: attr.addAttribute(HTML.Attribute.ID, value);
293: htmlDoc.insertString(0, "0000", attr);
294: assertSame(root.getElement(0).getElement(0).getElement(0),
295: htmlDoc.getElement(value));
296: assertNull(htmlDoc.getElement("AAA"));
297: }
298:
299: public void testGetIterator() throws BadLocationException {
300: final SimpleAttributeSet attr = new SimpleAttributeSet();
301: attr.addAttribute(StyleConstants.NameAttribute, Tag.B
302: .toString());
303: htmlDoc.insertString(0, "0000", attr);
304: StyleConstants.setItalic(attr, true);
305: htmlDoc.insertString(4, "1111", attr);
306:
307: final Iterator iterator1 = htmlDoc.getIterator(Tag.HTML);
308: final Iterator iterator2 = htmlDoc.getIterator(Tag.HTML);
309: final Iterator iterator3 = htmlDoc.getIterator(Tag.A);
310: final Iterator iterator4 = htmlDoc.getIterator(Tag.P);
311: final Iterator iterator5 = htmlDoc.getIterator(Tag.B);
312: assertNotNull(iterator1);
313: assertNotNull(iterator2);
314: assertNotNull(iterator3);
315: if (isHarmony()) {
316: assertNotNull(iterator4);
317: }
318: assertNotNull(iterator5);
319: assertNotSame(iterator1, iterator2);
320: assertNotSame(iterator2, iterator3);
321: assertNotSame(iterator3, iterator5);
322:
323: assertEquals(Tag.HTML, iterator1.getTag());
324: if (isHarmony()) {
325: assertTrue(iterator1.isValid());
326: assertEquals(0, iterator1.getStartOffset());
327: assertEquals(9, iterator1.getEndOffset());
328: assertNotNull(iterator1.getAttributes());
329: iterator1.next();
330: assertFalse(iterator1.isValid());
331: assertEquals(-1, iterator1.getStartOffset());
332: assertEquals(-1, iterator1.getEndOffset());
333: assertNull(iterator1.getAttributes());
334: }
335:
336: assertEquals(Tag.HTML, iterator2.getTag());
337: if (isHarmony()) {
338: assertTrue(iterator2.isValid());
339: assertEquals(0, iterator2.getStartOffset());
340: assertEquals(9, iterator2.getEndOffset());
341: assertNotNull(iterator2.getAttributes());
342: iterator2.next();
343: assertFalse(iterator2.isValid());
344: assertEquals(-1, iterator2.getStartOffset());
345: assertEquals(-1, iterator2.getEndOffset());
346: assertNull(iterator2.getAttributes());
347: }
348:
349: assertEquals(Tag.A, iterator3.getTag());
350: if (isHarmony()) {
351: assertFalse(iterator3.isValid());
352: assertNull(iterator3.getAttributes());
353: iterator3.next();
354: assertEquals(-1, iterator3.getStartOffset());
355: assertEquals(-1, iterator3.getEndOffset());
356: }
357:
358: if (isHarmony()) {
359: assertEquals(Tag.P, iterator4.getTag());
360: assertTrue(iterator4.isValid());
361: assertEquals(0, iterator4.getStartOffset());
362: assertEquals(9, iterator4.getEndOffset());
363: iterator4.next();
364: assertFalse(iterator4.isValid());
365: assertEquals(-1, iterator4.getStartOffset());
366: assertEquals(-1, iterator4.getEndOffset());
367: assertNull(iterator4.getAttributes());
368: }
369:
370: assertEquals(Tag.B, iterator5.getTag());
371: if (isHarmony()) {
372: assertTrue(iterator5.isValid());
373: assertEquals(0, iterator5.getStartOffset());
374: assertEquals(4, iterator5.getEndOffset());
375: assertFalse(StyleConstants
376: .isBold(iterator5.getAttributes()));
377: assertFalse(StyleConstants.isItalic(iterator5
378: .getAttributes()));
379: iterator5.next();
380: assertTrue(iterator5.isValid());
381: assertEquals(4, iterator5.getStartOffset());
382: assertEquals(8, iterator5.getEndOffset());
383: assertFalse(StyleConstants
384: .isBold(iterator5.getAttributes()));
385: assertTrue(StyleConstants.isItalic(iterator5
386: .getAttributes()));
387: iterator5.next();
388: assertFalse(iterator5.isValid());
389: assertEquals(-1, iterator5.getStartOffset());
390: assertEquals(-1, iterator5.getEndOffset());
391: assertNull(iterator5.getAttributes());
392: }
393: }
394:
395: public void testGetReaderIntIntIntTag() {
396: HTMLEditorKit.ParserCallback reader1 = htmlDoc.getReader(0, 10,
397: 100, null);
398: HTMLEditorKit.ParserCallback reader2 = htmlDoc.getReader(0, 10,
399: 100, null);
400: HTMLEditorKit.ParserCallback reader3 = htmlDoc.getReader(10,
401: 100, 10, Tag.P);
402:
403: assertNotNull(reader1);
404: assertNotNull(reader2);
405: assertNotNull(reader3);
406: assertTrue(reader1 instanceof HTMLDocument.HTMLReader);
407: assertTrue(reader2 instanceof HTMLDocument.HTMLReader);
408: assertTrue(reader3 instanceof HTMLDocument.HTMLReader);
409:
410: assertNotSame(reader1, reader2);
411: assertNotSame(reader2, reader3);
412: }
413:
414: public void testGetReaderInt() {
415: HTMLEditorKit.ParserCallback reader1 = htmlDoc.getReader(0);
416: HTMLEditorKit.ParserCallback reader2 = htmlDoc.getReader(0);
417: HTMLEditorKit.ParserCallback reader3 = htmlDoc.getReader(1);
418:
419: assertNotNull(reader1);
420: assertNotNull(reader2);
421: assertNotNull(reader3);
422: assertTrue(reader1 instanceof HTMLDocument.HTMLReader);
423: assertTrue(reader2 instanceof HTMLDocument.HTMLReader);
424: assertTrue(reader3 instanceof HTMLDocument.HTMLReader);
425:
426: assertNotSame(reader1, reader2);
427: assertNotSame(reader2, reader3);
428: }
429:
430: public void testGetStyleSheet() {
431: AttributeContext styleSheet = htmlDoc
432: .getAttributeContextPublicly();
433: assertTrue(styleSheet instanceof StyleSheet);
434: assertSame(styleSheet, htmlDoc.getStyleSheet());
435: }
436:
437: public void testProcessHTMLFrameHyperlinkEvent() throws Exception {
438: final String frameSetHTML = "<FRAMESET><FRAME name=\"1\" src=\"1.html\"><FRAME name=\"2\" src=\"2.html\"><img name=\"3\" src=\"3.jpg\"></FRAMESET>";
439: HTMLDocumentTestCase.loadDocument(htmlDoc, frameSetHTML);
440: final Element body = htmlDoc.getDefaultRootElement()
441: .getElement(1);
442: final Element frameSet = body.getElement(0);
443: final Element frame1 = frameSet.getElement(0);
444: final Element frame2 = frameSet.getElement(1);
445:
446: final String urlStr1 = "file:/test1.html";
447: final String urlStr2 = "file:/test2.html";
448: HTMLFrameHyperlinkEvent event1 = new HTMLFrameHyperlinkEvent(
449: htmlDoc, HyperlinkEvent.EventType.ACTIVATED, new URL(
450: urlStr1), "3");
451: HTMLFrameHyperlinkEvent event2 = new HTMLFrameHyperlinkEvent(
452: htmlDoc, HyperlinkEvent.EventType.ACTIVATED, new URL(
453: urlStr1), frame2, "_self");
454: HTMLFrameHyperlinkEvent event3 = new HTMLFrameHyperlinkEvent(
455: htmlDoc, HyperlinkEvent.EventType.ACTIVATED, new URL(
456: urlStr2), frame2, "_top");
457: HTMLFrameHyperlinkEvent event4 = new HTMLFrameHyperlinkEvent(
458: htmlDoc, HyperlinkEvent.EventType.ACTIVATED, new URL(
459: urlStr2), frame2, "_parent");
460: HTMLFrameHyperlinkEvent event5 = new HTMLFrameHyperlinkEvent(
461: htmlDoc, HyperlinkEvent.EventType.ACTIVATED, new URL(
462: urlStr1), "1");
463:
464: final DocumentController controller = new DocumentController();
465: htmlDoc.addDocumentListener(controller);
466:
467: htmlDoc.processHTMLFrameHyperlinkEvent(event1);
468: assertFalse(controller.isChanged());
469: controller.reset();
470:
471: assertSame(frame2, event2.getSourceElement());
472: htmlDoc.processHTMLFrameHyperlinkEvent(event2);
473: assertNull(htmlDoc.getParser());
474: assertTrue(controller.isChanged());
475: assertEquals(1, controller.getNumEvents());
476: assertTrue(controller.getEvent(0) instanceof AbstractDocument.DefaultDocumentEvent);
477: AbstractDocument.DefaultDocumentEvent event = (AbstractDocument.DefaultDocumentEvent) controller
478: .getEvent(0);
479: assertEquals(DocumentEvent.EventType.CHANGE, event.getType());
480: assertEquals(frame2.getStartOffset(), event.getOffset());
481: assertEquals(frame2.getEndOffset() - frame2.getStartOffset(),
482: event.getLength());
483: assertSame(htmlDoc, event.getDocument());
484: assertNull(event.getChange(frame2));
485: assertEquals(DocumentEvent.EventType.CHANGE, controller
486: .getEvent(0).getType());
487: assertEquals(urlStr1, frame2.getAttributes().getAttribute(
488: HTML.Attribute.SRC));
489: controller.reset();
490:
491: htmlDoc.setParser(null);
492: htmlDoc.processHTMLFrameHyperlinkEvent(event4);
493: assertNotNull(htmlDoc.getParser());
494: assertTrue(controller.isChanged());
495: assertEquals(2, controller.getNumEvents());
496: assertEquals(DocumentEvent.EventType.INSERT, controller
497: .getEvent(0).getType());
498: assertEquals(DocumentEvent.EventType.REMOVE, controller
499: .getEvent(1).getType());
500: Element newFrame = body.getElement(0);
501: AttributeSet frameAttr = newFrame.getAttributes();
502: assertEquals(2, frameAttr.getAttributeCount());
503: assertEquals(urlStr2, frameAttr
504: .getAttribute(HTML.Attribute.SRC));
505: assertEquals(Tag.FRAME, frameAttr
506: .getAttribute(StyleConstants.NameAttribute));
507: controller.reset();
508:
509: // tests improper use behaviour (for compatibility reasons)
510: HTMLFrameHyperlinkEvent event6 = new HTMLFrameHyperlinkEvent(
511: htmlDoc, HyperlinkEvent.EventType.ACTIVATED, new URL(
512: urlStr1), newFrame, "_parent");
513: ParserDelegator parser = new ParserDelegator();
514: htmlDoc.setParser(parser);
515: htmlDoc.processHTMLFrameHyperlinkEvent(event6);
516: assertSame(parser, htmlDoc.getParser());
517: assertTrue(controller.isChanged());
518: assertEquals(2, controller.getNumEvents());
519: assertEquals(DocumentEvent.EventType.INSERT, controller
520: .getEvent(0).getType());
521: assertEquals(DocumentEvent.EventType.REMOVE, controller
522: .getEvent(1).getType());
523: newFrame = htmlDoc.getDefaultRootElement().getElement(1);
524: assertNotSame(frameSet, newFrame);
525: frameAttr = newFrame.getAttributes();
526: assertEquals(2, frameAttr.getAttributeCount());
527: assertEquals(urlStr1, frameAttr
528: .getAttribute(HTML.Attribute.SRC));
529: assertEquals(Tag.FRAME, frameAttr
530: .getAttribute(StyleConstants.NameAttribute));
531: controller.reset();
532: }
533:
534: public void testGetSetBase() throws Exception {
535: URL u1 = new URL("http://www.apache.org");
536: URL u2 = new URL("http://www.harmony.incubator.apache.org");
537: String tail = "tail";
538: htmlDoc.setBase(u1);
539: assertSame(u1, htmlDoc.getBase());
540: assertSame(u1, htmlDoc.getStyleSheet().getBase());
541: htmlDoc.getStyleSheet().setBase(u2);
542: assertSame(u2, htmlDoc.getStyleSheet().getBase());
543: assertSame(u1, htmlDoc.getBase());
544: }
545:
546: public void testGetSetParser() {
547: assertNull(htmlDoc.getParser());
548: ParserDelegator parser = new ParserDelegator();
549: htmlDoc.setParser(parser);
550: assertSame(parser, htmlDoc.getParser());
551: }
552:
553: public void testGetSetPreservesUnknownTags() throws Exception {
554: assertTrue(htmlDoc.getPreservesUnknownTags());
555:
556: htmlDoc.setPreservesUnknownTags(false);
557: assertFalse(htmlDoc.getPreservesUnknownTags());
558:
559: Marker createMarker = htmlDoc.getCreateMarker();
560: final String htmlStr = "<html><body><badtag>0</badtag></body></html>";
561: HTMLDocumentTestCase.loadDocument(htmlDoc, htmlStr);
562: Element parent = htmlDoc.getDefaultRootElement().getElement(1)
563: .getElement(0);
564: ArrayList array = (ArrayList) createMarker.getAuxiliary();
565: assertEquals(1, array.size());
566: assertEquals(13, ((ElementSpec[]) (array.get(0))).length);
567: assertEquals(2, parent.getElementCount());
568: createMarker.reset();
569:
570: htmlDoc = new PublicHTMLDocument();
571: htmlDoc.setPreservesUnknownTags(true);
572: createMarker = htmlDoc.getCreateMarker();
573: HTMLDocumentTestCase.loadDocument(htmlDoc, htmlStr);
574: parent = htmlDoc.getDefaultRootElement().getElement(1)
575: .getElement(0);
576: assertTrue(createMarker.isOccurred());
577: array = (ArrayList) createMarker.getAuxiliary();
578: assertEquals(1, array.size());
579: assertEquals(15, ((ElementSpec[]) (array.get(0))).length);
580: assertEquals(4, parent.getElementCount());
581: assertEquals("badtag", parent.getElement(0).getName());
582: assertEquals(Tag.CONTENT.toString(), parent.getElement(1)
583: .getName());
584: assertEquals("badtag", parent.getElement(2).getName());
585: checkAttributes(parent.getElement(2).getAttributes(),
586: HTML.Attribute.ENDTAG, Boolean.TRUE);
587: createMarker.reset();
588: }
589:
590: public void testGetSetTokenThreshold() throws Exception {
591: assertEquals(Integer.MAX_VALUE, htmlDoc.getTokenThreshold());
592:
593: htmlDoc.setTokenThreshold(100);
594: assertEquals(100, htmlDoc.getTokenThreshold());
595:
596: final String longString = "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
597: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
598: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
599: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
600: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
601: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
602: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
603: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
604: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
605: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
606: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
607: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
608: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
609: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
610: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
611: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
612: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
613: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
614: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
615: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
616: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
617: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a"
618: + "<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a";
619:
620: htmlDoc = new PublicHTMLDocument();
621: insertMarker = htmlDoc.getInsertMarker();
622: Marker createMarker = htmlDoc.getCreateMarker();
623: final ParserDelegator parser = new ParserDelegator();
624: htmlDoc.setParser(parser);
625: htmlDoc.setEditable(false);
626: htmlDoc.setTokenThreshold(1);
627: Element root = htmlDoc.getDefaultRootElement();
628: Element branch = root.getElement(0).getElement(0);
629: htmlDoc.insertAfterStart(branch, longString);
630: assertFalse(createMarker.isOccurred());
631: assertTrue(insertMarker.isOccurred());
632: ArrayList info = (ArrayList) insertMarker.getAuxiliary();
633: assertNotNull(info);
634: assertEquals(5, info.size() / 2);
635: assertEquals(2, ((ElementSpec[]) (info.get(0))).length);
636: assertEquals(6, ((ElementSpec[]) info.get(2)).length);
637: assertEquals(26, ((ElementSpec[]) info.get(4)).length);
638: assertEquals(126, ((ElementSpec[]) info.get(6)).length);
639: assertEquals(305, ((ElementSpec[]) info.get(8)).length);
640: assertEquals(1, htmlDoc.getTokenThreshold());
641: insertMarker.reset();
642:
643: htmlDoc = new PublicHTMLDocument();
644: insertMarker = htmlDoc.getInsertMarker();
645: htmlDoc.setEditable(false);
646: htmlDoc.setParser(parser);
647: htmlDoc.setTokenThreshold(2);
648: root = htmlDoc.getDefaultRootElement();
649: branch = root.getElement(0).getElement(0);
650: htmlDoc.insertAfterStart(branch, longString);
651: assertFalse(createMarker.isOccurred());
652: assertTrue(insertMarker.isOccurred());
653: info = (ArrayList) insertMarker.getAuxiliary();
654: assertNotNull(info);
655: assertEquals(5, info.size() / 2);
656: assertEquals(4, ((ElementSpec[]) info.get(0)).length);
657: assertEquals(12, ((ElementSpec[]) info.get(2)).length);
658: assertEquals(52, ((ElementSpec[]) info.get(4)).length);
659: assertEquals(252, ((ElementSpec[]) info.get(6)).length);
660: assertEquals(145, ((ElementSpec[]) info.get(8)).length);
661: assertEquals(2, htmlDoc.getTokenThreshold());
662: insertMarker.reset();
663:
664: htmlDoc = new PublicHTMLDocument();
665: insertMarker = htmlDoc.getInsertMarker();
666: htmlDoc.setEditable(false);
667: htmlDoc.setParser(parser);
668: htmlDoc.setTokenThreshold(5);
669: root = htmlDoc.getDefaultRootElement();
670: branch = root.getElement(0).getElement(0);
671: htmlDoc.insertAfterStart(branch, longString);
672: assertFalse(createMarker.isOccurred());
673: assertTrue(insertMarker.isOccurred());
674: info = (ArrayList) insertMarker.getAuxiliary();
675: assertNotNull(info);
676: assertEquals(4, info.size() / 2);
677: assertEquals(6, ((ElementSpec[]) info.get(0)).length);
678: assertEquals(26, ((ElementSpec[]) info.get(2)).length);
679: assertEquals(126, ((ElementSpec[]) info.get(4)).length);
680: assertEquals(307, ((ElementSpec[]) info.get(6)).length);
681: assertEquals(5, htmlDoc.getTokenThreshold());
682: insertMarker.reset();
683: }
684:
685: public void testGetDefaultRootElement() {
686: }
687:
688: public void testInsertUpdate() throws Exception {
689: doc.insertString(0, "1111", null);
690: final AttributeSet attr = doc.getCharacterElement(2)
691: .getAttributes();
692: assertEquals(1, attr.getAttributeCount());
693: assertEquals(Tag.CONTENT, attr
694: .getAttribute(StyleConstants.NameAttribute));
695: }
696:
697: public void testSerializable() throws Exception {
698: }
699:
700: protected static void checkAttributes(final AttributeSet attr,
701: final Object key, final Object value) {
702: HTMLDocumentTestCase.checkAttributes(attr, key, value);
703: }
704:
705: }
|