Skip to content

Commit

Permalink
port isDateTime placeholder from XMLUnit.NET, addresses #174
Browse files Browse the repository at this point in the history
  • Loading branch information
bodewig committed May 9, 2020
1 parent f41ed66 commit 7dc79d7
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 1 deletion.
6 changes: 6 additions & 0 deletions RELEASE_NOTES.md
Expand Up @@ -20,6 +20,12 @@ element/attribute. This allows placeholders like
PR [#178](https://github.com/xmlunit/xmlunit/issues/178) by
[@Jazzyekim](https://github.com/Jazzyekim).

* add a new `${xmlunit.isDateTime}` placeholder
inspired by [#xmlunit.net/31](https://github.com/xmlunit/xmlunit.net/pull/31) and
[#xmlunit.net/32](https://github.com/xmlunit/xmlunit.net/pull/32) by
[MilkyWare](https://github.com/MilkyWare)
Issue [#174](https://github.com/xmlunit/xmlunit/issues/174)

## XMLUnit for Java 2.6.4 - /Released 2020-03-08/

* the dependencies on JAXB implementation and its transitive
Expand Down
@@ -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;
}
}
}
@@ -1,3 +1,4 @@
org.xmlunit.placeholder.IgnorePlaceholderHandler
org.xmlunit.placeholder.IsNumberPlaceholderHandler
org.xmlunit.placeholder.MatchesRegexPlaceholderHandler
org.xmlunit.placeholder.MatchesRegexPlaceholderHandler
org.xmlunit.placeholder.IsDateTimePlaceholderHandler
@@ -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));
}
}
Expand Up @@ -415,4 +415,40 @@ public void hasMalformedPlaceholder_Attribute() {

assertTrue(diff.hasDifferences());
}

@Test
public void isDateTimePlaceholder_Attribute_NotDateTime() {
String control = "<elem1 attr='${xmlunit.isDateTime}'/>";
String test = "<elem1 attr='abc'/>";
Diff diff = PlaceholderSupport.withPlaceholderSupport(DiffBuilder.compare(control).withTest(test)).build();

assertTrue(diff.hasDifferences());
}

@Test
public void isDateTimePlaceholder_Attribute_IsDateTime() {
String control = "<elem1 attr='${xmlunit.isDateTime}'/>";
String test = "<elem1 attr='2020-01-01 15:00:00Z'/>";
Diff diff = PlaceholderSupport.withPlaceholderSupport(DiffBuilder.compare(control).withTest(test)).build();

assertFalse(diff.hasDifferences());
}

@Test
public void isDateTimePlaceholder_Element_NotDateTime() {
String control = "<elem1>${xmlunit.isDateTime}</elem1>";
String test = "<elem1>abc</elem1>";
Diff diff = PlaceholderSupport.withPlaceholderSupport(DiffBuilder.compare(control).withTest(test)).build();

assertTrue(diff.hasDifferences());
}

@Test
public void isDateTimePlaceholder_Element_IsDateTime() {
String control = "<elem1>${xmlunit.isDateTime}</elem1>";
String test = "<elem1>2020-01-01 15:00:00Z</elem1>";
Diff diff = PlaceholderSupport.withPlaceholderSupport(DiffBuilder.compare(control).withTest(test)).build();

assertFalse(diff.hasDifferences());
}
}

0 comments on commit 7dc79d7

Please sign in to comment.