From b577890db4c2aa8274983667bc0dd96e8b3d10e6 Mon Sep 17 00:00:00 2001 From: mfarid Date: Sun, 22 Nov 2015 05:44:45 +0530 Subject: [PATCH] Added UnitTest cases for adapter. --- adapter/pom.xml | 5 ++ .../main/java/com/iluwatar/adapter/App.java | 16 ++--- .../adapter/GnomeEngineeringManager.java | 12 +++- .../iluwatar/adapter/AdapterPatternTest.java | 68 +++++++++++++++++++ .../java/com/iluwatar/adapter/AppTest.java | 19 ------ 5 files changed, 89 insertions(+), 31 deletions(-) create mode 100644 adapter/src/test/java/com/iluwatar/adapter/AdapterPatternTest.java delete mode 100644 adapter/src/test/java/com/iluwatar/adapter/AppTest.java diff --git a/adapter/pom.xml b/adapter/pom.xml index 2284c2974..ddb81441f 100644 --- a/adapter/pom.xml +++ b/adapter/pom.xml @@ -14,5 +14,10 @@ junit test + + org.mockito + mockito-core + test + diff --git a/adapter/src/main/java/com/iluwatar/adapter/App.java b/adapter/src/main/java/com/iluwatar/adapter/App.java index f912efb03..d2353eec4 100644 --- a/adapter/src/main/java/com/iluwatar/adapter/App.java +++ b/adapter/src/main/java/com/iluwatar/adapter/App.java @@ -1,30 +1,28 @@ package com.iluwatar.adapter; /** - * * An adapter helps two incompatible interfaces to work together. This is the real world definition * for an adapter. Interfaces may be incompatible but the inner functionality should suit the need. * The Adapter design pattern allows otherwise incompatible classes to work together by converting * the interface of one class into an interface expected by the clients. - *

- * There are two variations of the Adapter pattern: The class adapter implements the adaptee's + * + *

There are two variations of the Adapter pattern: The class adapter implements the adaptee's * interface whereas the object adapter uses composition to contain the adaptee in the adapter * object. This example uses the object adapter approach. - *

- * The Adapter ({@link GnomeEngineer}) converts the interface of the target class ( + * + *

The Adapter ({@link GnomeEngineer}) converts the interface of the target class ( * {@link GoblinGlider}) into a suitable one expected by the client ({@link GnomeEngineeringManager} * ). - * */ public class App { /** - * Program entry point - * + * Program entry point. + * * @param args command line args */ public static void main(String[] args) { - Engineer manager = new GnomeEngineeringManager(); + Engineer manager = new GnomeEngineeringManager(new GnomeEngineer()); manager.operateDevice(); } } diff --git a/adapter/src/main/java/com/iluwatar/adapter/GnomeEngineeringManager.java b/adapter/src/main/java/com/iluwatar/adapter/GnomeEngineeringManager.java index a2faf1e54..ff4ddb617 100644 --- a/adapter/src/main/java/com/iluwatar/adapter/GnomeEngineeringManager.java +++ b/adapter/src/main/java/com/iluwatar/adapter/GnomeEngineeringManager.java @@ -1,20 +1,26 @@ package com.iluwatar.adapter; /** - * * GnomeEngineering manager uses {@link Engineer} to operate devices. - * */ public class GnomeEngineeringManager implements Engineer { private Engineer engineer; public GnomeEngineeringManager() { - engineer = new GnomeEngineer(); + + } + + public GnomeEngineeringManager(Engineer engineer) { + this.engineer = engineer; } @Override public void operateDevice() { engineer.operateDevice(); } + + public void setEngineer(Engineer engineer) { + this.engineer = engineer; + } } diff --git a/adapter/src/test/java/com/iluwatar/adapter/AdapterPatternTest.java b/adapter/src/test/java/com/iluwatar/adapter/AdapterPatternTest.java new file mode 100644 index 000000000..866bfb968 --- /dev/null +++ b/adapter/src/test/java/com/iluwatar/adapter/AdapterPatternTest.java @@ -0,0 +1,68 @@ +package com.iluwatar.adapter; + +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + +/** + * An adapter helps two incompatible interfaces to work together. This is the real world definition + * for an adapter. Interfaces may be incompatible but the inner functionality should suit the need. + * The Adapter design pattern allows otherwise incompatible classes to work together by converting + * the interface of one class into an interface expected by the clients. + * + *

There are two variations of the Adapter pattern: + * The class adapter implements the adaptee's + * interface whereas the object adapter uses composition to contain the adaptee in the adapter + * object. This example uses the object adapter approach. + * + *

The Adapter ({@link GnomeEngineer}) converts the interface + * of the target class ({@link GoblinGlider}) into a suitable one expected by + * the client ({@link GnomeEngineeringManager} + * ). + */ +public class AdapterPatternTest { + + private Map beans; + + private static final String ENGINEER_BEAN = "engineer"; + + private static final String MANAGER_BEAN = "manager"; + + /** + * This method runs before the test execution and sets the bean objects in the beans Map. + */ + @Before + public void setup() { + beans = new HashMap<>(); + + GnomeEngineer gnomeEngineer = spy(new GnomeEngineer()); + beans.put(ENGINEER_BEAN, gnomeEngineer); + + GnomeEngineeringManager manager = new GnomeEngineeringManager(); + manager.setEngineer((GnomeEngineer) beans.get(ENGINEER_BEAN)); + beans.put(MANAGER_BEAN, manager); + } + + /** + * This test asserts that when we call operateDevice() method on a manager bean, it is internally + * calling operateDevice method on the engineer object. The Adapter ({@link GnomeEngineer}) + * converts the interface of the target class ( {@link GoblinGlider}) into a suitable one expected + * by the client ({@link GnomeEngineeringManager} ). + */ + @Test + public void testAdapter() { + Engineer manager = (Engineer) beans.get(MANAGER_BEAN); + + // when manager is asked to operate device + manager.operateDevice(); + + // Manager internally calls the engineer object to operateDevice + Engineer engineer = (Engineer) beans.get(ENGINEER_BEAN); + verify(engineer).operateDevice(); + } +} diff --git a/adapter/src/test/java/com/iluwatar/adapter/AppTest.java b/adapter/src/test/java/com/iluwatar/adapter/AppTest.java deleted file mode 100644 index a7462bd9a..000000000 --- a/adapter/src/test/java/com/iluwatar/adapter/AppTest.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.iluwatar.adapter; - -import org.junit.Test; - -import com.iluwatar.adapter.App; - -/** - * - * Application test - * - */ -public class AppTest { - - @Test - public void test() { - String[] args = {}; - App.main(args); - } -}