Skip to content

Commit

Permalink
Make EqualsMatcher sports multi values.
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglifan committed May 20, 2018
1 parent ae72b45 commit 6a74b6b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Arrays;
import java.util.List;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
Expand All @@ -18,7 +20,7 @@
public @interface EqualsMatcher {
String propertyPath();

String expectValue();
String[] expectValue();

class Processor extends OnePropertyMatcherProcessor<EqualsMatcher> {

Expand All @@ -29,8 +31,9 @@ protected String getPropertyPath(EqualsMatcher matcherAnnotation) {

@Override
protected MatchResult doProcess(Object request, EqualsMatcher matcherAnnotation) {
boolean isEquals = matcherAnnotation.expectValue().equals(request.toString());
return isEquals ? MatchResult.ACCEPT : MatchResult.REJECT;
List<String> expectValueList = Arrays.asList(matcherAnnotation.expectValue());
boolean isContains = expectValueList.contains(request.toString());
return isContains ? MatchResult.ACCEPT : MatchResult.REJECT;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public class EqualsMatcherTests {

private EqualsMatcher.Processor equalsMatcherProcessor = new EqualsMatcher.Processor();

private EqualsMatcher usernameEqualsStark = new EqualsMatcher() {
private EqualsMatcher starkOrThor = new EqualsMatcher() {
@Override
public String propertyPath() {
return "username";
}

@Override
public String expectValue() {
return "stark";
public String[] expectValue() {
return new String[]{"stark", "thor"};
}

@Override
Expand All @@ -30,12 +30,27 @@ public Class<? extends Annotation> annotationType() {
}
};


@Test
public void simple() {
TestRequest testRequest = new TestRequest("stark");
MatchResult matchResult = equalsMatcherProcessor.process(testRequest, usernameEqualsStark);
MatchResult matchResult = equalsMatcherProcessor.process(testRequest, starkOrThor);
assertThat(matchResult).isEqualTo(MatchResult.ACCEPT);
}

@Test
public void match_multi_values() {
Map<String, String> request = new HashMap<>();
request.put("username", "stark");
MatchResult matchResult = equalsMatcherProcessor.process(request, starkOrThor);
assertThat(matchResult).isEqualTo(MatchResult.ACCEPT);

request.put("username", "thor");
matchResult = equalsMatcherProcessor.process(request, starkOrThor);
assertThat(matchResult).isEqualTo(MatchResult.ACCEPT);

request.put("username", "rogers");
matchResult = equalsMatcherProcessor.process(request, starkOrThor);
assertThat(matchResult).isEqualTo(MatchResult.REJECT);
}

@Test
Expand All @@ -51,8 +66,8 @@ public String propertyPath() {
}

@Override
public String expectValue() {
return "stark";
public String[] expectValue() {
return new String[]{"stark"};
}

@Override
Expand All @@ -72,7 +87,7 @@ public Class<? extends Annotation> annotationType() {
public void doMatchWithMapRequest() {
Map<String, String> mapRequest = new HashMap<>();
mapRequest.put("username", "stark");
MatchResult matchResult = equalsMatcherProcessor.process(mapRequest, usernameEqualsStark);
MatchResult matchResult = equalsMatcherProcessor.process(mapRequest, starkOrThor);
assertThat(matchResult).isEqualTo(MatchResult.ACCEPT);
}

Expand Down

0 comments on commit 6a74b6b

Please sign in to comment.