Skip to content

Commit 1f09032

Browse files
authored
Merge pull request #82 from rothso/master
Issue 81: Fixed CommandGroup behavior inconsistency
2 parents 9cfa5ac + f93616e commit 1f09032

File tree

4 files changed

+55
-16
lines changed

4 files changed

+55
-16
lines changed

CONTRIBUTORS.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ Braden Steffaniak
22
Matthew Alonso
33
Nathan Brown
44
Randall Hauch
5-
Zach Anderson
5+
Zach Anderson
6+
Rothanak So

strongback-tests/src/org/strongback/command/CommandGroupTest.java

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616

1717
package org.strongback.command;
1818

19-
import static org.fest.assertions.Assertions.assertThat;
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
import org.strongback.Logger;
2022

2123
import java.util.Arrays;
2224
import java.util.LinkedList;
2325
import java.util.List;
2426
import java.util.Queue;
2527

26-
import org.junit.Before;
27-
import org.junit.Test;
28-
import org.strongback.Logger;
28+
import static org.fest.assertions.Assertions.assertThat;
2929

3030
public class CommandGroupTest {
3131
private Scheduler scheduler;
@@ -332,6 +332,51 @@ public OtherDiagramFromBoard() {
332332
}
333333
}
334334

335+
@Test
336+
public void shouldExecuteComposedNamedCommandGroups() {
337+
scheduler.submit(new OuterCommandGroup());
338+
339+
assertThat(list).isEmpty();
340+
341+
// First step should come from the OuterCommandGroup
342+
scheduler.execute(0);
343+
assertThat(list).isEqualTo(listOf("C0 init", "C0 exe", "C0 fin" ));
344+
list.clear();
345+
346+
// Second step should come from NestedCommandGroup
347+
scheduler.execute(0);
348+
assertThat(list).isEqualTo(listOf("C1 init", "C1 exe", "C1 fin" ));
349+
list.clear();
350+
351+
// Third step should come from NestedCommandGroup
352+
scheduler.execute(0);
353+
assertThat(list).isEqualTo(listOf("C2 init", "C2 exe", "C2 fin" ));
354+
list.clear();
355+
356+
// Fourth step should resume the OuterCommandGroup sequence
357+
scheduler.execute(0);
358+
assertThat(list).isEqualTo(listOf("C3 init", "C3 exe", "C3 fin" ));
359+
list.clear();
360+
361+
// Fifth step is empty, nothing executed
362+
scheduler.execute(0);
363+
assertThat(list).isEmpty();
364+
}
365+
366+
private final class OuterCommandGroup extends CommandGroup {
367+
@SuppressWarnings("synthetic-access")
368+
public OuterCommandGroup() {
369+
sequentially(c[0], new InnerNamedCommandGroup(), c[3]);
370+
}
371+
372+
private final class InnerNamedCommandGroup extends CommandGroup {
373+
@SuppressWarnings("synthetic-access")
374+
public InnerNamedCommandGroup() {
375+
sequentially(c[1], c[2]);
376+
}
377+
}
378+
}
379+
335380
private static final class TestCommand extends Command {
336381
private static int commandID = 0;
337382

@@ -454,4 +499,4 @@ public String toString() {
454499
return "R" + number;
455500
}
456501
}
457-
}
502+
}

strongback/src/org/strongback/command/CommandGroup.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ static enum Type {
131131
SEQUENTIAL, PARRALLEL, FORK;
132132
}
133133

134-
private Command root;
134+
private CommandGroup root;
135135
private final Command[] commands;
136136
private final Type type;
137137

@@ -151,15 +151,11 @@ private CommandGroup(Command[] commands, Type type) {
151151
}
152152

153153
Type getType() {
154-
return type;
154+
return root != null ? root.type : type;
155155
}
156156

157157
Command[] getCommands() {
158-
return commands;
159-
}
160-
161-
Command getRoot() {
162-
return root;
158+
return root != null ? root.getCommands() : commands;
163159
}
164160

165161
/**

strongback/src/org/strongback/command/Scheduler.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ public void killAll() {
6565
*/
6666
public void submit(Command command) {
6767
if (command != null) {
68-
if (command instanceof CommandGroup) {
69-
command = ((CommandGroup) command).getRoot();
70-
}
7168
CommandRunner runner = buildRunner(command, null);
7269
commands.add(runner);
7370
}

0 commit comments

Comments
 (0)