Skip to content

Commit 1fa617d

Browse files
committed
Add proper unit tests for chain pattern iluwatar#293
1 parent 6c1f025 commit 1fa617d

File tree

5 files changed

+93
-13
lines changed

5 files changed

+93
-13
lines changed

chain/src/main/java/com/iluwatar/chain/OrcCommander.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public OrcCommander(RequestHandler handler) {
1515
public void handleRequest(Request req) {
1616
if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) {
1717
printHandling(req);
18+
req.markHandled();
1819
} else {
1920
super.handleRequest(req);
2021
}

chain/src/main/java/com/iluwatar/chain/OrcOfficer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public OrcOfficer(RequestHandler handler) {
1515
public void handleRequest(Request req) {
1616
if (req.getRequestType().equals(RequestType.TORTURE_PRISONER)) {
1717
printHandling(req);
18+
req.markHandled();
1819
} else {
1920
super.handleRequest(req);
2021
}

chain/src/main/java/com/iluwatar/chain/OrcSoldier.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public OrcSoldier(RequestHandler handler) {
1515
public void handleRequest(Request req) {
1616
if (req.getRequestType().equals(RequestType.COLLECT_TAX)) {
1717
printHandling(req);
18+
req.markHandled();
1819
} else {
1920
super.handleRequest(req);
2021
}
Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,78 @@
11
package com.iluwatar.chain;
22

3+
import java.util.Objects;
4+
35
/**
4-
*
56
* Request
6-
*
77
*/
88
public class Request {
99

10-
private String requestDescription;
11-
private RequestType requestType;
10+
/**
11+
* The type of this request, used by each item in the chain to see if they should or can handle
12+
* this particular request
13+
*/
14+
private final RequestType requestType;
15+
16+
/**
17+
* A description of the request
18+
*/
19+
private final String requestDescription;
1220

13-
public Request(RequestType requestType, String requestDescription) {
14-
this.setRequestType(requestType);
15-
this.setRequestDescription(requestDescription);
21+
/**
22+
* Indicates if the request is handled or not. A request can only switch state from unhandled to
23+
* handled, there's no way to 'unhandle' a request
24+
*/
25+
private boolean handled = false;
26+
27+
/**
28+
* Create a new request of the given type and accompanied description.
29+
*
30+
* @param requestType The type of request
31+
* @param requestDescription The description of the request
32+
*/
33+
public Request(final RequestType requestType, final String requestDescription) {
34+
this.requestType = Objects.requireNonNull(requestType);
35+
this.requestDescription = Objects.requireNonNull(requestDescription);
1636
}
1737

38+
/**
39+
* Get a description of the request
40+
*
41+
* @return A human readable description of the request
42+
*/
1843
public String getRequestDescription() {
1944
return requestDescription;
2045
}
2146

22-
public void setRequestDescription(String requestDescription) {
23-
this.requestDescription = requestDescription;
24-
}
25-
47+
/**
48+
* Get the type of this request, used by each person in the chain of command to see if they should
49+
* or can handle this particular request
50+
*
51+
* @return The request type
52+
*/
2653
public RequestType getRequestType() {
2754
return requestType;
2855
}
2956

30-
public void setRequestType(RequestType requestType) {
31-
this.requestType = requestType;
57+
/**
58+
* Mark the request as handled
59+
*/
60+
public void markHandled() {
61+
this.handled = true;
62+
}
63+
64+
/**
65+
* Indicates if this request is handled or not
66+
*
67+
* @return <tt>true</tt> when the request is handled, <tt>false</tt> if not
68+
*/
69+
public boolean isHandled() {
70+
return this.handled;
3271
}
3372

3473
@Override
3574
public String toString() {
3675
return getRequestDescription();
3776
}
77+
3878
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.iluwatar.chain;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertTrue;
6+
7+
/**
8+
* Date: 12/6/15 - 9:29 PM
9+
*
10+
* @author Jeroen Meulemeester
11+
*/
12+
public class OrcKingTest {
13+
14+
/**
15+
* All possible requests
16+
*/
17+
private static final Request[] REQUESTS = new Request[]{
18+
new Request(RequestType.DEFEND_CASTLE, "Don't let the barbarians enter my castle!!"),
19+
new Request(RequestType.TORTURE_PRISONER, "Don't just stand there, tickle him!"),
20+
new Request(RequestType.COLLECT_TAX, "Don't steal, the King hates competition ..."),
21+
};
22+
23+
@Test
24+
public void testMakeRequest() throws Exception {
25+
final OrcKing king = new OrcKing();
26+
27+
for (final Request request : REQUESTS) {
28+
king.makeRequest(request);
29+
assertTrue(
30+
"Expected all requests from King to be handled, but [" + request + "] was not!",
31+
request.isHandled()
32+
);
33+
}
34+
35+
}
36+
37+
}

0 commit comments

Comments
 (0)