Polishing

This commit is contained in:
Juergen Hoeller 2019-05-21 19:26:39 +02:00
parent ff838fd011
commit 3645281c35
3 changed files with 24 additions and 34 deletions

View File

@ -48,18 +48,18 @@ import static org.mockito.Mockito.verifyZeroInteractions;
*/
public abstract class AbstractReactiveTransactionAspectTests {
protected Method exceptionalMethod;
protected Method getNameMethod;
protected Method setNameMethod;
protected Method exceptionalMethod;
@Before
public void setup() throws Exception {
exceptionalMethod = TestBean.class.getMethod("exceptional", Throwable.class);
getNameMethod = TestBean.class.getMethod("getName");
setNameMethod = TestBean.class.getMethod("setName", String.class);
exceptionalMethod = TestBean.class.getMethod("exceptional", Throwable.class);
}
@ -382,11 +382,11 @@ public abstract class AbstractReactiveTransactionAspectTests {
public interface TestBean {
Mono<Void> exceptional(Throwable t);
Mono<String> getName();
Publisher<Void> setName(String name);
Mono<Void> exceptional(Throwable t);
}
@ -400,7 +400,7 @@ public abstract class AbstractReactiveTransactionAspectTests {
}
@Override
public Mono<Void> setName(String name) {
public Publisher<Void> setName(String name) {
return Mono.fromRunnable(() -> this.name = name);
}

View File

@ -48,31 +48,31 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
/**
* Mock object based tests for transaction aspects.
* True unit test in that it tests how the transaction aspect uses
* the PlatformTransactionManager helper, rather than indirectly
* testing the helper implementation.
* Mock object based tests for transaction aspects. A true unit test in that it
* tests how the transaction aspect uses the PlatformTransactionManager helper,
* rather than indirectly testing the helper implementation.
*
* This is a superclass to allow testing both the AOP Alliance MethodInterceptor
* <p>This is a superclass to allow testing both the AOP Alliance MethodInterceptor
* and the AspectJ aspect.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @since 16.03.2003
*/
public abstract class AbstractTransactionAspectTests {
protected Method exceptionalMethod;
protected Method getNameMethod;
protected Method setNameMethod;
protected Method exceptionalMethod;
@Before
public void setup() throws Exception {
exceptionalMethod = ITestBean.class.getMethod("exceptional", Throwable.class);
getNameMethod = ITestBean.class.getMethod("getName");
setNameMethod = ITestBean.class.getMethod("setName", String.class);
exceptionalMethod = ITestBean.class.getMethod("exceptional", Throwable.class);
}

View File

@ -17,6 +17,7 @@
package org.springframework.http.client;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Locale;
@ -34,6 +35,7 @@ import org.springframework.util.StreamUtils;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@ -71,13 +73,9 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb
assertEquals("Invalid HTTP method", HttpMethod.GET, request.getMethod());
assertEquals("Invalid HTTP URI", uri, request.getURI());
ClientHttpResponse response = request.execute();
try {
try (ClientHttpResponse response = request.execute()) {
assertEquals("Invalid status code", HttpStatus.NOT_FOUND, response.getStatusCode());
}
finally {
response.close();
}
}
@Test
@ -90,7 +88,7 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb
request.getHeaders().add(headerName, headerValue1);
String headerValue2 = "value2";
request.getHeaders().add(headerName, headerValue2);
final byte[] body = "Hello World".getBytes("UTF-8");
final byte[] body = "Hello World".getBytes(StandardCharsets.UTF_8);
request.getHeaders().setContentLength(body.length);
if (request instanceof StreamingHttpOutputMessage) {
@ -101,17 +99,13 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb
StreamUtils.copy(body, request.getBody());
}
ClientHttpResponse response = request.execute();
try {
try (ClientHttpResponse response = request.execute()) {
assertEquals("Invalid status code", HttpStatus.OK, response.getStatusCode());
assertTrue("Header not found", response.getHeaders().containsKey(headerName));
assertEquals("Header value not found", Arrays.asList(headerValue1, headerValue2),
response.getHeaders().get(headerName));
byte[] result = FileCopyUtils.copyToByteArray(response.getBody());
assertTrue("Invalid body", Arrays.equals(body, result));
}
finally {
response.close();
assertArrayEquals("Invalid body", body, result);
}
}
@ -119,7 +113,7 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb
public void multipleWrites() throws Exception {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.POST);
final byte[] body = "Hello World".getBytes("UTF-8");
final byte[] body = "Hello World".getBytes(StandardCharsets.UTF_8);
if (request instanceof StreamingHttpOutputMessage) {
StreamingHttpOutputMessage streamingRequest = (StreamingHttpOutputMessage) request;
streamingRequest.setBody(outputStream -> {
@ -142,10 +136,10 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/status/ok"), HttpMethod.POST);
request.getHeaders().add("MyHeader", "value");
byte[] body = "Hello World".getBytes("UTF-8");
byte[] body = "Hello World".getBytes(StandardCharsets.UTF_8);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> {
FileCopyUtils.copy(body, request.getBody());
try(ClientHttpResponse response = request.execute()) {
try (ClientHttpResponse response = request.execute()) {
request.getHeaders().add("MyHeader", "value");
}
});
@ -190,13 +184,9 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb
URI uri = new URI(baseUrl + "/params?param1=value&param2=value1&param2=value2");
ClientHttpRequest request = factory.createRequest(uri, HttpMethod.GET);
ClientHttpResponse response = request.execute();
try {
try (ClientHttpResponse response = request.execute()) {
assertEquals("Invalid status code", HttpStatus.OK, response.getStatusCode());
}
finally {
response.close();
}
}
}