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