Changeset 11734 in josm


Ignore:
Timestamp:
2017-03-16T18:46:07+01:00 (5 months ago)
Author:
stoecker
Message:

improve SequenceCommandTest, see #14526 - patch by Tyndare

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/command/SequenceCommandTest.java

    r10758 r11734  
    9696        assertTrue(command1.executed);
    9797        assertTrue(command2.executed);
     98    }
     99
     100    /**
     101     * Test {@link SequenceCommand#executeCommand()} rollback if case of subcommand failure.
     102     */
     103    @Test
     104    public void testExecuteRollback() {
     105        TestCommand command1 = new TestCommand(null);
     106        FailingCommand command2 = new FailingCommand();
     107        TestCommand command3 = new TestCommand(null);
     108        SequenceCommand command = new SequenceCommand("seq", Arrays.<Command>asList(command1, command2, command3));
     109        assertFalse(command.executeCommand());
     110        assertFalse(command1.executed);
     111        // Don't check command2 executed state as it's possible but not necessary to undo failed commands
     112        assertFalse(command3.executed);
     113        command.undoCommand();
     114    }
     115
     116    /**
     117     * Test {@link SequenceCommand#executeCommand()} with continueOnError = true
     118     */
     119    @Test
     120    public void testContinueOnErrors() {
     121        TestCommand command1 = new TestCommand(null);
     122        FailingCommand command2 = new FailingCommand();
     123        TestCommand command3 = new TestCommand(null);
     124        SequenceCommand command = new SequenceCommand("seq", Arrays.<Command>asList(command1, command2, command3));
     125        command.continueOnError = true;
     126        assertTrue(command.executeCommand());
     127        assertTrue(command1.executed);
     128        assertTrue(command3.executed);
     129        command.undoCommand();
     130        assertFalse(command1.executed);
     131        // Don't check command2 executed state as it's possible but not necessary to undo failed commands
     132        assertFalse(command3.executed);
    98133    }
    99134
     
    229264
    230265    }
     266
     267    private static class FailingCommand extends TestCommand {
     268
     269        FailingCommand() {
     270            super(null);
     271        }
     272
     273        @Override
     274        public boolean executeCommand() {
     275            executed = true;
     276            return false;
     277        }
     278
     279        @Override
     280        public void undoCommand() {
     281            assertTrue("Cannot undo without execute", executed);
     282            executed = false;
     283        }
     284
     285        @Override
     286        public String toString() {
     287            return "FailingCommand";
     288        }
     289
     290    }
     291
    231292}
Note: See TracChangeset for help on using the changeset viewer.