|
15 | 15 | import com.iluwatar.adapter.FishingBoat; |
16 | 16 |
|
17 | 17 | /** |
18 | | - * An adapter helps two incompatible interfaces to work together. This is the |
19 | | - * real world definition for an adapter. Interfaces may be incompatible but the |
20 | | - * inner functionality should suit the need. The Adapter design pattern allows |
21 | | - * otherwise incompatible classes to work together by converting the interface |
22 | | - * of one class into an interface expected by the clients. |
23 | | - * |
24 | | - * <p> |
25 | | - * There are two variations of the Adapter pattern: The class adapter implements |
26 | | - * the adaptee's interface whereas the object adapter uses composition to |
27 | | - * contain the adaptee in the adapter object. This example uses the object |
28 | | - * adapter approach. |
29 | | - * |
30 | | - * <p> |
31 | | - * The Adapter ({@link BattleFishingBoat}) converts the interface of the adaptee |
32 | | - * class ( {@link FishingBoat}) into a suitable one expected by the client ( |
33 | | - * {@link BattleShip} ). |
34 | | - * |
35 | | - * <p> |
36 | | - * The story of this implementation is this. <br> |
37 | | - * Pirates are coming! we need a {@link BattleShip} to fight! We have a |
38 | | - * {@link FishingBoat} and our captain. We have no time to make up a new ship! |
39 | | - * we need to reuse this {@link FishingBoat}. The captain needs a battleship |
40 | | - * which can fire and move. The spec is in {@link BattleShip}. We will use the |
41 | | - * Adapter pattern to reuse {@link FishingBoat} which operates properly |
| 18 | + * Test class |
42 | 19 | * |
43 | 20 | */ |
44 | 21 | public class AdapterPatternTest { |
45 | 22 |
|
46 | | - private Map<String, Object> beans; |
| 23 | + private Map<String, Object> beans; |
47 | 24 |
|
48 | | - private static final String BATTLESHIP_BEAN = "engineer"; |
| 25 | + private static final String BATTLESHIP_BEAN = "engineer"; |
49 | 26 |
|
50 | | - private static final String CAPTAIN_BEAN = "captain"; |
| 27 | + private static final String CAPTAIN_BEAN = "captain"; |
51 | 28 |
|
52 | | - /** |
53 | | - * This method runs before the test execution and sets the bean objects in |
54 | | - * the beans Map. |
55 | | - */ |
56 | | - @Before |
57 | | - public void setup() { |
58 | | - beans = new HashMap<>(); |
| 29 | + /** |
| 30 | + * This method runs before the test execution and sets the bean objects in the beans Map. |
| 31 | + */ |
| 32 | + @Before |
| 33 | + public void setup() { |
| 34 | + beans = new HashMap<>(); |
59 | 35 |
|
60 | | - BattleFishingBoat battleFishingBoat = spy(new BattleFishingBoat()); |
61 | | - beans.put(BATTLESHIP_BEAN, battleFishingBoat); |
| 36 | + BattleFishingBoat battleFishingBoat = spy(new BattleFishingBoat()); |
| 37 | + beans.put(BATTLESHIP_BEAN, battleFishingBoat); |
62 | 38 |
|
63 | | - Captain captain = new Captain(); |
64 | | - captain.setBattleship((BattleFishingBoat) beans.get(BATTLESHIP_BEAN)); |
65 | | - beans.put(CAPTAIN_BEAN, captain); |
66 | | - } |
| 39 | + Captain captain = new Captain(); |
| 40 | + captain.setBattleship((BattleFishingBoat) beans.get(BATTLESHIP_BEAN)); |
| 41 | + beans.put(CAPTAIN_BEAN, captain); |
| 42 | + } |
67 | 43 |
|
68 | | - /** |
69 | | - * This test asserts that when we use the move() method on a captain |
70 | | - * bean(client), it is internally calling move method on the battleship |
71 | | - * object. The Adapter ({@link BattleFishingBoat}) converts the interface of |
72 | | - * the target class ( {@link FishingBoat}) into a suitable one expected by |
73 | | - * the client ({@link Captain} ). |
74 | | - */ |
75 | | - @Test |
76 | | - public void testAdapter() { |
77 | | - BattleShip captain = (BattleShip) beans.get(CAPTAIN_BEAN); |
| 44 | + /** |
| 45 | + * This test asserts that when we use the move() method on a captain bean(client), it is |
| 46 | + * internally calling move method on the battleship object. The Adapter ({@link BattleFishingBoat} |
| 47 | + * ) converts the interface of the target class ( {@link FishingBoat}) into a suitable one |
| 48 | + * expected by the client ({@link Captain} ). |
| 49 | + */ |
| 50 | + @Test |
| 51 | + public void testAdapter() { |
| 52 | + BattleShip captain = (BattleShip) beans.get(CAPTAIN_BEAN); |
78 | 53 |
|
79 | | - // when captain moves |
80 | | - captain.move(); |
| 54 | + // when captain moves |
| 55 | + captain.move(); |
81 | 56 |
|
82 | | - // the captain internally calls the battleship object to move |
83 | | - BattleShip battleship = (BattleShip) beans.get(BATTLESHIP_BEAN); |
84 | | - verify(battleship).move(); |
| 57 | + // the captain internally calls the battleship object to move |
| 58 | + BattleShip battleship = (BattleShip) beans.get(BATTLESHIP_BEAN); |
| 59 | + verify(battleship).move(); |
85 | 60 |
|
86 | | - // same with above with firing |
87 | | - captain.fire(); |
88 | | - verify(battleship).fire(); |
| 61 | + // same with above with firing |
| 62 | + captain.fire(); |
| 63 | + verify(battleship).fire(); |
89 | 64 |
|
90 | | - } |
| 65 | + } |
91 | 66 | } |
0 commit comments