Skip to content

Commit 092d48d

Browse files
committed
Merge pull request iluwatar#294 from DevFactory/release1
Unit tests for adapter, business-delegate, factory-method and command modules
2 parents 8ba0192 + 6b99f26 commit 092d48d

File tree

22 files changed

+445
-122
lines changed

22 files changed

+445
-122
lines changed

CODE_COVERAGE.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Code Coverage Report generation
2+
3+
To generate the code coverage report, execute the following command:
4+
> mvn clean verify
5+
6+
This will generate code coverage report in each of the modules. In order to view the same, open the following file in your browser.
7+
> target/site/jacoco/index.html
8+
9+
Please note that the above folder is created under each of the modules. For example:
10+
* adapter/target/site/jacoco/index.html
11+
* busniess-delegate/target/site/jacoco/index.html
12+
13+

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.

business-delegate/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,10 @@
1515
<artifactId>junit</artifactId>
1616
<scope>test</scope>
1717
</dependency>
18+
<dependency>
19+
<groupId>org.mockito</groupId>
20+
<artifactId>mockito-core</artifactId>
21+
<scope>test</scope>
22+
</dependency>
1823
</dependencies>
1924
</project>

business-delegate/src/main/java/com/iluwatar/business/delegate/App.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
package com.iluwatar.business.delegate;
22

33
/**
4-
*
54
* The Business Delegate pattern adds an abstraction layer between the presentation and business
65
* tiers. By using the pattern we gain loose coupling between the tiers. The Business Delegate
76
* encapsulates knowledge about how to locate, connect to, and interact with the business objects
87
* that make up the application.
9-
* <p>
10-
* Some of the services the Business Delegate uses are instantiated directly, and some can be
8+
*
9+
* <p>Some of the services the Business Delegate uses are instantiated directly, and some can be
1110
* retrieved through service lookups. The Business Delegate itself may contain business logic too
1211
* potentially tying together multiple service calls, exception handling, retrying etc.
13-
* <p>
14-
* In this example the client ({@link Client}) utilizes a business delegate (
12+
*
13+
* <p>In this example the client ({@link Client}) utilizes a business delegate (
1514
* {@link BusinessDelegate}) to execute a task. The Business Delegate then selects the appropriate
1615
* service and makes the service call.
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) {
2725

2826
BusinessDelegate businessDelegate = new BusinessDelegate();
27+
BusinessLookup businessLookup = new BusinessLookup();
28+
businessLookup.setEjbService(new EjbService());
29+
businessLookup.setJmsService(new JmsService());
30+
31+
businessDelegate.setLookupService(businessLookup);
2932
businessDelegate.setServiceType(ServiceType.EJB);
3033

3134
Client client = new Client(businessDelegate);
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
package com.iluwatar.business.delegate;
22

33
/**
4-
*
54
* BusinessDelegate separates the presentation and business tiers
6-
*
75
*/
86
public class BusinessDelegate {
97

10-
private BusinessLookup lookupService = new BusinessLookup();
11-
private BusinessService businessService;
12-
private ServiceType serviceType;
8+
private BusinessLookup lookupService;
9+
private BusinessService businessService;
10+
private ServiceType serviceType;
1311

14-
public void setServiceType(ServiceType serviceType) {
15-
this.serviceType = serviceType;
16-
}
12+
public void setLookupService(BusinessLookup businessLookup) {
13+
this.lookupService = businessLookup;
14+
}
1715

18-
public void doTask() {
19-
businessService = lookupService.getBusinessService(serviceType);
20-
businessService.doProcessing();
21-
}
16+
public void setServiceType(ServiceType serviceType) {
17+
this.serviceType = serviceType;
18+
}
19+
20+
public void doTask() {
21+
businessService = lookupService.getBusinessService(serviceType);
22+
businessService.doProcessing();
23+
}
2224
}
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
package com.iluwatar.business.delegate;
22

33
/**
4-
*
5-
* Class for performing service lookups
6-
*
4+
* Class for performing service lookups.
75
*/
86
public class BusinessLookup {
97

8+
private EjbService ejbService;
9+
10+
private JmsService jmsService;
11+
12+
/**
13+
* @param serviceType Type of service instance to be returned.
14+
* @return Service instance.
15+
*/
1016
public BusinessService getBusinessService(ServiceType serviceType) {
1117
if (serviceType.equals(ServiceType.EJB)) {
12-
return new EjbService();
18+
return ejbService;
1319
} else {
14-
return new JmsService();
20+
return jmsService;
1521
}
1622
}
23+
24+
public void setJmsService(JmsService jmsService) {
25+
this.jmsService = jmsService;
26+
}
27+
28+
public void setEjbService(EjbService ejbService) {
29+
this.ejbService = ejbService;
30+
}
1731
}

0 commit comments

Comments
 (0)