0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: /**
0018: * @author Alexander T. Simbirtsev
0019: * @version $Revision$
0020: */package javax.swing.text.html;
0021:
0022: import java.util.ArrayList;
0023:
0024: import javax.swing.event.DocumentEvent;
0025: import javax.swing.event.DocumentEvent.EventType;
0026: import javax.swing.text.AttributeSet;
0027: import javax.swing.text.BadLocationException;
0028: import javax.swing.text.Document;
0029: import javax.swing.text.Element;
0030: import javax.swing.text.SimpleAttributeSet;
0031: import javax.swing.text.StyleConstants;
0032: import javax.swing.text.DefaultStyledDocument.ElementSpec;
0033: import javax.swing.text.html.HTML.Tag;
0034: import javax.swing.text.html.parser.ParserDelegator;
0035:
0036: public class HTMLDocument_InsertsTest extends HTMLDocumentTestCase {
0037:
0038: protected PublicHTMLDocument htmlDoc;
0039:
0040: protected void setUp() throws Exception {
0041: super .setUp();
0042: setIgnoreNotImplemented(true);
0043: htmlDoc = new PublicHTMLDocument();
0044: timeoutDelay = Integer.MAX_VALUE;
0045: }
0046:
0047: protected void tearDown() throws Exception {
0048: htmlDoc = null;
0049: super .tearDown();
0050: }
0051:
0052: public void testInsertAfterEnd_Specs() throws Exception {
0053: htmlDoc.setParser(new ParserDelegator());
0054: loadDocument(htmlDoc,
0055: "<table><tr><td>table</td></td></tr></table>");
0056: htmlDoc.setEditable(false);
0057:
0058: Element root = htmlDoc.getDefaultRootElement();
0059: Element body = root.getElement(1);
0060: Element table = body.getElement(0);
0061: Element tr = table.getElement(0);
0062: Element td = tr.getElement(0);
0063:
0064: htmlDoc.insertAfterEnd(td, "<a>link</a>");
0065: Marker insertMarker = htmlDoc.getInsertMarker();
0066: ElementSpec[] specs = (ElementSpec[]) (getInsertInfo(insertMarker)
0067: .get(0));
0068: insertMarker.reset();
0069: assertEquals(3, specs.length);
0070: checkEndTagSpec(specs[0]);
0071: checkEndTagSpec(specs[1]);
0072: assertSpec(specs[2], ElementSpec.ContentType,
0073: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0074: AttributeSet specAttr = specs[2].getAttributes();
0075: assertEquals(2, specAttr.getAttributeCount());
0076: checkAttributes(specAttr, StyleConstants.NameAttribute,
0077: Tag.CONTENT);
0078:
0079: htmlDoc.insertAfterEnd(tr, "<a>link</a>");
0080: specs = (ElementSpec[]) (getInsertInfo(insertMarker).get(0));
0081: insertMarker.reset();
0082: assertEquals(4, specs.length);
0083: checkEndTagSpec(specs[0]);
0084: checkEndTagSpec(specs[1]);
0085: checkEndTagSpec(specs[2]);
0086: assertSpec(specs[3], ElementSpec.ContentType,
0087: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0088: specAttr = specs[3].getAttributes();
0089: assertEquals(2, specAttr.getAttributeCount());
0090: checkAttributes(specAttr, StyleConstants.NameAttribute,
0091: Tag.CONTENT);
0092:
0093: htmlDoc.insertAfterEnd(table, "<a>link</a>");
0094: specs = (ElementSpec[]) (getInsertInfo(insertMarker).get(0));
0095: insertMarker.reset();
0096: assertEquals(5, specs.length);
0097: checkEndTagSpec(specs[0]);
0098: checkEndTagSpec(specs[1]);
0099: checkEndTagSpec(specs[2]);
0100: checkEndTagSpec(specs[3]);
0101: assertSpec(specs[4], ElementSpec.ContentType,
0102: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0103: specAttr = specs[4].getAttributes();
0104: assertEquals(2, specAttr.getAttributeCount());
0105: checkAttributes(specAttr, StyleConstants.NameAttribute,
0106: Tag.CONTENT);
0107: }
0108:
0109: public void testInsertAfterEnd_Specs2() throws Exception {
0110: htmlDoc.setParser(new ParserDelegator());
0111: htmlDoc.setEditable(false);
0112:
0113: Element root = htmlDoc.getDefaultRootElement();
0114: Element body = root.getElement(0);
0115: Element p = body.getElement(0);
0116:
0117: htmlDoc.insertAfterEnd(p, "<a>link</a>");
0118: Marker insertMarker = htmlDoc.getInsertMarker();
0119: assertEquals(new Integer(0), getInsertInfo(insertMarker).get(1));
0120: ElementSpec[] specs = (ElementSpec[]) (getInsertInfo(insertMarker)
0121: .get(0));
0122: insertMarker.reset();
0123: assertEquals(2, specs.length);
0124: checkEndTagSpec(specs[0]);
0125: assertSpec(specs[1], ElementSpec.ContentType,
0126: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0127: AttributeSet specAttr = specs[1].getAttributes();
0128: assertEquals(2, specAttr.getAttributeCount());
0129: checkAttributes(specAttr, StyleConstants.NameAttribute,
0130: Tag.CONTENT);
0131: }
0132:
0133: private ArrayList getInsertInfo(Marker insertMarker) {
0134: return (ArrayList) insertMarker.getAuxiliary();
0135: }
0136:
0137: public void testInsertAfterEnd_Events() throws Exception {
0138: Element root = htmlDoc.getDefaultRootElement();
0139: Element body = root.getElement(0);
0140: Element p = body.getElement(0);
0141: addElement();
0142:
0143: htmlDoc.setParser(new ParserDelegator());
0144: DocumentController listener = new DocumentController();
0145: htmlDoc.addDocumentListener(listener);
0146: htmlDoc.insertAfterEnd(p, "<a>link</a><b>bold</b>");
0147: assertEquals(1, listener.getNumEvents());
0148: checkEvent(body, listener.getEvent(0),
0149: DocumentEvent.EventType.INSERT, 3, 8, 4);
0150:
0151: listener.reset();
0152: htmlDoc.insertAfterEnd(body, "<a>link</a><b>bold</b>");
0153: assertEquals(1, listener.getNumEvents());
0154: checkEvent(root, listener.getEvent(0),
0155: DocumentEvent.EventType.INSERT, 3, 8, 12);
0156: }
0157:
0158: public void testInsertAfterEnd_Structure() throws Exception {
0159: Element root = htmlDoc.getDefaultRootElement();
0160: Element body = root.getElement(0);
0161: Element p = body.getElement(0);
0162: addElement();
0163:
0164: htmlDoc.setParser(new ParserDelegator());
0165: assertEquals(1, body.getElementCount());
0166: htmlDoc.insertAfterEnd(p, "<a>link</a><b>bold</b>");
0167: Marker insertMarker = htmlDoc.getInsertMarker();
0168: assertEquals(new Integer(4), getInsertInfo(insertMarker).get(1));
0169: insertMarker.reset();
0170: assertEquals("0000linkbold", htmlDoc.getText(0, htmlDoc
0171: .getLength()));
0172: checkStructure(htmlDoc, new String[] { "html", "body", "p",
0173: "b", "content", "content", "p", "content" });
0174: assertEquals(4, body.getElementCount());
0175: assertEquals(1, p.getElementCount());
0176: assertEquals(1, root.getElementCount());
0177:
0178: htmlDoc.insertAfterEnd(body, "<a>link</a><b>bold</b>");
0179: assertEquals(new Integer(12), getInsertInfo(insertMarker)
0180: .get(1));
0181: insertMarker.reset();
0182: assertEquals("0000linkboldlinkbold", htmlDoc.getText(0, htmlDoc
0183: .getLength()));
0184: checkStructure(htmlDoc, new String[] { "html", "body", "p",
0185: "b", "content", "content", "content", "content",
0186: "body", "p", "content" });
0187: assertEquals(3, body.getElementCount());
0188: assertEquals(1, p.getElementCount());
0189: assertEquals(4, root.getElementCount());
0190:
0191: htmlDoc.insertAfterEnd(root, "<a>link</a><b>bold</b>");
0192: assertEquals("0000linkboldlinkbold", htmlDoc.getText(0, htmlDoc
0193: .getLength()));
0194: checkStructure(htmlDoc, new String[] { "html", "body", "p",
0195: "b", "content", "content", "content", "content",
0196: "body", "p", "content" });
0197: assertEquals(3, body.getElementCount());
0198: assertEquals(1, p.getElementCount());
0199: assertEquals(4, root.getElementCount());
0200: }
0201:
0202: public void testInsertAfterEnd_Exceptions() throws Exception {
0203: addElements();
0204: Element root = htmlDoc.getDefaultRootElement();
0205: Element body = root.getElement(0);
0206: Element p = body.getElement(0);
0207: Element leaf = p.getElement(0);
0208:
0209: try {
0210: htmlDoc.insertAfterEnd(leaf, "<a>link</a>");
0211: fail("IllegalStateException should be thrown");
0212: } catch (IllegalStateException e) {
0213: }
0214:
0215: htmlDoc.setParser(new ParserDelegator());
0216: htmlDoc.insertAfterEnd(null, "<a>link</a>");
0217: }
0218:
0219: public void testInsertAfterStart_Specs() throws Exception {
0220: htmlDoc.setParser(new ParserDelegator());
0221: loadDocument(htmlDoc,
0222: "<table><tr><td><div>cell</div></td></tr></table>");
0223: htmlDoc.setEditable(true);
0224:
0225: Element root = htmlDoc.getDefaultRootElement();
0226: Element body = root.getElement(1);
0227: Element table = body.getElement(0);
0228: Element tr = table.getElement(0);
0229: Element td = tr.getElement(0);
0230: Element div = td.getElement(0);
0231:
0232: htmlDoc.insertAfterStart(div, "<a>link</a>");
0233: Marker insertMarker = htmlDoc.getInsertMarker();
0234: assertEquals(new Integer(1), getInsertInfo(insertMarker).get(1));
0235: ElementSpec[] specs = (ElementSpec[]) (getInsertInfo(insertMarker)
0236: .get(0));
0237: insertMarker.reset();
0238: assertEquals(8, specs.length);
0239: checkEndTagSpec(specs[0]);
0240: checkEndTagSpec(specs[1]);
0241: checkStartJNTagSpec(specs[2]);
0242: checkStartJNTagSpec(specs[3]);
0243: checkStartJNTagSpec(specs[4]);
0244: checkStartJNTagSpec(specs[5]);
0245: checkStartJNTagSpec(specs[6]);
0246: assertSpec(specs[7], ElementSpec.ContentType,
0247: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0248: AttributeSet specAttr = specs[7].getAttributes();
0249: assertEquals(2, specAttr.getAttributeCount());
0250: checkAttributes(specAttr, StyleConstants.NameAttribute,
0251: Tag.CONTENT);
0252:
0253: htmlDoc.insertAfterStart(tr, "<a>link</a>");
0254: assertEquals(new Integer(1), getInsertInfo(insertMarker).get(1));
0255: specs = (ElementSpec[]) (getInsertInfo(insertMarker).get(0));
0256: insertMarker.reset();
0257: assertEquals(6, specs.length);
0258: checkEndTagSpec(specs[0]);
0259: checkEndTagSpec(specs[1]);
0260: checkStartJNTagSpec(specs[2]);
0261: checkStartJNTagSpec(specs[3]);
0262: checkStartJNTagSpec(specs[4]);
0263: assertSpec(specs[5], ElementSpec.ContentType,
0264: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0265: specAttr = specs[5].getAttributes();
0266: assertEquals(2, specAttr.getAttributeCount());
0267: checkAttributes(specAttr, StyleConstants.NameAttribute,
0268: Tag.CONTENT);
0269:
0270: htmlDoc.insertAfterStart(table, "<a>link</a>");
0271: assertEquals(new Integer(1), getInsertInfo(insertMarker).get(1));
0272: specs = (ElementSpec[]) (getInsertInfo(insertMarker).get(0));
0273: insertMarker.reset();
0274: assertEquals(5, specs.length);
0275: checkEndTagSpec(specs[0]);
0276: checkEndTagSpec(specs[1]);
0277: checkStartJNTagSpec(specs[2]);
0278: checkStartJNTagSpec(specs[3]);
0279: assertSpec(specs[4], ElementSpec.ContentType,
0280: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0281: specAttr = specs[4].getAttributes();
0282: assertEquals(2, specAttr.getAttributeCount());
0283: checkAttributes(specAttr, StyleConstants.NameAttribute,
0284: Tag.CONTENT);
0285: }
0286:
0287: public void testInsertAfterStart_Specs2() throws Exception {
0288: htmlDoc.setParser(new ParserDelegator());
0289: htmlDoc.setEditable(false);
0290:
0291: Element root = htmlDoc.getDefaultRootElement();
0292: Element body = root.getElement(0);
0293: Element p = body.getElement(0);
0294:
0295: htmlDoc.insertAfterStart(p, "<a>link</a>");
0296: Marker insertMarker = htmlDoc.getInsertMarker();
0297: assertEquals(new Integer(0), getInsertInfo(insertMarker).get(1));
0298: ElementSpec[] specs = (ElementSpec[]) (getInsertInfo(insertMarker)
0299: .get(0));
0300: insertMarker.reset();
0301: assertEquals(1, specs.length);
0302: assertSpec(specs[0], ElementSpec.ContentType,
0303: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0304: AttributeSet specAttr = specs[0].getAttributes();
0305: assertEquals(2, specAttr.getAttributeCount());
0306: checkAttributes(specAttr, StyleConstants.NameAttribute,
0307: Tag.CONTENT);
0308: }
0309:
0310: public void testInsertAfterStart_Events() throws Exception {
0311: Element root = htmlDoc.getDefaultRootElement();
0312: Element body = root.getElement(0);
0313: Element p = body.getElement(0);
0314: addElement();
0315:
0316: htmlDoc.setParser(new ParserDelegator());
0317: DocumentController listener = new DocumentController();
0318: htmlDoc.addDocumentListener(listener);
0319: htmlDoc.insertAfterStart(p, "<a>link</a><b>bold</b>");
0320: assertEquals(1, listener.getNumEvents());
0321: checkEvent(p, listener.getEvent(0),
0322: DocumentEvent.EventType.INSERT, 3, 8, 0);
0323:
0324: listener.reset();
0325: htmlDoc.insertAfterStart(body, "<a>link</a><b>bold</b>");
0326: assertEquals(1, listener.getNumEvents());
0327: if (!isHarmony()) {
0328: checkEvent(body, listener.getEvent(0),
0329: DocumentEvent.EventType.INSERT, 3, 8, 0);
0330: } else {
0331: checkEvent(body, listener.getEvent(0),
0332: DocumentEvent.EventType.INSERT, 2, 8, 0);
0333: }
0334: }
0335:
0336: public void testInsertAfterStart_Strucutre() throws Exception {
0337: Element root = htmlDoc.getDefaultRootElement();
0338: Element body = root.getElement(0);
0339: Element p = body.getElement(0);
0340: addElement();
0341:
0342: htmlDoc.setParser(new ParserDelegator());
0343: assertEquals(1, body.getElementCount());
0344: htmlDoc.insertAfterStart(p, "<a>link</a><b>bold</b>");
0345: assertEquals("linkbold0000", htmlDoc.getText(0, htmlDoc
0346: .getLength()));
0347: checkStructure(htmlDoc, new String[] { "html", "body", "p",
0348: "content", "content", "b", "content" });
0349: assertEquals(1, body.getElementCount());
0350: assertEquals(4, p.getElementCount());
0351: assertEquals(1, root.getElementCount());
0352:
0353: htmlDoc.insertAfterStart(body, "<a>link</a><b>bold</b>");
0354: assertEquals("linkboldlinkbold0000", htmlDoc.getText(0, htmlDoc
0355: .getLength()));
0356: checkStructure(htmlDoc, new String[] { "html", "body",
0357: "content", "content", "p", "content", "content", "b",
0358: "content" });
0359: assertEquals(1, root.getElementCount());
0360: assertEquals(3, body.getElementCount());
0361: p = body.getElement(2);
0362: assertEquals(4, p.getElementCount());
0363:
0364: htmlDoc.insertAfterStart(root, "<a>link</a><b>bold</b>");
0365: assertEquals("linkboldlinkboldlinkbold0000", htmlDoc.getText(0,
0366: htmlDoc.getLength()));
0367: checkStructure(htmlDoc, new String[] { "html", "content",
0368: "content", "body", "content", "content", "p",
0369: "content", "content", "b", "content" });
0370: assertEquals(3, root.getElementCount());
0371: body = root.getElement(2);
0372: assertEquals(3, body.getElementCount());
0373: assertEquals(4, p.getElementCount());
0374: }
0375:
0376: public void testInsertAfterStart_Exceptions() throws Exception {
0377: addElements();
0378: Element root = htmlDoc.getDefaultRootElement();
0379: Element branch1 = root.getElement(0);
0380: Element branch2 = branch1.getElement(0);
0381: Element branch3 = branch2.getElement(0);
0382:
0383: try {
0384: htmlDoc.insertAfterStart(branch3, "<a>link</a>");
0385: fail("IllegalStateException should be thrown");
0386: } catch (IllegalStateException e) {
0387: }
0388:
0389: htmlDoc.setParser(new ParserDelegator());
0390:
0391: try {
0392: htmlDoc.insertAfterStart(branch3, "<a>link</a>");
0393: fail("IllegalArgumentException should be thrown");
0394: } catch (IllegalArgumentException e) {
0395: }
0396:
0397: try {
0398: htmlDoc.insertAfterStart(null, "<a>link</a>");
0399: fail("NullPointerException should be thrown");
0400: } catch (NullPointerException e) {
0401: }
0402: }
0403:
0404: public void testInsertBeforeEnd_Specs() throws Exception {
0405: htmlDoc.setParser(new ParserDelegator());
0406: loadDocument(htmlDoc,
0407: "<table><tr><td><div>table</div></td</td></tr></table>");
0408: htmlDoc.setEditable(false);
0409:
0410: Element root = htmlDoc.getDefaultRootElement();
0411: Element body = root.getElement(1);
0412: Element table = body.getElement(0);
0413: Element tr = table.getElement(0);
0414: Element td = tr.getElement(0);
0415:
0416: htmlDoc.insertBeforeEnd(td, "<a>link</a>");
0417: Marker insertMarker = htmlDoc.getInsertMarker();
0418: assertEquals(new Integer(7), getInsertInfo(insertMarker).get(1));
0419: ElementSpec[] specs = (ElementSpec[]) (getInsertInfo(insertMarker)
0420: .get(0));
0421: insertMarker.reset();
0422: assertEquals(3, specs.length);
0423: checkEndTagSpec(specs[0]);
0424: checkEndTagSpec(specs[1]);
0425: assertSpec(specs[2], ElementSpec.ContentType,
0426: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0427: AttributeSet specAttr = specs[2].getAttributes();
0428: assertEquals(2, specAttr.getAttributeCount());
0429: checkAttributes(specAttr, StyleConstants.NameAttribute,
0430: Tag.CONTENT);
0431: insertMarker.reset();
0432:
0433: htmlDoc.insertBeforeEnd(tr, "<a>link</a>");
0434: assertEquals(new Integer(7), getInsertInfo(insertMarker).get(1));
0435: specs = (ElementSpec[]) (getInsertInfo(insertMarker).get(0));
0436: insertMarker.reset();
0437: assertEquals(4, specs.length);
0438: checkEndTagSpec(specs[0]);
0439: checkEndTagSpec(specs[1]);
0440: checkEndTagSpec(specs[2]);
0441: assertSpec(specs[3], ElementSpec.ContentType,
0442: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0443: specAttr = specs[3].getAttributes();
0444: assertEquals(2, specAttr.getAttributeCount());
0445: checkAttributes(specAttr, StyleConstants.NameAttribute,
0446: Tag.CONTENT);
0447:
0448: htmlDoc.insertBeforeEnd(table, "<a>link</a>");
0449: assertEquals(new Integer(7), getInsertInfo(insertMarker).get(1));
0450: specs = (ElementSpec[]) (getInsertInfo(insertMarker).get(0));
0451: insertMarker.reset();
0452: assertEquals(5, specs.length);
0453: checkEndTagSpec(specs[0]);
0454: checkEndTagSpec(specs[1]);
0455: checkEndTagSpec(specs[2]);
0456: checkEndTagSpec(specs[3]);
0457: assertSpec(specs[4], ElementSpec.ContentType,
0458: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0459: specAttr = specs[4].getAttributes();
0460: assertEquals(2, specAttr.getAttributeCount());
0461: checkAttributes(specAttr, StyleConstants.NameAttribute,
0462: Tag.CONTENT);
0463: }
0464:
0465: public void testInsertBeforeEnd_Specs2() throws Exception {
0466: htmlDoc.setParser(new ParserDelegator());
0467: htmlDoc.setEditable(false);
0468:
0469: Element root = htmlDoc.getDefaultRootElement();
0470: Element body = root.getElement(0);
0471: Element p = body.getElement(0);
0472:
0473: htmlDoc.insertBeforeEnd(p, "<a>link</a>");
0474: Marker insertMarker = htmlDoc.getInsertMarker();
0475: assertEquals(new Integer(0), getInsertInfo(insertMarker).get(1));
0476: ElementSpec[] specs = (ElementSpec[]) (getInsertInfo(insertMarker)
0477: .get(0));
0478: insertMarker.reset();
0479: assertEquals(1, specs.length);
0480: assertSpec(specs[0], ElementSpec.ContentType,
0481: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0482: AttributeSet specAttr = specs[0].getAttributes();
0483: assertEquals(2, specAttr.getAttributeCount());
0484: checkAttributes(specAttr, StyleConstants.NameAttribute,
0485: Tag.CONTENT);
0486: }
0487:
0488: public void testInsertBeforeEnd_Events() throws Exception {
0489: Element root = htmlDoc.getDefaultRootElement();
0490: Element body = root.getElement(0);
0491: Element p = body.getElement(0);
0492: addElement();
0493:
0494: htmlDoc.setParser(new ParserDelegator());
0495: DocumentController listener = new DocumentController();
0496: htmlDoc.addDocumentListener(listener);
0497: htmlDoc.insertBeforeEnd(p, "<a>link</a><b>bold</b>");
0498: assertEquals(1, listener.getNumEvents());
0499: checkEvent(p, listener.getEvent(0),
0500: DocumentEvent.EventType.INSERT, 3, 8, 4);
0501:
0502: listener.reset();
0503: htmlDoc.insertBeforeEnd(body, "<a>link</a><b>bold</b>");
0504: assertEquals(1, listener.getNumEvents());
0505: checkEvent(body, listener.getEvent(0),
0506: DocumentEvent.EventType.INSERT, 2, 8, 13);
0507: }
0508:
0509: public void testInsertBeforeEnd_Strucutre() throws Exception {
0510: Element root = htmlDoc.getDefaultRootElement();
0511: Element body = root.getElement(0);
0512: Element p = body.getElement(0);
0513: addElement();
0514:
0515: htmlDoc.setParser(new ParserDelegator());
0516: assertEquals(1, body.getElementCount());
0517: htmlDoc.insertBeforeEnd(p, "<a>link</a><b>bold</b>");
0518: Marker insertMarker = htmlDoc.getInsertMarker();
0519: assertEquals(new Integer(4), getInsertInfo(insertMarker).get(1));
0520: assertEquals("0000linkbold", htmlDoc.getText(0, htmlDoc
0521: .getLength()));
0522: checkStructure(htmlDoc, new String[] { "html", "body", "p",
0523: "b", "content", "content", "content" });
0524: assertEquals(1, body.getElementCount());
0525: assertEquals(4, p.getElementCount());
0526: assertEquals(1, root.getElementCount());
0527: insertMarker.reset();
0528:
0529: htmlDoc.insertBeforeEnd(body, "<a>link</a><b>bold</b>");
0530: assertEquals(new Integer(13), getInsertInfo(insertMarker)
0531: .get(1));
0532: assertEquals("0000linkbold\nlinkbol", htmlDoc.getText(0,
0533: htmlDoc.getLength()));
0534: checkStructure(htmlDoc, new String[] { "html", "body", "p",
0535: "b", "content", "content", "content", "content",
0536: "content" });
0537: assertEquals(3, body.getElementCount());
0538: assertEquals(4, p.getElementCount());
0539: assertEquals(1, root.getElementCount());
0540: insertMarker.reset();
0541:
0542: htmlDoc.insertBeforeEnd(root, "<a>link</a><b>bold</b>");
0543: assertEquals(new Integer(21), getInsertInfo(insertMarker)
0544: .get(1));
0545: assertEquals("0000linkbold\nlinkboldlinkbol", htmlDoc.getText(
0546: 0, htmlDoc.getLength()));
0547: checkStructure(htmlDoc, new String[] { "html", "body", "p",
0548: "b", "content", "content", "content", "content",
0549: "content", "content", "content" });
0550: assertEquals(3, body.getElementCount());
0551: assertEquals(4, p.getElementCount());
0552: assertEquals(3, root.getElementCount());
0553: }
0554:
0555: public void testInsertBeforeEnd_Exceptions() throws Exception {
0556: addElements();
0557: Element root = htmlDoc.getDefaultRootElement();
0558: Element branch1 = root.getElement(0);
0559: Element branch2 = branch1.getElement(0);
0560: Element branch3 = branch2.getElement(0);
0561:
0562: try {
0563: htmlDoc.insertBeforeEnd(branch3, "<a>link</a>");
0564: fail("IllegalStateException should be thrown");
0565: } catch (IllegalStateException e) {
0566: }
0567:
0568: htmlDoc.setParser(new ParserDelegator());
0569:
0570: try {
0571: htmlDoc.insertBeforeEnd(branch3, "<a>link</a>");
0572: fail("IllegalArgumentException should be thrown");
0573: } catch (IllegalArgumentException e) {
0574: }
0575:
0576: try {
0577: htmlDoc.insertBeforeEnd(null, "<a>link</a>");
0578: fail("NullPointerException should be thrown");
0579: } catch (NullPointerException e) {
0580: }
0581: }
0582:
0583: public void testInsertBeforeStart_Specs() throws Exception {
0584: htmlDoc.setParser(new ParserDelegator());
0585: loadDocument(htmlDoc, "<table><tr><td>table</td></tr></table>");
0586: htmlDoc.setEditable(true);
0587:
0588: Element root = htmlDoc.getDefaultRootElement();
0589: Element body = root.getElement(1);
0590: Element table = body.getElement(0);
0591: Element tr = table.getElement(0);
0592: Element td = tr.getElement(0);
0593:
0594: htmlDoc.insertBeforeStart(td, "<a>link</a>");
0595: Marker insertMarker = htmlDoc.getInsertMarker();
0596: assertEquals(new Integer(1), getInsertInfo(insertMarker).get(1));
0597: ElementSpec[] specs = (ElementSpec[]) (getInsertInfo(insertMarker)
0598: .get(0));
0599: insertMarker.reset();
0600: assertEquals(6, specs.length);
0601: checkEndTagSpec(specs[0]);
0602: checkEndTagSpec(specs[1]);
0603: checkStartJNTagSpec(specs[2]);
0604: checkStartJNTagSpec(specs[3]);
0605: checkStartJNTagSpec(specs[4]);
0606: assertSpec(specs[5], ElementSpec.ContentType,
0607: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0608: AttributeSet specAttr = specs[5].getAttributes();
0609: assertEquals(2, specAttr.getAttributeCount());
0610: checkAttributes(specAttr, StyleConstants.NameAttribute,
0611: Tag.CONTENT);
0612:
0613: htmlDoc.insertBeforeStart(tr, "<a>link</a>");
0614: assertEquals(new Integer(1), getInsertInfo(insertMarker).get(1));
0615: specs = (ElementSpec[]) (getInsertInfo(insertMarker).get(0));
0616: insertMarker.reset();
0617: assertEquals(5, specs.length);
0618: checkEndTagSpec(specs[0]);
0619: checkEndTagSpec(specs[1]);
0620: checkStartJNTagSpec(specs[2]);
0621: checkStartJNTagSpec(specs[3]);
0622: assertSpec(specs[4], ElementSpec.ContentType,
0623: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0624: specAttr = specs[4].getAttributes();
0625: assertEquals(2, specAttr.getAttributeCount());
0626: checkAttributes(specAttr, StyleConstants.NameAttribute,
0627: Tag.CONTENT);
0628:
0629: htmlDoc.insertBeforeStart(table, "<a>link</a>");
0630: assertEquals(new Integer(1), getInsertInfo(insertMarker).get(1));
0631: specs = (ElementSpec[]) (getInsertInfo(insertMarker).get(0));
0632: insertMarker.reset();
0633: assertEquals(4, specs.length);
0634: checkEndTagSpec(specs[0]);
0635: checkEndTagSpec(specs[1]);
0636: checkStartJNTagSpec(specs[2]);
0637: assertSpec(specs[3], ElementSpec.ContentType,
0638: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0639: specAttr = specs[3].getAttributes();
0640: assertEquals(2, specAttr.getAttributeCount());
0641: checkAttributes(specAttr, StyleConstants.NameAttribute,
0642: Tag.CONTENT);
0643: }
0644:
0645: public void testInsertBeforeStart_Specs2() throws Exception {
0646: htmlDoc.setParser(new ParserDelegator());
0647: htmlDoc.setEditable(false);
0648:
0649: Element root = htmlDoc.getDefaultRootElement();
0650: Element body = root.getElement(0);
0651: Element p = body.getElement(0);
0652:
0653: htmlDoc.insertBeforeStart(p, "<a>link</a>");
0654: Marker insertMarker = htmlDoc.getInsertMarker();
0655: assertEquals(new Integer(0), getInsertInfo(insertMarker).get(1));
0656: ElementSpec[] specs = (ElementSpec[]) (getInsertInfo(insertMarker)
0657: .get(0));
0658: insertMarker.reset();
0659: assertEquals(2, specs.length);
0660: checkEndTagSpec(specs[0]);
0661: assertSpec(specs[1], ElementSpec.ContentType,
0662: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0663: AttributeSet specAttr = specs[1].getAttributes();
0664: assertEquals(2, specAttr.getAttributeCount());
0665: checkAttributes(specAttr, StyleConstants.NameAttribute,
0666: Tag.CONTENT);
0667: }
0668:
0669: public void testInsertBeforeStart_Events() throws Exception {
0670: Element root = htmlDoc.getDefaultRootElement();
0671: Element branch1 = root.getElement(0);
0672: Element branch2 = branch1.getElement(0);
0673: addElement();
0674:
0675: htmlDoc.setParser(new ParserDelegator());
0676: DocumentController listener = new DocumentController();
0677: htmlDoc.addDocumentListener(listener);
0678: htmlDoc.insertBeforeStart(branch2, "<a>link</a><b>bold</b>");
0679: assertEquals(1, listener.getNumEvents());
0680: if (!isHarmony()) {
0681: checkEvent(branch1, listener.getEvent(0),
0682: DocumentEvent.EventType.INSERT, 3, 8, 0);
0683: } else {
0684: checkEvent(branch1, listener.getEvent(0),
0685: DocumentEvent.EventType.INSERT, 2, 8, 0);
0686: }
0687:
0688: listener.reset();
0689: htmlDoc.insertBeforeStart(branch1, "<a>link</a><b>bold</b>");
0690: assertEquals(1, listener.getNumEvents());
0691: if (!isHarmony()) {
0692: checkEvent(root, listener.getEvent(0),
0693: DocumentEvent.EventType.INSERT, 3, 8, 0);
0694: } else {
0695: checkEvent(root, listener.getEvent(0),
0696: DocumentEvent.EventType.INSERT, 2, 8, 0);
0697: }
0698: }
0699:
0700: public void testInsertBeforeStart_Structure() throws Exception {
0701: Element root = htmlDoc.getDefaultRootElement();
0702: Element body = root.getElement(0);
0703: Element p = body.getElement(0);
0704: addElement();
0705:
0706: htmlDoc.setParser(new ParserDelegator());
0707: assertEquals(1, body.getElementCount());
0708: htmlDoc.insertBeforeStart(p, "<a>link</a><b>bold</b>");
0709: assertEquals("linkbold0000", htmlDoc.getText(0, htmlDoc
0710: .getLength()));
0711: checkStructure(htmlDoc, new String[] { "html", "body",
0712: "content", "content", "p", "b", "content" });
0713: assertEquals(1, root.getElementCount());
0714: assertEquals(3, body.getElementCount());
0715: p = body.getElement(2);
0716: assertEquals(2, p.getElementCount());
0717:
0718: htmlDoc.insertBeforeStart(body, "<a>link</a><b>bold</b>");
0719: assertEquals("linkboldlinkbold0000", htmlDoc.getText(0, htmlDoc
0720: .getLength()));
0721: checkStructure(htmlDoc, new String[] { "html", "content",
0722: "content", "body", "content", "content", "p", "b",
0723: "content" });
0724: assertEquals(3, root.getElementCount());
0725: body = root.getElement(2);
0726: assertEquals(3, body.getElementCount());
0727: assertEquals(2, p.getElementCount());
0728:
0729: htmlDoc.insertBeforeStart(root, "<a>link</a><b>bold</b>");
0730: assertEquals("linkboldlinkbold0000", htmlDoc.getText(0, htmlDoc
0731: .getLength()));
0732: checkStructure(htmlDoc, new String[] { "html", "content",
0733: "content", "body", "content", "content", "p", "b",
0734: "content" });
0735: assertEquals(3, root.getElementCount());
0736: assertEquals(3, body.getElementCount());
0737: assertEquals(2, p.getElementCount());
0738: }
0739:
0740: public void testInsertBeforeStart_Exceptions() throws Exception {
0741: addElements();
0742: Element root = htmlDoc.getDefaultRootElement();
0743: Element branch1 = root.getElement(0);
0744: Element branch2 = branch1.getElement(0);
0745: Element branch3 = branch2.getElement(0);
0746:
0747: try {
0748: htmlDoc.insertBeforeStart(branch3, "<a>link</a>");
0749: fail("IllegalStateException should be thrown");
0750: } catch (IllegalStateException e) {
0751: }
0752:
0753: htmlDoc.setParser(new ParserDelegator());
0754: htmlDoc.insertBeforeStart(null, "<a>link</a>");
0755: }
0756:
0757: public void testSetInnerHTML_Specs() throws Exception {
0758: htmlDoc.setParser(new ParserDelegator());
0759: loadDocument(htmlDoc, "<table><tr><td>table</td></tr></table>");
0760: htmlDoc.setEditable(false);
0761:
0762: Element root = htmlDoc.getDefaultRootElement();
0763: Element body = root.getElement(1);
0764: Element table = body.getElement(0);
0765: Element tr = table.getElement(0);
0766: Element td = tr.getElement(0);
0767:
0768: htmlDoc.setInnerHTML(td, "<a>link</a>");
0769: Marker insertMarker = htmlDoc.getInsertMarker();
0770: assertEquals(new Integer(1), getInsertInfo(insertMarker).get(1));
0771: ElementSpec[] specs = (ElementSpec[]) (getInsertInfo(insertMarker)
0772: .get(0));
0773: insertMarker.reset();
0774: assertEquals(10, specs.length);
0775: checkEndTagSpec(specs[0]);
0776: checkEndTagSpec(specs[1]);
0777: checkStartJNTagSpec(specs[2]);
0778: checkStartJNTagSpec(specs[3]);
0779: checkStartJNTagSpec(specs[4]);
0780: checkStartJNTagSpec(specs[5]);
0781: assertSpec(specs[6], ElementSpec.ContentType,
0782: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0783: AttributeSet specAttr = specs[6].getAttributes();
0784: assertEquals(2, specAttr.getAttributeCount());
0785: checkAttributes(specAttr, StyleConstants.NameAttribute,
0786: Tag.CONTENT);
0787: assertSpec(specs[7], ElementSpec.ContentType,
0788: ElementSpec.OriginateDirection, 0, new char[] { '\n' });
0789: checkEndTagSpec(specs[8]);
0790: checkEndTagSpec(specs[9]);
0791:
0792: htmlDoc.setInnerHTML(tr, "<a>link</a>");
0793: assertEquals(new Integer(1), getInsertInfo(insertMarker).get(1));
0794: specs = (ElementSpec[]) (getInsertInfo(insertMarker).get(0));
0795: insertMarker.reset();
0796: assertEquals(9, specs.length);
0797: checkEndTagSpec(specs[0]);
0798: checkEndTagSpec(specs[1]);
0799: checkStartJNTagSpec(specs[2]);
0800: checkStartJNTagSpec(specs[3]);
0801: checkStartJNTagSpec(specs[4]);
0802: assertSpec(specs[5], ElementSpec.ContentType,
0803: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0804: specAttr = specs[5].getAttributes();
0805: assertEquals(2, specAttr.getAttributeCount());
0806: checkAttributes(specAttr, StyleConstants.NameAttribute,
0807: Tag.CONTENT);
0808: assertSpec(specs[6], ElementSpec.ContentType,
0809: ElementSpec.OriginateDirection, 0, new char[] { '\n' });
0810: checkEndTagSpec(specs[7]);
0811: checkEndTagSpec(specs[8]);
0812:
0813: htmlDoc.setInnerHTML(table, "<a>link</a>");
0814: assertEquals(new Integer(1), getInsertInfo(insertMarker).get(1));
0815: specs = (ElementSpec[]) (getInsertInfo(insertMarker).get(0));
0816: insertMarker.reset();
0817: assertEquals(8, specs.length);
0818: checkEndTagSpec(specs[0]);
0819: checkEndTagSpec(specs[1]);
0820: checkStartJNTagSpec(specs[2]);
0821: checkStartJNTagSpec(specs[3]);
0822: assertSpec(specs[4], ElementSpec.ContentType,
0823: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0824: specAttr = specs[4].getAttributes();
0825: assertEquals(2, specAttr.getAttributeCount());
0826: checkAttributes(specAttr, StyleConstants.NameAttribute,
0827: Tag.CONTENT);
0828: assertSpec(specs[5], ElementSpec.ContentType,
0829: ElementSpec.OriginateDirection, 0, new char[] { '\n' });
0830: checkEndTagSpec(specs[6]);
0831: checkEndTagSpec(specs[7]);
0832: }
0833:
0834: public void testSetInnerHTML_Specs2() throws Exception {
0835: htmlDoc.setParser(new ParserDelegator());
0836: htmlDoc.setEditable(false);
0837:
0838: Element root = htmlDoc.getDefaultRootElement();
0839: Element body = root.getElement(0);
0840: Element p = body.getElement(0);
0841:
0842: htmlDoc.setInnerHTML(p, "<a>link</a>");
0843: Marker insertMarker = htmlDoc.getInsertMarker();
0844: assertEquals(new Integer(0), getInsertInfo(insertMarker).get(1));
0845: ElementSpec[] specs = (ElementSpec[]) (getInsertInfo(insertMarker)
0846: .get(0));
0847: insertMarker.reset();
0848: assertEquals(4, specs.length);
0849: assertSpec(specs[0], ElementSpec.ContentType,
0850: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0851: AttributeSet specAttr = specs[0].getAttributes();
0852: assertEquals(2, specAttr.getAttributeCount());
0853: checkAttributes(specAttr, StyleConstants.NameAttribute,
0854: Tag.CONTENT);
0855: assertSpec(specs[1], ElementSpec.ContentType,
0856: ElementSpec.OriginateDirection, 0, new char[] { '\n' });
0857: checkEndTagSpec(specs[2]);
0858: checkEndTagSpec(specs[3]);
0859: }
0860:
0861: public void testSetInnerHTML_Events() throws Exception {
0862: addElements();
0863: Element root = htmlDoc.getDefaultRootElement();
0864: Element branch1 = root.getElement(0);
0865: final Element branch2 = branch1.getElement(0);
0866: Element branch3 = branch2.getElement(0);
0867:
0868: htmlDoc.setParser(new ParserDelegator());
0869: DocumentController listener = new DocumentController();
0870: htmlDoc.addDocumentListener(listener);
0871: htmlDoc.setInnerHTML(branch2, "<a>link</a><b>bold</b>");
0872: assertEquals(2, listener.getNumEvents());
0873: checkEvent(branch2, listener.getEvent(0),
0874: DocumentEvent.EventType.INSERT, 4, 9, 0);
0875: checkEvent(branch2, listener.getEvent(1),
0876: DocumentEvent.EventType.REMOVE, 4, 14, 8);
0877:
0878: listener.reset();
0879: htmlDoc.setInnerHTML(branch1, "<a>link</a><b>bold</b>");
0880: assertEquals(2, listener.getNumEvents());
0881: checkEvent(branch1, listener.getEvent(0),
0882: DocumentEvent.EventType.INSERT, 4, 9, 0);
0883: checkEvent(branch1, listener.getEvent(1),
0884: DocumentEvent.EventType.REMOVE, 2, 10, 8);
0885: }
0886:
0887: public void testSetInnerHTML_Structure() throws Exception {
0888: addElements();
0889: Element root = htmlDoc.getDefaultRootElement();
0890: Element branch1 = root.getElement(0);
0891: Element branch2 = branch1.getElement(0);
0892: Element branch3 = branch2.getElement(0);
0893:
0894: htmlDoc.setParser(new ParserDelegator());
0895: assertEquals(4, branch2.getElementCount());
0896: htmlDoc.setInnerHTML(branch2, "<a>link</a><b>bold</b>");
0897: assertEquals("linkbold", htmlDoc
0898: .getText(0, htmlDoc.getLength()));
0899: assertEquals(3, branch2.getElementCount());
0900:
0901: htmlDoc.setInnerHTML(branch1, "<a>link</a><b>bold</b>");
0902: assertEquals("linkbold", htmlDoc
0903: .getText(0, htmlDoc.getLength()));
0904: assertEquals(3, branch1.getElementCount());
0905:
0906: htmlDoc.setInnerHTML(root, "<a>link</a><b>bold</b>");
0907: assertEquals("linkbold", htmlDoc
0908: .getText(0, htmlDoc.getLength()));
0909: assertEquals(3, root.getElementCount());
0910: }
0911:
0912: public void testSetInnerHTML_Exceptions() throws Exception {
0913: addElements();
0914: Element root = htmlDoc.getDefaultRootElement();
0915: Element branch1 = root.getElement(0);
0916: Element branch2 = branch1.getElement(0);
0917: Element branch3 = branch2.getElement(0);
0918:
0919: try {
0920: htmlDoc.setInnerHTML(branch3, "<a>link</a>");
0921: fail("IllegalStateException should be thrown");
0922: } catch (IllegalStateException e) {
0923: }
0924:
0925: htmlDoc.setParser(new ParserDelegator());
0926: try {
0927: htmlDoc.setInnerHTML(branch3, "<a>link</a>");
0928: fail("IllegalArgumentException should be thrown");
0929: } catch (IllegalArgumentException e) {
0930: }
0931:
0932: htmlDoc.setInnerHTML(null, "<a>link</a>");
0933: }
0934:
0935: public void testSetOuterHTML_Specs() throws Exception {
0936: htmlDoc.setParser(new ParserDelegator());
0937: loadDocument(htmlDoc,
0938: "<table><tr><td><br>table</td></tr></table>");
0939:
0940: Element root = htmlDoc.getDefaultRootElement();
0941: Element body = root.getElement(1);
0942: Element table = body.getElement(0);
0943: Element tr = table.getElement(0);
0944: Element td = tr.getElement(0);
0945: Element br = td.getElement(0).getElement(0);
0946:
0947: htmlDoc.setOuterHTML(br, "<a>link</a>");
0948: Marker insertMarker = htmlDoc.getInsertMarker();
0949: ElementSpec[] specs = (ElementSpec[]) (getInsertInfo(insertMarker)
0950: .get(0));
0951: assertEquals(new Integer(1), getInsertInfo(insertMarker).get(1));
0952: insertMarker.reset();
0953: checkEndTagSpec(specs[0]);
0954: checkEndTagSpec(specs[1]);
0955: checkStartJNTagSpec(specs[2]);
0956: checkStartJNTagSpec(specs[3]);
0957: checkStartJNTagSpec(specs[4]);
0958: checkStartJNTagSpec(specs[5]);
0959: checkStartJNTagSpec(specs[6]);
0960: assertSpec(specs[7], ElementSpec.ContentType,
0961: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0962:
0963: htmlDoc.setOuterHTML(td, "<a>link</a>");
0964: insertMarker = htmlDoc.getInsertMarker();
0965: assertEquals(new Integer(1), getInsertInfo(insertMarker).get(1));
0966: specs = (ElementSpec[]) (getInsertInfo(insertMarker).get(0));
0967: assertEquals(new Integer(1), getInsertInfo(insertMarker).get(1));
0968: insertMarker.reset();
0969: assertEquals(9, specs.length);
0970: checkEndTagSpec(specs[0]);
0971: checkEndTagSpec(specs[1]);
0972: checkStartJNTagSpec(specs[2]);
0973: checkStartJNTagSpec(specs[3]);
0974: checkStartJNTagSpec(specs[4]);
0975: assertSpec(specs[5], ElementSpec.ContentType,
0976: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0977: AttributeSet specAttr = specs[5].getAttributes();
0978: assertEquals(2, specAttr.getAttributeCount());
0979: checkAttributes(specAttr, StyleConstants.NameAttribute,
0980: Tag.CONTENT);
0981: assertSpec(specs[6], ElementSpec.ContentType,
0982: ElementSpec.OriginateDirection, 0, new char[] { '\n' });
0983: checkEndTagSpec(specs[7]);
0984: checkEndTagSpec(specs[8]);
0985:
0986: htmlDoc.setOuterHTML(tr, "<a>link</a>");
0987: assertEquals(new Integer(1), getInsertInfo(insertMarker).get(1));
0988: specs = (ElementSpec[]) (getInsertInfo(insertMarker).get(0));
0989: assertEquals(new Integer(1), getInsertInfo(insertMarker).get(1));
0990: insertMarker.reset();
0991: assertEquals(8, specs.length);
0992: checkEndTagSpec(specs[0]);
0993: checkEndTagSpec(specs[1]);
0994: checkStartJNTagSpec(specs[2]);
0995: checkStartJNTagSpec(specs[3]);
0996: assertSpec(specs[4], ElementSpec.ContentType,
0997: ElementSpec.OriginateDirection, 0, "link".toCharArray());
0998: specAttr = specs[4].getAttributes();
0999: assertEquals(2, specAttr.getAttributeCount());
1000: checkAttributes(specAttr, StyleConstants.NameAttribute,
1001: Tag.CONTENT);
1002: assertSpec(specs[5], ElementSpec.ContentType,
1003: ElementSpec.OriginateDirection, 0, new char[] { '\n' });
1004: checkEndTagSpec(specs[6]);
1005: checkEndTagSpec(specs[7]);
1006:
1007: htmlDoc.setOuterHTML(table, "<a>link</a>");
1008: assertEquals(new Integer(1), getInsertInfo(insertMarker).get(1));
1009: specs = (ElementSpec[]) (getInsertInfo(insertMarker).get(0));
1010: assertEquals(new Integer(1), getInsertInfo(insertMarker).get(1));
1011: insertMarker.reset();
1012: assertEquals(6, specs.length);
1013: checkEndTagSpec(specs[0]);
1014: checkEndTagSpec(specs[1]);
1015: checkStartJNTagSpec(specs[2]);
1016: assertSpec(specs[3], ElementSpec.ContentType,
1017: ElementSpec.OriginateDirection, 0, "link".toCharArray());
1018: specAttr = specs[3].getAttributes();
1019: assertEquals(2, specAttr.getAttributeCount());
1020: checkAttributes(specAttr, StyleConstants.NameAttribute,
1021: Tag.CONTENT);
1022: assertSpec(specs[4], ElementSpec.ContentType,
1023: ElementSpec.OriginateDirection, 0, new char[] { '\n' });
1024: checkEndTagSpec(specs[5]);
1025: }
1026:
1027: public void testSetOuterHTML_Specs2() throws Exception {
1028: htmlDoc.setParser(new ParserDelegator());
1029:
1030: Element root = htmlDoc.getDefaultRootElement();
1031: Element body = root.getElement(0);
1032: Element p = body.getElement(0);
1033:
1034: htmlDoc.setOuterHTML(p, "<a>link</a>");
1035: Marker insertMarker = htmlDoc.getInsertMarker();
1036: assertEquals(new Integer(0), getInsertInfo(insertMarker).get(1));
1037: ElementSpec[] specs = (ElementSpec[]) (getInsertInfo(insertMarker)
1038: .get(0));
1039: insertMarker.reset();
1040: assertEquals(4, specs.length);
1041: checkEndTagSpec(specs[0]);
1042: assertSpec(specs[1], ElementSpec.ContentType,
1043: ElementSpec.OriginateDirection, 0, "link".toCharArray());
1044: AttributeSet specAttr = specs[1].getAttributes();
1045: assertEquals(2, specAttr.getAttributeCount());
1046: checkAttributes(specAttr, StyleConstants.NameAttribute,
1047: Tag.CONTENT);
1048: assertSpec(specs[2], ElementSpec.ContentType,
1049: ElementSpec.OriginateDirection, 0, new char[] { '\n' });
1050: checkEndTagSpec(specs[3]);
1051: }
1052:
1053: public void testSetOuterHTML_Events() throws Exception {
1054: addElements();
1055: Element root = htmlDoc.getDefaultRootElement();
1056: Element branch1 = root.getElement(0);
1057: Element branch2 = branch1.getElement(0);
1058: Element branch3 = branch2.getElement(0);
1059: htmlDoc.setParser(new ParserDelegator());
1060:
1061: DocumentController listener = new DocumentController();
1062: htmlDoc.addDocumentListener(listener);
1063:
1064: assertEquals(4, branch2.getElementCount());
1065: htmlDoc.setOuterHTML(branch3, "<u>link</u>");
1066: assertEquals(4, branch2.getElementCount());
1067: assertEquals(2, listener.getNumEvents());
1068: checkEvent(branch2, listener.getEvent(0),
1069: DocumentEvent.EventType.INSERT, 2, 4, 0);
1070: checkEvent(branch2, listener.getEvent(1),
1071: DocumentEvent.EventType.REMOVE, 1, 4, 4);
1072:
1073: assertEquals(1, branch1.getElementCount());
1074: listener.reset();
1075: htmlDoc.setOuterHTML(branch2, "<a>link</a><b>aaaaaa</b>");
1076: assertEquals(3, branch1.getElementCount());
1077: assertEquals(2, listener.getNumEvents());
1078: checkEvent(branch1, listener.getEvent(0), EventType.INSERT, 4,
1079: 11, 0);
1080: checkEvent(branch1, listener.getEvent(1), EventType.REMOVE, 2,
1081: 14, 10);
1082: }
1083:
1084: public void testSetOuterHTML_Structure() throws Exception {
1085: addElements();
1086: Element root = htmlDoc.getDefaultRootElement();
1087: Element branch1 = root.getElement(0);
1088: Element branch2 = branch1.getElement(0);
1089:
1090: htmlDoc.setParser(new ParserDelegator());
1091: assertEquals(1, branch1.getElementCount());
1092: htmlDoc.setOuterHTML(branch2, "<a>link</a><b>bold</b>");
1093: assertEquals("linkbold", htmlDoc
1094: .getText(0, htmlDoc.getLength()));
1095: assertEquals(3, branch1.getElementCount());
1096:
1097: htmlDoc.setOuterHTML(branch1, "<a>link</a><b>bold</b>");
1098: assertEquals("linkbold", htmlDoc
1099: .getText(0, htmlDoc.getLength()));
1100: assertEquals(3, root.getElementCount());
1101:
1102: htmlDoc.setOuterHTML(root, "<a>link</a><b>bold</b>");
1103: assertEquals("linkbold", htmlDoc
1104: .getText(0, htmlDoc.getLength()));
1105: assertEquals(3, htmlDoc.getDefaultRootElement()
1106: .getElementCount());
1107: }
1108:
1109: public void testSetOuterHTML_Exceptions() throws Exception {
1110: addElements();
1111: Element root = htmlDoc.getDefaultRootElement();
1112: Element branch1 = root.getElement(0);
1113: Element branch2 = branch1.getElement(0);
1114: Element branch3 = branch2.getElement(0);
1115:
1116: try {
1117: htmlDoc.setOuterHTML(branch3, "<a>link</a>");
1118: fail("IllegalStateException should be thrown");
1119: } catch (IllegalStateException e) {
1120: }
1121:
1122: htmlDoc.setParser(new ParserDelegator());
1123: htmlDoc.setOuterHTML(null, "<a>link</a>");
1124: }
1125:
1126: private void checkEvent(final Element elem,
1127: final DocumentEvent event, final EventType type,
1128: final int numChanged, final int length, final int offset) {
1129: assertEquals("type", type, event.getType());
1130: if (type == EventType.INSERT) {
1131: assertEquals("inserted", numChanged, event.getChange(elem)
1132: .getChildrenAdded().length);
1133: } else if (type == EventType.REMOVE) {
1134: assertEquals("removed", numChanged, event.getChange(elem)
1135: .getChildrenRemoved().length);
1136: }
1137: assertEquals("length", length, event.getLength());
1138: assertEquals("offset", offset, event.getOffset());
1139: }
1140:
1141: private void addElements() throws BadLocationException {
1142: SimpleAttributeSet attr = new SimpleAttributeSet();
1143: attr.addAttribute(StyleConstants.NameAttribute, Tag.B
1144: .toString());
1145: htmlDoc.insertString(0, "0000", attr);
1146: attr.addAttribute(StyleConstants.NameAttribute, Tag.I
1147: .toString());
1148: htmlDoc.insertString(4, "1111", attr);
1149: attr.addAttribute(StyleConstants.NameAttribute, Tag.B
1150: .toString());
1151: htmlDoc.insertString(8, "2222", attr);
1152: }
1153:
1154: private void addElement() throws BadLocationException {
1155: SimpleAttributeSet attr = new SimpleAttributeSet();
1156: attr.addAttribute(StyleConstants.NameAttribute, Tag.B
1157: .toString());
1158: htmlDoc.insertString(0, "0000", attr);
1159: }
1160:
1161: private void checkStructure(final Document doc, final String[] names) {
1162: Element root = doc.getDefaultRootElement();
1163: ArrayList array = new ArrayList();
1164: addChildren(array, root);
1165: String[] structure = (String[]) array.toArray(new String[array
1166: .size()]);
1167: assertEquals(names.length, structure.length);
1168: for (int i = 0; i < structure.length; i++) {
1169: assertEquals(names[i], structure[i]);
1170: }
1171: }
1172:
1173: private void addChildren(final ArrayList array, final Element parent) {
1174: array.add(parent.getName());
1175: for (int i = 0; i < parent.getElementCount(); i++) {
1176: addChildren(array, parent.getElement(i));
1177: }
1178: }
1179: }
|