Added tests for specification pattern
This commit is contained in:
		@@ -14,5 +14,10 @@
 | 
				
			|||||||
      <artifactId>junit</artifactId>
 | 
					      <artifactId>junit</artifactId>
 | 
				
			||||||
      <scope>test</scope>
 | 
					      <scope>test</scope>
 | 
				
			||||||
    </dependency>
 | 
					    </dependency>
 | 
				
			||||||
 | 
					    <dependency>
 | 
				
			||||||
 | 
					      <groupId>org.mockito</groupId>
 | 
				
			||||||
 | 
					      <artifactId>mockito-core</artifactId>
 | 
				
			||||||
 | 
					      <scope>test</scope>
 | 
				
			||||||
 | 
					    </dependency>
 | 
				
			||||||
  </dependencies>
 | 
					  </dependencies>
 | 
				
			||||||
</project>
 | 
					</project>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -0,0 +1,111 @@
 | 
				
			|||||||
 | 
					package com.iluwatar.specification.creature;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.iluwatar.specification.property.Color;
 | 
				
			||||||
 | 
					import com.iluwatar.specification.property.Movement;
 | 
				
			||||||
 | 
					import com.iluwatar.specification.property.Size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.junit.Test;
 | 
				
			||||||
 | 
					import org.junit.runner.RunWith;
 | 
				
			||||||
 | 
					import org.junit.runners.Parameterized;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.Arrays;
 | 
				
			||||||
 | 
					import java.util.Collection;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import static org.junit.Assert.assertEquals;
 | 
				
			||||||
 | 
					import static org.junit.Assert.assertNotNull;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Date: 12/29/15 - 7:47 PM
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @author Jeroen Meulemeester
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					@RunWith(Parameterized.class)
 | 
				
			||||||
 | 
					public class CreatureTest {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * @return The tested {@link Creature} instance and its expected specs
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  @Parameterized.Parameters
 | 
				
			||||||
 | 
					  public static Collection<Object[]> data() {
 | 
				
			||||||
 | 
					    return Arrays.asList(
 | 
				
			||||||
 | 
					            new Object[]{new Dragon(), "Dragon", Size.LARGE, Movement.FLYING, Color.RED},
 | 
				
			||||||
 | 
					            new Object[]{new Goblin(), "Goblin", Size.SMALL, Movement.WALKING, Color.GREEN},
 | 
				
			||||||
 | 
					            new Object[]{new KillerBee(), "KillerBee", Size.SMALL, Movement.FLYING, Color.LIGHT},
 | 
				
			||||||
 | 
					            new Object[]{new Octopus(), "Octopus", Size.NORMAL, Movement.SWIMMING, Color.DARK},
 | 
				
			||||||
 | 
					            new Object[]{new Shark(), "Shark", Size.NORMAL, Movement.SWIMMING, Color.LIGHT},
 | 
				
			||||||
 | 
					            new Object[]{new Troll(), "Troll", Size.LARGE, Movement.WALKING, Color.DARK}
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * The tested creature
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  private final Creature testedCreature;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * The expected name of the tested creature
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  private final String name;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * The expected size of the tested creature
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  private final Size size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * The expected movement type of the tested creature
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  private final Movement movement;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * The expected color of the tested creature
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  private final Color color;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * @param testedCreature The tested creature
 | 
				
			||||||
 | 
					   * @param name           The expected name of the creature
 | 
				
			||||||
 | 
					   * @param size           The expected size of the creature
 | 
				
			||||||
 | 
					   * @param movement       The expected movement type of the creature
 | 
				
			||||||
 | 
					   * @param color          The expected color of the creature
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  public CreatureTest(final Creature testedCreature, final String name, final Size size,
 | 
				
			||||||
 | 
					                      final Movement movement, final Color color) {
 | 
				
			||||||
 | 
					    this.testedCreature = testedCreature;
 | 
				
			||||||
 | 
					    this.name = name;
 | 
				
			||||||
 | 
					    this.size = size;
 | 
				
			||||||
 | 
					    this.movement = movement;
 | 
				
			||||||
 | 
					    this.color = color;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @Test
 | 
				
			||||||
 | 
					  public void testGetName() throws Exception {
 | 
				
			||||||
 | 
					    assertEquals(this.name, this.testedCreature.getName());
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @Test
 | 
				
			||||||
 | 
					  public void testGetSize() throws Exception {
 | 
				
			||||||
 | 
					    assertEquals(this.size, this.testedCreature.getSize());
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @Test
 | 
				
			||||||
 | 
					  public void testGetMovement() throws Exception {
 | 
				
			||||||
 | 
					    assertEquals(this.movement, this.testedCreature.getMovement());
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @Test
 | 
				
			||||||
 | 
					  public void testGetColor() throws Exception {
 | 
				
			||||||
 | 
					    assertEquals(this.color, this.testedCreature.getColor());
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @Test
 | 
				
			||||||
 | 
					  public void testToString() throws Exception {
 | 
				
			||||||
 | 
					    final String toString = this.testedCreature.toString();
 | 
				
			||||||
 | 
					    assertNotNull(toString);
 | 
				
			||||||
 | 
					    assertEquals(
 | 
				
			||||||
 | 
					            String.format("%s [size=%s, movement=%s, color=%s]", name, size, movement, color),
 | 
				
			||||||
 | 
					            toString
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,37 @@
 | 
				
			|||||||
 | 
					package com.iluwatar.specification.selector;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.iluwatar.specification.creature.Creature;
 | 
				
			||||||
 | 
					import com.iluwatar.specification.property.Color;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.junit.Test;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import static org.junit.Assert.assertFalse;
 | 
				
			||||||
 | 
					import static org.junit.Assert.assertTrue;
 | 
				
			||||||
 | 
					import static org.mockito.Mockito.mock;
 | 
				
			||||||
 | 
					import static org.mockito.Mockito.when;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Date: 12/29/15 - 7:35 PM
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @author Jeroen Meulemeester
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public class ColorSelectorTest {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * Verify if the color selector gives the correct results
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  @Test
 | 
				
			||||||
 | 
					  public void testColor() {
 | 
				
			||||||
 | 
					    final Creature greenCreature = mock(Creature.class);
 | 
				
			||||||
 | 
					    when(greenCreature.getColor()).thenReturn(Color.GREEN);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    final Creature redCreature = mock(Creature.class);
 | 
				
			||||||
 | 
					    when(redCreature.getColor()).thenReturn(Color.RED);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    final ColorSelector greenSelector = new ColorSelector(Color.GREEN);
 | 
				
			||||||
 | 
					    assertTrue(greenSelector.test(greenCreature));
 | 
				
			||||||
 | 
					    assertFalse(greenSelector.test(redCreature));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,38 @@
 | 
				
			|||||||
 | 
					package com.iluwatar.specification.selector;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.iluwatar.specification.creature.Creature;
 | 
				
			||||||
 | 
					import com.iluwatar.specification.property.Color;
 | 
				
			||||||
 | 
					import com.iluwatar.specification.property.Movement;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.junit.Test;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import static org.junit.Assert.assertFalse;
 | 
				
			||||||
 | 
					import static org.junit.Assert.assertTrue;
 | 
				
			||||||
 | 
					import static org.mockito.Mockito.mock;
 | 
				
			||||||
 | 
					import static org.mockito.Mockito.when;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Date: 12/29/15 - 7:37 PM
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @author Jeroen Meulemeester
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public class MovementSelectorTest {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * Verify if the movement selector gives the correct results
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  @Test
 | 
				
			||||||
 | 
					  public void testMovement() {
 | 
				
			||||||
 | 
					    final Creature swimmingCreature = mock(Creature.class);
 | 
				
			||||||
 | 
					    when(swimmingCreature.getMovement()).thenReturn(Movement.SWIMMING);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    final Creature flyingCreature = mock(Creature.class);
 | 
				
			||||||
 | 
					    when(flyingCreature.getMovement()).thenReturn(Movement.FLYING);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    final MovementSelector swimmingSelector = new MovementSelector(Movement.SWIMMING);
 | 
				
			||||||
 | 
					    assertTrue(swimmingSelector.test(swimmingCreature));
 | 
				
			||||||
 | 
					    assertFalse(swimmingSelector.test(flyingCreature));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,36 @@
 | 
				
			|||||||
 | 
					package com.iluwatar.specification.selector;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.iluwatar.specification.creature.Creature;
 | 
				
			||||||
 | 
					import com.iluwatar.specification.property.Size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.junit.Test;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import static org.junit.Assert.assertFalse;
 | 
				
			||||||
 | 
					import static org.junit.Assert.assertTrue;
 | 
				
			||||||
 | 
					import static org.mockito.Mockito.mock;
 | 
				
			||||||
 | 
					import static org.mockito.Mockito.when;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Date: 12/29/15 - 7:43 PM
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @author Jeroen Meulemeester
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public class SizeSelectorTest {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * Verify if the size selector gives the correct results
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  @Test
 | 
				
			||||||
 | 
					  public void testMovement() {
 | 
				
			||||||
 | 
					    final Creature normalCreature = mock(Creature.class);
 | 
				
			||||||
 | 
					    when(normalCreature.getSize()).thenReturn(Size.NORMAL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    final Creature smallCreature = mock(Creature.class);
 | 
				
			||||||
 | 
					    when(smallCreature.getSize()).thenReturn(Size.SMALL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    final SizeSelector normalSelector = new SizeSelector(Size.NORMAL);
 | 
				
			||||||
 | 
					    assertTrue(normalSelector.test(normalCreature));
 | 
				
			||||||
 | 
					    assertFalse(normalSelector.test(smallCreature));
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user