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.Iterator;
023: import java.util.List;
024: import javax.swing.event.UndoableEditEvent;
025: import javax.swing.event.UndoableEditListener;
026: import javax.swing.text.AbstractDocument.DefaultDocumentEvent;
027: import javax.swing.text.DefaultStyledDocument.AttributeUndoableEdit;
028: import junit.framework.TestCase;
029:
030: /**
031: * Tests DefaultStyledDocument.AttributeUndoableEdit class
032: *
033: */
034: public class DefaultStyledDocument_AttributeUndoableEditTest extends
035: TestCase implements UndoableEditListener {
036: private AttributeUndoableEdit undoEdit;
037:
038: private DefaultStyledDocument doc;
039:
040: private static final AttributeSet italic = DefStyledDoc_Helpers.italic;
041:
042: private static final AttributeSet bold = DefStyledDoc_Helpers.bold;
043:
044: @Override
045: protected void setUp() throws Exception {
046: super .setUp();
047: doc = new DefaultStyledDocument();
048: doc.insertString(0, "a text portion", italic);
049: // 01234567890123
050: doc.addUndoableEditListener(this );
051: }
052:
053: /**
054: * Tests the functionality of <code>undo()</code>/<code>redo()</code>
055: * when attributes are applied in non-replacing way.
056: */
057: public void testUndoRedoNoReplace() {
058: doc.setCharacterAttributes(2, 4, bold, false);
059: doc.writeLock();
060: try {
061: undoEdit.undo();
062: AttributeSet attrs = doc.getCharacterElement(4)
063: .getAttributes();
064: assertEquals(1, attrs.getAttributeCount());
065: assertFalse(attrs.containsAttributes(bold));
066: assertTrue(attrs.containsAttributes(italic));
067: undoEdit.redo();
068: attrs = doc.getCharacterElement(4).getAttributes();
069: assertEquals(2, attrs.getAttributeCount());
070: assertTrue(attrs.containsAttributes(bold));
071: assertTrue(attrs.containsAttributes(italic));
072: } finally {
073: doc.writeUnlock();
074: }
075: }
076:
077: /**
078: * Tests the functionality of <code>undo()</code>/<code>redo()</code>
079: * when attribute added has another value of the same attribute.
080: */
081: public void testUndoRedoItalicFalse() {
082: final MutableAttributeSet italicFalse = new SimpleAttributeSet();
083: StyleConstants.setItalic(italicFalse, false);
084: doc.setCharacterAttributes(2, 4, italicFalse, false);
085: doc.writeLock();
086: try {
087: undoEdit.undo();
088: AttributeSet attrs = doc.getCharacterElement(4)
089: .getAttributes();
090: assertEquals(1, attrs.getAttributeCount());
091: assertTrue(attrs.containsAttributes(italic));
092: undoEdit.redo();
093: attrs = doc.getCharacterElement(4).getAttributes();
094: assertEquals(1, attrs.getAttributeCount());
095: assertTrue(attrs.containsAttributes(italicFalse));
096: } finally {
097: doc.writeUnlock();
098: }
099: }
100:
101: /**
102: * Tests the functionality of <code>undo()</code>/<code>redo()</code>
103: * when attributes are replaced.
104: */
105: public void testUndoRedoWithReplace() {
106: doc.setCharacterAttributes(2, 4, bold, true);
107: doc.writeLock();
108: try {
109: undoEdit.undo();
110: AttributeSet attrs = doc.getCharacterElement(4)
111: .getAttributes();
112: assertEquals(1, attrs.getAttributeCount());
113: assertFalse(attrs.containsAttributes(bold));
114: assertTrue(attrs.containsAttributes(italic));
115: undoEdit.redo();
116: attrs = doc.getCharacterElement(4).getAttributes();
117: assertEquals(1, attrs.getAttributeCount());
118: assertTrue(attrs.containsAttributes(bold));
119: assertFalse(attrs.containsAttributes(italic));
120: } finally {
121: doc.writeUnlock();
122: }
123: }
124:
125: /**
126: * Tests the constructor of the class. The element has no attributes
127: * in this test.
128: */
129: public void testAttributeUndoableEdit() {
130: Element elem = new PlainDocument().getDefaultRootElement();
131: AttributeSet attrs = new SimpleAttributeSet();
132: undoEdit = new AttributeUndoableEdit(elem, attrs, true);
133: assertSame(elem, undoEdit.element);
134: assertSame(attrs, undoEdit.newAttributes);
135: assertTrue(undoEdit.isReplacing);
136: assertSame(SimpleAttributeSet.EMPTY, undoEdit.copy);
137: }
138:
139: /**
140: * Tests the constructor of the class. The element has
141: * <code>bidiLevel</code> attributes in this test.
142: */
143: public void testAttributeUndoableEditBidiElement() {
144: Element elem = new PlainDocument().getBidiRootElement()
145: .getElement(0);
146: AttributeSet attrs = new SimpleAttributeSet();
147: undoEdit = new AttributeUndoableEdit(elem, attrs, true);
148: assertSame(elem, undoEdit.element);
149: assertSame(attrs, undoEdit.newAttributes);
150: assertTrue(undoEdit.isReplacing);
151: assertEquals(1, undoEdit.copy.getAttributeCount());
152: assertTrue(undoEdit.copy.containsAttribute(
153: StyleConstants.BidiLevel, new Integer(0)));
154: }
155:
156: /**
157: * Adds attributes to a portion of text.
158: * Asserts the AttributeUndoableEdit instance will have the expected values.
159: * <p>It also checks the attributes of that text portion.
160: */
161: public void testChangeNoReplace() {
162: doc.setCharacterAttributes(2, 4, bold, false);
163: assertNotNull(undoEdit);
164: assertSame(doc.getCharacterElement(4), undoEdit.element);
165: assertTrue(undoEdit.newAttributes.isEqual(bold));
166: assertFalse(undoEdit.isReplacing);
167: assertTrue(undoEdit.copy.isEqual(italic));
168: AttributeSet attrs = doc.getCharacterElement(4).getAttributes();
169: assertEquals(2, attrs.getAttributeCount());
170: assertTrue(attrs.containsAttributes(bold));
171: assertTrue(attrs.containsAttributes(italic));
172: }
173:
174: /**
175: * Replaces attributes in a portion of text.
176: * Asserts the AttributeUndoableEdit instance will have the expected values.
177: * <p>It also checks the attributes of that text portion.
178: */
179: public void testChangeWithReplace() {
180: doc.setCharacterAttributes(2, 4, bold, true);
181: assertNotNull(undoEdit);
182: assertSame(doc.getCharacterElement(4), undoEdit.element);
183: assertTrue(undoEdit.newAttributes.isEqual(bold));
184: assertTrue(undoEdit.isReplacing);
185: assertTrue(undoEdit.copy.isEqual(italic));
186: AttributeSet attrs = doc.getCharacterElement(4).getAttributes();
187: assertEquals(1, attrs.getAttributeCount());
188: assertTrue(attrs.containsAttributes(bold));
189: assertFalse(attrs.containsAttributes(italic));
190: }
191:
192: public void undoableEditHappened(UndoableEditEvent e) {
193: searchForAttributeUE((DefaultDocumentEvent) e.getEdit());
194: }
195:
196: private void searchForAttributeUE(DefaultDocumentEvent e) {
197: List<?> edits = DefStyledDoc_Helpers.getEdits(e);
198: for (Iterator<?> i = edits.iterator(); i.hasNext();) {
199: Object edit = i.next();
200: if (edit instanceof AttributeUndoableEdit) {
201: undoEdit = (AttributeUndoableEdit) edit;
202: break;
203: }
204: }
205: }
206: }
|