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 javax.swing.BasicSwingTestCase;
023: import javax.swing.UIManager;
024: import junit.framework.TestCase;
025:
026: public class AbstractUndoableEditTest extends TestCase {
027: private static final String UNDO_NAME_KEY = "AbstractUndoableEdit.undoText";
028:
029: private static final String REDO_NAME_KEY = "AbstractUndoableEdit.redoText";
030:
031: protected AbstractUndoableEdit obj;
032:
033: private String defaultUndoName;
034:
035: private String defaultRedoName;
036:
037: @Override
038: protected void setUp() throws Exception {
039: obj = new AbstractUndoableEdit();
040: defaultUndoName = UIManager.getString(UNDO_NAME_KEY);
041: defaultRedoName = UIManager.getString(REDO_NAME_KEY);
042: }
043:
044: @Override
045: protected void tearDown() throws Exception {
046: UIManager.put(UNDO_NAME_KEY, defaultUndoName);
047: UIManager.put(REDO_NAME_KEY, defaultRedoName);
048: }
049:
050: public void testToString() {
051: String s = obj.toString();
052: assertNotNull(s);
053: assertNotSame("", s);
054: }
055:
056: protected boolean isAlive(final Object o) {
057: return o.toString().indexOf("alive: true") != -1;
058: }
059:
060: protected boolean hasBeenDone(final Object o) {
061: return o.toString().indexOf("hasBeenDone: true") != -1;
062: }
063:
064: public void testAbstractUndoableEdit() {
065: if (BasicSwingTestCase.isHarmony()) {
066: assertTrue(isAlive(obj));
067: }
068: }
069:
070: public void testDie() {
071: obj.die();
072: if (BasicSwingTestCase.isHarmony()) {
073: assertFalse(isAlive(obj));
074: }
075: boolean wasException = false;
076: try {
077: obj.redo();
078: } catch (CannotRedoException e) {
079: wasException = true;
080: }
081: assertTrue("CannotRedoException was expected", wasException);
082: wasException = false;
083: try {
084: obj.undo();
085: } catch (CannotUndoException e) {
086: wasException = true;
087: }
088: assertTrue("CannotUndoException was expected", wasException);
089: }
090:
091: public void testRedo() {
092: obj.undo();
093: obj.redo();
094: if (BasicSwingTestCase.isHarmony()) {
095: assertTrue(hasBeenDone(obj));
096: }
097: if (!obj.canRedo()) {
098: boolean wasException = false;
099: try {
100: obj.redo();
101: } catch (CannotRedoException e) {
102: wasException = true;
103: }
104: assertTrue("CannotRedoException was expected", wasException);
105: }
106: }
107:
108: public void testUndo() {
109: obj.undo();
110: if (BasicSwingTestCase.isHarmony()) {
111: assertFalse(hasBeenDone(obj));
112: }
113: if (!obj.canUndo()) {
114: boolean wasException = false;
115: try {
116: obj.undo();
117: } catch (CannotUndoException e) {
118: wasException = true;
119: }
120: assertTrue("CannotUndoException was expected", wasException);
121: }
122: }
123:
124: public void testCanRedo() {
125: if (BasicSwingTestCase.isHarmony()) {
126: assertEquals(isAlive(obj) && !hasBeenDone(obj), obj
127: .canRedo());
128: }
129: obj.die();
130: assertFalse(obj.canRedo());
131: }
132:
133: public void testCanUndo() {
134: if (BasicSwingTestCase.isHarmony()) {
135: assertEquals(isAlive(obj) && hasBeenDone(obj), obj
136: .canUndo());
137: }
138: obj.die();
139: assertFalse(obj.canUndo());
140: }
141:
142: public void testIsSignificant() {
143: assertTrue(obj.isSignificant());
144: }
145:
146: public void testGetPresentationName() {
147: assertEquals("", obj.getPresentationName());
148: }
149:
150: public void testGetRedoPresentationName() {
151: assertEquals(UIManager.getString(REDO_NAME_KEY), obj
152: .getRedoPresentationName());
153: }
154:
155: public void testGetRedoPresentationNameModified() {
156: String redoName = "name of Redo";
157: UIManager.put(REDO_NAME_KEY, redoName);
158: assertEquals(redoName, obj.getRedoPresentationName());
159: redoName = "alternative redo";
160: UIManager.put(REDO_NAME_KEY, redoName);
161: assertEquals(redoName, obj.getRedoPresentationName());
162: }
163:
164: public void testGetUndoPresentationName() {
165: assertEquals(UIManager.getString(UNDO_NAME_KEY), obj
166: .getUndoPresentationName());
167: }
168:
169: public void testGetUndoPresentationNameModified() {
170: String undoName = "name of Undo";
171: UIManager.put(UNDO_NAME_KEY, undoName);
172: assertEquals(undoName, obj.getUndoPresentationName());
173: undoName = "alternative undo";
174: UIManager.put(UNDO_NAME_KEY, undoName);
175: assertEquals(undoName, obj.getUndoPresentationName());
176: }
177:
178: public void testGetUndoPresentationNameNull() {
179: obj = new AbstractUndoableEdit() {
180: private static final long serialVersionUID = 1L;
181:
182: @Override
183: public String getPresentationName() {
184: return null;
185: }
186: };
187: assertEquals(UIManager.getString(UNDO_NAME_KEY), obj
188: .getUndoPresentationName());
189: }
190:
191: public void testAddEdit() {
192: assertFalse(obj.addEdit(null));
193: }
194:
195: public void testReplaceEdit() {
196: assertFalse(obj.replaceEdit(null));
197: }
198: }
|