Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for matchesRegex placeholder
- Loading branch information
Mykhailo.Dyrda
committed
Mar 24, 2020
1 parent
4866621
commit fea1758
Showing
8 changed files
with
211 additions
and
5 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
43 changes: 43 additions & 0 deletions
43
...it-placeholders/src/main/java/org/xmlunit/placeholder/MatchesRegexPlaceholderHandler.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,43 @@ | ||
package org.xmlunit.placeholder; | ||
|
||
import org.xmlunit.XMLUnitException; | ||
import org.xmlunit.diff.ComparisonResult; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.regex.PatternSyntaxException; | ||
|
||
import static org.xmlunit.diff.ComparisonResult.DIFFERENT; | ||
import static org.xmlunit.diff.ComparisonResult.EQUAL; | ||
|
||
/** | ||
* Handler for the {@code matchesRegex()} placeholder keyword. | ||
*/ | ||
public class MatchesRegexPlaceholderHandler implements PlaceholderHandler { | ||
private static final String PLACEHOLDER_NAME = "matchesRegex"; | ||
|
||
@Override | ||
public String getKeyword() { | ||
return PLACEHOLDER_NAME; | ||
} | ||
|
||
@Override | ||
public ComparisonResult evaluate(String testText, String... param) { | ||
if (param.length > 0 && param[0] != null && !param[0].equals("")) { | ||
try { | ||
Pattern pattern = Pattern.compile(param[0].trim()); | ||
if (testText != null && evaluate(testText.trim(), pattern)) { | ||
return EQUAL; | ||
} | ||
} catch(PatternSyntaxException e) { | ||
throw new XMLUnitException(e.getMessage(), e); | ||
} | ||
} | ||
return DIFFERENT; | ||
} | ||
|
||
private boolean evaluate(String testText, Pattern pattern) { | ||
Matcher matcher = pattern.matcher(testText); | ||
return matcher.find(); | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
...ceholders/src/main/resources/META-INF/services/org.xmlunit.placeholder.PlaceholderHandler
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
org.xmlunit.placeholder.IgnorePlaceholderHandler | ||
org.xmlunit.placeholder.IsNumberPlaceholderHandler | ||
org.xmlunit.placeholder.MatchesRegexPlaceholderHandler |
88 changes: 88 additions & 0 deletions
88
...laceholders/src/test/java/org/xmlunit/placeholder/MatchesRegexPlaceholderHandlerTest.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,88 @@ | ||
/* | ||
This file is licensed to You under the Apache License, Version 2.0 | ||
(the "License"); you may not use this file except in compliance with | ||
the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package org.xmlunit.placeholder; | ||
|
||
import org.junit.Test; | ||
import org.xmlunit.diff.ComparisonResult; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class MatchesRegexPlaceholderHandlerTest { | ||
|
||
private PlaceholderHandler placeholderHandler = new MatchesRegexPlaceholderHandler(); | ||
|
||
@Test | ||
public void shouldGetKeyword() { | ||
String expected = "matchesRegex"; | ||
String keyword = placeholderHandler.getKeyword(); | ||
|
||
assertThat(keyword, equalTo(expected)); | ||
} | ||
|
||
@Test | ||
public void shouldEvaluateGivenSimpleRegex() { | ||
String testTest = "1234"; | ||
String regex = "^\\d+$"; | ||
ComparisonResult comparisonResult = placeholderHandler.evaluate(testTest, regex); | ||
|
||
assertThat(comparisonResult, equalTo(ComparisonResult.EQUAL)); | ||
} | ||
|
||
@Test | ||
public void shouldNotEvaluateGivenNull() { | ||
String testTest = null; | ||
String regex = "^\\d+$"; | ||
ComparisonResult comparisonResult = placeholderHandler.evaluate(testTest, regex); | ||
|
||
assertThat(comparisonResult, equalTo(ComparisonResult.DIFFERENT)); | ||
} | ||
|
||
@Test | ||
public void shouldNotEvaluateGivenEmptyString() { | ||
String testTest = ""; | ||
String regex = "^\\d+$"; | ||
ComparisonResult comparisonResult = placeholderHandler.evaluate(testTest, regex); | ||
|
||
assertThat(comparisonResult, equalTo(ComparisonResult.DIFFERENT)); | ||
} | ||
|
||
@Test | ||
public void shouldNotEvaluateStringDoesNotMatchRegex() { | ||
String testTest = "not parsable as a number even though it contains 123 numbers"; | ||
String regex = "^\\d+$"; | ||
ComparisonResult comparisonResult = placeholderHandler.evaluate(testTest, regex); | ||
|
||
assertThat(comparisonResult, equalTo(ComparisonResult.DIFFERENT)); | ||
} | ||
|
||
@Test | ||
public void shouldNotEvaluateWithNullRegex() { | ||
String testTest = "a string"; | ||
String regex = null; | ||
ComparisonResult comparisonResult = placeholderHandler.evaluate(testTest, regex); | ||
|
||
assertThat(comparisonResult, equalTo(ComparisonResult.DIFFERENT)); | ||
} | ||
|
||
@Test | ||
public void shouldNotEvaluateWithEmptyRegex() { | ||
String testTest = "a string"; | ||
String regex = ""; | ||
ComparisonResult comparisonResult = placeholderHandler.evaluate(testTest, regex); | ||
|
||
assertThat(comparisonResult, equalTo(ComparisonResult.DIFFERENT)); | ||
} | ||
|
||
} |
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