Skip to content

Commit b577890

Browse files
mfaridmfarid
authored andcommitted
Added UnitTest cases for adapter.
1 parent ba3f583 commit b577890

File tree

5 files changed

+89
-31
lines changed

5 files changed

+89
-31
lines changed

adapter/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@
1414
<artifactId>junit</artifactId>
1515
<scope>test</scope>
1616
</dependency>
17+
<dependency>
18+
<groupId>org.mockito</groupId>
19+
<artifactId>mockito-core</artifactId>
20+
<scope>test</scope>
21+
</dependency>
1722
</dependencies>
1823
</project>
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
package com.iluwatar.adapter;
22

33
/**
4-
*
54
* An adapter helps two incompatible interfaces to work together. This is the real world definition
65
* for an adapter. Interfaces may be incompatible but the inner functionality should suit the need.
76
* The Adapter design pattern allows otherwise incompatible classes to work together by converting
87
* the interface of one class into an interface expected by the clients.
9-
* <p>
10-
* There are two variations of the Adapter pattern: The class adapter implements the adaptee's
8+
*
9+
* <p>There are two variations of the Adapter pattern: The class adapter implements the adaptee's
1110
* interface whereas the object adapter uses composition to contain the adaptee in the adapter
1211
* object. This example uses the object adapter approach.
13-
* <p>
14-
* The Adapter ({@link GnomeEngineer}) converts the interface of the target class (
12+
*
13+
* <p>The Adapter ({@link GnomeEngineer}) converts the interface of the target class (
1514
* {@link GoblinGlider}) into a suitable one expected by the client ({@link GnomeEngineeringManager}
1615
* ).
17-
*
1816
*/
1917
public class App {
2018

2119
/**
22-
* Program entry point
23-
*
20+
* Program entry point.
21+
*
2422
* @param args command line args
2523
*/
2624
public static void main(String[] args) {
27-
Engineer manager = new GnomeEngineeringManager();
25+
Engineer manager = new GnomeEngineeringManager(new GnomeEngineer());
2826
manager.operateDevice();
2927
}
3028
}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
package com.iluwatar.adapter;
22

33
/**
4-
*
54
* GnomeEngineering manager uses {@link Engineer} to operate devices.
6-
*
75
*/
86
public class GnomeEngineeringManager implements Engineer {
97

108
private Engineer engineer;
119

1210
public GnomeEngineeringManager() {
13-
engineer = new GnomeEngineer();
11+
12+
}
13+
14+
public GnomeEngineeringManager(Engineer engineer) {
15+
this.engineer = engineer;
1416
}
1517

1618
@Override
1719
public void operateDevice() {
1820
engineer.operateDevice();
1921
}
22+
23+
public void setEngineer(Engineer engineer) {
24+
this.engineer = engineer;
25+
}
2026
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
}

adapter/src/test/java/com/iluwatar/adapter/AppTest.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)