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 javax.swing.event.DocumentEvent;
024: import javax.swing.event.DocumentListener;
025: import javax.swing.event.DocumentEvent.EventType;
026: import javax.swing.text.AbstractDocument.AbstractElement;
027: import javax.swing.text.AbstractDocument.DefaultDocumentEvent;
028: import javax.swing.text.DefaultStyledDocument.AttributeUndoableEdit;
029: import javax.swing.text.DefaultStyledDocument.ElementBuffer;
030: import javax.swing.text.DefaultStyledDocument.ElementSpec;
031: import junit.framework.TestCase;
032:
033: /**
034: * Tests <code>create()</code> and <code>insert()</code> methods of
035: * DefaultStyledDocument.
036: *
037: */
038: public class DefaultStyledDocument_CreateInsertTest extends TestCase
039: implements DocumentListener {
040: private DefaultStyledDocument doc;
041:
042: private ElementBuffer buf;
043:
044: private Element root;
045:
046: private DocumentEvent event;
047:
048: private static final String plainText = "plain";
049:
050: private static final String boldText = "bold";
051:
052: private static final String italicText = "italic";
053:
054: private static final String newLine = "\n";
055:
056: private static final String paragraph2Text = "line\n";
057:
058: private static final AttributeSet bold = DefStyledDoc_Helpers.bold;
059:
060: private static final AttributeSet italic = DefStyledDoc_Helpers.italic;
061:
062: @Override
063: protected void setUp() throws Exception {
064: super .setUp();
065: doc = new DefStyledDoc_Helpers.DefStyledDocWithLogging();
066: root = doc.getDefaultRootElement();
067: buf = new DefStyledDoc_Helpers.ElementBufferWithLogging(doc,
068: root);
069: doc.buffer = buf;
070: }
071:
072: /**
073: * Calls <code>create()</code> on empty document.
074: * The attributes of the default root modified before the call to make
075: * sure they are reset on call.
076: */
077: public void testCreateWithoutText() throws BadLocationException {
078: final ElementSpec[] specs = {
079: new ElementSpec(null, ElementSpec.StartTagType),
080: new ElementSpec(null, ElementSpec.StartTagType),
081: new ElementSpec(null, ElementSpec.ContentType,
082: plainText.toCharArray(), 0, plainText.length()),
083: new ElementSpec(bold, ElementSpec.ContentType, boldText
084: .toCharArray(), 0, boldText.length()),
085: new ElementSpec(italic, ElementSpec.ContentType,
086: italicText.toCharArray(), 0, italicText
087: .length()),
088: new ElementSpec(null, ElementSpec.ContentType, newLine
089: .toCharArray(), 0, newLine.length()),
090: new ElementSpec(null, ElementSpec.EndTagType),
091: new ElementSpec(null, ElementSpec.StartTagType),
092: new ElementSpec(null, ElementSpec.ContentType,
093: paragraph2Text.toCharArray(), 0, paragraph2Text
094: .length()),
095: new ElementSpec(null, ElementSpec.EndTagType), };
096: specs[0].setDirection(ElementSpec.JoinPreviousDirection);
097: // Add an attribute to the root
098: doc.writeLock();
099: try {
100: ((AbstractElement) root).addAttributes(bold);
101: } finally {
102: doc.writeUnlock();
103: }
104: doc.addDocumentListener(this );
105: assertNull(event);
106: doc.create(specs);
107: assertNotNull(event);
108: assertEquals(0, event.getOffset());
109: assertEquals(plainText.length() + boldText.length()
110: + italicText.length() + newLine.length()
111: + paragraph2Text.length(), event.getLength());
112: assertSame(EventType.INSERT, event.getType());
113: assertChildren(root.getElement(0), plainText + boldText
114: + italicText + newLine, new int[] { 0, 5, 5, 9, 9, 15,
115: 15, 16 },
116: new AttributeSet[] { null, bold, italic, null });
117: assertChildren(root.getElement(1), paragraph2Text, new int[] {
118: 16, 21 }, new AttributeSet[] { null });
119: assertEquals(0, root.getElement(1).getAttributes()
120: .getAttributeCount());
121: assertChildren(root.getElement(2), newLine,
122: new int[] { 21, 22 }, new AttributeSet[] { null });
123: final List<?> edits = DefStyledDoc_Helpers
124: .getEdits((DefaultDocumentEvent) event);
125: assertEquals(6, edits.size());
126: final AttributeUndoableEdit undo = (AttributeUndoableEdit) edits
127: .get(1);
128: assertSame(root, undo.element);
129: assertTrue(undo.copy.isEqual(bold));
130: assertEquals(0, undo.newAttributes.getAttributeCount());
131: assertTrue(undo.isReplacing);
132: assertChange(edits.get(2), root.getElement(root
133: .getElementCount() - 1), 0, new int[] { 0, 22 },
134: new int[] { 21, 22 });
135: assertChange(edits.get(3), root.getElement(0), 0, new int[] {},
136: new int[] { 0, 5, 5, 9, 9, 15, 15, 16 });
137: assertChange(edits.get(4), root.getElement(1), 0, new int[] {},
138: new int[] { 16, 21 });
139: assertChange(edits.get(5), root, 0, new int[] {}, new int[] {
140: 0, 16, 16, 21 });
141: }
142:
143: /**
144: * Calls <code>create</code> on non-empty document.
145: * This asserts the document contents is removed before applying
146: * new structure.
147: */
148: public void testCreateWithText() throws BadLocationException {
149: doc.insertString(0, "some text", bold);
150: doc.insertString(doc.getLength(), "\nmore text", italic);
151: final ElementSpec[] specs = {
152: new ElementSpec(null, ElementSpec.StartTagType),
153: new ElementSpec(null, ElementSpec.StartTagType),
154: new ElementSpec(null, ElementSpec.ContentType,
155: plainText.toCharArray(), 0, plainText.length()),
156: new ElementSpec(bold, ElementSpec.ContentType, boldText
157: .toCharArray(), 0, boldText.length()),
158: new ElementSpec(italic, ElementSpec.ContentType,
159: italicText.toCharArray(), 0, italicText
160: .length()),
161: new ElementSpec(null, ElementSpec.ContentType, newLine
162: .toCharArray(), 0, newLine.length()),
163: new ElementSpec(null, ElementSpec.EndTagType),
164: new ElementSpec(null, ElementSpec.StartTagType),
165: new ElementSpec(null, ElementSpec.ContentType,
166: paragraph2Text.toCharArray(), 0, paragraph2Text
167: .length()),
168: new ElementSpec(null, ElementSpec.EndTagType), };
169: doc.addDocumentListener(this );
170: assertNull(event);
171: doc.create(specs);
172: assertNotNull(event);
173: assertEquals(0, event.getOffset());
174: assertEquals(plainText.length() + boldText.length()
175: + italicText.length() + newLine.length()
176: + paragraph2Text.length(), event.getLength());
177: assertSame(EventType.INSERT, event.getType());
178: assertChildren(root.getElement(0), plainText + boldText
179: + italicText + newLine, new int[] { 0, 5, 5, 9, 9, 15,
180: 15, 16 },
181: new AttributeSet[] { null, bold, italic, null });
182: assertChildren(root.getElement(1), paragraph2Text, new int[] {
183: 16, 21 }, new AttributeSet[] { null });
184: assertEquals(0, root.getElement(1).getAttributes()
185: .getAttributeCount());
186: assertChildren(root.getElement(2), newLine,
187: new int[] { 21, 22 }, new AttributeSet[] { null });
188: final List<?> edits = DefStyledDoc_Helpers
189: .getEdits((DefaultDocumentEvent) event);
190: assertEquals(6, edits.size());
191: assertChange(edits.get(2), root.getElement(root
192: .getElementCount() - 1), 0, new int[] { 0, 22 },
193: new int[] { 21, 22 });
194: assertChange(edits.get(3), root.getElement(0), 0, new int[] {},
195: new int[] { 0, 5, 5, 9, 9, 15, 15, 16 });
196: assertChange(edits.get(4), root.getElement(1), 0, new int[] {},
197: new int[] { 16, 21 });
198: assertChange(edits.get(5), root, 0, new int[] {}, new int[] {
199: 0, 16, 16, 21 });
200: }
201:
202: public void testInsertWithStartTag() throws BadLocationException {
203: doc.insertString(0, "some text", bold);
204: doc.insertString(doc.getLength(), "\nmore text", italic);
205: ElementSpec[] specs = {
206: new ElementSpec(null, ElementSpec.ContentType,
207: plainText.toCharArray(), 0, plainText.length()),
208: new ElementSpec(bold, ElementSpec.ContentType, boldText
209: .toCharArray(), 0, boldText.length()),
210: new ElementSpec(italic, ElementSpec.ContentType,
211: italicText.toCharArray(), 0, italicText
212: .length()),
213: new ElementSpec(null, ElementSpec.ContentType, newLine
214: .toCharArray(), 0, newLine.length()),
215: new ElementSpec(null, ElementSpec.EndTagType),
216: new ElementSpec(null, ElementSpec.StartTagType),
217: new ElementSpec(null, ElementSpec.ContentType,
218: paragraph2Text.toCharArray(), 0, paragraph2Text
219: .length()),
220: //It is mandatory for our implementation.
221: new ElementSpec(null, ElementSpec.EndTagType),
222: new ElementSpec(null, ElementSpec.StartTagType) };
223: specs[specs.length - 1]
224: .setDirection(ElementSpec.JoinFractureDirection);
225: doc.addDocumentListener(this );
226: assertNull(event);
227: doc.insert(5, specs);
228: assertNotNull(event);
229: assertEquals(5, event.getOffset());
230: assertEquals(plainText.length() + boldText.length()
231: + italicText.length() + newLine.length()
232: + paragraph2Text.length(), event.getLength());
233: assertSame(EventType.INSERT, event.getType());
234: assertChildren(root.getElement(0), "some " + plainText
235: + boldText + italicText + newLine, new int[] { 0, 5, 5,
236: 10, 10, 14, 14, 20, 20, 21 }, new AttributeSet[] {
237: bold, null, bold, italic, null });
238: assertChildren(root.getElement(1), paragraph2Text, new int[] {
239: 21, 26 }, new AttributeSet[] { null });
240: assertEquals(0, root.getElement(1).getAttributes()
241: .getAttributeCount());
242: assertChildren(root.getElement(2), " text\n", new int[] { 26,
243: 31, 31, 32 }, new AttributeSet[] { bold, italic });
244: assertChildren(root.getElement(3), "more text\n", new int[] {
245: 32, 41, 41, 42 }, new AttributeSet[] { italic, null });
246: }
247:
248: /**
249: * Calls <code>insert()</code> on non-empty document.
250: * The <code>ElementSpec</code>s applied contain no
251: * <code>StartTagType</code> specs.
252: */
253: public void testInsertNoStartTags() throws BadLocationException {
254: doc.insertString(0, "some text", bold);
255: doc.insertString(doc.getLength(), "\nmore text", italic);
256: ElementSpec[] specs = {
257: new ElementSpec(null, ElementSpec.ContentType,
258: plainText.toCharArray(), 0, plainText.length()),
259: new ElementSpec(bold, ElementSpec.ContentType, boldText
260: .toCharArray(), 0, boldText.length()),
261: new ElementSpec(italic, ElementSpec.ContentType,
262: italicText.toCharArray(), 0, italicText
263: .length()),
264: new ElementSpec(null, ElementSpec.ContentType, newLine
265: .toCharArray(), 0, newLine.length()),
266: new ElementSpec(null, ElementSpec.ContentType,
267: paragraph2Text.toCharArray(), 0, paragraph2Text
268: .length()), };
269: doc.addDocumentListener(this );
270: assertNull(event);
271: doc.insert(5, specs);
272: assertNotNull(event);
273: assertEquals(5, event.getOffset());
274: assertEquals(plainText.length() + boldText.length()
275: + italicText.length() + newLine.length()
276: + paragraph2Text.length(), event.getLength());
277: assertSame(EventType.INSERT, event.getType());
278: assertChildren(root.getElement(0), "some " + plainText
279: + boldText + italicText + newLine + paragraph2Text
280: + " text\n", new int[] { 0, 5, 5, 10, 10, 14, 14, 20,
281: 20, 21, 21, 26, 26, 31, 31, 32 }, new AttributeSet[] {
282: bold, null, bold, italic, null, null, bold, italic });
283: assertChildren(root.getElement(1), "more text\n", new int[] {
284: 32, 41, 41, 42 }, new AttributeSet[] { italic, null });
285: }
286:
287: private static void assertChange(final Object change,
288: final Element element, final int index,
289: final int[] removedOffsets, final int[] addedOffsets) {
290: DefStyledDoc_Helpers.assertChange(change, element, index,
291: removedOffsets, addedOffsets);
292: }
293:
294: private static void assertChildren(final Element element,
295: final String text, final int[] offsets,
296: final AttributeSet[] attributes)
297: throws BadLocationException {
298: DefStyledDoc_Helpers.assertChildren(element, offsets,
299: attributes);
300: assertEquals("element.text " + element, text, getText(element));
301: }
302:
303: private static String getText(final Element element)
304: throws BadLocationException {
305: final int start = element.getStartOffset();
306: final int end = element.getEndOffset();
307: return element.getDocument().getText(start, end - start);
308: }
309:
310: public void insertUpdate(DocumentEvent e) {
311: event = e;
312: }
313:
314: public void removeUpdate(DocumentEvent e) {
315: }
316:
317: public void changedUpdate(DocumentEvent e) {
318: fail("changedUpdate is not expected to be called");
319: }
320: }
|