Skip to content

Commit

Permalink
Merge pull request #294 from DevFactory/release1
Browse files Browse the repository at this point in the history
Unit tests for adapter, business-delegate, factory-method and command modules
  • Loading branch information
iluwatar committed Nov 22, 2015
2 parents 8ba0192 + 6b99f26 commit 092d48d
Show file tree
Hide file tree
Showing 22 changed files with 445 additions and 122 deletions.
13 changes: 13 additions & 0 deletions CODE_COVERAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Code Coverage Report generation

To generate the code coverage report, execute the following command:
> mvn clean verify
This will generate code coverage report in each of the modules. In order to view the same, open the following file in your browser.
> target/site/jacoco/index.html
Please note that the above folder is created under each of the modules. For example:
* adapter/target/site/jacoco/index.html
* busniess-delegate/target/site/jacoco/index.html


5 changes: 5 additions & 0 deletions adapter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
16 changes: 7 additions & 9 deletions adapter/src/main/java/com/iluwatar/adapter/App.java
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* There are two variations of the Adapter pattern: The class adapter implements the adaptee's
*
* <p>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.
* <p>
* The Adapter ({@link GnomeEngineer}) converts the interface of the target class (
*
* <p>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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
68 changes: 68 additions & 0 deletions adapter/src/test/java/com/iluwatar/adapter/AdapterPatternTest.java
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>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.
*
* <p>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<String, Object> 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();
}
}
19 changes: 0 additions & 19 deletions adapter/src/test/java/com/iluwatar/adapter/AppTest.java

This file was deleted.

5 changes: 5 additions & 0 deletions business-delegate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
package com.iluwatar.business.delegate;

/**
*
* The Business Delegate pattern adds an abstraction layer between the presentation and business
* tiers. By using the pattern we gain loose coupling between the tiers. The Business Delegate
* encapsulates knowledge about how to locate, connect to, and interact with the business objects
* that make up the application.
* <p>
* Some of the services the Business Delegate uses are instantiated directly, and some can be
*
* <p>Some of the services the Business Delegate uses are instantiated directly, and some can be
* retrieved through service lookups. The Business Delegate itself may contain business logic too
* potentially tying together multiple service calls, exception handling, retrying etc.
* <p>
* In this example the client ({@link Client}) utilizes a business delegate (
*
* <p>In this example the client ({@link Client}) utilizes a business delegate (
* {@link BusinessDelegate}) to execute a task. The Business Delegate then selects the appropriate
* service and makes the service call.
*
*/
public class App {

/**
* Program entry point
*
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) {

BusinessDelegate businessDelegate = new BusinessDelegate();
BusinessLookup businessLookup = new BusinessLookup();
businessLookup.setEjbService(new EjbService());
businessLookup.setJmsService(new JmsService());

businessDelegate.setLookupService(businessLookup);
businessDelegate.setServiceType(ServiceType.EJB);

Client client = new Client(businessDelegate);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package com.iluwatar.business.delegate;

/**
*
* BusinessDelegate separates the presentation and business tiers
*
*/
public class BusinessDelegate {

private BusinessLookup lookupService = new BusinessLookup();
private BusinessService businessService;
private ServiceType serviceType;
private BusinessLookup lookupService;
private BusinessService businessService;
private ServiceType serviceType;

public void setServiceType(ServiceType serviceType) {
this.serviceType = serviceType;
}
public void setLookupService(BusinessLookup businessLookup) {
this.lookupService = businessLookup;
}

public void doTask() {
businessService = lookupService.getBusinessService(serviceType);
businessService.doProcessing();
}
public void setServiceType(ServiceType serviceType) {
this.serviceType = serviceType;
}

public void doTask() {
businessService = lookupService.getBusinessService(serviceType);
businessService.doProcessing();
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
package com.iluwatar.business.delegate;

/**
*
* Class for performing service lookups
*
* Class for performing service lookups.
*/
public class BusinessLookup {

private EjbService ejbService;

private JmsService jmsService;

/**
* @param serviceType Type of service instance to be returned.
* @return Service instance.
*/
public BusinessService getBusinessService(ServiceType serviceType) {
if (serviceType.equals(ServiceType.EJB)) {
return new EjbService();
return ejbService;
} else {
return new JmsService();
return jmsService;
}
}

public void setJmsService(JmsService jmsService) {
this.jmsService = jmsService;
}

public void setEjbService(EjbService ejbService) {
this.ejbService = ejbService;
}
}

This file was deleted.

Loading

0 comments on commit 092d48d

Please sign in to comment.