@@ -29,37 +29,49 @@ public class MultiCommandTests extends CommandTestCase {
29
29
30
30
static class DummyMultiCommand extends MultiCommand {
31
31
32
- AtomicBoolean closed = new AtomicBoolean ();
32
+ final AtomicBoolean closed = new AtomicBoolean ();
33
+
33
34
DummyMultiCommand () {
34
- super ("A dummy multi command" , () -> {});
35
+ super ("A dummy multi command" , () -> {
36
+ });
35
37
}
38
+
36
39
@ Override
37
40
public void close () throws IOException {
38
41
super .close ();
39
- this .closed .compareAndSet (false , true );
42
+ if (!this .closed .compareAndSet (false , true )) {
43
+ throw new IOException ("DummyMultiCommand closed" );
44
+ }
40
45
}
41
46
}
42
47
43
48
static class DummySubCommand extends Command {
44
- AtomicBoolean throwsExceptionOnClose = new AtomicBoolean ();
45
- AtomicBoolean closed = new AtomicBoolean ();
49
+ final boolean throwsExceptionOnClose ;
50
+ final AtomicBoolean closed = new AtomicBoolean ();
51
+
46
52
DummySubCommand () {
47
- super ( "A dummy subcommand" , () -> {} );
53
+ this ( false );
48
54
}
55
+
49
56
DummySubCommand (boolean throwsExceptionOnClose ) {
50
- super ("A dummy subcommand" , () -> {});
51
- this .throwsExceptionOnClose .compareAndSet (false , throwsExceptionOnClose );
57
+ super ("A dummy subcommand" , () -> {
58
+ });
59
+ this .throwsExceptionOnClose = throwsExceptionOnClose ;
52
60
}
61
+
53
62
@ Override
54
63
protected void execute (Terminal terminal , OptionSet options ) throws Exception {
55
64
terminal .println ("Arguments: " + options .nonOptionArguments ().toString ());
56
65
}
66
+
57
67
@ Override
58
68
public void close () throws IOException {
59
- if (throwsExceptionOnClose . get () ) {
69
+ if (throwsExceptionOnClose ) {
60
70
throw new IOException ();
61
71
} else {
62
- closed .compareAndSet (false , true );
72
+ if (!this .closed .compareAndSet (false , true )) {
73
+ throw new IOException ("DummySubCommand closed" );
74
+ }
63
75
}
64
76
}
65
77
}
@@ -133,20 +145,27 @@ public void testClose() throws Exception {
133
145
multiCommand .subcommands .put ("command1" , subCommand1 );
134
146
multiCommand .subcommands .put ("command2" , subCommand2 );
135
147
multiCommand .close ();
136
- assertTrue ("MultiCommand must have been closed when close method is invoked" , multiCommand .closed .get ());
137
- assertTrue ("SubCommand1 must have been closed when close method is invoked" , subCommand1 .closed .get ());
138
- assertTrue ("SubCommand2 must have been closed when close method is invoked" , subCommand2 .closed .get ());
148
+ assertTrue ("MultiCommand was not closed when close method is invoked" , multiCommand .closed .get ());
149
+ assertTrue ("SubCommand1 was not closed when close method is invoked" , subCommand1 .closed .get ());
150
+ assertTrue ("SubCommand2 was not closed when close method is invoked" , subCommand2 .closed .get ());
139
151
}
140
152
141
153
public void testCloseWhenSubCommandCloseThrowsException () {
142
- boolean throwExceptionWhenClosed = true ;
143
- DummySubCommand subCommand1 = new DummySubCommand (throwExceptionWhenClosed );
144
- DummySubCommand subCommand2 = new DummySubCommand ();
154
+ boolean command1Throws = randomBoolean ();
155
+ boolean command2Throws = randomBoolean ();
156
+ System .out .println ("c1 " +command1Throws +", c2 " +command2Throws );
157
+ DummySubCommand subCommand1 = new DummySubCommand (command1Throws );
158
+ DummySubCommand subCommand2 = new DummySubCommand (command2Throws );
145
159
multiCommand .subcommands .put ("command1" , subCommand1 );
146
160
multiCommand .subcommands .put ("command2" , subCommand2 );
147
161
// verify exception is thrown, as well as other non failed sub-commands closed
148
162
// properly.
149
- expectThrows (IOException .class , () -> multiCommand .close ());
150
- assertTrue ("SubCommand2 must have been closed when close method is invoked" , subCommand2 .closed .get ());
163
+ IOException ioe = expectThrows (IOException .class , () -> multiCommand .close ());
164
+ if (command1Throws && command2Throws ) {
165
+ assertEquals (1 , ioe .getSuppressed ().length );
166
+ assertTrue ("Missing suppressed exceptions" , ioe .getSuppressed ()[0 ] instanceof IOException );
167
+ }
168
+ assertTrue ("SubCommand1 was not closed when close method is invoked" , command1Throws ^ subCommand1 .closed .get ());
169
+ assertTrue ("SubCommand2 was not closed when close method is invoked" , command2Throws ^ subCommand2 .closed .get ());
151
170
}
152
171
}
0 commit comments