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 java.util.List;
023: import java.util.Vector;
024: import javax.swing.event.DocumentEvent;
025: import javax.swing.event.DocumentListener;
026: import javax.swing.text.AbstractDocument.DefaultDocumentEvent;
027: import javax.swing.text.DefaultStyledDocument.ElementBuffer;
028: import javax.swing.text.DefaultStyledDocument.ElementSpec;
029: import junit.framework.TestCase;
030:
031: /**
032: * Tests DefaultStyledDocument class, in particular which ElementSpecs are
033: * created during insertString with different parameters.
034: * <p>
035: * These tests insert some text into empty document,
036: * i.e. <code>doc.getLength() == 0</code>.
037: * But it is important to note that actually document contains the implied
038: * newline character. Thus document will have one <em>paragraph</em> (Branch)
039: * and one <em>content</em> (Leaf) under the paragraph.
040: * <p>
041: * Tests are classified as follows:
042: * <dl>
043: * <dt>0x</dt>
044: * <dd>No attributes are set or passed as parameters.</dd>
045: *
046: * <dt>1x</dt>
047: * <dd>The paragraph contains <code>bold</code> attributes;
048: * the text is inserted with <code>null</code>
049: * as <code>attrs</code> parameter.</dd>
050: *
051: * <dt>2x</dt>
052: * <dd>The character attributes set to <code>bold</code>;
053: * the text is inserted with <code>null</code>
054: * as <code>attrs</code> parameter.</dd>
055: *
056: * <dt>3x</dt>
057: * <dd>No attributes are set;
058: * the text is inserted with <code>italic</code> attributes.</dd>
059: *
060: * <dt>4x</dt>
061: * <dd>The paragraph contains <code>bold</code> attributes;
062: * the text is inserted with <code>italic</code> attributes.</dd>
063: *
064: * <dt>5x</dt>
065: * <dd>The character attributes set to <code>bold</code>;
066: * the text is inserted with <code>italic</code> attributes.</dd>
067: * </dl>
068: * <p>Each test-case region currently contains four tests.
069: *
070: */
071: public class DefaultStyledDocument_ElementBuffer_Specs1Test extends
072: TestCase implements DocumentListener {
073: private DefaultStyledDocument doc;
074:
075: private Element root;
076:
077: private ElementBuffer buf;
078:
079: private ElementSpec[] specs;
080:
081: private DefaultDocumentEvent insertEvent;
082:
083: private static final AttributeSet bold = DefStyledDoc_Helpers.bold;
084:
085: private static final AttributeSet italic = DefStyledDoc_Helpers.italic;
086:
087: @Override
088: protected void setUp() throws Exception {
089: super .setUp();
090: doc = new DefStyledDoc_Helpers.DefStyledDocWithLogging();
091: root = doc.getDefaultRootElement();
092: buf = new DefStyledDoc_Helpers.ElementBufferWithLogging(doc,
093: root) {
094: private static final long serialVersionUID = 1L;
095:
096: @Override
097: public void insert(int offset, int length,
098: ElementSpec[] spec, DefaultDocumentEvent event) {
099: super .insert(offset, length, specs = spec, event);
100: }
101: };
102: doc.buffer = buf;
103: doc.addDocumentListener(this );
104: }
105:
106: /**
107: * No attributes, text 'one', doc is empty.
108: */
109: public void testInsertString01() throws Exception {
110: doc.insertString(0, "one", null);
111: assertEquals(1, getEdits(insertEvent).size());
112: assertEquals(1, specs.length);
113: assertSpec(specs[0], ElementSpec.ContentType,
114: ElementSpec.JoinPreviousDirection, 0, 3);
115: }
116:
117: /**
118: * No attributes, text 'one\n', doc is empty.
119: */
120: public void testInsertString02() throws Exception {
121: doc.insertString(0, "one\n", null);
122: List<?> edits = getEdits(insertEvent);
123: assertEquals(3, edits.size());
124: assertChange(edits.get(1), root.getElement(0), 1, 1);
125: assertChange(edits.get(2), root, 0, 1);
126: assertEquals(3, specs.length);
127: assertSpec(specs[0], ElementSpec.ContentType,
128: ElementSpec.JoinPreviousDirection, 0, 4);
129: assertSpec(specs[1], ElementSpec.EndTagType,
130: ElementSpec.OriginateDirection, 0, 0);
131: assertSpec(specs[2], ElementSpec.StartTagType,
132: ElementSpec.JoinFractureDirection, 0, 0);
133: }
134:
135: /**
136: * No attributes, text '\none', doc is empty.
137: */
138: public void testInsertString03() throws Exception {
139: doc.insertString(0, "\none", null);
140: List<?> edits = getEdits(insertEvent);
141: assertEquals(4, edits.size());
142: assertChange(edits.get(1), root.getElement(0), 1, 1);
143: assertChange(edits.get(2), root.getElement(1), 1, 1);
144: assertChange(edits.get(3), root, 0, 1);
145: assertEquals(4, specs.length);
146: assertSpec(specs[0], ElementSpec.ContentType,
147: ElementSpec.JoinPreviousDirection, 0, 1);
148: assertSpec(specs[1], ElementSpec.EndTagType,
149: ElementSpec.OriginateDirection, 0, 0);
150: assertSpec(specs[2], ElementSpec.StartTagType,
151: ElementSpec.JoinFractureDirection, 0, 0);
152: assertSpec(specs[3], ElementSpec.ContentType,
153: ElementSpec.JoinNextDirection, 0, 3);
154: /*
155: * createLeaf(paragraph[0, 5], , 0, 1)
156: * New leaf containing \n in the first paragraph
157: *
158: * createBranch(section[0, 5], resolver=**AttributeSet** )
159: * New branch as result of Fracture
160: * createLeaf(paragraph[N/A], , 4, 5)
161: * As fracture copies elements, this is "the copy" added to
162: * the newly created branch
163: *
164: * createLeaf(paragraph[4, 5], , 1, 5)
165: * This leaf goes the newly created branch and contains
166: * the right characters. The leaf created as the result
167: * of fracture is removed.
168: */
169: }
170:
171: /**
172: * No attributes, text 'one\ntwo', doc is empty.
173: */
174: public void testInsertString04() throws Exception {
175: doc.insertString(0, "one\ntwo", null);
176: List<?> edits = getEdits(insertEvent);
177: assertEquals(4, edits.size());
178: assertChange(edits.get(1), root.getElement(0), 1, 1);
179: assertChange(edits.get(2), root.getElement(1), 1, 1);
180: assertChange(edits.get(3), root, 0, 1);
181: assertEquals(4, specs.length);
182: assertSpec(specs[0], ElementSpec.ContentType,
183: ElementSpec.JoinPreviousDirection, 0, 4);
184: assertSpec(specs[1], ElementSpec.EndTagType,
185: ElementSpec.OriginateDirection, 0, 0);
186: assertSpec(specs[2], ElementSpec.StartTagType,
187: ElementSpec.JoinFractureDirection, 0, 0);
188: assertSpec(specs[3], ElementSpec.ContentType,
189: ElementSpec.JoinNextDirection, 0, 3);
190: }
191:
192: //---------------------------------------------------------------------------
193: /**
194: * Bold attribute on paragraph, text 'one' with no attributes, doc is empty.
195: */
196: public void testInsertString11() throws Exception {
197: doc.setParagraphAttributes(0, 1, bold, false);
198: doc.insertString(0, "one", null);
199: assertEquals(1, getEdits(insertEvent).size());
200: assertTrue(root.getElement(0).getAttributes()
201: .containsAttributes(bold));
202: assertEquals(1, specs.length);
203: assertSpec(specs[0], ElementSpec.ContentType,
204: ElementSpec.JoinPreviousDirection, 0, 3);
205: }
206:
207: /**
208: * Bold attribute on paragraph, text 'one\n' with no attributes, doc is
209: * empty.
210: */
211: public void testInsertString12() throws Exception {
212: doc.setParagraphAttributes(0, 1, bold, false);
213: doc.insertString(0, "one\n", null);
214: List<?> edits = getEdits(insertEvent);
215: assertEquals(3, edits.size());
216: assertChange(edits.get(1), root.getElement(0), 1, 1);
217: assertChange(edits.get(2), root, 0, 1);
218: assertTrue(root.getElement(0).getAttributes()
219: .containsAttributes(bold));
220: assertTrue(root.getElement(1).getAttributes()
221: .containsAttributes(bold));
222: assertEquals(3, specs.length);
223: assertSpec(specs[0], ElementSpec.ContentType,
224: ElementSpec.JoinPreviousDirection, 0, 4);
225: assertSpec(specs[1], ElementSpec.EndTagType,
226: ElementSpec.OriginateDirection, 0, 0);
227: assertSpec(specs[2], ElementSpec.StartTagType,
228: ElementSpec.JoinFractureDirection, 0, 0);
229: /*
230: * Fracture copies attributes of the paragraph:
231: *
232: * createBranch(section[0, 5], resolver=**AttributeSet** bold=true )
233: */
234: }
235:
236: /**
237: * Bold attribute on paragraph, text '\none' with no attributes, doc is
238: * empty.
239: */
240: public void testInsertString13() throws Exception {
241: doc.setParagraphAttributes(0, 1, bold, false);
242: doc.insertString(0, "\none", null);
243: List<?> edits = getEdits(insertEvent);
244: assertEquals(4, edits.size());
245: assertChange(edits.get(1), root.getElement(0), 1, 1);
246: assertChange(edits.get(2), root.getElement(1), 1, 1);
247: assertChange(edits.get(3), root, 0, 1);
248: assertTrue(root.getElement(0).getAttributes()
249: .containsAttributes(bold));
250: assertTrue(root.getElement(1).getAttributes()
251: .containsAttributes(bold));
252: assertEquals(4, specs.length);
253: assertSpec(specs[0], ElementSpec.ContentType,
254: ElementSpec.JoinPreviousDirection, 0, 1);
255: assertSpec(specs[1], ElementSpec.EndTagType,
256: ElementSpec.OriginateDirection, 0, 0);
257: assertSpec(specs[2], ElementSpec.StartTagType,
258: ElementSpec.JoinFractureDirection, 0, 0);
259: assertSpec(specs[3], ElementSpec.ContentType,
260: ElementSpec.JoinNextDirection, 0, 3);
261: }
262:
263: /**
264: * Bold attribute on paragraph, text 'one\ntwo' with no attributes, doc is
265: * empty.
266: */
267: public void testInsertString14() throws Exception {
268: doc.setParagraphAttributes(0, 1, bold, false);
269: doc.insertString(0, "one\ntwo", null);
270: List<?> edits = getEdits(insertEvent);
271: assertEquals(4, edits.size());
272: assertChange(edits.get(1), root.getElement(0), 1, 1);
273: assertChange(edits.get(2), root.getElement(1), 1, 1);
274: assertChange(edits.get(3), root, 0, 1);
275: assertTrue(root.getElement(0).getAttributes()
276: .containsAttributes(bold));
277: assertTrue(root.getElement(1).getAttributes()
278: .containsAttributes(bold));
279: assertEquals(4, specs.length);
280: assertSpec(specs[0], ElementSpec.ContentType,
281: ElementSpec.JoinPreviousDirection, 0, 4);
282: assertSpec(specs[1], ElementSpec.EndTagType,
283: ElementSpec.OriginateDirection, 0, 0);
284: assertSpec(specs[2], ElementSpec.StartTagType,
285: ElementSpec.JoinFractureDirection, 0, 0);
286: assertSpec(specs[3], ElementSpec.ContentType,
287: ElementSpec.JoinNextDirection, 0, 3);
288: }
289:
290: //---------------------------------------------------------------------------
291: /**
292: * Bold attribute on character, text 'one' with no attributes, doc is empty.
293: */
294: public void testInsertString21() throws Exception {
295: doc.setCharacterAttributes(0, 1, bold, false);
296: doc.insertString(0, "one", null);
297: List<?> edits = getEdits(insertEvent);
298: assertEquals(2, edits.size());
299: assertChange(edits.get(1), root.getElement(0), 1, 2);
300: Element charElem = doc.getCharacterElement(0);
301: assertEquals(0, charElem.getAttributes().getAttributeCount());
302: charElem = doc.getCharacterElement(doc.getLength());
303: assertTrue(charElem.getAttributes().isEqual(bold));
304: assertEquals(1, specs.length);
305: assertSpec(specs[0], ElementSpec.ContentType,
306: ElementSpec.OriginateDirection, 0, 3);
307: /*
308: * createLeaf(paragraph[0, 4], , 0, 3)
309: * The word 'one'
310: * createLeaf(paragraph[0, 4], bold=true , 3, 4)
311: * The attributed newline char
312: */
313: }
314:
315: /**
316: * Bold attribute on character, text 'one\n' with no attributes, doc is
317: * empty.
318: */
319: public void testInsertString22() throws Exception {
320: doc.setCharacterAttributes(0, 1, bold, false);
321: doc.insertString(0, "one\n", null);
322: List<?> edits = getEdits(insertEvent);
323: assertEquals(3, edits.size());
324: assertChange(edits.get(1), root.getElement(0), 1, 1);
325: assertChange(edits.get(2), root, 0, 1);
326: Element charElem = doc.getCharacterElement(0);
327: assertEquals(0, charElem.getAttributes().getAttributeCount());
328: assertEquals(0, charElem.getStartOffset());
329: assertEquals(4, charElem.getEndOffset());
330: charElem = doc.getCharacterElement(doc.getLength());
331: assertTrue(charElem.getAttributes().isEqual(bold));
332: assertEquals(4, charElem.getStartOffset());
333: assertEquals(5, charElem.getEndOffset());
334: assertEquals(3, specs.length);
335: assertSpec(specs[0], ElementSpec.ContentType,
336: ElementSpec.OriginateDirection, 0, 4);
337: assertSpec(specs[1], ElementSpec.EndTagType,
338: ElementSpec.OriginateDirection, 0, 0);
339: assertSpec(specs[2], ElementSpec.StartTagType,
340: ElementSpec.JoinFractureDirection, 0, 0);
341: /*
342: * While copying elements with fracture, it will copy leaf attributes:
343: *
344: * createBranch(section[0, 5], resolver=**AttributeSet** )
345: * createLeaf(paragraph[N/A], bold=true , 4, 5)
346: */
347: }
348:
349: /**
350: * Bold attribute on character, text '\none' with no attributes, doc is
351: * empty.
352: */
353: public void testInsertString23() throws Exception {
354: doc.setCharacterAttributes(0, 1, bold, false);
355: doc.insertString(0, "\none", null);
356: List<?> edits = getEdits(insertEvent);
357: assertEquals(4, edits.size());
358: assertChange(edits.get(1), root.getElement(0), 1, 1);
359: assertChange(edits.get(2), root.getElement(1), 0, 1);
360: assertChange(edits.get(3), root, 0, 1);
361: Element charElem = doc.getCharacterElement(0);
362: assertEquals(0, charElem.getAttributes().getAttributeCount());
363: assertEquals(0, charElem.getStartOffset());
364: assertEquals(1, charElem.getEndOffset());
365: charElem = doc.getCharacterElement(1);
366: assertEquals(0, charElem.getAttributes().getAttributeCount());
367: assertEquals(1, charElem.getStartOffset());
368: assertEquals(4, charElem.getEndOffset());
369: charElem = doc.getCharacterElement(doc.getLength());
370: assertTrue(charElem.getAttributes().isEqual(bold));
371: assertEquals(4, charElem.getStartOffset());
372: assertEquals(5, charElem.getEndOffset());
373: assertEquals(4, specs.length);
374: assertSpec(specs[0], ElementSpec.ContentType,
375: ElementSpec.OriginateDirection, 0, 1);
376: assertSpec(specs[1], ElementSpec.EndTagType,
377: ElementSpec.OriginateDirection, 0, 0);
378: assertSpec(specs[2], ElementSpec.StartTagType,
379: ElementSpec.JoinFractureDirection, 0, 0);
380: assertSpec(specs[3], ElementSpec.ContentType,
381: ElementSpec.OriginateDirection, 0, 3);
382: /*
383: * createLeaf(paragraph[0, 5], , 0, 1)
384: * createBranch(section[0, 5], * resolver=**AttributeSet** )
385: * createLeaf(paragraph[N/A], bold=true , 4, 5)
386: * createLeaf(paragraph[4, 5], , 1, 4)
387: */
388: }
389:
390: /**
391: * Bold attribute on character, text 'one\ntwo' with no attributes, doc is
392: * empty.
393: */
394: public void testInsertString24() throws Exception {
395: doc.setCharacterAttributes(0, 1, bold, false);
396: doc.insertString(0, "one\ntwo", null);
397: List<?> edits = getEdits(insertEvent);
398: assertEquals(4, edits.size());
399: assertChange(edits.get(1), root.getElement(0), 1, 1);
400: assertChange(edits.get(2), root.getElement(1), 0, 1);
401: assertChange(edits.get(3), root, 0, 1);
402: Element charElem = doc.getCharacterElement(0);
403: assertEquals(0, charElem.getAttributes().getAttributeCount());
404: assertEquals(0, charElem.getStartOffset());
405: assertEquals(4, charElem.getEndOffset());
406: charElem = doc.getCharacterElement(4);
407: assertEquals(0, charElem.getAttributes().getAttributeCount());
408: assertEquals(4, charElem.getStartOffset());
409: assertEquals(7, charElem.getEndOffset());
410: charElem = doc.getCharacterElement(doc.getLength());
411: assertTrue(charElem.getAttributes().isEqual(bold));
412: assertEquals(7, charElem.getStartOffset());
413: assertEquals(8, charElem.getEndOffset());
414: assertEquals(4, specs.length);
415: assertSpec(specs[0], ElementSpec.ContentType,
416: ElementSpec.OriginateDirection, 0, 4);
417: assertSpec(specs[1], ElementSpec.EndTagType,
418: ElementSpec.OriginateDirection, 0, 0);
419: assertSpec(specs[2], ElementSpec.StartTagType,
420: ElementSpec.JoinFractureDirection, 0, 0);
421: assertSpec(specs[3], ElementSpec.ContentType,
422: ElementSpec.OriginateDirection, 0, 3);
423: }
424:
425: //===========================================================================
426: /**
427: * No attributes, text 'one' with italic, doc is empty.
428: */
429: public void testInsertString31() throws Exception {
430: doc.insertString(0, "one", italic);
431: List<?> edits = getEdits(insertEvent);
432: assertEquals(2, edits.size());
433: assertChange(edits.get(1), root.getElement(0), 1, 2);
434: Element charElem = doc.getCharacterElement(0);
435: assertTrue(charElem.getAttributes().isEqual(italic));
436: charElem = doc.getCharacterElement(doc.getLength());
437: assertEquals(0, charElem.getAttributes().getAttributeCount());
438: assertEquals(1, specs.length);
439: assertSpec(specs[0], ElementSpec.ContentType,
440: ElementSpec.OriginateDirection, 0, 3);
441: }
442:
443: /**
444: * No attributes, text 'one\n' with italic, doc is empty.
445: */
446: public void testInsertString32() throws Exception {
447: doc.insertString(0, "one\n", italic);
448: List<?> edits = getEdits(insertEvent);
449: assertEquals(3, edits.size());
450: assertChange(edits.get(1), root.getElement(0), 1, 1);
451: assertChange(edits.get(2), root, 0, 1);
452: Element charElem = doc.getCharacterElement(0);
453: assertTrue(charElem.getAttributes().isEqual(italic));
454: assertEquals(0, charElem.getStartOffset());
455: assertEquals(4, charElem.getEndOffset());
456: charElem = doc.getCharacterElement(doc.getLength());
457: assertEquals(0, charElem.getAttributes().getAttributeCount());
458: assertEquals(4, charElem.getStartOffset());
459: assertEquals(5, charElem.getEndOffset());
460: assertEquals(3, specs.length);
461: assertSpec(specs[0], ElementSpec.ContentType,
462: ElementSpec.OriginateDirection, 0, 4);
463: assertSpec(specs[1], ElementSpec.EndTagType,
464: ElementSpec.OriginateDirection, 0, 0);
465: assertSpec(specs[2], ElementSpec.StartTagType,
466: ElementSpec.JoinFractureDirection, 0, 0);
467: }
468:
469: /**
470: * No attributes, text '\none' with italic, doc is empty.
471: */
472: public void testInsertString33() throws Exception {
473: doc.insertString(0, "\none", italic);
474: List<?> edits = getEdits(insertEvent);
475: assertEquals(4, edits.size());
476: assertChange(edits.get(1), root.getElement(0), 1, 1);
477: assertChange(edits.get(2), root.getElement(1), 0, 1);
478: assertChange(edits.get(3), root, 0, 1);
479: Element charElem = doc.getCharacterElement(0);
480: assertTrue(charElem.getAttributes().isEqual(italic));
481: assertEquals(0, charElem.getStartOffset());
482: assertEquals(1, charElem.getEndOffset());
483: charElem = doc.getCharacterElement(1);
484: assertTrue(charElem.getAttributes().isEqual(italic));
485: assertEquals(1, charElem.getStartOffset());
486: assertEquals(4, charElem.getEndOffset());
487: charElem = doc.getCharacterElement(doc.getLength());
488: assertEquals(0, charElem.getAttributes().getAttributeCount());
489: assertEquals(4, charElem.getStartOffset());
490: assertEquals(5, charElem.getEndOffset());
491: assertEquals(4, specs.length);
492: assertSpec(specs[0], ElementSpec.ContentType,
493: ElementSpec.OriginateDirection, 0, 1);
494: assertSpec(specs[1], ElementSpec.EndTagType,
495: ElementSpec.OriginateDirection, 0, 0);
496: assertSpec(specs[2], ElementSpec.StartTagType,
497: ElementSpec.JoinFractureDirection, 0, 0);
498: assertSpec(specs[3], ElementSpec.ContentType,
499: ElementSpec.OriginateDirection, 0, 3);
500: }
501:
502: /**
503: * No attributes, text 'one\ntwo' with italic, doc is empty.
504: */
505: public void testInsertString34() throws Exception {
506: doc.insertString(0, "one\ntwo", italic);
507: List<?> edits = getEdits(insertEvent);
508: assertEquals(4, edits.size());
509: assertChange(edits.get(1), root.getElement(0), 1, 1);
510: assertChange(edits.get(2), root.getElement(1), 0, 1);
511: assertChange(edits.get(3), root, 0, 1);
512: Element charElem = doc.getCharacterElement(0);
513: assertTrue(charElem.getAttributes().isEqual(italic));
514: assertEquals(0, charElem.getStartOffset());
515: assertEquals(4, charElem.getEndOffset());
516: charElem = doc.getCharacterElement(4);
517: assertTrue(charElem.getAttributes().isEqual(italic));
518: assertEquals(4, charElem.getStartOffset());
519: assertEquals(7, charElem.getEndOffset());
520: charElem = doc.getCharacterElement(doc.getLength());
521: assertEquals(0, charElem.getAttributes().getAttributeCount());
522: assertEquals(7, charElem.getStartOffset());
523: assertEquals(8, charElem.getEndOffset());
524: assertEquals(4, specs.length);
525: assertSpec(specs[0], ElementSpec.ContentType,
526: ElementSpec.OriginateDirection, 0, 4);
527: assertSpec(specs[1], ElementSpec.EndTagType,
528: ElementSpec.OriginateDirection, 0, 0);
529: assertSpec(specs[2], ElementSpec.StartTagType,
530: ElementSpec.JoinFractureDirection, 0, 0);
531: assertSpec(specs[3], ElementSpec.ContentType,
532: ElementSpec.OriginateDirection, 0, 3);
533: }
534:
535: //---------------------------------------------------------------------------
536: /**
537: * Bold attribute on paragraph, text 'one' with italic, doc is empty.
538: */
539: public void testInsertString41() throws Exception {
540: doc.setParagraphAttributes(0, 1, bold, false);
541: doc.insertString(0, "one", italic);
542: List<?> edits = getEdits(insertEvent);
543: assertEquals(2, edits.size());
544: assertChange(edits.get(1), root.getElement(0), 1, 2);
545: assertTrue(root.getElement(0).getAttributes()
546: .containsAttributes(bold));
547: Element charElem = doc.getCharacterElement(0);
548: assertTrue(charElem.getAttributes().isEqual(italic));
549: charElem = doc.getCharacterElement(doc.getLength());
550: assertEquals(0, charElem.getAttributes().getAttributeCount());
551: assertEquals(1, specs.length);
552: assertSpec(specs[0], ElementSpec.ContentType,
553: ElementSpec.OriginateDirection, 0, 3);
554: }
555:
556: /**
557: * Bold attribute on paragraph, text 'one\n' with italic, doc is empty.
558: */
559: public void testInsertString42() throws Exception {
560: doc.setParagraphAttributes(0, 1, bold, false);
561: doc.insertString(0, "one\n", italic);
562: List<?> edits = getEdits(insertEvent);
563: assertEquals(3, edits.size());
564: assertChange(edits.get(1), root.getElement(0), 1, 1);
565: assertChange(edits.get(2), root, 0, 1);
566: assertTrue(root.getElement(0).getAttributes()
567: .containsAttributes(bold));
568: assertTrue(root.getElement(1).getAttributes()
569: .containsAttributes(bold));
570: Element charElem = doc.getCharacterElement(0);
571: assertTrue(charElem.getAttributes().isEqual(italic));
572: assertEquals(0, charElem.getStartOffset());
573: assertEquals(4, charElem.getEndOffset());
574: charElem = doc.getCharacterElement(doc.getLength());
575: assertEquals(0, charElem.getAttributes().getAttributeCount());
576: assertEquals(4, charElem.getStartOffset());
577: assertEquals(5, charElem.getEndOffset());
578: assertEquals(3, specs.length);
579: assertSpec(specs[0], ElementSpec.ContentType,
580: ElementSpec.OriginateDirection, 0, 4);
581: assertSpec(specs[1], ElementSpec.EndTagType,
582: ElementSpec.OriginateDirection, 0, 0);
583: assertSpec(specs[2], ElementSpec.StartTagType,
584: ElementSpec.JoinFractureDirection, 0, 0);
585: /*
586: * Fracture copies attributes of the paragraph:
587: *
588: * createBranch(section[0, 5], resolver=**AttributeSet** bold=true )
589: */
590: }
591:
592: /**
593: * Bold attribute on paragraph, text '\none' with italic, doc is empty.
594: */
595: public void testInsertString43() throws Exception {
596: doc.setParagraphAttributes(0, 1, bold, false);
597: doc.insertString(0, "\none", italic);
598: List<?> edits = getEdits(insertEvent);
599: assertEquals(4, edits.size());
600: assertChange(edits.get(1), root.getElement(0), 1, 1);
601: assertChange(edits.get(2), root.getElement(1), 0, 1);
602: assertChange(edits.get(3), root, 0, 1);
603: assertTrue(root.getElement(0).getAttributes()
604: .containsAttributes(bold));
605: assertTrue(root.getElement(1).getAttributes()
606: .containsAttributes(bold));
607: Element charElem = doc.getCharacterElement(0);
608: assertTrue(charElem.getAttributes().isEqual(italic));
609: assertEquals(0, charElem.getStartOffset());
610: assertEquals(1, charElem.getEndOffset());
611: charElem = doc.getCharacterElement(1);
612: assertTrue(charElem.getAttributes().isEqual(italic));
613: assertEquals(1, charElem.getStartOffset());
614: assertEquals(4, charElem.getEndOffset());
615: charElem = doc.getCharacterElement(doc.getLength());
616: assertEquals(0, charElem.getAttributes().getAttributeCount());
617: assertEquals(4, charElem.getStartOffset());
618: assertEquals(5, charElem.getEndOffset());
619: assertEquals(4, specs.length);
620: assertSpec(specs[0], ElementSpec.ContentType,
621: ElementSpec.OriginateDirection, 0, 1);
622: assertSpec(specs[1], ElementSpec.EndTagType,
623: ElementSpec.OriginateDirection, 0, 0);
624: assertSpec(specs[2], ElementSpec.StartTagType,
625: ElementSpec.JoinFractureDirection, 0, 0);
626: assertSpec(specs[3], ElementSpec.ContentType,
627: ElementSpec.OriginateDirection, 0, 3);
628: }
629:
630: /**
631: * Bold attribute on paragraph, text 'one\ntwo' with italic, doc is empty.
632: */
633: public void testInsertString44() throws Exception {
634: doc.setParagraphAttributes(0, 1, bold, false);
635: doc.insertString(0, "one\ntwo", italic);
636: List<?> edits = getEdits(insertEvent);
637: assertEquals(4, edits.size());
638: assertChange(edits.get(1), root.getElement(0), 1, 1);
639: assertChange(edits.get(2), root.getElement(1), 0, 1);
640: assertChange(edits.get(3), root, 0, 1);
641: assertTrue(root.getElement(0).getAttributes()
642: .containsAttributes(bold));
643: assertTrue(root.getElement(1).getAttributes()
644: .containsAttributes(bold));
645: Element charElem = doc.getCharacterElement(0);
646: assertTrue(charElem.getAttributes().isEqual(italic));
647: assertEquals(0, charElem.getStartOffset());
648: assertEquals(4, charElem.getEndOffset());
649: charElem = doc.getCharacterElement(4);
650: assertTrue(charElem.getAttributes().isEqual(italic));
651: assertEquals(4, charElem.getStartOffset());
652: assertEquals(7, charElem.getEndOffset());
653: charElem = doc.getCharacterElement(doc.getLength());
654: assertEquals(0, charElem.getAttributes().getAttributeCount());
655: assertEquals(7, charElem.getStartOffset());
656: assertEquals(8, charElem.getEndOffset());
657: assertEquals(4, specs.length);
658: assertSpec(specs[0], ElementSpec.ContentType,
659: ElementSpec.OriginateDirection, 0, 4);
660: assertSpec(specs[1], ElementSpec.EndTagType,
661: ElementSpec.OriginateDirection, 0, 0);
662: assertSpec(specs[2], ElementSpec.StartTagType,
663: ElementSpec.JoinFractureDirection, 0, 0);
664: assertSpec(specs[3], ElementSpec.ContentType,
665: ElementSpec.OriginateDirection, 0, 3);
666: }
667:
668: //---------------------------------------------------------------------------
669: /**
670: * Bold attribute on character, text 'one' with italic, doc is empty.
671: */
672: public void testInsertString51() throws Exception {
673: doc.setCharacterAttributes(0, 1, bold, false);
674: doc.insertString(0, "one", italic);
675: List<?> edits = getEdits(insertEvent);
676: assertEquals(2, edits.size());
677: assertChange(edits.get(1), root.getElement(0), 1, 2);
678: Element charElem = doc.getCharacterElement(0);
679: assertTrue(charElem.getAttributes().isEqual(italic));
680: charElem = doc.getCharacterElement(doc.getLength());
681: assertTrue(charElem.getAttributes().isEqual(bold));
682: assertEquals(1, specs.length);
683: assertSpec(specs[0], ElementSpec.ContentType,
684: ElementSpec.OriginateDirection, 0, 3);
685: /*
686: * createLeaf(paragraph[0, 4], , 0, 3)
687: * The word 'one'
688: * createLeaf(paragraph[0, 4], bold=true , 3, 4)
689: * The attributed newline char
690: */
691: }
692:
693: /**
694: * Bold attribute on character, text 'one\n' with italic, doc is empty.
695: */
696: public void testInsertString52() throws Exception {
697: doc.setCharacterAttributes(0, 1, bold, false);
698: doc.insertString(0, "one\n", italic);
699: List<?> edits = getEdits(insertEvent);
700: assertEquals(3, edits.size());
701: assertChange(edits.get(1), root.getElement(0), 1, 1);
702: assertChange(edits.get(2), root, 0, 1);
703: Element charElem = doc.getCharacterElement(0);
704: assertTrue(charElem.getAttributes().isEqual(italic));
705: assertEquals(0, charElem.getStartOffset());
706: assertEquals(4, charElem.getEndOffset());
707: charElem = doc.getCharacterElement(doc.getLength());
708: assertTrue(charElem.getAttributes().isEqual(bold));
709: assertEquals(4, charElem.getStartOffset());
710: assertEquals(5, charElem.getEndOffset());
711: assertEquals(3, specs.length);
712: assertSpec(specs[0], ElementSpec.ContentType,
713: ElementSpec.OriginateDirection, 0, 4);
714: assertSpec(specs[1], ElementSpec.EndTagType,
715: ElementSpec.OriginateDirection, 0, 0);
716: assertSpec(specs[2], ElementSpec.StartTagType,
717: ElementSpec.JoinFractureDirection, 0, 0);
718: /*
719: * While copying elements with fracture, it will copy leaf attributes:
720: *
721: * createBranch(section[0, 5], resolver=**AttributeSet** )
722: * createLeaf(paragraph[N/A], bold=true , 4, 5)
723: */
724: }
725:
726: /**
727: * Bold attribute on character, text '\none' with italic, doc is empty.
728: */
729: public void testInsertString53() throws Exception {
730: doc.setCharacterAttributes(0, 1, bold, false);
731: doc.insertString(0, "\none", italic);
732: List<?> edits = getEdits(insertEvent);
733: assertEquals(4, edits.size());
734: assertChange(edits.get(1), root.getElement(0), 1, 1);
735: assertChange(edits.get(2), root.getElement(1), 0, 1);
736: assertChange(edits.get(3), root, 0, 1);
737: Element charElem = doc.getCharacterElement(0);
738: assertTrue(charElem.getAttributes().isEqual(italic));
739: assertEquals(0, charElem.getStartOffset());
740: assertEquals(1, charElem.getEndOffset());
741: charElem = doc.getCharacterElement(1);
742: assertTrue(charElem.getAttributes().isEqual(italic));
743: assertEquals(1, charElem.getStartOffset());
744: assertEquals(4, charElem.getEndOffset());
745: charElem = doc.getCharacterElement(doc.getLength());
746: assertTrue(charElem.getAttributes().isEqual(bold));
747: assertEquals(4, charElem.getStartOffset());
748: assertEquals(5, charElem.getEndOffset());
749: assertEquals(4, specs.length);
750: assertSpec(specs[0], ElementSpec.ContentType,
751: ElementSpec.OriginateDirection, 0, 1);
752: assertSpec(specs[1], ElementSpec.EndTagType,
753: ElementSpec.OriginateDirection, 0, 0);
754: assertSpec(specs[2], ElementSpec.StartTagType,
755: ElementSpec.JoinFractureDirection, 0, 0);
756: assertSpec(specs[3], ElementSpec.ContentType,
757: ElementSpec.OriginateDirection, 0, 3);
758: /*
759: * createLeaf(paragraph[0, 5], , 0, 1)
760: * createBranch(section[0, 5], resolver=**AttributeSet** )
761: * createLeaf(paragraph[N/A], bold=true , 4, 5)
762: * createLeaf(paragraph[4, 5], , 1, 4)
763: */
764: }
765:
766: /**
767: * Bold attribute on character, text 'one\ntwo' with italic, doc is empty.
768: */
769: public void testInsertString54() throws Exception {
770: doc.setCharacterAttributes(0, 1, bold, false);
771: doc.insertString(0, "one\ntwo", italic);
772: List<?> edits = getEdits(insertEvent);
773: assertEquals(4, edits.size());
774: assertChange(edits.get(1), root.getElement(0), 1, 1);
775: assertChange(edits.get(2), root.getElement(1), 0, 1);
776: assertChange(edits.get(3), root, 0, 1);
777: Element charElem = doc.getCharacterElement(0);
778: assertTrue(charElem.getAttributes().isEqual(italic));
779: assertEquals(0, charElem.getStartOffset());
780: assertEquals(4, charElem.getEndOffset());
781: charElem = doc.getCharacterElement(4);
782: assertTrue(charElem.getAttributes().isEqual(italic));
783: assertEquals(4, charElem.getStartOffset());
784: assertEquals(7, charElem.getEndOffset());
785: charElem = doc.getCharacterElement(doc.getLength());
786: assertTrue(charElem.getAttributes().isEqual(bold));
787: assertEquals(7, charElem.getStartOffset());
788: assertEquals(8, charElem.getEndOffset());
789: assertEquals(4, specs.length);
790: assertSpec(specs[0], ElementSpec.ContentType,
791: ElementSpec.OriginateDirection, 0, 4);
792: assertSpec(specs[1], ElementSpec.EndTagType,
793: ElementSpec.OriginateDirection, 0, 0);
794: assertSpec(specs[2], ElementSpec.StartTagType,
795: ElementSpec.JoinFractureDirection, 0, 0);
796: assertSpec(specs[3], ElementSpec.ContentType,
797: ElementSpec.OriginateDirection, 0, 3);
798: }
799:
800: private static void assertChange(final Object object,
801: final Element element, final int removed, int added) {
802: DefStyledDoc_Helpers.assertChange(object, element, removed,
803: added);
804: }
805:
806: private static void assertSpec(final ElementSpec spec,
807: final short type, final short direction, final int offset,
808: final int length) {
809: DefStyledDoc_Helpers.assertSpec(spec, type, direction, offset,
810: length);
811: }
812:
813: private static Vector<?> getEdits(final DefaultDocumentEvent event) {
814: return DefStyledDoc_Helpers.getEdits(event);
815: }
816:
817: public void changedUpdate(DocumentEvent e) {
818: }
819:
820: public void insertUpdate(DocumentEvent e) {
821: insertEvent = (DefaultDocumentEvent) e;
822: }
823:
824: public void removeUpdate(DocumentEvent e) {
825: }
826: }
|