Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
port isDateTime placeholder from XMLUnit.NET, addresses #174
- Loading branch information
Showing
5 changed files
with
195 additions
and
1 deletion.
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
83 changes: 83 additions & 0 deletions
83
xmlunit-placeholders/src/main/java/org/xmlunit/placeholder/IsDateTimePlaceholderHandler.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,83 @@ | ||
/* | ||
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 java.text.DateFormat; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.xmlunit.diff.ComparisonResult; | ||
|
||
/** | ||
* Handler for the {@code isDateTime} placeholder keyword. | ||
* @since 2.7.0 | ||
*/ | ||
public class IsDateTimePlaceholderHandler implements PlaceholderHandler { | ||
private static final String PLACEHOLDER_NAME = "isDateTime"; | ||
|
||
private static final List<String> ISO_PATTERNS = Collections.unmodifiableList(Arrays.asList( | ||
"yyyy-MM-dd", | ||
"yyyy-MM-dd'T'HH:mm:ss", | ||
"yyyy-MM-dd'T'HH:mm:ss.SSS", | ||
"yyyy-MM-dd'T'HH:mm:ssZ", | ||
"yyyy-MM-dd'T'HH:mm:ss.SSSZ", | ||
"yyyy-MM-dd'T'HH:mm:ssXXX", | ||
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX", | ||
"yyyy-MM-dd HH:mm:ss", | ||
"yyyy-MM-dd HH:mm:ss.SSS", | ||
"yyyy-MM-dd HH:mm:ssZ", | ||
"yyyy-MM-dd HH:mm:ss.SSSZ", | ||
"yyyy-MM-dd HH:mm:ssXXX", | ||
"yyyy-MM-dd HH:mm:ss.SSSXXX" | ||
)); | ||
|
||
@Override | ||
public String getKeyword() { | ||
return PLACEHOLDER_NAME; | ||
} | ||
|
||
@Override | ||
public ComparisonResult evaluate(final String testText, final String... param) { | ||
return canParse(testText) | ||
? ComparisonResult.EQUAL | ||
: ComparisonResult.DIFFERENT; | ||
} | ||
|
||
private boolean canParse(final String testText) { | ||
if (testText == null || "".equals(testText)) { | ||
return false; | ||
} | ||
if (canParse(DateFormat.getDateInstance(DateFormat.SHORT), testText) || | ||
canParse(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT), testText)) { | ||
return true; | ||
} | ||
for (final String pattern : ISO_PATTERNS) { | ||
if (canParse(new SimpleDateFormat(pattern), testText)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private boolean canParse(final DateFormat fmt, final String testText) { | ||
try { | ||
return null != fmt.parse(testText); | ||
} catch (ParseException ex) { | ||
return false; | ||
} | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
...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,3 +1,4 @@ | ||
org.xmlunit.placeholder.IgnorePlaceholderHandler | ||
org.xmlunit.placeholder.IsNumberPlaceholderHandler | ||
org.xmlunit.placeholder.MatchesRegexPlaceholderHandler | ||
org.xmlunit.placeholder.MatchesRegexPlaceholderHandler | ||
org.xmlunit.placeholder.IsDateTimePlaceholderHandler |
68 changes: 68 additions & 0 deletions
68
...-placeholders/src/test/java/org/xmlunit/placeholder/IsDateTimePlaceholderHandlerTest.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,68 @@ | ||
/* | ||
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 java.util.Locale; | ||
|
||
import org.junit.Test; | ||
import org.xmlunit.diff.ComparisonResult; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class IsDateTimePlaceholderHandlerTest { | ||
|
||
private PlaceholderHandler placeholderHandler = new IsDateTimePlaceholderHandler(); | ||
|
||
@Test | ||
public void shouldGetKeyword() { | ||
String expected = "isDateTime"; | ||
String keyword = placeholderHandler.getKeyword(); | ||
|
||
assertThat(keyword, equalTo(expected)); | ||
} | ||
|
||
@Test | ||
public void shouldAcceptABunchOfStrings() { | ||
final Locale l = Locale.getDefault(); | ||
try { | ||
Locale.setDefault(Locale.US); | ||
for (final String s : new String[] { | ||
"2020-01-01", | ||
"01/01/2020", | ||
"01/01/2020", | ||
"2020-01-01T15:00", | ||
"2020-01-01 15:00:00Z", | ||
"01/01/2020 15:00", | ||
}) { | ||
final ComparisonResult comparisonResult = placeholderHandler.evaluate(s); | ||
assertThat(comparisonResult, equalTo(ComparisonResult.EQUAL)); | ||
} | ||
} finally { | ||
Locale.setDefault(l); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldRejectNullAndEmpty() { | ||
assertThat(placeholderHandler.evaluate(null), equalTo(ComparisonResult.DIFFERENT)); | ||
assertThat(placeholderHandler.evaluate(""), equalTo(ComparisonResult.DIFFERENT)); | ||
} | ||
|
||
@Test | ||
public void shouldRejectExtraContent() { | ||
assertThat(placeholderHandler.evaluate("This is a test date 2020-01-01"), | ||
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