Add proper unit tests for chain pattern #293

This commit is contained in:
Jeroen Meulemeester 2015-12-06 22:58:16 +01:00
parent 6c1f025d0f
commit 1fa617d08d
5 changed files with 93 additions and 13 deletions

View File

@ -15,6 +15,7 @@ public class OrcCommander extends RequestHandler {
public void handleRequest(Request req) {
if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) {
printHandling(req);
req.markHandled();
} else {
super.handleRequest(req);
}

View File

@ -15,6 +15,7 @@ public class OrcOfficer extends RequestHandler {
public void handleRequest(Request req) {
if (req.getRequestType().equals(RequestType.TORTURE_PRISONER)) {
printHandling(req);
req.markHandled();
} else {
super.handleRequest(req);
}

View File

@ -15,6 +15,7 @@ public class OrcSoldier extends RequestHandler {
public void handleRequest(Request req) {
if (req.getRequestType().equals(RequestType.COLLECT_TAX)) {
printHandling(req);
req.markHandled();
} else {
super.handleRequest(req);
}

View File

@ -1,38 +1,78 @@
package com.iluwatar.chain;
import java.util.Objects;
/**
*
* Request
*
*/
public class Request {
private String requestDescription;
private RequestType requestType;
/**
* The type of this request, used by each item in the chain to see if they should or can handle
* this particular request
*/
private final RequestType requestType;
public Request(RequestType requestType, String requestDescription) {
this.setRequestType(requestType);
this.setRequestDescription(requestDescription);
/**
* A description of the request
*/
private final String requestDescription;
/**
* Indicates if the request is handled or not. A request can only switch state from unhandled to
* handled, there's no way to 'unhandle' a request
*/
private boolean handled = false;
/**
* Create a new request of the given type and accompanied description.
*
* @param requestType The type of request
* @param requestDescription The description of the request
*/
public Request(final RequestType requestType, final String requestDescription) {
this.requestType = Objects.requireNonNull(requestType);
this.requestDescription = Objects.requireNonNull(requestDescription);
}
/**
* Get a description of the request
*
* @return A human readable description of the request
*/
public String getRequestDescription() {
return requestDescription;
}
public void setRequestDescription(String requestDescription) {
this.requestDescription = requestDescription;
}
/**
* Get the type of this request, used by each person in the chain of command to see if they should
* or can handle this particular request
*
* @return The request type
*/
public RequestType getRequestType() {
return requestType;
}
public void setRequestType(RequestType requestType) {
this.requestType = requestType;
/**
* Mark the request as handled
*/
public void markHandled() {
this.handled = true;
}
/**
* Indicates if this request is handled or not
*
* @return <tt>true</tt> when the request is handled, <tt>false</tt> if not
*/
public boolean isHandled() {
return this.handled;
}
@Override
public String toString() {
return getRequestDescription();
}
}

View File

@ -0,0 +1,37 @@
package com.iluwatar.chain;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
/**
* Date: 12/6/15 - 9:29 PM
*
* @author Jeroen Meulemeester
*/
public class OrcKingTest {
/**
* All possible requests
*/
private static final Request[] REQUESTS = new Request[]{
new Request(RequestType.DEFEND_CASTLE, "Don't let the barbarians enter my castle!!"),
new Request(RequestType.TORTURE_PRISONER, "Don't just stand there, tickle him!"),
new Request(RequestType.COLLECT_TAX, "Don't steal, the King hates competition ..."),
};
@Test
public void testMakeRequest() throws Exception {
final OrcKing king = new OrcKing();
for (final Request request : REQUESTS) {
king.makeRequest(request);
assertTrue(
"Expected all requests from King to be handled, but [" + request + "] was not!",
request.isHandled()
);
}
}
}