-
Notifications
You must be signed in to change notification settings - Fork 7
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 #12 from antkorwin/feature/expected-regexp
Using regular expressions in expected datasets
- Loading branch information
Showing
12 changed files
with
567 additions
and
166 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
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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/antkorwin/springtestmongo/internal/expect/matcher/MatcherFactory.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,33 @@ | ||
package com.antkorwin.springtestmongo.internal.expect.matcher; | ||
|
||
|
||
/** | ||
* Factory for obtaining a {@link ValueMatcher} to match the given value | ||
* | ||
* @author Korovin Anatoliy | ||
*/ | ||
public class MatcherFactory { | ||
|
||
private final RegexMatcher regexMatcher; | ||
private final SimpleMatcher simpleMatcher; | ||
|
||
public MatcherFactory() { | ||
regexMatcher = new RegexMatcher(); | ||
simpleMatcher = new SimpleMatcher(); | ||
} | ||
|
||
/** | ||
* get matcher by the value of expected object | ||
* | ||
* @param value the value of expected object | ||
* | ||
* @return matcher for this value | ||
*/ | ||
public ValueMatcher getMatcher(Object value) { | ||
if (regexMatcher.isNecessary(value)) { | ||
return regexMatcher; | ||
} else { | ||
return simpleMatcher; | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/antkorwin/springtestmongo/internal/expect/matcher/RegexMatcher.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,31 @@ | ||
package com.antkorwin.springtestmongo.internal.expect.matcher; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
|
||
/** | ||
* Match two objects with using a regular expression in the expected value. | ||
* | ||
* @author Korovin Anatoliy | ||
*/ | ||
public class RegexMatcher implements ValueMatcher { | ||
|
||
@Override | ||
public boolean match(Object originValue, Object comparableValue) { | ||
String cmpValue = (String) comparableValue; | ||
cmpValue = cmpValue.replaceFirst("regex:", "").trim(); | ||
Pattern pattern = Pattern.compile(cmpValue); | ||
Matcher matcher = pattern.matcher((String) originValue); | ||
return matcher.matches(); | ||
} | ||
|
||
@Override | ||
public boolean isNecessary(Object value) { | ||
if (!(value instanceof String)) { | ||
return false; | ||
} | ||
String strValue = (String) value; | ||
return (strValue.startsWith("regex:")); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/antkorwin/springtestmongo/internal/expect/matcher/SimpleMatcher.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,20 @@ | ||
package com.antkorwin.springtestmongo.internal.expect.matcher; | ||
|
||
|
||
/** | ||
* Match two objects of simple types. | ||
* | ||
* @author Korovin Anatoliy | ||
*/ | ||
class SimpleMatcher implements ValueMatcher { | ||
|
||
@Override | ||
public boolean match(Object originValue, Object comparableValue) { | ||
return originValue.equals(comparableValue); | ||
} | ||
|
||
@Override | ||
public boolean isNecessary(Object value) { | ||
return true; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/antkorwin/springtestmongo/internal/expect/matcher/ValueMatcher.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,25 @@ | ||
package com.antkorwin.springtestmongo.internal.expect.matcher; | ||
|
||
|
||
/** | ||
* Algorithm to match two objects and check equals. | ||
* | ||
* @author Korovin Anatoliy | ||
*/ | ||
public interface ValueMatcher { | ||
|
||
/** | ||
* Match two objects | ||
* | ||
* @param originValue value of original object | ||
* @param comparableValue value of comparable(expected) object | ||
* | ||
* @return tru is this objects are same and false if not. | ||
*/ | ||
boolean match(Object originValue, Object comparableValue); | ||
|
||
/** | ||
* Check necessary of applied this matcher to current value of expected object. | ||
*/ | ||
boolean isNecessary(Object value); | ||
} |
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
Oops, something went wrong.