-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #294 from DevFactory/release1
Unit tests for adapter, business-delegate, factory-method and command modules
- Loading branch information
Showing
22 changed files
with
445 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
12 changes: 9 additions & 3 deletions
12
adapter/src/main/java/com/iluwatar/adapter/GnomeEngineeringManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
68
adapter/src/test/java/com/iluwatar/adapter/AdapterPatternTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 11 additions & 8 deletions
19
business-delegate/src/main/java/com/iluwatar/business/delegate/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 14 additions & 12 deletions
26
business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
24 changes: 19 additions & 5 deletions
24
business-delegate/src/main/java/com/iluwatar/business/delegate/BusinessLookup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
19 changes: 0 additions & 19 deletions
19
business-delegate/src/test/java/com/iluwatar/business/delegate/AppTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.