|
22 | 22 | import joptsimple.OptionSet;
|
23 | 23 | import org.junit.Before;
|
24 | 24 |
|
| 25 | +import java.io.IOException; |
| 26 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 27 | + |
25 | 28 | public class MultiCommandTests extends CommandTestCase {
|
26 | 29 |
|
27 | 30 | static class DummyMultiCommand extends MultiCommand {
|
| 31 | + |
| 32 | + final AtomicBoolean closed = new AtomicBoolean(); |
| 33 | + |
28 | 34 | DummyMultiCommand() {
|
29 |
| - super("A dummy multi command", () -> {}); |
| 35 | + super("A dummy multi command", () -> { |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void close() throws IOException { |
| 41 | + super.close(); |
| 42 | + if (this.closed.compareAndSet(false, true) == false) { |
| 43 | + throw new IllegalStateException("DummyMultiCommand already closed"); |
| 44 | + } |
30 | 45 | }
|
31 | 46 | }
|
32 | 47 |
|
33 | 48 | static class DummySubCommand extends Command {
|
| 49 | + final boolean throwsExceptionOnClose; |
| 50 | + final AtomicBoolean closeCalled = new AtomicBoolean(); |
| 51 | + |
34 | 52 | DummySubCommand() {
|
35 |
| - super("A dummy subcommand", () -> {}); |
| 53 | + this(false); |
36 | 54 | }
|
| 55 | + |
| 56 | + DummySubCommand(final boolean throwsExceptionOnClose) { |
| 57 | + super("A dummy subcommand", () -> { |
| 58 | + }); |
| 59 | + this.throwsExceptionOnClose = throwsExceptionOnClose; |
| 60 | + } |
| 61 | + |
37 | 62 | @Override
|
38 | 63 | protected void execute(Terminal terminal, OptionSet options) throws Exception {
|
39 | 64 | terminal.println("Arguments: " + options.nonOptionArguments().toString());
|
40 | 65 | }
|
| 66 | + |
| 67 | + @Override |
| 68 | + public void close() throws IOException { |
| 69 | + if (this.closeCalled.compareAndSet(false, true) == false) { |
| 70 | + throw new IllegalStateException("DummySubCommand already closed"); |
| 71 | + } |
| 72 | + if (throwsExceptionOnClose) { |
| 73 | + throw new IOException("Error occurred while closing DummySubCommand"); |
| 74 | + } |
| 75 | + } |
41 | 76 | }
|
42 | 77 |
|
43 | 78 | DummyMultiCommand multiCommand;
|
@@ -102,4 +137,40 @@ public void testSubcommandArguments() throws Exception {
|
102 | 137 | assertFalse(output, output.contains("command1"));
|
103 | 138 | assertTrue(output, output.contains("Arguments: [foo, bar]"));
|
104 | 139 | }
|
| 140 | + |
| 141 | + public void testClose() throws Exception { |
| 142 | + DummySubCommand subCommand1 = new DummySubCommand(); |
| 143 | + DummySubCommand subCommand2 = new DummySubCommand(); |
| 144 | + multiCommand.subcommands.put("command1", subCommand1); |
| 145 | + multiCommand.subcommands.put("command2", subCommand2); |
| 146 | + multiCommand.close(); |
| 147 | + assertTrue("MultiCommand was not closed when close method is invoked", multiCommand.closed.get()); |
| 148 | + assertTrue("SubCommand1 was not closed when close method is invoked", subCommand1.closeCalled.get()); |
| 149 | + assertTrue("SubCommand2 was not closed when close method is invoked", subCommand2.closeCalled.get()); |
| 150 | + } |
| 151 | + |
| 152 | + public void testCloseWhenSubCommandCloseThrowsException() throws Exception { |
| 153 | + final boolean command1Throws = randomBoolean(); |
| 154 | + final boolean command2Throws = randomBoolean(); |
| 155 | + final DummySubCommand subCommand1 = new DummySubCommand(command1Throws); |
| 156 | + final DummySubCommand subCommand2 = new DummySubCommand(command2Throws); |
| 157 | + multiCommand.subcommands.put("command1", subCommand1); |
| 158 | + multiCommand.subcommands.put("command2", subCommand2); |
| 159 | + if (command1Throws || command2Throws) { |
| 160 | + // verify exception is thrown, as well as other non failed sub-commands closed |
| 161 | + // properly. |
| 162 | + IOException ioe = expectThrows(IOException.class, multiCommand::close); |
| 163 | + assertEquals("Error occurred while closing DummySubCommand", ioe.getMessage()); |
| 164 | + if (command1Throws && command2Throws) { |
| 165 | + assertEquals(1, ioe.getSuppressed().length); |
| 166 | + assertTrue("Missing suppressed exceptions", ioe.getSuppressed()[0] instanceof IOException); |
| 167 | + assertEquals("Error occurred while closing DummySubCommand", ioe.getSuppressed()[0].getMessage()); |
| 168 | + } |
| 169 | + } else { |
| 170 | + multiCommand.close(); |
| 171 | + } |
| 172 | + assertTrue("SubCommand1 was not closed when close method is invoked", subCommand1.closeCalled.get()); |
| 173 | + assertTrue("SubCommand2 was not closed when close method is invoked", subCommand2.closeCalled.get()); |
| 174 | + } |
| 175 | + |
105 | 176 | }
|
0 commit comments