|
| 1 | +package com.iluwatar.adapter; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.spy; |
| 4 | +import static org.mockito.Mockito.verify; |
| 5 | + |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +/** |
| 13 | + * An adapter helps two incompatible interfaces to work together. This is the real world definition |
| 14 | + * for an adapter. Interfaces may be incompatible but the inner functionality should suit the need. |
| 15 | + * The Adapter design pattern allows otherwise incompatible classes to work together by converting |
| 16 | + * the interface of one class into an interface expected by the clients. |
| 17 | + * |
| 18 | + * <p>There are two variations of the Adapter pattern: |
| 19 | + * The class adapter implements the adaptee's |
| 20 | + * interface whereas the object adapter uses composition to contain the adaptee in the adapter |
| 21 | + * object. This example uses the object adapter approach. |
| 22 | + * |
| 23 | + * <p>The Adapter ({@link GnomeEngineer}) converts the interface |
| 24 | + * of the target class ({@link GoblinGlider}) into a suitable one expected by |
| 25 | + * the client ({@link GnomeEngineeringManager} |
| 26 | + * ). |
| 27 | + */ |
| 28 | +public class AdapterPatternTest { |
| 29 | + |
| 30 | + private Map<String, Object> beans; |
| 31 | + |
| 32 | + private static final String ENGINEER_BEAN = "engineer"; |
| 33 | + |
| 34 | + private static final String MANAGER_BEAN = "manager"; |
| 35 | + |
| 36 | + /** |
| 37 | + * This method runs before the test execution and sets the bean objects in the beans Map. |
| 38 | + */ |
| 39 | + @Before |
| 40 | + public void setup() { |
| 41 | + beans = new HashMap<>(); |
| 42 | + |
| 43 | + GnomeEngineer gnomeEngineer = spy(new GnomeEngineer()); |
| 44 | + beans.put(ENGINEER_BEAN, gnomeEngineer); |
| 45 | + |
| 46 | + GnomeEngineeringManager manager = new GnomeEngineeringManager(); |
| 47 | + manager.setEngineer((GnomeEngineer) beans.get(ENGINEER_BEAN)); |
| 48 | + beans.put(MANAGER_BEAN, manager); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * This test asserts that when we call operateDevice() method on a manager bean, it is internally |
| 53 | + * calling operateDevice method on the engineer object. The Adapter ({@link GnomeEngineer}) |
| 54 | + * converts the interface of the target class ( {@link GoblinGlider}) into a suitable one expected |
| 55 | + * by the client ({@link GnomeEngineeringManager} ). |
| 56 | + */ |
| 57 | + @Test |
| 58 | + public void testAdapter() { |
| 59 | + Engineer manager = (Engineer) beans.get(MANAGER_BEAN); |
| 60 | + |
| 61 | + // when manager is asked to operate device |
| 62 | + manager.operateDevice(); |
| 63 | + |
| 64 | + // Manager internally calls the engineer object to operateDevice |
| 65 | + Engineer engineer = (Engineer) beans.get(ENGINEER_BEAN); |
| 66 | + verify(engineer).operateDevice(); |
| 67 | + } |
| 68 | +} |
0 commit comments