-
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.
- Loading branch information
Showing
10 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
src/main/java/com/dongho/df/domain/behavioral/chain_of_responsibility/ResultSet.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,15 @@ | ||
package com.dongho.df.domain.behavioral.chain_of_responsibility; | ||
|
||
public class ResultSet { | ||
|
||
private String value; | ||
|
||
public ResultSet(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
...a/com/dongho/df/domain/behavioral/chain_of_responsibility/handler/AnchorChainHandler.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,16 @@ | ||
package com.dongho.df.domain.behavioral.chain_of_responsibility.handler; | ||
|
||
import com.dongho.df.domain.behavioral.chain_of_responsibility.ResultSet; | ||
import com.dongho.df.domain.behavioral.chain_of_responsibility.request.Request; | ||
import com.dongho.df.domain.behavioral.chain_of_responsibility.request.RequestType; | ||
|
||
public class AnchorChainHandler extends ChainHandler { | ||
|
||
@Override | ||
public ResultSet handleRequest(Request request) { | ||
if (request.getRequestType().equals(RequestType.ANCHOR_CLICK)) { | ||
return new ResultSet(RequestType.ANCHOR_CLICK.name()); | ||
} | ||
return delegateRequest(request); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...a/com/dongho/df/domain/behavioral/chain_of_responsibility/handler/ButtonChainHandler.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,17 @@ | ||
package com.dongho.df.domain.behavioral.chain_of_responsibility.handler; | ||
|
||
import com.dongho.df.domain.behavioral.chain_of_responsibility.ResultSet; | ||
import com.dongho.df.domain.behavioral.chain_of_responsibility.request.Request; | ||
import com.dongho.df.domain.behavioral.chain_of_responsibility.request.RequestType; | ||
|
||
public class ButtonChainHandler extends ChainHandler { | ||
|
||
@Override | ||
public ResultSet handleRequest(Request request) { | ||
if (request.getRequestType().equals(RequestType.BUTTON_CLICK)) { | ||
return new ResultSet(RequestType.BUTTON_CLICK.name()); | ||
} | ||
return delegateRequest(request); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
...in/java/com/dongho/df/domain/behavioral/chain_of_responsibility/handler/ChainHandler.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,24 @@ | ||
package com.dongho.df.domain.behavioral.chain_of_responsibility.handler; | ||
|
||
import com.dongho.df.domain.behavioral.chain_of_responsibility.ResultSet; | ||
import com.dongho.df.domain.behavioral.chain_of_responsibility.request.Request; | ||
|
||
public abstract class ChainHandler { | ||
|
||
private ChainHandler chainHandler = null; | ||
|
||
public abstract ResultSet handleRequest(Request request); | ||
|
||
public void linkDelegator(ChainHandler handler) { | ||
chainHandler = handler; | ||
} | ||
|
||
protected ResultSet delegateRequest(Request request) { | ||
if (chainHandler == null) { | ||
return new ResultSet(""); | ||
} | ||
|
||
return chainHandler.handleRequest(request); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...n/java/com/dongho/df/domain/behavioral/chain_of_responsibility/request/AnchorRequest.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,10 @@ | ||
package com.dongho.df.domain.behavioral.chain_of_responsibility.request; | ||
|
||
public class AnchorRequest implements Request { | ||
|
||
@Override | ||
public RequestType getRequestType() { | ||
return RequestType.ANCHOR_CLICK; | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...n/java/com/dongho/df/domain/behavioral/chain_of_responsibility/request/ButtonRequest.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,10 @@ | ||
package com.dongho.df.domain.behavioral.chain_of_responsibility.request; | ||
|
||
public class ButtonRequest implements Request { | ||
|
||
@Override | ||
public RequestType getRequestType() { | ||
return RequestType.BUTTON_CLICK; | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...ain/java/com/dongho/df/domain/behavioral/chain_of_responsibility/request/LinkRequest.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,10 @@ | ||
package com.dongho.df.domain.behavioral.chain_of_responsibility.request; | ||
|
||
public class LinkRequest implements Request { | ||
|
||
@Override | ||
public RequestType getRequestType() { | ||
return RequestType.LINK_CLICK; | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/dongho/df/domain/behavioral/chain_of_responsibility/request/Request.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,7 @@ | ||
package com.dongho.df.domain.behavioral.chain_of_responsibility.request; | ||
|
||
public interface Request { | ||
|
||
RequestType getRequestType(); | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...ain/java/com/dongho/df/domain/behavioral/chain_of_responsibility/request/RequestType.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,9 @@ | ||
package com.dongho.df.domain.behavioral.chain_of_responsibility.request; | ||
|
||
public enum RequestType { | ||
|
||
BUTTON_CLICK, | ||
ANCHOR_CLICK, | ||
LINK_CLICK | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
...va/com/dongho/df/domain/behavioral/chain_of_responsibility/ChainOfResponsibilityTest.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,86 @@ | ||
package com.dongho.df.domain.behavioral.chain_of_responsibility; | ||
|
||
import com.dongho.df.domain.behavioral.chain_of_responsibility.handler.AnchorChainHandler; | ||
import com.dongho.df.domain.behavioral.chain_of_responsibility.handler.ButtonChainHandler; | ||
import com.dongho.df.domain.behavioral.chain_of_responsibility.handler.ChainHandler; | ||
import com.dongho.df.domain.behavioral.chain_of_responsibility.request.*; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@RunWith(JUnit4.class) | ||
public class ChainOfResponsibilityTest { | ||
|
||
private ChainHandler root; | ||
|
||
@Before | ||
public void before() { | ||
ChainHandler buttonChainHandler = new ButtonChainHandler(); | ||
ChainHandler anchorChainHandler = new AnchorChainHandler(); | ||
|
||
buttonChainHandler.linkDelegator(anchorChainHandler); | ||
root = buttonChainHandler; | ||
} | ||
|
||
|
||
@Test | ||
public void buttonClickRequestTest() { | ||
Request request; | ||
ResultSet resultSet; | ||
|
||
given: { | ||
request = new ButtonRequest(); | ||
} | ||
|
||
when: { | ||
resultSet = root.handleRequest(request); | ||
} | ||
|
||
then: { | ||
assertThat(resultSet.getValue()).isEqualTo(RequestType.BUTTON_CLICK.name()); | ||
} | ||
} | ||
|
||
|
||
@Test | ||
public void anchorClickRequest() { | ||
Request request; | ||
ResultSet resultSet; | ||
|
||
given: { | ||
request = new AnchorRequest(); | ||
} | ||
|
||
when: { | ||
resultSet = root.handleRequest(request); | ||
} | ||
|
||
then: { | ||
assertThat(resultSet.getValue()).isEqualTo(RequestType.ANCHOR_CLICK.name()); | ||
} | ||
} | ||
|
||
|
||
@Test | ||
public void linkClickRequestDefault() { | ||
Request request; | ||
ResultSet resultSet; | ||
|
||
given: { | ||
request = new LinkRequest(); | ||
} | ||
|
||
when: { | ||
resultSet = root.handleRequest(request); | ||
} | ||
|
||
then: { | ||
assertThat(resultSet.getValue()).isEqualTo(""); | ||
} | ||
} | ||
|
||
|
||
} |