|
| 1 | +package com.iluwatar.command; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +/** |
| 8 | + * The Command pattern is a behavioral design pattern in which an object is used to encapsulate all |
| 9 | + * information needed to perform an action or trigger an event at a later time. This information |
| 10 | + * includes the method name, the object that owns the method and values for the method parameters. |
| 11 | + * |
| 12 | + * <p>Four terms always associated with the command pattern are command, receiver, invoker and |
| 13 | + * client. A command object (spell) knows about the receiver (target) and invokes a method of |
| 14 | + * the receiver.Values for parameters of the receiver method are stored in the command. The receiver |
| 15 | + * then does the work. An invoker object (wizard) knows how to execute a command, and optionally |
| 16 | + * does bookkeeping about the command execution. The invoker does not know anything about a |
| 17 | + * concrete command, it knows only about command interface. Both an invoker object and several |
| 18 | + * command objects are held by a client object (app). The client decides which commands to execute |
| 19 | + * at which points. To execute a command, it passes the command object to the invoker object. |
| 20 | + */ |
| 21 | +public class CommandTest { |
| 22 | + |
| 23 | + private static final String GOBLIN = "Goblin"; |
| 24 | + |
| 25 | + /** |
| 26 | + * This test verifies that when the wizard casts spells on the goblin. The wizard keeps track of |
| 27 | + * the previous spells cast, so it is easy to undo them. In addition, it also verifies that the |
| 28 | + * wizard keeps track of the spells undone, so they can be redone. |
| 29 | + */ |
| 30 | + @Test |
| 31 | + public void testCommand() { |
| 32 | + |
| 33 | + Wizard wizard = new Wizard(); |
| 34 | + Goblin goblin = new Goblin(); |
| 35 | + |
| 36 | + wizard.castSpell(new ShrinkSpell(), goblin); |
| 37 | + verifyGoblin(goblin, GOBLIN, Size.SMALL, Visibility.VISIBLE); |
| 38 | + |
| 39 | + wizard.castSpell(new InvisibilitySpell(), goblin); |
| 40 | + verifyGoblin(goblin, GOBLIN, Size.SMALL, Visibility.INVISIBLE); |
| 41 | + |
| 42 | + wizard.undoLastSpell(); |
| 43 | + verifyGoblin(goblin, GOBLIN, Size.SMALL, Visibility.VISIBLE); |
| 44 | + |
| 45 | + wizard.undoLastSpell(); |
| 46 | + verifyGoblin(goblin, GOBLIN, Size.NORMAL, Visibility.VISIBLE); |
| 47 | + |
| 48 | + wizard.redoLastSpell(); |
| 49 | + verifyGoblin(goblin, GOBLIN, Size.SMALL, Visibility.VISIBLE); |
| 50 | + |
| 51 | + wizard.redoLastSpell(); |
| 52 | + verifyGoblin(goblin, GOBLIN, Size.SMALL, Visibility.INVISIBLE); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * This method asserts that the passed goblin object has the name as expectedName, size as |
| 57 | + * expectedSize and visibility as expectedVisibility. |
| 58 | + * |
| 59 | + * @param goblin a goblin object whose state is to be verified against other parameters |
| 60 | + * @param expectedName expectedName of the goblin |
| 61 | + * @param expectedSize expected size of the goblin |
| 62 | + * @param expectedVisibilty exepcted visibility of the goblin |
| 63 | + */ |
| 64 | + private void verifyGoblin(Goblin goblin, String expectedName, Size expectedSize, |
| 65 | + Visibility expectedVisibilty) { |
| 66 | + assertEquals("Goblin's name must be same as expectedName", expectedName, goblin.toString()); |
| 67 | + assertEquals("Goblin's size must be same as expectedSize", expectedSize, goblin.getSize()); |
| 68 | + assertEquals("Goblin's visibility must be same as expectedVisibility", expectedVisibilty, |
| 69 | + goblin.getVisibility()); |
| 70 | + } |
| 71 | +} |
0 commit comments