diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index b104bc4..b473ee9 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -2,4 +2,5 @@ Braden Steffaniak Matthew Alonso Nathan Brown Randall Hauch -Zach Anderson \ No newline at end of file +Zach Anderson +Rothanak So \ No newline at end of file diff --git a/strongback-tests/src/org/strongback/command/CommandGroupTest.java b/strongback-tests/src/org/strongback/command/CommandGroupTest.java index 0a0864e..fa43f30 100644 --- a/strongback-tests/src/org/strongback/command/CommandGroupTest.java +++ b/strongback-tests/src/org/strongback/command/CommandGroupTest.java @@ -16,16 +16,16 @@ package org.strongback.command; -import static org.fest.assertions.Assertions.assertThat; +import org.junit.Before; +import org.junit.Test; +import org.strongback.Logger; import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.Queue; -import org.junit.Before; -import org.junit.Test; -import org.strongback.Logger; +import static org.fest.assertions.Assertions.assertThat; public class CommandGroupTest { private Scheduler scheduler; @@ -332,6 +332,51 @@ public OtherDiagramFromBoard() { } } + @Test + public void shouldExecuteComposedNamedCommandGroups() { + scheduler.submit(new OuterCommandGroup()); + + assertThat(list).isEmpty(); + + // First step should come from the OuterCommandGroup + scheduler.execute(0); + assertThat(list).isEqualTo(listOf("C0 init", "C0 exe", "C0 fin" )); + list.clear(); + + // Second step should come from NestedCommandGroup + scheduler.execute(0); + assertThat(list).isEqualTo(listOf("C1 init", "C1 exe", "C1 fin" )); + list.clear(); + + // Third step should come from NestedCommandGroup + scheduler.execute(0); + assertThat(list).isEqualTo(listOf("C2 init", "C2 exe", "C2 fin" )); + list.clear(); + + // Fourth step should resume the OuterCommandGroup sequence + scheduler.execute(0); + assertThat(list).isEqualTo(listOf("C3 init", "C3 exe", "C3 fin" )); + list.clear(); + + // Fifth step is empty, nothing executed + scheduler.execute(0); + assertThat(list).isEmpty(); + } + + private final class OuterCommandGroup extends CommandGroup { + @SuppressWarnings("synthetic-access") + public OuterCommandGroup() { + sequentially(c[0], new InnerNamedCommandGroup(), c[3]); + } + + private final class InnerNamedCommandGroup extends CommandGroup { + @SuppressWarnings("synthetic-access") + public InnerNamedCommandGroup() { + sequentially(c[1], c[2]); + } + } + } + private static final class TestCommand extends Command { private static int commandID = 0; @@ -454,4 +499,4 @@ public String toString() { return "R" + number; } } -} \ No newline at end of file +} diff --git a/strongback/src/org/strongback/command/CommandGroup.java b/strongback/src/org/strongback/command/CommandGroup.java index 7ab3441..b4c7243 100644 --- a/strongback/src/org/strongback/command/CommandGroup.java +++ b/strongback/src/org/strongback/command/CommandGroup.java @@ -131,7 +131,7 @@ static enum Type { SEQUENTIAL, PARRALLEL, FORK; } - private Command root; + private CommandGroup root; private final Command[] commands; private final Type type; @@ -151,15 +151,11 @@ private CommandGroup(Command[] commands, Type type) { } Type getType() { - return type; + return root != null ? root.type : type; } Command[] getCommands() { - return commands; - } - - Command getRoot() { - return root; + return root != null ? root.getCommands() : commands; } /** diff --git a/strongback/src/org/strongback/command/Scheduler.java b/strongback/src/org/strongback/command/Scheduler.java index bd695ea..97eb838 100644 --- a/strongback/src/org/strongback/command/Scheduler.java +++ b/strongback/src/org/strongback/command/Scheduler.java @@ -65,9 +65,6 @@ public void killAll() { */ public void submit(Command command) { if (command != null) { - if (command instanceof CommandGroup) { - command = ((CommandGroup) command).getRoot(); - } CommandRunner runner = buildRunner(command, null); commands.add(runner); }