Skip to content

Commit 6bfe0c0

Browse files
committed
Polish
1 parent 59c88eb commit 6bfe0c0

File tree

1 file changed

+71
-83
lines changed

1 file changed

+71
-83
lines changed

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessorMockTests.java

Lines changed: 71 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import java.net.URI;
2121
import java.nio.charset.Charset;
2222
import java.text.SimpleDateFormat;
23+
import java.util.ArrayList;
2324
import java.util.Collections;
2425
import java.util.Date;
26+
import java.util.List;
2527
import java.util.Locale;
2628
import java.util.TimeZone;
2729

@@ -48,9 +50,18 @@
4850
import org.springframework.web.context.request.ServletWebRequest;
4951
import org.springframework.web.method.support.ModelAndViewContainer;
5052

51-
import static org.junit.Assert.*;
52-
import static org.mockito.BDDMockito.*;
53-
import static org.springframework.web.servlet.HandlerMapping.*;
53+
import static org.junit.Assert.assertEquals;
54+
import static org.junit.Assert.assertFalse;
55+
import static org.junit.Assert.assertTrue;
56+
import static org.junit.Assert.fail;
57+
import static org.mockito.BDDMockito.any;
58+
import static org.mockito.BDDMockito.eq;
59+
import static org.mockito.BDDMockito.given;
60+
import static org.mockito.BDDMockito.isA;
61+
import static org.mockito.BDDMockito.mock;
62+
import static org.mockito.BDDMockito.reset;
63+
import static org.mockito.BDDMockito.verify;
64+
import static org.springframework.web.servlet.HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE;
5465

5566
/**
5667
* Test fixture for {@link HttpEntityMethodProcessor} delegating to a mock
@@ -96,11 +107,14 @@ public void setUp() throws Exception {
96107

97108
messageConverter = mock(HttpMessageConverter.class);
98109
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
99-
100-
processor = new HttpEntityMethodProcessor(Collections.<HttpMessageConverter<?>>singletonList(messageConverter));
110+
List<HttpMessageConverter<?>> converters = new ArrayList<>();
111+
converters.add(messageConverter);
112+
processor = new HttpEntityMethodProcessor(converters);
101113
reset(messageConverter);
102114

103-
Method handle1 = getClass().getMethod("handle1", HttpEntity.class, ResponseEntity.class, Integer.TYPE, RequestEntity.class);
115+
Method handle1 = getClass().getMethod("handle1", HttpEntity.class, ResponseEntity.class,
116+
Integer.TYPE, RequestEntity.class);
117+
104118
paramHttpEntity = new MethodParameter(handle1, 0);
105119
paramRequestEntity = new MethodParameter(handle1, 3);
106120
paramResponseEntity = new MethodParameter(handle1, 1);
@@ -205,14 +219,12 @@ public void resolveArgumentNoContentType() throws Exception {
205219
@Test
206220
public void handleReturnValue() throws Exception {
207221
String body = "Foo";
208-
ResponseEntity<String> returnValue = new ResponseEntity<String>(body, HttpStatus.OK);
222+
ResponseEntity<String> returnValue = new ResponseEntity<>(body, HttpStatus.OK);
209223

210224
MediaType accepted = MediaType.TEXT_PLAIN;
211225
servletRequest.addHeader("Accept", accepted.toString());
212226

213-
given(messageConverter.canWrite(String.class, null)).willReturn(true);
214-
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
215-
given(messageConverter.canWrite(String.class, accepted)).willReturn(true);
227+
initStringMessageConversion(accepted);
216228

217229
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
218230

@@ -223,7 +235,7 @@ public void handleReturnValue() throws Exception {
223235
@Test
224236
public void handleReturnValueProduces() throws Exception {
225237
String body = "Foo";
226-
ResponseEntity<String> returnValue = new ResponseEntity<String>(body, HttpStatus.OK);
238+
ResponseEntity<String> returnValue = new ResponseEntity<>(body, HttpStatus.OK);
227239

228240
servletRequest.addHeader("Accept", "text/*");
229241
servletRequest.setAttribute(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(MediaType.TEXT_HTML));
@@ -263,7 +275,7 @@ public void handleReturnValueWithResponseBodyAdvice() throws Exception {
263275
@Test(expected = HttpMediaTypeNotAcceptableException.class)
264276
public void handleReturnValueNotAcceptable() throws Exception {
265277
String body = "Foo";
266-
ResponseEntity<String> returnValue = new ResponseEntity<String>(body, HttpStatus.OK);
278+
ResponseEntity<String> returnValue = new ResponseEntity<>(body, HttpStatus.OK);
267279

268280
MediaType accepted = MediaType.APPLICATION_ATOM_XML;
269281
servletRequest.addHeader("Accept", accepted.toString());
@@ -280,7 +292,7 @@ public void handleReturnValueNotAcceptable() throws Exception {
280292
@Test(expected = HttpMediaTypeNotAcceptableException.class)
281293
public void handleReturnValueNotAcceptableProduces() throws Exception {
282294
String body = "Foo";
283-
ResponseEntity<String> returnValue = new ResponseEntity<String>(body, HttpStatus.OK);
295+
ResponseEntity<String> returnValue = new ResponseEntity<>(body, HttpStatus.OK);
284296

285297
MediaType accepted = MediaType.TEXT_PLAIN;
286298
servletRequest.addHeader("Accept", accepted.toString());
@@ -298,18 +310,18 @@ public void handleReturnValueNotAcceptableProduces() throws Exception {
298310

299311
@Test(expected=HttpMediaTypeNotAcceptableException.class)
300312
public void handleReturnValueNotAcceptableParseError() throws Exception {
301-
ResponseEntity<String> returnValue = new ResponseEntity<String>("Body", HttpStatus.ACCEPTED);
313+
ResponseEntity<String> returnValue = new ResponseEntity<>("Body", HttpStatus.ACCEPTED);
302314
servletRequest.addHeader("Accept", "01");
303315

304316
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
305317
fail("Expected exception");
306318
}
307319

308320
@Test
309-
public void responseHeaderNoBody() throws Exception {
321+
public void handleReturnValueResponseHeaderNoBody() throws Exception {
310322
HttpHeaders headers = new HttpHeaders();
311323
headers.set("headerName", "headerValue");
312-
ResponseEntity<String> returnValue = new ResponseEntity<String>(headers, HttpStatus.ACCEPTED);
324+
ResponseEntity<String> returnValue = new ResponseEntity<>(headers, HttpStatus.ACCEPTED);
313325

314326
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
315327

@@ -318,15 +330,12 @@ public void responseHeaderNoBody() throws Exception {
318330
}
319331

320332
@Test
321-
public void responseHeaderAndBody() throws Exception {
333+
public void handleReturnValueResponseHeaderAndBody() throws Exception {
322334
HttpHeaders responseHeaders = new HttpHeaders();
323335
responseHeaders.set("header", "headerValue");
324-
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.ACCEPTED);
325-
326-
given(messageConverter.canWrite(String.class, null)).willReturn(true);
327-
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
328-
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
336+
ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.ACCEPTED);
329337

338+
initStringMessageConversion(MediaType.TEXT_PLAIN);
330339
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
331340

332341
ArgumentCaptor<HttpOutputMessage> outputMessage = ArgumentCaptor.forClass(HttpOutputMessage.class);
@@ -336,18 +345,15 @@ public void responseHeaderAndBody() throws Exception {
336345
}
337346

338347
@Test
339-
public void handleReturnTypeLastModified() throws Exception {
348+
public void handleReturnValueLastModified() throws Exception {
340349
long currentTime = new Date().getTime();
341350
long oneMinuteAgo = currentTime - (1000 * 60);
342351
servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, dateFormat.format(currentTime));
343352
HttpHeaders responseHeaders = new HttpHeaders();
344353
responseHeaders.setDate(HttpHeaders.LAST_MODIFIED, oneMinuteAgo);
345-
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.OK);
346-
347-
given(messageConverter.canWrite(String.class, null)).willReturn(true);
348-
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
349-
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
354+
ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.OK);
350355

356+
initStringMessageConversion(MediaType.TEXT_PLAIN);
351357
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
352358

353359
assertResponseNotModified();
@@ -356,17 +362,14 @@ public void handleReturnTypeLastModified() throws Exception {
356362
}
357363

358364
@Test
359-
public void handleReturnTypeEtag() throws Exception {
365+
public void handleReturnValueEtag() throws Exception {
360366
String etagValue = "\"deadb33f8badf00d\"";
361367
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue);
362368
HttpHeaders responseHeaders = new HttpHeaders();
363369
responseHeaders.set(HttpHeaders.ETAG, etagValue);
364-
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.OK);
365-
366-
given(messageConverter.canWrite(String.class, null)).willReturn(true);
367-
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
368-
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
370+
ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.OK);
369371

372+
initStringMessageConversion(MediaType.TEXT_PLAIN);
370373
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
371374

372375
assertResponseNotModified();
@@ -375,7 +378,7 @@ public void handleReturnTypeEtag() throws Exception {
375378
}
376379

377380
@Test
378-
public void handleReturnTypeETagAndLastModified() throws Exception {
381+
public void handleReturnValueETagAndLastModified() throws Exception {
379382
long currentTime = new Date().getTime();
380383
long oneMinuteAgo = currentTime - (1000 * 60);
381384
String etagValue = "\"deadb33f8badf00d\"";
@@ -384,12 +387,9 @@ public void handleReturnTypeETagAndLastModified() throws Exception {
384387
HttpHeaders responseHeaders = new HttpHeaders();
385388
responseHeaders.setDate(HttpHeaders.LAST_MODIFIED, oneMinuteAgo);
386389
responseHeaders.set(HttpHeaders.ETAG, etagValue);
387-
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.OK);
388-
389-
given(messageConverter.canWrite(String.class, null)).willReturn(true);
390-
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
391-
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
390+
ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.OK);
392391

392+
initStringMessageConversion(MediaType.TEXT_PLAIN);
393393
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
394394

395395
assertResponseNotModified();
@@ -400,19 +400,16 @@ public void handleReturnTypeETagAndLastModified() throws Exception {
400400
}
401401

402402
@Test
403-
public void handleReturnTypeNotModified() throws Exception {
403+
public void handleReturnValueNotModified() throws Exception {
404404
long currentTime = new Date().getTime();
405405
long oneMinuteAgo = currentTime - (1000 * 60);
406406
String etagValue = "\"deadb33f8badf00d\"";
407407
HttpHeaders responseHeaders = new HttpHeaders();
408408
responseHeaders.setDate(HttpHeaders.LAST_MODIFIED, oneMinuteAgo);
409409
responseHeaders.set(HttpHeaders.ETAG, etagValue);
410-
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.NOT_MODIFIED);
411-
412-
given(messageConverter.canWrite(String.class, null)).willReturn(true);
413-
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
414-
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
410+
ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.NOT_MODIFIED);
415411

412+
initStringMessageConversion(MediaType.TEXT_PLAIN);
416413
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
417414

418415
assertResponseNotModified();
@@ -422,14 +419,8 @@ public void handleReturnTypeNotModified() throws Exception {
422419
assertEquals(etagValue, servletResponse.getHeader(HttpHeaders.ETAG));
423420
}
424421

425-
private void assertResponseNotModified() {
426-
assertTrue(mavContainer.isRequestHandled());
427-
assertEquals(HttpStatus.NOT_MODIFIED.value(), servletResponse.getStatus());
428-
assertEquals(0, servletResponse.getContentAsByteArray().length);
429-
}
430-
431422
@Test
432-
public void handleReturnTypeChangedETagAndLastModified() throws Exception {
423+
public void handleReturnValueChangedETagAndLastModified() throws Exception {
433424
long currentTime = new Date().getTime();
434425
long oneMinuteAgo = currentTime - (1000 * 60);
435426
String etagValue = "\"deadb33f8badf00d\"";
@@ -439,12 +430,9 @@ public void handleReturnTypeChangedETagAndLastModified() throws Exception {
439430
HttpHeaders responseHeaders = new HttpHeaders();
440431
responseHeaders.setDate(HttpHeaders.LAST_MODIFIED, oneMinuteAgo);
441432
responseHeaders.set(HttpHeaders.ETAG, changedEtagValue);
442-
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.OK);
443-
444-
given(messageConverter.canWrite(String.class, null)).willReturn(true);
445-
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
446-
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
433+
ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.OK);
447434

435+
initStringMessageConversion(MediaType.TEXT_PLAIN);
448436
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
449437

450438
assertTrue(mavContainer.isRequestHandled());
@@ -458,19 +446,16 @@ public void handleReturnTypeChangedETagAndLastModified() throws Exception {
458446

459447
// SPR-13496
460448
@Test
461-
public void handleReturnTypePostRequestWithIfNotModified() throws Exception {
449+
public void handleReturnValuePostRequestWithIfNotModified() throws Exception {
462450
String wildcardValue = "*";
463451
String etagValue = "\"some-etag\"";
464452
servletRequest.setMethod("POST");
465453
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, wildcardValue);
466454
HttpHeaders responseHeaders = new HttpHeaders();
467455
responseHeaders.set(HttpHeaders.ETAG, etagValue);
468-
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.OK);
469-
470-
given(messageConverter.canWrite(String.class, null)).willReturn(true);
471-
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
472-
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
456+
ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.OK);
473457

458+
initStringMessageConversion(MediaType.TEXT_PLAIN);
474459
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
475460

476461
assertResponseOkWithBody("body");
@@ -480,18 +465,15 @@ public void handleReturnTypePostRequestWithIfNotModified() throws Exception {
480465

481466
// SPR-13626
482467
@Test
483-
public void handleReturnTypeGetIfNoneMatchWildcard() throws Exception {
468+
public void handleReturnValueGetIfNoneMatchWildcard() throws Exception {
484469
String wildcardValue = "*";
485470
String etagValue = "\"some-etag\"";
486471
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, wildcardValue);
487472
HttpHeaders responseHeaders = new HttpHeaders();
488473
responseHeaders.set(HttpHeaders.ETAG, etagValue);
489-
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.OK);
490-
491-
given(messageConverter.canWrite(String.class, null)).willReturn(true);
492-
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
493-
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
474+
ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.OK);
494475

476+
initStringMessageConversion(MediaType.TEXT_PLAIN);
495477
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
496478

497479
assertResponseOkWithBody("body");
@@ -501,18 +483,15 @@ public void handleReturnTypeGetIfNoneMatchWildcard() throws Exception {
501483

502484
// SPR-13626
503485
@Test
504-
public void handleReturnTypeIfNoneMatchIfMatch() throws Exception {
486+
public void handleReturnValueIfNoneMatchIfMatch() throws Exception {
505487
String etagValue = "\"some-etag\"";
506488
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue);
507489
servletRequest.addHeader(HttpHeaders.IF_MATCH, "ifmatch");
508490
HttpHeaders responseHeaders = new HttpHeaders();
509491
responseHeaders.set(HttpHeaders.ETAG, etagValue);
510-
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.OK);
511-
512-
given(messageConverter.canWrite(String.class, null)).willReturn(true);
513-
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
514-
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
492+
ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.OK);
515493

494+
initStringMessageConversion(MediaType.TEXT_PLAIN);
516495
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
517496

518497
assertResponseOkWithBody("body");
@@ -522,30 +501,39 @@ public void handleReturnTypeIfNoneMatchIfMatch() throws Exception {
522501

523502
// SPR-13626
524503
@Test
525-
public void handleReturnTypeIfNoneMatchIfUnmodifiedSince() throws Exception {
504+
public void handleReturnValueIfNoneMatchIfUnmodifiedSince() throws Exception {
526505
String etagValue = "\"some-etag\"";
527506
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue);
528507
servletRequest.addHeader(HttpHeaders.IF_UNMODIFIED_SINCE, dateFormat.format(new Date().getTime()));
529508
HttpHeaders responseHeaders = new HttpHeaders();
530509
responseHeaders.set(HttpHeaders.ETAG, etagValue);
531-
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.OK);
532-
533-
given(messageConverter.canWrite(String.class, null)).willReturn(true);
534-
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
535-
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
510+
ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.OK);
536511

512+
initStringMessageConversion(MediaType.TEXT_PLAIN);
537513
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
538514

539515
assertResponseOkWithBody("body");
540516
assertEquals(1, servletResponse.getHeaderValues(HttpHeaders.ETAG).size());
541517
assertEquals(etagValue, servletResponse.getHeader(HttpHeaders.ETAG));
542518
}
543519

520+
private void initStringMessageConversion(MediaType accepted) {
521+
given(messageConverter.canWrite(String.class, null)).willReturn(true);
522+
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
523+
given(messageConverter.canWrite(String.class, accepted)).willReturn(true);
524+
}
525+
526+
private void assertResponseNotModified() {
527+
assertTrue(mavContainer.isRequestHandled());
528+
assertEquals(HttpStatus.NOT_MODIFIED.value(), servletResponse.getStatus());
529+
assertEquals(0, servletResponse.getContentAsByteArray().length);
530+
}
531+
544532
private void assertResponseOkWithBody(String body) throws Exception {
545533
assertTrue(mavContainer.isRequestHandled());
546534
assertEquals(HttpStatus.OK.value(), servletResponse.getStatus());
547535
ArgumentCaptor<HttpOutputMessage> outputMessage = ArgumentCaptor.forClass(HttpOutputMessage.class);
548-
verify(messageConverter).write(eq("body"), eq(MediaType.TEXT_PLAIN), outputMessage.capture());
536+
verify(messageConverter).write(eq(body), eq(MediaType.TEXT_PLAIN), outputMessage.capture());
549537
}
550538

551539
@SuppressWarnings("unused")

0 commit comments

Comments
 (0)