001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import javax.swing.BasicSwingTestCase;
023: import javax.swing.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.BranchElement;
028: import javax.swing.text.AbstractDocument.LeafElement;
029: import javax.swing.text.DefaultStyledDocument.SectionElement;
030:
031: public class DefaultStyledDocumentTest extends BasicSwingTestCase {
032: protected DefaultStyledDocument doc;
033:
034: @Override
035: protected void setUp() throws Exception {
036: super .setUp();
037: doc = new DefaultStyledDocument(new StyleContext());
038: }
039:
040: /**
041: * Tests <code>addDocumentListener</code> and
042: * <code>removeDocumentListener</code> methods.
043: */
044: public void testAddRemoveDocumentListener()
045: throws BadLocationException {
046: final class Listener implements DocumentListener {
047: private boolean insert;
048:
049: private boolean remove;
050:
051: private boolean change;
052:
053: public void insertUpdate(DocumentEvent e) {
054: insert = true;
055: }
056:
057: public void removeUpdate(DocumentEvent e) {
058: remove = true;
059: }
060:
061: public void changedUpdate(DocumentEvent e) {
062: change = true;
063: }
064:
065: public void check(final boolean insert,
066: final boolean remove, final boolean change) {
067: assertEquals("Insert", insert, this .insert);
068: assertEquals("Remove", remove, this .remove);
069: assertEquals("Change", change, this .change);
070: this .insert = this .remove = this .change = false;
071: }
072: }
073: ;
074: final Listener listener = new Listener();
075: final AttributeSet attrs = new SimpleAttributeSet();
076: StyleConstants.setFontSize((MutableAttributeSet) attrs, 36);
077: // The listener has not been added to the document yet
078: doc.insertString(0, "1", null);
079: listener.check(false, false, false);
080: doc.remove(0, 1);
081: listener.check(false, false, false);
082: doc.setCharacterAttributes(0, 1, attrs, false);
083: listener.check(false, false, false);
084: // The listener has been added to the document
085: doc.addDocumentListener(listener);
086: doc.insertString(0, "1", null);
087: listener.check(true, false, false);
088: doc.remove(0, 1);
089: listener.check(false, true, false);
090: doc.setCharacterAttributes(0, 1, attrs, false);
091: listener.check(false, false, true);
092: // The listener has been removed from the document
093: doc.removeDocumentListener(listener);
094: doc.insertString(0, "1", null);
095: listener.check(false, false, false);
096: doc.remove(0, 1);
097: listener.check(false, false, false);
098: doc.setCharacterAttributes(0, 1, attrs, false);
099: listener.check(false, false, false);
100: }
101:
102: public void testGetDefaultRootElement() {
103: assertTrue(doc.getDefaultRootElement() instanceof SectionElement);
104: }
105:
106: /*
107: * DefaultStyledDocument()
108: */
109: public void testDefaultStyledDocument() {
110: doc = new DefaultStyledDocument();
111: assertEquals(DefaultStyledDocument.BUFFER_SIZE_DEFAULT,
112: ((GapContent) doc.getContent()).getArrayLength());
113: }
114:
115: /*
116: * DefaultStyledDocument(AbstractDocument.Content, StyleContext)
117: */
118: public void testDefaultStyledDocumentContentStyleContext() {
119: StyleContext styles = new StyleContext();
120: doc = new DefaultStyledDocument(new GapContent(10), styles);
121: assertEquals(10, ((GapContent) doc.getContent())
122: .getArrayLength());
123: Element root = doc.getDefaultRootElement();
124: assertTrue(root instanceof SectionElement);
125: assertEquals(1, root.getElementCount());
126: Element child = root.getElement(0);
127: assertTrue(child instanceof BranchElement);
128: assertEquals(1, child.getElementCount());
129: assertTrue(child.getElement(0) instanceof LeafElement);
130: assertSame(styles, doc.getAttributeContext());
131: assertSame(styles.getStyle(StyleContext.DEFAULT_STYLE), child
132: .getAttributes().getResolveParent());
133: }
134:
135: /*
136: * DefaultStyledDocument(StyleContext)
137: */
138: public void testDefaultStyledDocumentStyleContext() {
139: StyleContext styles = new StyleContext();
140: doc = new DefaultStyledDocument(styles);
141: DefaultStyledDocument anotherDoc = new DefaultStyledDocument(
142: styles);
143: assertSame(doc.getAttributeContext(), anotherDoc
144: .getAttributeContext());
145: }
146:
147: public void testCreateDefaultRoot() {
148: AbstractElement defRoot = doc.createDefaultRoot();
149: assertTrue(defRoot instanceof SectionElement);
150: assertEquals(0, defRoot.getAttributeCount());
151: assertEquals(1, defRoot.getElementCount());
152: assertTrue(defRoot.getElement(0) instanceof BranchElement);
153: }
154:
155: /**
156: * Tests AbstractDocument.insertString() in respect to handling
157: * of filterNewLine property.
158: * <p><i>The property has no effect.</i></p>
159: */
160: public void testInsertString() throws BadLocationException {
161: final String content = "one\ntwo\nthree";
162: final String filterNewLinesProperty = "filterNewlines";
163: doc.insertString(0, content, null);
164: assertNull(getNewLineProperty());
165: assertEquals(content, getText());
166: doc.remove(0, doc.getLength());
167: doc.putProperty(filterNewLinesProperty, Boolean.TRUE);
168: doc.insertString(0, content, null);
169: assertSame(Boolean.TRUE, getNewLineProperty());
170: assertEquals(content, getText());
171: doc.remove(0, doc.getLength());
172: doc.putProperty(filterNewLinesProperty, Boolean.FALSE);
173: doc.insertString(0, content, null);
174: assertSame(Boolean.FALSE, getNewLineProperty());
175: assertEquals(content, getText());
176: }
177:
178: public void testSerializable() throws Exception {
179: final String text = "some sample text";
180: doc.insertString(0, text, DefStyledDoc_Helpers.bold);
181: doc = (DefaultStyledDocument) BasicSwingTestCase
182: .serializeObject(doc);
183: final Element root = doc.getDefaultRootElement();
184: assertEquals(1, root.getElementCount());
185: final Element paragraph = root.getElement(0);
186: assertEquals(2, paragraph.getElementCount());
187: final Element character = paragraph.getElement(0);
188: assertEquals(0, character.getStartOffset());
189: assertEquals(text.length(), character.getEndOffset());
190: assertTrue(character.getAttributes().isEqual(
191: DefStyledDoc_Helpers.bold));
192: assertEquals(text.length() + 1, paragraph.getEndOffset());
193: assertEquals(text, doc.getText(0, doc.getLength()));
194: // Check that ElementBuffer is also functional
195: doc.writeLock();
196: try {
197: doc.buffer.change(2, 6, doc.new DefaultDocumentEvent(2, 6,
198: EventType.CHANGE));
199: assertEquals(4, paragraph.getElementCount());
200: doc.buffer.remove(2, 6, doc.new DefaultDocumentEvent(2, 6,
201: EventType.REMOVE));
202: assertEquals(3, paragraph.getElementCount());
203: } finally {
204: doc.writeUnlock();
205: }
206: doc.insertString(0, "1\n2\n3\n", null);
207: for (int i = 0; i < root.getElementCount(); i++) {
208: Element branch = root.getElement(i);
209: assertEquals("root.children[" + i + "].attributes.count",
210: 1, branch.getAttributes().getAttributeCount());
211: assertNotNull("root.children[" + i
212: + "].attributes.getResolver()", branch
213: .getAttributes().getResolveParent());
214: }
215: }
216:
217: private Object getNewLineProperty() {
218: return doc.getProperty("filterNewlines");
219: }
220:
221: private String getText() throws BadLocationException {
222: return doc.getText(0, doc.getLength());
223: }
224: /*
225: These methods are not tested because they are tests by their side effects:
226: insertUpdate prepares ElementSpecs and calls buffer.insert()
227: removeUpdate has nothing to do except calling buffer.remove()
228: public void testInsertUpdate() {
229: }
230:
231: public void testRemoveUpdate() {
232: }
233: */
234: }
|