|
| 1 | +package com.iluwatar.factory.method; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +/** |
| 9 | + * The Factory Method is a creational design pattern which uses factory methods to deal with the |
| 10 | + * problem of creating objects without specifying the exact class of object that will be created. |
| 11 | + * This is done by creating objects via calling a factory method either specified in an interface |
| 12 | + * and implemented by child classes, or implemented in a base class and optionally overridden by |
| 13 | + * derived classes—rather than by calling a constructor. |
| 14 | + * |
| 15 | + * <p>Factory produces the object of its liking. |
| 16 | + * The weapon {@link Weapon} manufactured by the |
| 17 | + * blacksmith depends on the kind of factory implementation it is referring to. |
| 18 | + * </p> |
| 19 | + */ |
| 20 | +public class FactoryMethodTest { |
| 21 | + |
| 22 | + /** |
| 23 | + * Testing {@link OrcBlacksmith} to produce a SPEAR asserting that the Weapon is an instance |
| 24 | + * of {@link OrcWeapon}. |
| 25 | + */ |
| 26 | + @Test |
| 27 | + public void testOrcBlacksmithWithSpear() { |
| 28 | + Blacksmith blacksmith = new OrcBlacksmith(); |
| 29 | + Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); |
| 30 | + verifyWeapon(weapon, WeaponType.SPEAR, OrcWeapon.class); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Testing {@link OrcBlacksmith} to produce a AXE asserting that the Weapon is an instance |
| 35 | + * of {@link OrcWeapon}. |
| 36 | + */ |
| 37 | + @Test |
| 38 | + public void testOrcBlacksmithWithAxe() { |
| 39 | + Blacksmith blacksmith = new OrcBlacksmith(); |
| 40 | + Weapon weapon = blacksmith.manufactureWeapon(WeaponType.AXE); |
| 41 | + verifyWeapon(weapon, WeaponType.AXE, OrcWeapon.class); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Testing {@link ElfBlacksmith} to produce a SHORT_SWORD asserting that the Weapon is an |
| 46 | + * instance of {@link ElfWeapon}. |
| 47 | + */ |
| 48 | + @Test |
| 49 | + public void testElfBlacksmithWithShortSword() { |
| 50 | + Blacksmith blacksmith = new ElfBlacksmith(); |
| 51 | + Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SHORT_SWORD); |
| 52 | + verifyWeapon(weapon, WeaponType.SHORT_SWORD, ElfWeapon.class); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Testing {@link ElfBlacksmith} to produce a SPEAR asserting that the Weapon is an instance |
| 57 | + * of {@link ElfWeapon}. |
| 58 | + */ |
| 59 | + @Test |
| 60 | + public void testElfBlacksmithWithSpear() { |
| 61 | + Blacksmith blacksmith = new ElfBlacksmith(); |
| 62 | + Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); |
| 63 | + verifyWeapon(weapon, WeaponType.SPEAR, ElfWeapon.class); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * This method asserts that the weapon object that is passed is an instance of the clazz and the |
| 68 | + * weapon is of type expectedWeaponType. |
| 69 | + * |
| 70 | + * @param weapon weapon object which is to be verified |
| 71 | + * @param expectedWeaponType expected WeaponType of the weapon |
| 72 | + * @param clazz expected class of the weapon |
| 73 | + */ |
| 74 | + private void verifyWeapon(Weapon weapon, WeaponType expectedWeaponType, Class clazz) { |
| 75 | + assertTrue("Weapon must be an object of: " + clazz.getName(), clazz.isInstance(weapon)); |
| 76 | + assertEquals("Weapon must be of weaponType: " + clazz.getName(), expectedWeaponType, |
| 77 | + weapon.getWeaponType()); |
| 78 | + } |
| 79 | +} |
0 commit comments