Polishing

See gh-2023
This commit is contained in:
Rossen Stoyanchev
2019-11-11 15:03:52 +00:00
parent 6db8306e46
commit 347f16c8ac
3 changed files with 34 additions and 31 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -103,17 +103,18 @@ public class XpathExpectationsHelper {
MatcherAssert.assertThat("XPath " + this.expression, node, matcher); MatcherAssert.assertThat("XPath " + this.expression, node, matcher);
} }
/** /**
* Parse the content, evaluate the XPath expression as a {@link NodeList}, * Parse the content, evaluate the XPath expression as a {@link NodeList},
* and assert it with the given {@code Matcher<NodeList>}. * and assert it with the given {@code Matcher<NodeList>}.
*/ * @since 5.2.2
public void assertNodeList(byte[] content, @Nullable String encoding, final Matcher<? super NodeList> matcher) */
throws Exception { public void assertNodeList(byte[] content, @Nullable String encoding, final Matcher<? super NodeList> matcher)
throws Exception {
Document document = parseXmlByteArray(content, encoding); Document document = parseXmlByteArray(content, encoding);
NodeList nodeList = evaluateXpath(document, XPathConstants.NODESET, NodeList.class); NodeList nodeList = evaluateXpath(document, XPathConstants.NODESET, NodeList.class);
MatcherAssert.assertThat("XPath " + this.getXpathExpression(), nodeList, matcher); MatcherAssert.assertThat("XPath " + this.getXpathExpression(), nodeList, matcher);
} }
/** /**
* Apply the XPath expression and assert the resulting content exists. * Apply the XPath expression and assert the resulting content exists.

View File

@@ -70,16 +70,17 @@ public class XpathResultMatchers {
}; };
} }
/** /**
* Evaluate the XPath and assert the {@link NodeList} content found with the * Evaluate the XPath and assert the {@link NodeList} content found with the
* given Hamcrest {@link Matcher}. * given Hamcrest {@link Matcher}.
*/ * @since 5.2.2
public ResultMatcher nodeList(final Matcher<? super NodeList> matcher) { */
return result -> { public ResultMatcher nodeList(final Matcher<? super NodeList> matcher) {
MockHttpServletResponse response = result.getResponse(); return result -> {
this.xpathHelper.assertNodeList(response.getContentAsByteArray(), getDefinedEncoding(response), matcher); MockHttpServletResponse response = result.getResponse();
}; this.xpathHelper.assertNodeList(response.getContentAsByteArray(), getDefinedEncoding(response), matcher);
} };
}
/** /**
* Get the response encoding if explicitly defined in the response, {code null} otherwise. * Get the response encoding if explicitly defined in the response, {code null} otherwise.

View File

@@ -43,7 +43,7 @@ public class XpathResultMatchersTests {
} }
@Test @Test
public void nodeNoMatch() throws Exception { public void nodeNoMatch() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new XpathResultMatchers("/foo/bar", null).node(Matchers.nullValue()).match(getStubMvcResult())); new XpathResultMatchers("/foo/bar", null).node(Matchers.nullValue()).match(getStubMvcResult()));
} }
@@ -53,9 +53,10 @@ public class XpathResultMatchersTests {
new XpathResultMatchers("/foo/bar", null).nodeList(Matchers.notNullValue()).match(getStubMvcResult()); new XpathResultMatchers("/foo/bar", null).nodeList(Matchers.notNullValue()).match(getStubMvcResult());
} }
@Test(expected = AssertionError.class) @Test
public void nodeListNoMatch() throws Exception { public void nodeListNoMatch() {
new XpathResultMatchers("/foo/bar", null).nodeList(Matchers.nullValue()).match(getStubMvcResult()); assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new XpathResultMatchers("/foo/bar", null).nodeList(Matchers.nullValue()).match(getStubMvcResult()));
} }
@Test @Test
@@ -64,7 +65,7 @@ public class XpathResultMatchersTests {
} }
@Test @Test
public void existsNoMatch() throws Exception { public void existsNoMatch() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new XpathResultMatchers("/foo/Bar", null).exists().match(getStubMvcResult())); new XpathResultMatchers("/foo/Bar", null).exists().match(getStubMvcResult()));
} }
@@ -75,7 +76,7 @@ public class XpathResultMatchersTests {
} }
@Test @Test
public void doesNotExistNoMatch() throws Exception { public void doesNotExistNoMatch() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new XpathResultMatchers("/foo/bar", null).doesNotExist().match(getStubMvcResult())); new XpathResultMatchers("/foo/bar", null).doesNotExist().match(getStubMvcResult()));
} }
@@ -86,7 +87,7 @@ public class XpathResultMatchersTests {
} }
@Test @Test
public void nodeCountNoMatch() throws Exception { public void nodeCountNoMatch() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new XpathResultMatchers("/foo/bar", null).nodeCount(1).match(getStubMvcResult())); new XpathResultMatchers("/foo/bar", null).nodeCount(1).match(getStubMvcResult()));
} }
@@ -97,7 +98,7 @@ public class XpathResultMatchersTests {
} }
@Test @Test
public void stringNoMatch() throws Exception { public void stringNoMatch() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new XpathResultMatchers("/foo/bar[1]", null).string("112").match(getStubMvcResult())); new XpathResultMatchers("/foo/bar[1]", null).string("112").match(getStubMvcResult()));
} }
@@ -108,7 +109,7 @@ public class XpathResultMatchersTests {
} }
@Test @Test
public void numberNoMatch() throws Exception { public void numberNoMatch() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new XpathResultMatchers("/foo/bar[1]", null).number(111.1).match(getStubMvcResult())); new XpathResultMatchers("/foo/bar[1]", null).number(111.1).match(getStubMvcResult()));
} }
@@ -119,7 +120,7 @@ public class XpathResultMatchersTests {
} }
@Test @Test
public void booleanValueNoMatch() throws Exception { public void booleanValueNoMatch() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new XpathResultMatchers("/foo/bar[2]", null).booleanValue(false).match(getStubMvcResult())); new XpathResultMatchers("/foo/bar[2]", null).booleanValue(false).match(getStubMvcResult()));
} }