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 Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import javax.swing.BasicSwingTestCase;
023: import javax.swing.text.AbstractDocument.BranchElement;
024: import javax.swing.text.AbstractDocument.DefaultDocumentEvent;
025: import javax.swing.text.AbstractDocument.LeafElement;
026: import javax.swing.text.DefStyledDoc_Helpers.ElementBufferWithLogging;
027: import javax.swing.text.DefaultStyledDocument.ElementBuffer;
028: import javax.swing.text.DefaultStyledDocument.ElementSpec;
029:
030: public class DefaultStyledDocumentRTest extends BasicSwingTestCase {
031: private static final String ELEMENT_NAME = AbstractDocument.ElementNameAttribute;
032:
033: private DefaultStyledDocument doc;
034:
035: private Element root;
036:
037: private Element paragraph;
038:
039: private Element child;
040:
041: private int insertOffset = 5;
042:
043: private MutableAttributeSet attrs;
044:
045: private ElementBuffer buf;
046:
047: private ElementSpec[] specs;
048:
049: /**
050: * Insert one space with attributes containing element name
051: * <code>"icon"</code>.
052: */
053: public void testIconElement() throws Exception {
054: attrs.addAttribute(ELEMENT_NAME, "icon");
055: doc.insertString(insertOffset, " ", attrs);
056: assertEquals(3, paragraph.getElementCount());
057: child = paragraph.getElement(0);
058: assertEquals(AbstractDocument.ContentElementName, child
059: .getName());
060: child = paragraph.getElement(1);
061: assertEquals("icon", child.getName());
062: assertEquals(insertOffset, child.getStartOffset());
063: assertEquals(insertOffset + 1, child.getEndOffset());
064: child = paragraph.getElement(2);
065: assertEquals(AbstractDocument.ContentElementName, child
066: .getName());
067: }
068:
069: /**
070: * Insert two spaces with attributes containing element name
071: * <code>"icon"</code>.
072: */
073: public void testIconElementTwoSpaces() throws Exception {
074: attrs.addAttribute(ELEMENT_NAME, "icon");
075: doc.insertString(insertOffset, " ", attrs);
076: assertEquals(3, paragraph.getElementCount());
077: child = paragraph.getElement(0);
078: assertEquals(AbstractDocument.ContentElementName, child
079: .getName());
080: child = paragraph.getElement(1);
081: assertEquals("icon", child.getName());
082: assertEquals(insertOffset, child.getStartOffset());
083: assertEquals(insertOffset + 2, child.getEndOffset());
084: child = paragraph.getElement(2);
085: assertEquals(AbstractDocument.ContentElementName, child
086: .getName());
087: }
088:
089: /**
090: * Insert not spaces with attributes containing element name
091: * <code>"icon"</code>.
092: */
093: public void testIconElementNotSpaces() throws Exception {
094: attrs.addAttribute(ELEMENT_NAME, "icon");
095: doc.insertString(insertOffset, "ab", attrs);
096: assertEquals(3, paragraph.getElementCount());
097: child = paragraph.getElement(0);
098: assertEquals(AbstractDocument.ContentElementName, child
099: .getName());
100: child = paragraph.getElement(1);
101: assertEquals("icon", child.getName());
102: assertEquals(insertOffset, child.getStartOffset());
103: assertEquals(insertOffset + 2, child.getEndOffset());
104: assertEquals("ab", doc.getText(insertOffset, 2));
105: child = paragraph.getElement(2);
106: assertEquals(AbstractDocument.ContentElementName, child
107: .getName());
108: }
109:
110: /**
111: * Insert not spaces with attributes containing element name
112: * <code>"component"</code>.
113: */
114: public void testComponentElement() throws Exception {
115: attrs.addAttribute(ELEMENT_NAME, "component");
116: doc.insertString(insertOffset, "ab", attrs);
117: assertEquals(3, paragraph.getElementCount());
118: child = paragraph.getElement(0);
119: assertEquals(AbstractDocument.ContentElementName, child
120: .getName());
121: child = paragraph.getElement(1);
122: assertEquals("component", child.getName());
123: assertEquals(insertOffset, child.getStartOffset());
124: assertEquals(insertOffset + 2, child.getEndOffset());
125: assertEquals("ab", doc.getText(insertOffset, 2));
126: child = paragraph.getElement(2);
127: assertEquals(AbstractDocument.ContentElementName, child
128: .getName());
129: }
130:
131: public void testDeepTreeInsertString01() throws Exception {
132: initStructure();
133: attrs.addAttribute(ELEMENT_NAME, "content");
134: doc.insertString(1, "\n", attrs);
135: assertEquals(7, specs.length);
136: assertSpec(specs[0], ElementSpec.EndTagType,
137: ElementSpec.OriginateDirection, 0, 0);
138: assertSpec(specs[1], ElementSpec.EndTagType,
139: ElementSpec.OriginateDirection, 0, 0);
140: assertSpec(specs[2], ElementSpec.StartTagType,
141: ElementSpec.JoinNextDirection, 0, 0);
142: assertSpec(specs[3], ElementSpec.StartTagType,
143: ElementSpec.OriginateDirection, 0, 0);
144: assertSpec(specs[4], ElementSpec.ContentType,
145: ElementSpec.OriginateDirection, 0, 1);
146: assertSpec(specs[5], ElementSpec.EndTagType,
147: ElementSpec.OriginateDirection, 0, 0);
148: assertSpec(specs[6], ElementSpec.StartTagType,
149: ElementSpec.JoinNextDirection, 0, 0);
150: }
151:
152: public void testDeepTreeInsertString02() throws Exception {
153: initStructure();
154: attrs.addAttribute(ELEMENT_NAME, "content");
155: doc.insertString(1, "^", attrs);
156: assertEquals(5, specs.length);
157: assertSpec(specs[0], ElementSpec.EndTagType,
158: ElementSpec.OriginateDirection, 0, 0);
159: assertSpec(specs[1], ElementSpec.EndTagType,
160: ElementSpec.OriginateDirection, 0, 0);
161: assertSpec(specs[2], ElementSpec.StartTagType,
162: ElementSpec.JoinNextDirection, 0, 0);
163: assertSpec(specs[3], ElementSpec.StartTagType,
164: ElementSpec.JoinNextDirection, 0, 0);
165: assertSpec(specs[4], ElementSpec.ContentType,
166: ElementSpec.OriginateDirection, 0, 1);
167: }
168:
169: public void testDeepTreeInsert01() throws Exception {
170: initStructure();
171: ElementSpec[] specs = {
172: new ElementSpec(null, ElementSpec.EndTagType), // 0
173: new ElementSpec(null, ElementSpec.EndTagType), // 1
174: new ElementSpec(null, ElementSpec.StartTagType), // 2
175: new ElementSpec(null, ElementSpec.StartTagType), // 3
176: new ElementSpec(null, ElementSpec.ContentType, // 4
177: "\n".toCharArray(), 0, 1),
178: new ElementSpec(null, ElementSpec.EndTagType), // 5
179: new ElementSpec(null, ElementSpec.StartTagType), // 6
180: };
181: specs[2].setDirection(ElementSpec.JoinNextDirection);
182: specs[6].setDirection(ElementSpec.JoinNextDirection);
183: doc.insert(1, specs);
184: final Element html = doc.getDefaultRootElement();
185: assertEquals(2, html.getElementCount());
186: final Element head = html.getElement(0);
187: assertEquals(1, head.getElementCount());
188: Element p = head.getElement(0);
189: assertChildren(p, new int[] { 0, 1 });
190: final Element body = html.getElement(1);
191: assertEquals(2, body.getElementCount());
192: p = body.getElement(0);
193: assertEquals("paragraph", p.getName());
194: assertChildren(p, new int[] { 1, 2 });
195: p = body.getElement(1);
196: assertEquals("p1", p.getName());
197: assertChildren(p, new int[] { 2, 6, 6, 7 });
198: }
199:
200: public void testDeepTreeInsert02() throws Exception {
201: initStructure();
202: ElementSpec[] specs = {
203: new ElementSpec(null, ElementSpec.EndTagType), // 0
204: new ElementSpec(null, ElementSpec.EndTagType), // 1
205: new ElementSpec(null, ElementSpec.StartTagType), // 2
206: new ElementSpec(null, ElementSpec.StartTagType), // 3
207: new ElementSpec(null, ElementSpec.ContentType, // 4
208: "^".toCharArray(), 0, 1), };
209: specs[2].setDirection(ElementSpec.JoinNextDirection);
210: specs[3].setDirection(ElementSpec.JoinNextDirection);
211: doc.insert(1, specs);
212: final Element html = doc.getDefaultRootElement();
213: assertEquals(2, html.getElementCount());
214: final Element head = html.getElement(0);
215: assertEquals(1, head.getElementCount());
216: Element p = head.getElement(0);
217: assertChildren(p, new int[] { 0, 1 });
218: final Element body = html.getElement(1);
219: assertEquals(1, body.getElementCount());
220: p = body.getElement(0);
221: assertEquals("p1", p.getName());
222: assertChildren(p, new int[] { 1, 2, 2, 6, 6, 7 });
223: }
224:
225: public void testDeepTreeInsertSpecs() throws Exception {
226: initStructure();
227: ElementSpec[] specs = {
228: new ElementSpec(null, ElementSpec.ContentType, "\n"
229: .toCharArray(), 0, 1),
230: new ElementSpec(null, ElementSpec.EndTagType),
231: new ElementSpec(null, ElementSpec.EndTagType), };
232: doc.insert(0, specs);
233: final Element html = doc.getDefaultRootElement();
234: assertEquals(2, html.getElementCount());
235: final Element head = html.getElement(0);
236: assertEquals(1, head.getElementCount());
237: Element p = head.getElement(0);
238: assertChildren(p, new int[] { 0, 1, 1, 2 });
239: assertEquals("\n\n", doc.getText(0, 2));
240: final Element body = html.getElement(1);
241: assertEquals(1, body.getElementCount());
242: p = body.getElement(0);
243: assertEquals("p1", p.getName());
244: assertChildren(p, new int[] { 2, 6, 6, 7 });
245: }
246:
247: public void testHTMLInsert() throws Exception {
248: createEmptyHTMLStructure();
249: doc.insertString(0, "0000", DefStyledDoc_Helpers.bold);
250: ElementSpec[] specs = {
251: new ElementSpec(null, ElementSpec.EndTagType),
252: new ElementSpec(null, ElementSpec.ContentType, "^^^^"
253: .toCharArray(), 0, 4), };
254: doc.insert(0, specs);
255: final Element html = doc.getDefaultRootElement();
256: assertEquals(1, html.getElementCount());
257: final Element body = html.getElement(0);
258: assertEquals(2, body.getElementCount());
259: Element child = body.getElement(0);
260: assertEquals("content", child.getName());
261: assertTrue(child.isLeaf());
262: child = body.getElement(1);
263: assertEquals("p", child.getName());
264: assertChildren(child, new int[] { 4, 8, 8, 9 });
265: }
266:
267: public void testCreate01() throws Exception {
268: ElementSpec[] specs = {
269: new ElementSpec(null, ElementSpec.StartTagType),
270: new ElementSpec(null, ElementSpec.ContentType, "^"
271: .toCharArray(), 0, 1),
272: new ElementSpec(null, ElementSpec.EndTagType), };
273: doc.create(specs);
274: root = doc.getDefaultRootElement();
275: assertEquals(2, root.getElementCount());
276: Element child = root.getElement(0);
277: assertTrue(child.isLeaf());
278: assertEquals(0, child.getStartOffset());
279: assertEquals(1, child.getEndOffset());
280: child = root.getElement(1);
281: assertFalse(child.isLeaf());
282: assertEquals(1, child.getStartOffset());
283: assertEquals(2, child.getEndOffset());
284: assertEquals(1, child.getElementCount());
285: assertTrue(child.getElement(0).isLeaf());
286: }
287:
288: public void testCreate02() throws Exception {
289: ElementSpec[] specs = {
290: new ElementSpec(null, ElementSpec.StartTagType),
291: new ElementSpec(null, ElementSpec.StartTagType),
292: new ElementSpec(null, ElementSpec.StartTagType),
293: new ElementSpec(null, ElementSpec.ContentType, "^"
294: .toCharArray(), 0, 1),
295: new ElementSpec(null, ElementSpec.EndTagType),
296: new ElementSpec(null, ElementSpec.EndTagType), };
297: doc.create(specs);
298: root = doc.getDefaultRootElement();
299: assertEquals(2, root.getElementCount());
300: Element child = root.getElement(0);
301: assertFalse(child.isLeaf());
302: assertEquals(0, child.getStartOffset());
303: assertEquals(1, child.getEndOffset());
304: assertEquals(1, child.getElementCount());
305: child = child.getElement(0);
306: assertFalse(child.isLeaf());
307: assertEquals(1, child.getElementCount());
308: assertTrue(child.getElement(0).isLeaf());
309: child = root.getElement(1);
310: assertFalse(child.isLeaf());
311: assertEquals(1, child.getStartOffset());
312: assertEquals(2, child.getEndOffset());
313: assertEquals(1, child.getElementCount());
314: assertTrue(child.getElement(0).isLeaf());
315: }
316:
317: /**
318: * Tests insertion of a character at the start of a paragraph where
319: * the previous paragraph and the character inserted have equal attribute
320: * sets whereas the next paragraph to which the character inserted should
321: * belong has other attribute set.
322: * @throws BadLocationException
323: */
324: public void testInsertString01() throws BadLocationException {
325: doc.remove(0, doc.getLength());
326: doc.buffer = new ElementBufferWithLogging(doc, root) {
327: private static final long serialVersionUID = 1L;
328:
329: @Override
330: public void insert(int offset, int length,
331: ElementSpec[] spec, DefaultDocumentEvent event) {
332: specs = spec;
333: super .insert(offset, length, spec, event);
334: }
335: };
336: StyleConstants.setBold(attrs, true);
337: doc.insertString(0, "b", attrs);
338: doc.insertString(0, "\n", null);
339: doc.insertString(1, "1", null);
340: ElementSpec[] ess = {
341: new ElementSpec(null, ElementSpec.EndTagType),
342: new ElementSpec(null, ElementSpec.StartTagType),
343: new ElementSpec(null, ElementSpec.ContentType, 1) };
344: ess[1].setDirection(ElementSpec.JoinNextDirection);
345: assertEquals(ess.length, specs.length);
346: for (int i = 0; i < ess.length; i++) {
347: assertEquals("@ " + i, ess[i].getType(), specs[i].getType());
348: assertEquals("@ " + i, ess[i].getDirection(), specs[i]
349: .getDirection());
350: }
351: int[][] offsets = { { 0, 1 }, { 1, 2, 2, 3, 3, 4 } };
352: for (int i = 0; i < root.getElementCount(); i++) {
353: Element paragraph = root.getElement(i);
354: for (int j = 0, oi = 0; j < paragraph.getElementCount(); j++) {
355: Element content = paragraph.getElement(j);
356: assertEquals("root[" + i + "].start", offsets[i][oi++],
357: content.getStartOffset());
358: assertEquals("root[" + i + "].end", offsets[i][oi++],
359: content.getEndOffset());
360: if (i == 1 && j == 1) {
361: assertTrue(StyleConstants.isBold(content
362: .getAttributes()));
363: } else {
364: assertEquals(0, content.getAttributes()
365: .getAttributeCount());
366: }
367: }
368: }
369: }
370:
371: /**
372: * Tests insertion of a character at the start of a styled run where
373: * the previous run and the character inserted have equal attribute
374: * sets whereas the next run has other attribute set.
375: * @throws BadLocationException
376: */
377: public void testInsertString02() throws BadLocationException {
378: doc.remove(0, doc.getLength());
379: doc.buffer = new ElementBufferWithLogging(doc, root) {
380: private static final long serialVersionUID = 1L;
381:
382: @Override
383: public void insert(int offset, int length,
384: ElementSpec[] spec, DefaultDocumentEvent event) {
385: specs = spec;
386: super .insert(offset, length, spec, event);
387: }
388: };
389: StyleConstants.setBold(attrs, true);
390: doc.insertString(0, "b", attrs);
391: doc.insertString(0, "\n", null);
392: doc.insertString(1, "1", null);
393: doc.insertString(2, "1", null);
394: assertEquals(1, specs.length);
395: assertEquals(ElementSpec.ContentType, specs[0].getType());
396: assertEquals(ElementSpec.JoinPreviousDirection, specs[0]
397: .getDirection());
398: int[][] offsets = { { 0, 1 }, { 1, 3, 3, 4, 4, 5 } };
399: for (int i = 0; i < root.getElementCount(); i++) {
400: Element paragraph = root.getElement(i);
401: for (int j = 0, oi = 0; j < paragraph.getElementCount(); j++) {
402: Element content = paragraph.getElement(j);
403: assertEquals("root[" + i + "].start", offsets[i][oi++],
404: content.getStartOffset());
405: assertEquals("root[" + i + "].end", offsets[i][oi++],
406: content.getEndOffset());
407: if (i == 1 && j == 1) {
408: assertTrue(StyleConstants.isBold(content
409: .getAttributes()));
410: } else {
411: assertEquals(0, content.getAttributes()
412: .getAttributeCount());
413: }
414: }
415: }
416: }
417:
418: @Override
419: protected void setUp() throws Exception {
420: super .setUp();
421: doc = new DefStyledDoc_Helpers.DefStyledDocWithLogging();
422: doc.insertString(0, "test text", null);
423: root = doc.getDefaultRootElement();
424: paragraph = root.getElement(0);
425: attrs = new SimpleAttributeSet();
426: }
427:
428: private void initStructure() throws BadLocationException {
429: doc = new DefStyledDoc_Helpers.DefStyledDocWithLogging();
430: root = doc.getDefaultRootElement();
431: buf = new DefStyledDoc_Helpers.ElementBufferWithLogging(doc,
432: root) {
433: private static final long serialVersionUID = 1L;
434:
435: @Override
436: public void insert(int offset, int length,
437: ElementSpec[] spec, DefaultDocumentEvent event) {
438: super .insert(offset, length, specs = spec, event);
439: }
440: };
441: doc.buffer = buf;
442: doc.writeLock();
443: try {
444: doc.getContent().insertString(0, "\n0000");
445: final BranchElement html = (BranchElement) root;
446: html.addAttribute(ELEMENT_NAME, "html");
447: final BranchElement head = createBranch(html);
448: head.addAttribute(ELEMENT_NAME, "head");
449: final BranchElement implied = createBranch(head);
450: implied.addAttribute(ELEMENT_NAME, "p-implied");
451: final LeafElement content0 = createLeaf(implied, 0, 1);
452: content0.addAttribute(ELEMENT_NAME, "head-content");
453: final BranchElement body = createBranch(html);
454: body.addAttribute(ELEMENT_NAME, "body");
455: final BranchElement p1 = createBranch(body);
456: p1.addAttribute(ELEMENT_NAME, "p1");
457: final LeafElement content1 = createLeaf(p1, 1, 5);
458: content1.addAttribute(ELEMENT_NAME, "leaf1");
459: final LeafElement content2 = createLeaf(p1, 5, 6);
460: content2.addAttribute(ELEMENT_NAME, "leaf2");
461: implied.replace(0, 0, new Element[] { content0 });
462: p1.replace(0, 0, new Element[] { content1, content2 });
463: head.replace(0, 0, new Element[] { implied });
464: body.replace(0, 0, new Element[] { p1 });
465: html.replace(0, 1, new Element[] { head, body });
466: } finally {
467: doc.writeUnlock();
468: }
469: }
470:
471: private void createEmptyHTMLStructure() {
472: doc = new DefStyledDoc_Helpers.DefStyledDocWithLogging();
473: root = doc.getDefaultRootElement();
474: buf = new DefStyledDoc_Helpers.ElementBufferWithLogging(doc,
475: root) {
476: private static final long serialVersionUID = 1L;
477:
478: @Override
479: public void insert(int offset, int length,
480: ElementSpec[] spec, DefaultDocumentEvent event) {
481: super .insert(offset, length, specs = spec, event);
482: }
483: };
484: doc.buffer = buf;
485: doc.writeLock();
486: try {
487: final BranchElement html = (BranchElement) root;
488: html.addAttribute(ELEMENT_NAME, "html");
489: final BranchElement body = createBranch(html);
490: body.addAttribute(ELEMENT_NAME, "body");
491: final BranchElement p = createBranch(body);
492: p.addAttribute(ELEMENT_NAME, "p");
493: final LeafElement content = createLeaf(p, 0, 1);
494: content.addAttribute(ELEMENT_NAME, "leaf1");
495: p.replace(0, 0, new Element[] { content });
496: body.replace(0, 0, new Element[] { p });
497: html.replace(0, 1, new Element[] { body });
498: } finally {
499: doc.writeUnlock();
500: }
501: }
502:
503: private BranchElement createBranch(final Element parent) {
504: return (BranchElement) doc.createBranchElement(parent, null);
505: }
506:
507: private LeafElement createLeaf(final Element parent,
508: final int start, final int end) {
509: return (LeafElement) doc.createLeafElement(parent, null, start,
510: end);
511: }
512:
513: private static void assertChildren(final Element element,
514: final int[] offsets) {
515: DefStyledDoc_Helpers.assertChildren(element, offsets);
516: }
517:
518: private static void assertSpec(final ElementSpec spec,
519: final short type, final short direction, final int offset,
520: final int length) {
521: DefStyledDoc_Helpers.assertSpec(spec, type, direction, offset,
522: length);
523: }
524: }
|