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 Evgeniya G. Maenkova
019: * @version $Revision$
020: */package javax.swing.undo;
021:
022: import java.util.Vector;
023: import javax.swing.SwingTestCase;
024: import javax.swing.event.UndoableEditEvent;
025: import javax.swing.event.UndoableEditListener;
026: import javax.swing.text.PlainDocument;
027:
028: public class UndoableEditSupportTest extends SwingTestCase {
029: ExtUESupport ues1;
030:
031: UndoableEditSupport ues2;
032:
033: PlainDocument realSource;
034:
035: String fireOrder;
036:
037: final String PATTERN = "@[^ }]*";
038:
039: class UEListener implements UndoableEditListener {
040: String name;
041:
042: UndoableEditEvent event = null;
043:
044: public UEListener(final String s) {
045: name = s;
046: }
047:
048: public void undoableEditHappened(final UndoableEditEvent e) {
049: fireOrder += name;
050: event = e;
051: }
052: }
053:
054: class ExtUESupport extends UndoableEditSupport {
055: boolean wasCallCreate = false;
056:
057: boolean wasCallPostEdit = false;
058:
059: UndoableEdit ue = null;
060:
061: void resetDbgInfo() {
062: wasCallPostEdit = false;
063: wasCallCreate = false;
064: ue = null;
065: }
066:
067: @Override
068: protected void _postEdit(final UndoableEdit e) {
069: wasCallPostEdit = true;
070: ue = e;
071: super ._postEdit(e);
072: }
073:
074: @Override
075: public synchronized void postEdit(final UndoableEdit a0) {
076: super .postEdit(a0);
077: }
078:
079: @Override
080: protected CompoundEdit createCompoundEdit() {
081: wasCallCreate = true;
082: return super .createCompoundEdit();
083: }
084: }
085:
086: class ExtCompoundEdit extends CompoundEdit {
087: private static final long serialVersionUID = 1L;
088:
089: boolean wasCallAddEdit = false;
090:
091: boolean wasCallEnd = false;
092:
093: public ExtCompoundEdit() {
094: super ();
095: }
096:
097: @Override
098: public boolean addEdit(final UndoableEdit anEdit) {
099: wasCallAddEdit = true;
100: return super .addEdit(anEdit);
101: }
102:
103: @Override
104: public void end() {
105: wasCallEnd = true;
106: super .end();
107: }
108:
109: void resetDbgInfo() {
110: wasCallAddEdit = false;
111: wasCallEnd = false;
112: }
113: }
114:
115: void resetDbgInfo(final ExtCompoundEdit ce, final ExtUESupport ues) {
116: ce.resetDbgInfo();
117: ues.resetDbgInfo();
118: }
119:
120: @Override
121: protected void setUp() throws Exception {
122: ues1 = new ExtUESupport();
123: realSource = new PlainDocument();
124: ues2 = new UndoableEditSupport(realSource);
125: fireOrder = "";
126: super .setUp();
127: }
128:
129: @Override
130: protected void tearDown() throws Exception {
131: super .tearDown();
132: }
133:
134: public void testUndoableEditSupport() {
135: assertEquals(0, ues1.updateLevel);
136: assertEquals(0, ues2.updateLevel);
137: assertEquals(0, ues1.getUpdateLevel());
138: assertEquals(0, ues2.getUpdateLevel());
139: assertEquals(ues1, ues1.realSource);
140: assertEquals(realSource, ues2.realSource);
141: assertNull(ues1.compoundEdit);
142: assertNull(ues2.compoundEdit);
143: assertEquals(0, ues1.getUndoableEditListeners().length);
144: assertEquals(0, ues2.getUndoableEditListeners().length);
145: UndoableEditSupport ues = new UndoableEditSupport(null);
146: assertEquals(ues, ues.realSource);
147: }
148:
149: void checkEvent(final UndoableEditEvent e1,
150: final UndoableEditEvent e2) {
151: assertEquals(e1.getSource(), e2.getSource());
152: assertEquals(e1.getEdit(), e2.getEdit());
153: }
154:
155: public void test_postEdit() {
156: UEListener listener1 = new UEListener("1");
157: UEListener listener2 = new UEListener("2");
158: UEListener listener3 = new UEListener("3");
159: ues1.addUndoableEditListener(listener1);
160: ues1.addUndoableEditListener(listener2);
161: ues1.addUndoableEditListener(listener3);
162: UndoableEdit ue = new CompoundEdit();
163: UndoableEditEvent uee = new UndoableEditEvent(ues1.realSource,
164: ue);
165: ues1._postEdit(ue);
166: assertEquals("123", fireOrder);
167: checkEvent(uee, listener1.event);
168: checkEvent(uee, listener2.event);
169: checkEvent(uee, listener3.event);
170: }
171:
172: public void testBeginUpdate() {
173: ues1.wasCallCreate = false;
174: for (int i = 0; i < 100; i++) {
175: ues1.resetDbgInfo();
176: ues1.beginUpdate();
177: if (i == 0) {
178: assertTrue(ues1.wasCallCreate);
179: } else {
180: assertFalse(ues1.wasCallCreate);
181: assertEquals(i + 1, ues1.getUpdateLevel());
182: }
183: }
184: }
185:
186: void checkCompoundEdit(final CompoundEdit ce, final int size) {
187: assertNotNull(ce);
188: assertEquals(size, ce.edits.size());
189: }
190:
191: public void testCreateCompoundEdit() {
192: checkCompoundEdit(ues1.createCompoundEdit(), 0);
193: checkCompoundEdit(ues2.createCompoundEdit(), 0);
194: }
195:
196: public void testEndUpdate() {
197: UEListener listener = new UEListener("1");
198: ues1.addUndoableEditListener(listener);
199: ExtCompoundEdit ce = new ExtCompoundEdit();
200: ues1.compoundEdit = ce;
201: for (int i = 0; i < 5; i++) {
202: ues1.beginUpdate();
203: assertEquals(0, ues1.compoundEdit.edits.size());
204: }
205: ues1.compoundEdit = ce;
206: for (int i = 0; i < 10; i++) {
207: CompoundEdit ce1 = ues1.compoundEdit;
208: UndoableEditEvent uee = new UndoableEditEvent(
209: ues1.realSource, ce1);
210: resetDbgInfo(ce, ues1);
211: ues1.endUpdate();
212: assertEquals(4 - i, ues1.updateLevel);
213: if (ues1.updateLevel == 0) {
214: assertTrue(ues1.wasCallPostEdit);
215: checkEvent(uee, listener.event);
216: assertEquals(ce1, ues1.ue);
217: assertTrue(ce.wasCallEnd);
218: } else {
219: assertFalse(ues1.wasCallPostEdit);
220: assertFalse(ce.wasCallEnd);
221: }
222: if (ues1.updateLevel > 0) {
223: assertEquals(0, ues1.compoundEdit.edits.size());
224: } else {
225: assertNull(ues1.compoundEdit);
226: }
227: }
228: for (int i = 0; i < 20; i++) {
229: ues1.wasCallCreate = false;
230: ues1.beginUpdate();
231: if (ues1.updateLevel == 1) {
232: assertTrue(ues1.wasCallCreate);
233: } else {
234: assertFalse(ues1.wasCallCreate);
235: }
236: }
237: }
238:
239: void checkUpdateLevel(final int count, final UndoableEditSupport ues) {
240: assertEquals(count, ues.updateLevel);
241: assertEquals(count, ues.getUpdateLevel());
242: }
243:
244: public void testGetUpdateLevel() {
245: checkUpdateLevel(0, ues1);
246: for (int i = 3; i < 10; i++) {
247: int count = ues1.getUpdateLevel();
248: for (int i1 = 0; i1 < i; i1++) {
249: ues1.beginUpdate();
250: count++;
251: checkUpdateLevel(count, ues1);
252: }
253: for (int i1 = 0; i1 < i / 2; i1++) {
254: ues1.endUpdate();
255: count--;
256: checkUpdateLevel(count, ues1);
257: }
258: for (int i1 = i / 2; i1 < i; i1++) {
259: ues1.beginUpdate();
260: count++;
261: checkUpdateLevel(count, ues1);
262: }
263: for (int i1 = count; i1 > 0; i1--) {
264: ues1.endUpdate();
265: count--;
266: checkUpdateLevel(count, ues1);
267: }
268: for (int i1 = 0; i1 > -3; i1--) {
269: ues1.endUpdate();
270: count--;
271: checkUpdateLevel(count, ues1);
272: }
273: }
274: }
275:
276: public void testPostEdit() {
277: ues1.postEdit(null);
278: assertTrue(true);
279: assertNull(ues1.ue);
280: UndoableEdit ue = new CompoundEdit();
281: ues1.postEdit(ue);
282: assertEquals(ue, ues1.ue);
283: }
284:
285: String getString(final UndoableEditSupport ues) {
286: return ues.getClass().getName() + " " + "updateLevel: "
287: + ues.updateLevel + " " + "listeners: "
288: + ues.listeners.toString() + " " + "compoundEdit: "
289: + ues.compoundEdit;
290: }
291:
292: void checkToString(final UndoableEditSupport ues) {
293: assertEquals(getString(ues), ues2.toString().replaceFirst(
294: PATTERN, ""));
295: }
296:
297: public void testToString() {
298: checkToString(ues2);
299: ues2.beginUpdate();
300: checkToString(ues2);
301: }
302:
303: @SuppressWarnings("unchecked")
304: void checkListeners(final UndoableEditSupport ues, final Vector v) {
305: UndoableEditListener[] listeners = ues
306: .getUndoableEditListeners();
307: int length = v.size();
308: assertEquals(length, listeners.length);
309: for (int i = 0; i < length; i++) {
310: assertEquals(v.get(i), listeners[i]);
311: }
312: assertEquals(v, ues.listeners);
313: }
314:
315: @SuppressWarnings("unchecked")
316: public void testAddRemoveGetUndoableEditListener() {
317: UEListener listener1 = new UEListener("1");
318: UEListener listener2 = new UEListener("2");
319: UEListener listener3 = new UEListener("3");
320: Vector listenersVector = new Vector();
321: ues1.addUndoableEditListener(listener1);
322: listenersVector.add(listener1);
323: checkListeners(ues1, listenersVector);
324: ues1.addUndoableEditListener(listener2);
325: listenersVector.add(listener2);
326: checkListeners(ues1, listenersVector);
327: ues1.addUndoableEditListener(listener3);
328: listenersVector.add(listener3);
329: checkListeners(ues1, listenersVector);
330: ues1.addUndoableEditListener(listener1);
331: listenersVector.add(listener1);
332: checkListeners(ues1, listenersVector);
333: ues1.removeUndoableEditListener(listener2);
334: listenersVector.remove(listener2);
335: checkListeners(ues1, listenersVector);
336: ues1.removeUndoableEditListener(listener1);
337: listenersVector.remove(listener1);
338: checkListeners(ues1, listenersVector);
339: ues1.removeUndoableEditListener(listener3);
340: listenersVector.remove(listener3);
341: checkListeners(ues1, listenersVector);
342: }
343:
344: public void testPostEdit2() {
345: ExtCompoundEdit ce = new ExtCompoundEdit();
346: ues1.compoundEdit = ce;
347: resetDbgInfo(ce, ues1);
348: UndoableEdit ue = new CompoundEdit();
349: assertEquals(0, ues1.updateLevel);
350: ues1.postEdit(ue);
351: assertTrue(ues1.wasCallPostEdit);
352: assertFalse(ce.wasCallAddEdit);
353: ues1.updateLevel = 3;
354: resetDbgInfo(ce, ues1);
355: assertEquals(ce, ues1.compoundEdit);
356: assertEquals(3, ues1.updateLevel);
357: ues1.postEdit(ue);
358: assertFalse(ues1.wasCallPostEdit);
359: assertTrue(ce.wasCallAddEdit);
360: }
361: }
|