Skip to content

Commit

Permalink
Polish tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rebwon authored and Zoran0104 committed Aug 20, 2021
1 parent d168748 commit 0f0de50
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
Expand Down Expand Up @@ -224,8 +224,7 @@ public void testQueryNoPersonsSharedNotTransactional() {
q.setFlushMode(FlushModeType.AUTO);
List<Person> people = q.getResultList();
assertThat(people.size()).isEqualTo(0);
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
q.getSingleResult())
assertThatExceptionOfType(Exception.class).isThrownBy(q::getSingleResult)
.withMessageContaining("closed");
// We would typically expect an IllegalStateException, but Hibernate throws a
// PersistenceException. So we assert the contents of the exception message instead.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
Expand Down Expand Up @@ -31,15 +31,15 @@
public class HttpEntityTests {

@Test
public void noHeaders() {
void noHeaders() {
String body = "foo";
HttpEntity<String> entity = new HttpEntity<>(body);
assertThat(entity.getBody()).isSameAs(body);
assertThat(entity.getHeaders().isEmpty()).isTrue();
}

@Test
public void httpHeaders() {
void httpHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
String body = "foo";
Expand All @@ -50,7 +50,7 @@ public void httpHeaders() {
}

@Test
public void multiValueMap() {
void multiValueMap() {
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.set("Content-Type", "text/plain");
String body = "foo";
Expand All @@ -61,30 +61,30 @@ public void multiValueMap() {
}

@Test
public void testEquals() {
void testEquals() {
MultiValueMap<String, String> map1 = new LinkedMultiValueMap<>();
map1.set("Content-Type", "text/plain");

MultiValueMap<String, String> map2 = new LinkedMultiValueMap<>();
map2.set("Content-Type", "application/json");

assertThat(new HttpEntity<>().equals(new HttpEntity<Object>())).isTrue();
assertThat(new HttpEntity<>(map1).equals(new HttpEntity<Object>())).isFalse();
assertThat(new HttpEntity<>().equals(new HttpEntity<Object>(map2))).isFalse();
assertThat(new HttpEntity<>().equals(new HttpEntity<>())).isTrue();
assertThat(new HttpEntity<>(map1).equals(new HttpEntity<>())).isFalse();
assertThat(new HttpEntity<>().equals(new HttpEntity<>(map2))).isFalse();

assertThat(new HttpEntity<>(map1).equals(new HttpEntity<Object>(map1))).isTrue();
assertThat(new HttpEntity<>(map1).equals(new HttpEntity<Object>(map2))).isFalse();
assertThat(new HttpEntity<>(map1).equals(new HttpEntity<>(map1))).isTrue();
assertThat(new HttpEntity<>(map1).equals(new HttpEntity<>(map2))).isFalse();

assertThat(new HttpEntity<String>(null, null).equals(new HttpEntity<String>(null, null))).isTrue();
assertThat(new HttpEntity<>("foo", null).equals(new HttpEntity<String>(null, null))).isFalse();
assertThat(new HttpEntity<String>(null, null).equals(new HttpEntity<>("bar", null))).isFalse();

assertThat(new HttpEntity<>("foo", map1).equals(new HttpEntity<String>("foo", map1))).isTrue();
assertThat(new HttpEntity<>("foo", map1).equals(new HttpEntity<String>("bar", map1))).isFalse();
assertThat(new HttpEntity<>("foo", map1).equals(new HttpEntity<>("foo", map1))).isTrue();
assertThat(new HttpEntity<>("foo", map1).equals(new HttpEntity<>("bar", map1))).isFalse();
}

@Test
public void responseEntity() {
void responseEntity() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
String body = "foo";
Expand All @@ -104,7 +104,7 @@ public void responseEntity() {
}

@Test
public void requestEntity() throws Exception {
void requestEntity() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
String body = "foo";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
Expand Down Expand Up @@ -45,20 +45,20 @@ public class ServletServerHttpRequestTests {


@BeforeEach
public void create() {
void create() {
mockRequest = new MockHttpServletRequest();
request = new ServletServerHttpRequest(mockRequest);
}


@Test
public void getMethod() {
void getMethod() {
mockRequest.setMethod("POST");
assertThat(request.getMethod()).as("Invalid method").isEqualTo(HttpMethod.POST);
}

@Test
public void getUriForSimplePath() throws URISyntaxException {
void getUriForSimplePath() throws URISyntaxException {
URI uri = new URI("https://example.com/path");
mockRequest.setScheme(uri.getScheme());
mockRequest.setServerName(uri.getHost());
Expand All @@ -69,7 +69,7 @@ public void getUriForSimplePath() throws URISyntaxException {
}

@Test
public void getUriWithQueryString() throws URISyntaxException {
void getUriWithQueryString() throws URISyntaxException {
URI uri = new URI("https://example.com/path?query");
mockRequest.setScheme(uri.getScheme());
mockRequest.setServerName(uri.getHost());
Expand All @@ -80,7 +80,7 @@ public void getUriWithQueryString() throws URISyntaxException {
}

@Test // SPR-16414
public void getUriWithQueryParam() throws URISyntaxException {
void getUriWithQueryParam() throws URISyntaxException {
mockRequest.setScheme("https");
mockRequest.setServerPort(443);
mockRequest.setServerName("example.com");
Expand All @@ -90,7 +90,7 @@ public void getUriWithQueryParam() throws URISyntaxException {
}

@Test // SPR-16414
public void getUriWithMalformedQueryParam() throws URISyntaxException {
void getUriWithMalformedQueryParam() throws URISyntaxException {
mockRequest.setScheme("https");
mockRequest.setServerPort(443);
mockRequest.setServerName("example.com");
Expand All @@ -100,7 +100,7 @@ public void getUriWithMalformedQueryParam() throws URISyntaxException {
}

@Test // SPR-13876
public void getUriWithEncoding() throws URISyntaxException {
void getUriWithEncoding() throws URISyntaxException {
URI uri = new URI("https://example.com/%E4%B8%AD%E6%96%87" +
"?redirect=https%3A%2F%2Fgithub.com%2Fspring-projects%2Fspring-framework");
mockRequest.setScheme(uri.getScheme());
Expand All @@ -112,7 +112,7 @@ public void getUriWithEncoding() throws URISyntaxException {
}

@Test
public void getHeaders() {
void getHeaders() {
String headerName = "MyHeader";
String headerValue1 = "value1";
String headerValue2 = "value2";
Expand All @@ -132,7 +132,7 @@ public void getHeaders() {
}

@Test
public void getHeadersWithEmptyContentTypeAndEncoding() {
void getHeadersWithEmptyContentTypeAndEncoding() {
String headerName = "MyHeader";
String headerValue1 = "value1";
String headerValue2 = "value2";
Expand All @@ -152,25 +152,26 @@ public void getHeadersWithEmptyContentTypeAndEncoding() {
}

@Test
public void getBody() throws IOException {
byte[] content = "Hello World".getBytes("UTF-8");
void getBody() throws IOException {
byte[] content = "Hello World".getBytes(StandardCharsets.UTF_8);
mockRequest.setContent(content);

byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
assertThat(result).as("Invalid content returned").isEqualTo(content);
}

@Test
public void getFormBody() throws IOException {
void getFormBody() throws IOException {
// Charset (SPR-8676)
mockRequest.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
mockRequest.setMethod("POST");
mockRequest.addParameter("name 1", "value 1");
mockRequest.addParameter("name 2", new String[] {"value 2+1", "value 2+2"});
mockRequest.addParameter("name 2", "value 2+1", "value 2+2");
mockRequest.addParameter("name 3", (String) null);

byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
byte[] content = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes("UTF-8");
byte[] content = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(
StandardCharsets.UTF_8);
assertThat(result).as("Invalid content returned").isEqualTo(content);
}

Expand Down

0 comments on commit 0f0de50

Please sign in to comment.