From d4d5e5ce4039793a6734e68b3d33ddf176ab4dd0 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 14 Aug 2015 22:45:10 +0200 Subject: [PATCH] Polishing --- .../client/match/JsonPathRequestMatchers.java | 13 +++++---- .../client/match/MockRestRequestMatchers.java | 15 +++++----- .../util/JsonPathExpectationsHelperTests.java | 26 ++++++++--------- .../match/JsonPathRequestMatchersTests.java | 19 +++++++------ .../client/match/RequestMatchersTests.java | 28 ++++++++----------- .../matchers/JsonPathRequestMatcherTests.java | 6 ++-- .../result/JsonPathResultMatchersTests.java | 4 +-- .../JsonPathAssertionTests.java | 15 ++++------ 8 files changed, 62 insertions(+), 64 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/client/match/JsonPathRequestMatchers.java b/spring-test/src/main/java/org/springframework/test/web/client/match/JsonPathRequestMatchers.java index 655959ebc8..eb57f0b6e3 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/match/JsonPathRequestMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/match/JsonPathRequestMatchers.java @@ -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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.test.web.client.match; import java.io.IOException; @@ -26,10 +27,10 @@ import org.springframework.test.util.JsonPathExpectationsHelper; import org.springframework.test.web.client.RequestMatcher; /** - * Factory methods for request content {@code RequestMatcher}'s using a JSONPath expression. - * An instance of this class is typically accessed via - * {@code RequestMatchers.jsonPath(..)}. + * Factory methods for request content {@code RequestMatcher}s using a + * JsonPath expression. + *

An instance of this class is typically accessed via + * {@link MockRestRequestMatchers#jsonPath}. * * @author Rossen Stoyanchev * @since 3.2 @@ -115,7 +116,7 @@ public class JsonPathRequestMatchers { /** - * Abstract base class for JSONPath {@link RequestMatcher}'s. + * Abstract base class for JSONPath {@link RequestMatcher}s. */ private abstract static class AbstractJsonPathRequestMatcher implements RequestMatcher { diff --git a/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java b/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java index 5a80c93859..9637bfc6f1 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java @@ -36,11 +36,12 @@ import static org.hamcrest.MatcherAssert.*; import static org.springframework.test.util.AssertionErrors.*; /** - * Static, factory methods for {@link RequestMatcher} classes. Typically used to + * Static factory methods for {@link RequestMatcher} classes. Typically used to * provide input for {@link MockRestServiceServer#expect(RequestMatcher)}. * - *

Eclipse users: Consider adding this class as a Java editor - * favorite. To navigate, open the Preferences and type "favorites". + *

Eclipse Users

+ *

Consider adding this class as a Java editor favorite. To navigate to + * this setting, open the Preferences and type "favorites". * * @author Craig Walls * @author Rossen Stoyanchev @@ -166,8 +167,8 @@ public abstract class MockRestRequestMatchers { } /** - * Access to request body matchers using a JSONPath expression to + * Access to request body matchers using a + * JsonPath expression to * inspect a specific subset of the body. The JSON path expression can be a * parameterized string using formatting specifiers as defined in * {@link String#format(String, Object...)}. @@ -179,8 +180,8 @@ public abstract class MockRestRequestMatchers { } /** - * Access to request body matchers using a JSONPath expression to + * Access to request body matchers using a + * JsonPath expression to * inspect a specific subset of the body and a Hamcrest match for asserting * the value found at the JSON path. * @param expression the JSON path expression diff --git a/spring-test/src/test/java/org/springframework/test/util/JsonPathExpectationsHelperTests.java b/spring-test/src/test/java/org/springframework/test/util/JsonPathExpectationsHelperTests.java index 7ac96a4729..8f5dc01d21 100644 --- a/spring-test/src/test/java/org/springframework/test/util/JsonPathExpectationsHelperTests.java +++ b/spring-test/src/test/java/org/springframework/test/util/JsonPathExpectationsHelperTests.java @@ -32,21 +32,21 @@ import static org.hamcrest.CoreMatchers.*; public class JsonPathExpectationsHelperTests { private static final String CONTENT = "{" + // - "\"str\": \"foo\", " + // - "\"num\": 5, " + // - "\"bool\": true, " + // - "\"arr\": [\"bar\"], " + // - "\"emptyArray\": [], " + // - "\"colorMap\": {\"red\": \"rojo\"}, " + // - "\"emptyMap\": {} " + // + "'str': 'foo', " + // + "'num': 5, " + // + "'bool': true, " + // + "'arr': [42], " + // + "'emptyArray': [], " + // + "'colorMap': {'red': 'rojo'}, " + // + "'emptyMap': {} " + // "}"; - private static final String SIMPSONS = "{ \"familyMembers\": [ " + // - "{\"name\": \"Homer\" }, " + // - "{\"name\": \"Marge\" }, " + // - "{\"name\": \"Bart\" }, " + // - "{\"name\": \"Lisa\" }, " + // - "{\"name\": \"Maggie\"} " + // + private static final String SIMPSONS = "{ 'familyMembers': [ " + // + "{'name': 'Homer' }, " + // + "{'name': 'Marge' }, " + // + "{'name': 'Bart' }, " + // + "{'name': 'Lisa' }, " + // + "{'name': 'Maggie'} " + // " ] }"; @Rule diff --git a/spring-test/src/test/java/org/springframework/test/web/client/match/JsonPathRequestMatchersTests.java b/spring-test/src/test/java/org/springframework/test/web/client/match/JsonPathRequestMatchersTests.java index 7610337917..1999f52509 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/match/JsonPathRequestMatchersTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/match/JsonPathRequestMatchersTests.java @@ -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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.test.web.client.match; import java.io.IOException; @@ -24,20 +25,20 @@ import org.junit.Test; import org.springframework.mock.http.client.MockClientHttpRequest; /** - * Tests for {@link JsonPathRequestMatchers}. + * Unit tests for {@link JsonPathRequestMatchers}. * * @author Rossen Stoyanchev */ public class JsonPathRequestMatchersTests { - private static final String RESPONSE_CONTENT = "{\"foo\":\"bar\", \"qux\":[\"baz1\",\"baz2\"]}"; + private static final String REQUEST_CONTENT = "{ 'foo': 'bar', 'qux': ['baz1', 'baz2'] }"; private MockClientHttpRequest request; @Before public void setUp() throws IOException { this.request = new MockClientHttpRequest(); - this.request.getBody().write(RESPONSE_CONTENT.getBytes()); + this.request.getBody().write(REQUEST_CONTENT.getBytes()); } @Test @@ -45,7 +46,7 @@ public class JsonPathRequestMatchersTests { new JsonPathRequestMatchers("$.foo").value("bar").match(this.request); } - @Test(expected=AssertionError.class) + @Test(expected = AssertionError.class) public void valueNoMatch() throws Exception { new JsonPathRequestMatchers("$.foo").value("bogus").match(this.request); } @@ -55,7 +56,7 @@ public class JsonPathRequestMatchersTests { new JsonPathRequestMatchers("$.foo").value(Matchers.equalTo("bar")).match(this.request); } - @Test(expected=AssertionError.class) + @Test(expected = AssertionError.class) public void valueMatcherNoMatch() throws Exception { new JsonPathRequestMatchers("$.foo").value(Matchers.equalTo("bogus")).match(this.request); } @@ -65,7 +66,7 @@ public class JsonPathRequestMatchersTests { new JsonPathRequestMatchers("$.foo").exists().match(this.request); } - @Test(expected=AssertionError.class) + @Test(expected = AssertionError.class) public void existsNoMatch() throws Exception { new JsonPathRequestMatchers("$.bogus").exists().match(this.request); } @@ -75,7 +76,7 @@ public class JsonPathRequestMatchersTests { new JsonPathRequestMatchers("$.bogus").doesNotExist().match(this.request); } - @Test(expected=AssertionError.class) + @Test(expected = AssertionError.class) public void doesNotExistNoMatch() throws Exception { new JsonPathRequestMatchers("$.foo").doesNotExist().match(this.request); } @@ -85,7 +86,7 @@ public class JsonPathRequestMatchersTests { new JsonPathRequestMatchers("$.qux").isArray().match(this.request); } - @Test(expected=AssertionError.class) + @Test(expected = AssertionError.class) public void isArrayNoMatch() throws Exception { new JsonPathRequestMatchers("$.bar").isArray().match(this.request); } diff --git a/spring-test/src/test/java/org/springframework/test/web/client/match/RequestMatchersTests.java b/spring-test/src/test/java/org/springframework/test/web/client/match/RequestMatchersTests.java index 98100fdce8..a0b49dcb31 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/match/RequestMatchersTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/match/RequestMatchersTests.java @@ -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. @@ -13,12 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.test.web.client.match; import java.net.URI; import java.util.Arrays; -import org.junit.Before; import org.junit.Test; import org.springframework.http.HttpMethod; @@ -27,19 +27,15 @@ import org.springframework.mock.http.client.MockClientHttpRequest; import static org.hamcrest.Matchers.*; /** - * Tests for {@link MockRestRequestMatchers}. + * Unit tests for {@link MockRestRequestMatchers}. * * @author Craig Walls * @author Rossen Stoyanchev */ public class RequestMatchersTests { - private MockClientHttpRequest request; + private final MockClientHttpRequest request = new MockClientHttpRequest(); - @Before - public void setUp() { - this.request = new MockClientHttpRequest(); - } @Test public void requestTo() throws Exception { @@ -48,7 +44,7 @@ public class RequestMatchersTests { MockRestRequestMatchers.requestTo("http://foo.com/bar").match(this.request); } - @Test(expected=AssertionError.class) + @Test(expected = AssertionError.class) public void requestToNoMatch() throws Exception { this.request.setURI(new URI("http://foo.com/bar")); @@ -69,7 +65,7 @@ public class RequestMatchersTests { MockRestRequestMatchers.method(HttpMethod.GET).match(this.request); } - @Test(expected=AssertionError.class) + @Test(expected = AssertionError.class) public void methodNoMatch() throws Exception { this.request.setMethod(HttpMethod.POST); @@ -83,12 +79,12 @@ public class RequestMatchersTests { MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request); } - @Test(expected=AssertionError.class) + @Test(expected = AssertionError.class) public void headerMissing() throws Exception { MockRestRequestMatchers.header("foo", "bar").match(this.request); } - @Test(expected=AssertionError.class) + @Test(expected = AssertionError.class) public void headerMissingValue() throws Exception { this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); @@ -104,13 +100,13 @@ public class RequestMatchersTests { } @SuppressWarnings("unchecked") - @Test(expected=AssertionError.class) + @Test(expected = AssertionError.class) public void headerContainsWithMissingHeader() throws Exception { MockRestRequestMatchers.header("foo", containsString("baz")).match(this.request); } @SuppressWarnings("unchecked") - @Test(expected=AssertionError.class) + @Test(expected = AssertionError.class) public void headerContainsWithMissingValue() throws Exception { this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); @@ -124,12 +120,12 @@ public class RequestMatchersTests { MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request); } - @Test(expected=AssertionError.class) + @Test(expected = AssertionError.class) public void headersWithMissingHeader() throws Exception { MockRestRequestMatchers.header("foo", "bar").match(this.request); } - @Test(expected=AssertionError.class) + @Test(expected = AssertionError.class) public void headersWithMissingValue() throws Exception { this.request.getHeaders().put("foo", Arrays.asList("bar")); diff --git a/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/JsonPathRequestMatcherTests.java b/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/JsonPathRequestMatcherTests.java index 71ca8ed2cf..4b666b1577 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/JsonPathRequestMatcherTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/JsonPathRequestMatcherTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.test.web.client.samples.matchers; import java.net.URI; @@ -37,9 +38,10 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat /** * Examples of defining expectations on JSON request content with - * JSONPath expressions. + * JsonPath expressions. * * @author Rossen Stoyanchev + * @see org.springframework.test.web.client.match.JsonPathRequestMatchers */ public class JsonPathRequestMatcherTests { diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/result/JsonPathResultMatchersTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/result/JsonPathResultMatchersTests.java index 5612e64a24..5c0fe97354 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/result/JsonPathResultMatchersTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/result/JsonPathResultMatchersTests.java @@ -24,7 +24,7 @@ import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.web.servlet.StubMvcResult; /** - * Tests for {@link JsonPathResultMatchers}. + * Unit tests for {@link JsonPathResultMatchers}. * * @author Rossen Stoyanchev * @author Craig Andrews @@ -32,7 +32,7 @@ import org.springframework.test.web.servlet.StubMvcResult; */ public class JsonPathResultMatchersTests { - private static final String RESPONSE_CONTENT = "{\"foo\": \"bar\", \"qux\": [\"baz\"], \"emptyArray\": [], \"icanhaz\": true, \"howmanies\": 5, \"cheeseburger\": {\"pickles\": true}, \"emptyMap\": {} }"; + private static final String RESPONSE_CONTENT = "{'foo': 'bar', 'qux': ['baz'], 'emptyArray': [], 'icanhaz': true, 'howmanies': 5, 'cheeseburger': {'pickles': true}, 'emptyMap': {} }"; private static final StubMvcResult stubMvcResult; diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/JsonPathAssertionTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/JsonPathAssertionTests.java index 4dbd4124b8..0b006f5c5c 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/JsonPathAssertionTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/JsonPathAssertionTests.java @@ -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. @@ -22,13 +22,12 @@ import org.junit.Before; import org.junit.Test; import org.springframework.http.MediaType; -import org.springframework.stereotype.Controller; import org.springframework.test.web.Person; import org.springframework.test.web.servlet.MockMvc; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; import static org.hamcrest.Matchers.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; @@ -37,10 +36,9 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; /** * Examples of defining expectations on JSON response content with - * JSONPath expressions. + * JsonPath expressions. * * @author Rossen Stoyanchev - * * @see ContentAssertionTests */ public class JsonPathAssertionTests { @@ -73,7 +71,6 @@ public class JsonPathAssertionTests { .andExpect(jsonPath("$.composers[1]").exists()) .andExpect(jsonPath("$.composers[2]").exists()) .andExpect(jsonPath("$.composers[3]").exists()); - } @Test @@ -120,11 +117,11 @@ public class JsonPathAssertionTests { } - @Controller + @RestController private class MusicController { - @RequestMapping(value="/music/people") - public @ResponseBody MultiValueMap get() { + @RequestMapping("/music/people") + public MultiValueMap get() { MultiValueMap map = new LinkedMultiValueMap(); map.add("composers", new Person("Johann Sebastian Bach"));