|
| 1 | +package testSuite.classes.state_test_method_error; |
| 2 | + |
| 3 | +import javax.swing.undo.AbstractUndoableEdit; |
| 4 | + |
| 5 | +public class EditMisuse { |
| 6 | + |
| 7 | + public static void undoInElseBranch(AbstractUndoableEdit edit) { |
| 8 | + if (edit.canUndo()) { |
| 9 | + } else { |
| 10 | + edit.undo(); // State Refinement Error |
| 11 | + } |
| 12 | + } |
| 13 | + |
| 14 | + public static void undoNotInElse(AbstractUndoableEdit edit) { |
| 15 | + if (!edit.canUndo()) { |
| 16 | + edit.undo(); // State Refinement Error |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + public static void wrongTesterForRedo(AbstractUndoableEdit edit) { |
| 21 | + if (edit.canUndo()) { |
| 22 | + edit.redo(); // State Refinement Error |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + public static void wrongTester2() { |
| 27 | + AbstractUndoableEdit edit = new AbstractUndoableEdit(); |
| 28 | + edit.undo(); |
| 29 | + if (edit.canUndo()) { |
| 30 | + edit.undo(); |
| 31 | + } |
| 32 | + edit.undo(); // State Refinement Error |
| 33 | + } |
| 34 | + |
| 35 | + // Two undos in the same then-branch: condition forces aliveDone for the first, but the second |
| 36 | + // is in aliveNotDone. |
| 37 | + public static void doubleUndoInThen(AbstractUndoableEdit edit) { |
| 38 | + if (edit.canUndo()) { |
| 39 | + edit.undo(); |
| 40 | + edit.undo(); // State Refinement Error |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // Nested ifs: outer canUndo => aliveDone, inner canUndo => aliveDone (still), so calling |
| 45 | + // redo() in the inner then is illegal (redo needs aliveNotDone). |
| 46 | + public static void nestedIfRedoFromAliveDone(AbstractUndoableEdit edit) { |
| 47 | + if (edit.canUndo()) { |
| 48 | + if (edit.canUndo()) { |
| 49 | + edit.redo(); // State Refinement Error |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Sequential guarded undos with no else: after the first if, state is aliveNotDone in BOTH |
| 55 | + // paths (then took canUndo=>aliveDone then undo=>aliveNotDone; else had canUndo=false meaning |
| 56 | + // !aliveDone, which can only be aliveNotDone given the state set). The second undo therefore |
| 57 | + // cannot succeed — but the verifier must not let the first if's truth assertion leak. |
| 58 | + public static void sequentialIfsLoseState() { |
| 59 | + AbstractUndoableEdit edit = new AbstractUndoableEdit(); |
| 60 | + if (edit.canUndo()) { |
| 61 | + edit.undo(); |
| 62 | + } |
| 63 | + if (edit.canUndo()) { |
| 64 | + } |
| 65 | + edit.undo(); // State Refinement Error |
| 66 | + } |
| 67 | + |
| 68 | + // Wrong direction: canRedo() implies aliveNotDone, so calling undo() in that branch is illegal. |
| 69 | + public static void undoGuardedByCanRedo(AbstractUndoableEdit edit) { |
| 70 | + if (edit.canRedo()) { |
| 71 | + edit.undo(); // State Refinement Error |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Redo after undo inside the then: the inner state is aliveDone -> aliveNotDone -> aliveDone |
| 76 | + // after redo, so a second redo from aliveDone is illegal. |
| 77 | + public static void doubleRedoAfterUndo(AbstractUndoableEdit edit) { |
| 78 | + if (edit.canUndo()) { |
| 79 | + edit.undo(); |
| 80 | + edit.redo(); |
| 81 | + edit.redo(); // State Refinement Error |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Empty if / empty else: the bug we fixed would have let `pathVar == true` (or false) leak past |
| 86 | + // the join, masking violations. Here the constructor leaves edit in aliveDone, neither branch |
| 87 | + // changes it, so after the if the state is still aliveDone — calling redo() is illegal. |
| 88 | + public static void redoAfterEmptyIfElse() { |
| 89 | + AbstractUndoableEdit edit = new AbstractUndoableEdit(); |
| 90 | + if (edit.canUndo()) { |
| 91 | + } else { |
| 92 | + } |
| 93 | + edit.redo(); // State Refinement Error |
| 94 | + } |
| 95 | +} |
0 commit comments