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.ElementChange;
026:
027: public class PlainDocumentRTest extends BasicSwingTestCase implements
028: DocumentListener {
029: private static final String filterNewLinesProperty = "filterNewlines";
030:
031: private PlainDocument doc;
032:
033: private Element root;
034:
035: private DocumentEvent insertEvent;
036:
037: private ElementChange change;
038:
039: /**
040: * Tests handling of <code>filterNewLine</code> property
041: * while using <code>replace</code>.
042: */
043: public void testReplace() throws BadLocationException {
044: final String content = "one\ntwo\nthree";
045: doc.replace(0, 0, content, null);
046: assertNull(getNewLineProperty());
047: assertEquals(content, getText());
048: setNewLineProperty(true);
049: doc.replace(0, doc.getLength(), content, null);
050: assertSame(Boolean.TRUE, getNewLineProperty());
051: assertEquals(content.replace('\n', ' '), getText());
052: doc.remove(0, doc.getLength());
053: setNewLineProperty(false);
054: doc.replace(0, doc.getLength(), content, null);
055: assertSame(Boolean.FALSE, getNewLineProperty());
056: assertEquals(content, getText());
057: }
058:
059: /**
060: * Tests the use of <code>filterNewlines</code> property set on
061: * a <code>PlainDocument</code> in the presence of
062: * a <code>DocumentFilter</code>.
063: */
064: public void testReplaceDF() throws BadLocationException {
065: DocumentFilter filter = new DocumentFilter() {
066: @Override
067: public void insertString(final FilterBypass fb,
068: final int offset, final String text,
069: final AttributeSet attrs)
070: throws BadLocationException {
071: super .insertString(fb, offset, "###\n^^^", attrs);
072: }
073:
074: @Override
075: public void remove(final FilterBypass fb, final int offset,
076: final int length) throws BadLocationException {
077: super .remove(fb, offset, length);
078: }
079:
080: @Override
081: public void replace(final FilterBypass fb,
082: final int offset, final int length,
083: final String text, final AttributeSet attrs)
084: throws BadLocationException {
085: super .replace(fb, offset, length, "!" + text + "!",
086: attrs);
087: }
088: };
089: doc.insertString(0, "plain text", null);
090: doc.setDocumentFilter(filter);
091: setNewLineProperty(true);
092: doc.replace(2, 6, "++\n--", null);
093: assertEquals(isHarmony() ? "pl!++ --!xt" : "pl!++\n--!xt",
094: getText());
095: doc.remove(0, doc.getLength());
096: doc.insertString(0, "^^^\n###", null);
097: assertEquals(isHarmony() ? "### ^^^" : "###\n^^^", getText());
098: }
099:
100: public void testInsertUpdate09() throws BadLocationException {
101: doc.insertString(0, "line1\n\n\n\n", null);
102: // 012345 6 7 8
103: doc.addDocumentListener(this );
104: doc.insertString(7, "\n", null);
105: change = insertEvent.getChange(root);
106: assertEquals(1, change.getChildrenRemoved().length);
107: assertEquals(2, change.getChildrenAdded().length);
108: checkOffsets(change.getChildrenRemoved()[0], 6, 8);
109: checkOffsets(change.getChildrenAdded()[0], 6, 7);
110: checkOffsets(change.getChildrenAdded()[1], 7, 8);
111: }
112:
113: public void testInsertUpdate10() throws BadLocationException {
114: doc.insertString(0, "line1\n\n1\n\n", null);
115: // 012345 6 78 9
116: doc.addDocumentListener(this );
117: doc.insertString(7, "\n", null);
118: change = insertEvent.getChange(root);
119: assertEquals(1, change.getChildrenRemoved().length);
120: assertEquals(2, change.getChildrenAdded().length);
121: checkOffsets(change.getChildrenRemoved()[0], 6, 8);
122: checkOffsets(change.getChildrenAdded()[0], 6, 7);
123: checkOffsets(change.getChildrenAdded()[1], 7, 8);
124: }
125:
126: public void testInsertUpdate11() throws BadLocationException {
127: doc.insertString(0, "line1\n\n1\n\n", null);
128: // 012345 6 78 9
129: doc.addDocumentListener(this );
130: doc.insertString(8, "\n", null);
131: change = insertEvent.getChange(root);
132: assertEquals(1, change.getChildrenRemoved().length);
133: assertEquals(2, change.getChildrenAdded().length);
134: checkOffsets(change.getChildrenRemoved()[0], 7, 10);
135: checkOffsets(change.getChildrenAdded()[0], 7, 9);
136: checkOffsets(change.getChildrenAdded()[1], 9, 10);
137: }
138:
139: public void insertUpdate(DocumentEvent e) {
140: insertEvent = e;
141: }
142:
143: public void removeUpdate(DocumentEvent e) {
144: }
145:
146: public void changedUpdate(DocumentEvent e) {
147: }
148:
149: @Override
150: protected void setUp() throws Exception {
151: super .setUp();
152: doc = new PlainDocument();
153: root = doc.getDefaultRootElement();
154: }
155:
156: private static void checkOffsets(final Element line,
157: final int start, final int end) {
158: assertEquals(start, line.getStartOffset());
159: assertEquals(end, line.getEndOffset());
160: }
161:
162: private void setNewLineProperty(final boolean state) {
163: doc.putProperty(filterNewLinesProperty, Boolean.valueOf(state));
164: }
165:
166: private Object getNewLineProperty() {
167: return doc.getProperty(filterNewLinesProperty);
168: }
169:
170: private String getText() throws BadLocationException {
171: return doc.getText(0, doc.getLength());
172: }
173: }
|