01: package abbot.editor.actions;
02:
03: import junit.extensions.abbot.TestHelper;
04: import junit.framework.TestCase;
05:
06: public class CommandHistoryTest extends TestCase {
07:
08: private UndoableCommand undoable = new UndoableCommand() {
09: public void execute() {
10: commandRun = true;
11: }
12:
13: public void undo() {
14: commandRun = false;
15: }
16:
17: public String toString() {
18: return "set true";
19: }
20: };
21: private Command command = new Command() {
22: public void execute() {
23: commandCount++;
24: }
25:
26: public String toString() {
27: return "increment";
28: }
29: };
30:
31: private boolean commandRun = false;
32: private int commandCount = 0;
33:
34: public void testUndo() throws Throwable {
35: commandRun = false;
36: CommandHistory history = new CommandHistory();
37: assertTrue("Should not be able to undo", !history.canUndo());
38:
39: // Command 1
40: command.execute();
41: history.add(command);
42: assertEquals("Command should have been run", 1, commandCount);
43: assertTrue("Should not be able to undo", !history.canUndo());
44:
45: // Command 2
46: undoable.execute();
47: history.add(undoable);
48: assertTrue("Command should have been run", commandRun);
49: assertTrue("Should be able to undo", history.canUndo());
50:
51: // Undo Command 2 (becomes Command 3)
52: history.undo();
53: assertTrue("Command should have been undone", !commandRun);
54: assertTrue("Should be no further undo information", !history
55: .canUndo());
56: try {
57: history.undo();
58: fail("Expected an exception with no further undo information");
59: } catch (NoUndoException nue) {
60: assertTrue("Should be no change", !commandRun);
61: assertTrue("Undo should now be available", history
62: .canUndo());
63: }
64:
65: // History should now wrap back to the end
66: // Undo Command 3 (becomes Command 4)
67: history.undo();
68: assertTrue("Command should have been undone", commandRun);
69: assertTrue("Should be able to undo", history.canUndo());
70: // Undo Command 2 (becomes Command 5)
71: history.undo();
72: assertTrue("Command should have been undone", !commandRun);
73: assertTrue("Should not be able to undo", !history.canUndo());
74:
75: // An undoable command should flush the history
76: command.execute();
77: history.add(command);
78: assertTrue("Should not be able to undo", !history.canUndo());
79: }
80:
81: public CommandHistoryTest(String name) {
82: super (name);
83: }
84:
85: public static void main(String[] args) {
86: TestHelper.runTests(args, CommandHistoryTest.class);
87: }
88: }
|