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: package javax.swing.undo;
018:
019: import java.util.Hashtable;
020: import javax.swing.SwingTestCase;
021:
022: @SuppressWarnings("unchecked")
023: public class StateEditTest extends SwingTestCase {
024: StateEdit se1;
025:
026: StateEdit se2;
027:
028: boolean bWasException;
029:
030: SimpleEditable obj;
031:
032: class SimpleEditable implements StateEditable {
033: boolean wasCallStore = false;
034:
035: boolean wasCallRestore = false;
036:
037: Hashtable state = null;
038:
039: public void storeState(final Hashtable ht) {
040: ht.put("store", "state");
041: ht.put("into", "this table");
042: wasCallStore = true;
043: state = ht;
044: }
045:
046: public void restoreState(final Hashtable ht) {
047: wasCallRestore = true;
048: state = ht;
049: }
050: }
051:
052: class SimpleStateEdit extends StateEdit {
053: private static final long serialVersionUID = 1L;
054:
055: boolean wasCallRemoveRedudant = false;
056:
057: Hashtable state1 = null;
058:
059: Hashtable state2 = null;
060:
061: void resetDbgInfo() {
062: wasCallRemoveRedudant = false;
063: state1 = null;
064: state2 = null;
065: }
066:
067: public SimpleStateEdit(final StateEditable s) {
068: super (s);
069: }
070:
071: public SimpleStateEdit(final StateEditable s, final String name) {
072: super (s, name);
073: }
074:
075: @Override
076: protected void removeRedundantState() {
077: wasCallRemoveRedudant = true;
078: state1 = preState;
079: state2 = postState;
080: super .removeRedundantState();
081: }
082: }
083:
084: @Override
085: protected void setUp() throws Exception {
086: bWasException = false;
087: se1 = new SimpleStateEdit(new SimpleEditable());
088: obj = (SimpleEditable) se1.object;
089: se2 = new StateEdit(new SimpleEditable(), "presentationName");
090: super .setUp();
091: }
092:
093: public void testGetPresentationName() {
094: assertNull(se1.getPresentationName());
095: assertEquals("presentationName", se2.getPresentationName());
096: }
097:
098: public void testUndo() {
099: se1.preState.put("1", new Integer(1));
100: se1.preState.put("2", new Integer(2));
101: se1.postState = new Hashtable();
102: se1.postState.put("3", new Integer(3));
103: se1.postState.put("4", new Integer(4));
104: Hashtable oldPreState = se1.preState;
105: Hashtable oldPostState = se1.postState;
106: se1.undo();
107: assertTrue(obj.wasCallRestore);
108: assertEquals(obj.state, se1.preState);
109: assertEquals(oldPreState, se1.preState);
110: assertEquals(oldPostState, se1.postState);
111: }
112:
113: public void testRedo() {
114: try {
115: se1.redo();
116: } catch (CannotRedoException e) {
117: bWasException = true;
118: }
119: assertTrue("ExpectedException", bWasException);
120: se1.undo();
121: obj.wasCallRestore = false;
122: se1.preState.put("1", new Integer(1));
123: se1.preState.put("2", new Integer(2));
124: se1.postState = new Hashtable();
125: se1.postState.put("3", new Integer(3));
126: se1.postState.put("4", new Integer(4));
127: Hashtable oldPreState = se1.preState;
128: Hashtable oldPostState = se1.postState;
129: se1.redo();
130: assertTrue(obj.wasCallRestore);
131: assertEquals(se1.postState, obj.state);
132: assertEquals(oldPreState, se1.preState);
133: assertEquals(oldPostState, se1.postState);
134: }
135:
136: public void testStateEditStateEditableString() {
137: }
138:
139: public void testStateEditStateEditable() {
140: }
141:
142: public void testEnd() {
143: SimpleStateEdit stEdit = (SimpleStateEdit) se1;
144: se1.preState.put("1", new Integer(1));
145: se1.preState.put("2", new Integer(2));
146: se1.postState = new Hashtable();
147: se1.postState.put("3", new Integer(3));
148: se1.postState.put("4", new Integer(4));
149: Hashtable oldPreState = se1.preState;
150: stEdit.resetDbgInfo();
151: se1.end();
152: assertTrue(obj.wasCallStore);
153: assertEquals(stEdit.state2, obj.state);
154: assertEquals(stEdit.state1, oldPreState);
155: assertEquals(stEdit.state2, obj.state);
156: assertTrue(stEdit.wasCallRemoveRedudant);
157: }
158:
159: public void testRemoveRedundantState() {
160: assertNotNull(se1.preState);
161: assertNull(se1.postState);
162: assertEquals(2, se1.preState.size());
163: se1.preState.remove("store");
164: se1.preState.remove("into");
165: se1.postState = new Hashtable();
166: se1.preState.put("1", new Integer(1));
167: se1.preState.put("2", new Integer(2));
168: se1.preState.put("3", new Integer(3));
169: se1.preState.put("4", new Integer(4));
170: se1.preState.put("5", new Integer(5));
171: se1.postState.put("1", new Integer(44));
172: se1.postState.put("2x", new Integer(2));
173: se1.postState.put("3x", new Integer(3));
174: se1.postState.put("4x", new Integer(4));
175: se1.postState.put("5", new Integer(5));
176: se1.removeRedundantState();
177: Hashtable preState = se1.preState;
178: Hashtable postState = se1.postState;
179: assertEquals(4, se1.preState.size());
180: assertEquals(new Integer(1), preState.get("1"));
181: assertEquals(new Integer(2), preState.get("2"));
182: assertEquals(new Integer(3), preState.get("3"));
183: assertEquals(new Integer(4), preState.get("4"));
184: assertEquals(4, se1.postState.size());
185: assertEquals(new Integer(44), postState.get("1"));
186: assertEquals(new Integer(2), postState.get("2x"));
187: assertEquals(new Integer(3), postState.get("3x"));
188: assertEquals(new Integer(4), postState.get("4x"));
189: }
190:
191: Hashtable getState(final StateEditable editable) {
192: Hashtable ht = new Hashtable();
193: editable.storeState(ht);
194: return ht;
195: }
196:
197: public void testInit() {
198: SimpleEditable newObj = new SimpleEditable();
199: obj.wasCallStore = false;
200: obj.state = null;
201: assertNull(se1.undoRedoName);
202: assertEquals("presentationName", se2.undoRedoName);
203: se1.init(newObj, "name");
204: assertEquals(newObj, se1.object);
205: assertEquals("name", se1.getPresentationName());
206: assertTrue(newObj.wasCallStore);
207: assertEquals(newObj.state, se1.preState);
208: assertNull(se1.postState);
209: assertEquals(getState(newObj), se1.preState);
210: assertEquals("name", se1.undoRedoName);
211:
212: try { // Regression test for HARMONY-2536
213: new StateEdit(null);
214: fail("NullPointerException should have been thrown");
215: } catch (NullPointerException e) {
216: // Expected
217: }
218: try { // Regression test for HARMONY-2536
219: new StateEdit(null, "str");
220: fail("NullPointerException should have been thrown");
221: } catch (NullPointerException e) {
222: // Expected
223: }
224: }
225:
226: // Regression test for HARMONY-2844
227: public void testInitNull() {
228: StateEdit se = new StateEdit(new SimpleEditable());
229: try {
230: se.init(null, "test");
231: fail("NullPointerException is expected");
232: } catch (NullPointerException e) {
233: // expected
234: }
235: }
236:
237: public void testConstants() {
238: assertEquals("$Id: StateEdit.java,v 1.6 1997/10"
239: + "/01 20:05:51 sandipc Exp $", StateEdit.RCSID);
240: assertEquals("$Id: StateEditable.java,v 1.2 1997/09"
241: + "/08 19:39:08 marklin Exp $", StateEditable.RCSID);
242: }
243: }
|