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.ElementChange;
026: import javax.swing.event.DocumentEvent.EventType;
027: import javax.swing.text.AbstractDocument.Content;
028: import javax.swing.text.AbstractDocument.DefaultDocumentEvent;
029: import javax.swing.text.DefaultStyledDocument.ElementBuffer;
030: import javax.swing.text.DefaultStyledDocument.ElementSpec;
031: import junit.framework.TestCase;
032:
033: /**
034: * Contains the test case that failed when inserting into the document using
035: * <code>insertString</code> method.
036: * This is to test that <code>ElementSpec</code>s when applied will generate
037: * the required structure; and that <code>insertUpdate()</code> generates the
038: * expected set of <code>ElementSpec</code>s.
039: *
040: */
041: public class DefaultStyledDocument_ElementBuffer_InsertMiscTest extends
042: TestCase implements DocumentListener {
043: private DefaultStyledDocument doc;
044:
045: private ElementBuffer buf;
046:
047: private Element root;
048:
049: private Content content;
050:
051: private DefaultDocumentEvent event;
052:
053: private DefaultDocumentEvent insertEvent;
054:
055: private ElementSpec[] insertSpecs;
056:
057: @Override
058: protected void setUp() throws Exception {
059: super .setUp();
060: doc = new DefaultStyledDocument();
061: root = doc.getDefaultRootElement();
062: buf = new DefStyledDoc_Helpers.ElementBufferWithLogging(doc,
063: root) {
064: private static final long serialVersionUID = 1L;
065:
066: @Override
067: public void insert(int offset, int length,
068: ElementSpec[] spec, DefaultDocumentEvent event) {
069: super .insert(offset, length, insertSpecs = spec, event);
070: }
071: };
072: doc.buffer = buf;
073: content = doc.getContent();
074: doc.addDocumentListener(this );
075: doc.writeLock();
076: }
077:
078: @Override
079: protected void tearDown() throws Exception {
080: super .tearDown();
081: doc.writeUnlock();
082: }
083:
084: /**
085: * Tests that ElementSpecs for inserting non-attributed string
086: * <code>"first\nsecond\nthird"</code> into an empty document will
087: * lead to the expected document structure.
088: */
089: public void testInsertThreePars() throws Exception {
090: final String text = "first\nsecond\nthird";
091: final int textLen = text.length();
092: //doc.insertString(0, text, null);
093: content.insertString(0, text);
094: event = doc.new DefaultDocumentEvent(0, textLen,
095: EventType.INSERT);
096: ElementSpec[] specs = {
097: new ElementSpec(null, ElementSpec.ContentType, 6),
098: new ElementSpec(null, ElementSpec.EndTagType),
099: new ElementSpec(null, ElementSpec.StartTagType),
100: new ElementSpec(null, ElementSpec.ContentType, 7),
101: new ElementSpec(null, ElementSpec.EndTagType),
102: new ElementSpec(null, ElementSpec.StartTagType),
103: new ElementSpec(null, ElementSpec.ContentType, 5), };
104: specs[0].setDirection(ElementSpec.JoinPreviousDirection);
105: specs[specs.length - 2]
106: .setDirection(ElementSpec.JoinFractureDirection);
107: specs[specs.length - 1]
108: .setDirection(ElementSpec.JoinNextDirection);
109: buf.insert(0, textLen, specs, event);
110: List<?> edits = getEdits(event);
111: assertEquals(4, edits.size());
112: assertChange(edits.get(0), 0, new int[] { 0, 19 }, new int[] {
113: 0, 6 });
114: assertChange(edits.get(1), 0, new int[] {}, new int[] { 6, 13 });
115: assertChange(edits.get(2), 0, new int[] { 18, 19 }, new int[] {
116: 13, 19 });
117: assertChange(edits.get(3), 1, new int[] {}, new int[] { 6, 13,
118: 13, 19 });
119: assertChildren(root.getElement(0), new int[] { 0, 6 },
120: new AttributeSet[] { null });
121: assertChildren(root.getElement(1), new int[] { 6, 13 },
122: new AttributeSet[] { null });
123: assertChildren(root.getElement(2), new int[] { 13, 19 },
124: new AttributeSet[] { null });
125: assertEquals("first\n", getText(doc.getCharacterElement(0)));
126: assertEquals("second\n",
127: getText(doc.getCharacterElement(0 + 6)));
128: assertEquals("third\n", getText(doc
129: .getCharacterElement(0 + 6 + 7)));
130: }
131:
132: /**
133: * Tests that DefaultStyledDocument.insertUpdate generates the expected
134: * set of ElementSpecs for inserting non-attributed string
135: * <code>"first\nsecond\nthird"</code> into an empty document and that
136: * the document has the expected structure after the insert.
137: */
138: public void testInsertUpdateThreePars() throws Exception {
139: final String text = "first\nsecond\nthird";
140: doc.insertString(0, text, null);
141: // doc.dump(System.out);
142: ElementSpec[] specs = {
143: new ElementSpec(null, ElementSpec.ContentType, 6),
144: new ElementSpec(null, ElementSpec.EndTagType),
145: new ElementSpec(null, ElementSpec.StartTagType),
146: new ElementSpec(null, ElementSpec.ContentType, 7),
147: new ElementSpec(null, ElementSpec.EndTagType),
148: new ElementSpec(null, ElementSpec.StartTagType),
149: new ElementSpec(null, ElementSpec.ContentType, 5), };
150: specs[0].setDirection(ElementSpec.JoinPreviousDirection);
151: specs[specs.length - 2]
152: .setDirection(ElementSpec.JoinFractureDirection);
153: specs[specs.length - 1]
154: .setDirection(ElementSpec.JoinNextDirection);
155: assertSpecs(specs, insertSpecs);
156: List<?> edits = getEdits(insertEvent);
157: assertEquals(5, edits.size());
158: assertChange(edits.get(1), 0, new int[] { 0, 19 }, new int[] {
159: 0, 6 });
160: assertChange(edits.get(2), 0, new int[] {}, new int[] { 6, 13 });
161: assertChange(edits.get(3), 0, new int[] { 18, 19 }, new int[] {
162: 13, 19 });
163: assertChange(edits.get(4), 1, new int[] {}, new int[] { 6, 13,
164: 13, 19 });
165: assertChildren(root.getElement(0), new int[] { 0, 6 },
166: new AttributeSet[] { null });
167: assertChildren(root.getElement(1), new int[] { 6, 13 },
168: new AttributeSet[] { null });
169: assertChildren(root.getElement(2), new int[] { 13, 19 },
170: new AttributeSet[] { null });
171: assertEquals("first\n", getText(doc.getCharacterElement(0)));
172: assertEquals("second\n",
173: getText(doc.getCharacterElement(0 + 6)));
174: assertEquals("third\n", getText(doc
175: .getCharacterElement(0 + 6 + 7)));
176: }
177:
178: public void testInsertRightAfterNewLineWithNewLine()
179: throws Exception {
180: final String initialText = "one\ntwo\nthree";
181: doc.insertString(0, initialText, null);
182: final int offset = root.getElement(0).getEndOffset();
183: doc.insertString(offset, "^^^\n", null);
184: ElementSpec[] specs = {
185: new ElementSpec(null, ElementSpec.EndTagType),
186: new ElementSpec(null, ElementSpec.StartTagType),
187: new ElementSpec(null, ElementSpec.ContentType, 4),
188: new ElementSpec(null, ElementSpec.EndTagType),
189: new ElementSpec(null, ElementSpec.StartTagType) };
190: specs[specs.length - 1]
191: .setDirection(ElementSpec.JoinNextDirection);
192: assertSpecs(specs, insertSpecs);
193: List<?> edits = getEdits(insertEvent);
194: assertEquals(4, edits.size());
195: assertChange(edits.get(1), 0, new int[] { 0, 8 }, new int[] {
196: 0, 4 });
197: assertChange(edits.get(2), 0, new int[] {}, new int[] { 4, 8 });
198: assertChange(edits.get(3), 1, new int[] {}, new int[] { 4, 8 });
199: assertChildren(root.getElement(0), new int[] { 0, 4 },
200: new AttributeSet[] { null });
201: assertChildren(root.getElement(1), new int[] { 4, 8 },
202: new AttributeSet[] { null });
203: assertChildren(root.getElement(2), new int[] { 8, 12 },
204: new AttributeSet[] { null });
205: assertChildren(root.getElement(3), new int[] { 12, 18 },
206: new AttributeSet[] { null });
207: assertEquals("one\n", getText(doc.getCharacterElement(0)));
208: assertEquals("^^^\n", getText(doc.getCharacterElement(0 + 4)));
209: assertEquals("two\n", getText(doc
210: .getCharacterElement(0 + 4 + 4)));
211: assertEquals("three\n", getText(doc
212: .getCharacterElement(0 + 4 + 4 + 4)));
213: }
214:
215: public void testInsertRightAfterNewLineWithoutNewLine()
216: throws Exception {
217: final String initialText = "one\ntwo\nthree";
218: doc.insertString(0, initialText, null);
219: final int offset = root.getElement(0).getEndOffset();
220: doc.insertString(offset, "^^^", null);
221: ElementSpec[] specs = {
222: new ElementSpec(null, ElementSpec.EndTagType),
223: new ElementSpec(null, ElementSpec.StartTagType),
224: new ElementSpec(null, ElementSpec.ContentType, 3), };
225: specs[specs.length - 2]
226: .setDirection(ElementSpec.JoinNextDirection);
227: specs[specs.length - 1]
228: .setDirection(ElementSpec.JoinNextDirection);
229: assertSpecs(specs, insertSpecs);
230: List<?> edits = getEdits(insertEvent);
231: assertEquals(3, edits.size());
232: assertChange(edits.get(1), 0, new int[] { 0, 7 }, new int[] {
233: 0, 4 });
234: assertChange(edits.get(2), 0, new int[] { 7, 11 }, new int[] {
235: 4, 11 });
236: assertChildren(root.getElement(0), new int[] { 0, 4 },
237: new AttributeSet[] { null });
238: assertChildren(root.getElement(1), new int[] { 4, 11 },
239: new AttributeSet[] { null });
240: assertChildren(root.getElement(2), new int[] { 11, 17 },
241: new AttributeSet[] { null });
242: assertEquals("one\n", getText(doc.getCharacterElement(0)));
243: assertEquals("^^^two\n",
244: getText(doc.getCharacterElement(0 + 4)));
245: assertEquals("three\n", getText(doc
246: .getCharacterElement(0 + 4 + 7)));
247: }
248:
249: private String getText(final int offset, final int length)
250: throws BadLocationException {
251: return doc.getText(offset, length);
252: }
253:
254: private String getText(final Element element)
255: throws BadLocationException {
256: return getText(element.getStartOffset(), element.getEndOffset()
257: - element.getStartOffset());
258: }
259:
260: private static void assertChange(final Object change,
261: final int index, final int[] removedOffsets,
262: final int[] addedOffsets) {
263: assertEquals("Change index", index, ((ElementChange) change)
264: .getIndex());
265: DefStyledDoc_Helpers.assertChange((ElementChange) change,
266: removedOffsets, addedOffsets);
267: }
268:
269: private static void assertChildren(final Element element,
270: final int[] offsets, final AttributeSet[] attributes) {
271: DefStyledDoc_Helpers.assertChildren(element, offsets,
272: attributes);
273: }
274:
275: private static void assertSpecs(final ElementSpec[] specs,
276: final ElementSpec[] insertSpecs) {
277: DefStyledDoc_Helpers.assertSpecs(specs, insertSpecs);
278: }
279:
280: private static List<?> getEdits(final DefaultDocumentEvent event) {
281: return DefStyledDoc_Helpers.getEdits(event);
282: }
283:
284: public void changedUpdate(DocumentEvent e) {
285: }
286:
287: public void insertUpdate(DocumentEvent e) {
288: insertEvent = (DefaultDocumentEvent) e;
289: }
290:
291: public void removeUpdate(DocumentEvent e) {
292: }
293: }
|