Polishing

This commit is contained in:
Juergen Hoeller
2015-11-26 01:10:31 +01:00
parent e410129aaf
commit 97148ce889
4 changed files with 41 additions and 23 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package org.springframework.test.web.servlet.result;
import com.jayway.jsonpath.JsonPath;
import org.hamcrest.Matcher;
import org.springframework.test.util.JsonPathExpectationsHelper;
@@ -23,30 +24,36 @@ import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher;
/**
* Factory for assertions on the response content using <a
* href="http://goessner.net/articles/JsonPath/">JSONPath</a> expressions.
* An instance of this class is typically accessed via
* {@link MockMvcResultMatchers#jsonPath}.
* Factory for assertions on the response content using
* <a href="https://github.com/jayway/JsonPath">JsonPath</a> expressions.
* <p>An instance of this class is typically accessed via
* {@link MockMvcResultMatchers#jsonPath(String, Matcher)} or
* {@link MockMvcResultMatchers#jsonPath(String, Object...)}.
*
* @author Rossen Stoyanchev
* @since 3.2
*/
public class JsonPathResultMatchers {
private JsonPathExpectationsHelper jsonPathHelper;
private final JsonPathExpectationsHelper jsonPathHelper;
/**
* Protected constructor. Use
* {@link MockMvcResultMatchers#jsonPath(String, Object...)} or
* Protected constructor.
* <p>Use {@link MockMvcResultMatchers#jsonPath(String, Object...)} or
* {@link MockMvcResultMatchers#jsonPath(String, Matcher)}.
* @param expression the {@link JsonPath} expression; never {@code null} or empty
* @param args arguments to parameterize the {@code JsonPath} expression with,
* using formatting specifiers defined in {@link String#format(String, Object...)}
*/
protected JsonPathResultMatchers(String expression, Object ... args) {
this.jsonPathHelper = new JsonPathExpectationsHelper(expression, args);
}
/**
* Evaluate the JSONPath and assert the value of the content found with the
* given Hamcrest {@code Matcher}.
* Evaluate the JSON path expression against the response content and
* assert the resulting value with the given Hamcrest {@link Matcher}.
*/
public <T> ResultMatcher value(final Matcher<T> matcher) {
return new ResultMatcher() {
@@ -59,7 +66,8 @@ public class JsonPathResultMatchers {
}
/**
* Evaluate the JSONPath and assert the value of the content found.
* Evaluate the JSON path expression against the response content and
* assert that the result is equal to the supplied value.
*/
public ResultMatcher value(final Object expectedValue) {
return new ResultMatcher() {
@@ -71,7 +79,11 @@ public class JsonPathResultMatchers {
}
/**
* Evaluate the JSONPath and assert that content exists.
* Evaluate the JSON path expression against the response content and
* assert that a non-null value exists at the given path.
* <p>If the JSON path expression is not {@linkplain JsonPath#isDefinite
* definite}, this method asserts that the value at the given path is not
* <em>empty</em>.
*/
public ResultMatcher exists() {
return new ResultMatcher() {
@@ -84,7 +96,11 @@ public class JsonPathResultMatchers {
}
/**
* Evaluate the JSON path and assert not content was found.
* Evaluate the JSON path expression against the response content and
* assert that a value does not exist at the given path.
* <p>If the JSON path expression is not {@linkplain JsonPath#isDefinite
* definite}, this method asserts that the value at the given path is
* <em>empty</em>.
*/
public ResultMatcher doesNotExist() {
return new ResultMatcher() {
@@ -97,7 +113,8 @@ public class JsonPathResultMatchers {
}
/**
* Evluate the JSON path and assert the content found is an array.
* Evaluate the JSON path expression against the response content and
* assert that the result is an array.
*/
public ResultMatcher isArray() {
return new ResultMatcher() {