From ba94a1216cc16a53c71ed0efb734fa7b84d34d5a Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 26 Sep 2020 16:06:31 +0200 Subject: [PATCH] Deprecate HttpStatus.Series.valueOf(HttpStatus) and polish See gh-22366 --- .../org/springframework/http/HttpStatus.java | 46 ++++++++++--------- .../springframework/http/HttpStatusTests.java | 28 +++++------ 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/HttpStatus.java b/spring-web/src/main/java/org/springframework/http/HttpStatus.java index 9f80c75282..2153139007 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpStatus.java +++ b/spring-web/src/main/java/org/springframework/http/HttpStatus.java @@ -418,14 +418,14 @@ public enum HttpStatus { private final int value; - private final String reasonPhrase; - private final Series series; + private final String reasonPhrase; + HttpStatus(int value, Series series, String reasonPhrase) { this.value = value; - this.reasonPhrase = reasonPhrase; this.series = series; + this.reasonPhrase = reasonPhrase; } @@ -436,13 +436,6 @@ public enum HttpStatus { return this.value; } - /** - * Return the reason phrase of this status code. - */ - public String getReasonPhrase() { - return this.reasonPhrase; - } - /** * Return the HTTP status series of this status code. * @see HttpStatus.Series @@ -451,10 +444,17 @@ public enum HttpStatus { return this.series; } + /** + * Return the reason phrase of this status code. + */ + public String getReasonPhrase() { + return this.reasonPhrase; + } + /** * Whether this status code is in the HTTP series * {@link org.springframework.http.HttpStatus.Series#INFORMATIONAL}. - * This is a shortcut for checking the value of {@link #series()}. + *

This is a shortcut for checking the value of {@link #series()}. * @since 4.0 * @see #series() */ @@ -465,7 +465,7 @@ public enum HttpStatus { /** * Whether this status code is in the HTTP series * {@link org.springframework.http.HttpStatus.Series#SUCCESSFUL}. - * This is a shortcut for checking the value of {@link #series()}. + *

This is a shortcut for checking the value of {@link #series()}. * @since 4.0 * @see #series() */ @@ -476,7 +476,7 @@ public enum HttpStatus { /** * Whether this status code is in the HTTP series * {@link org.springframework.http.HttpStatus.Series#REDIRECTION}. - * This is a shortcut for checking the value of {@link #series()}. + *

This is a shortcut for checking the value of {@link #series()}. * @since 4.0 * @see #series() */ @@ -487,7 +487,7 @@ public enum HttpStatus { /** * Whether this status code is in the HTTP series * {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR}. - * This is a shortcut for checking the value of {@link #series()}. + *

This is a shortcut for checking the value of {@link #series()}. * @since 4.0 * @see #series() */ @@ -498,7 +498,7 @@ public enum HttpStatus { /** * Whether this status code is in the HTTP series * {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR}. - * This is a shortcut for checking the value of {@link #series()}. + *

This is a shortcut for checking the value of {@link #series()}. * @since 4.0 * @see #series() */ @@ -510,7 +510,7 @@ public enum HttpStatus { * Whether this status code is in the HTTP series * {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR} or * {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR}. - * This is a shortcut for checking the value of {@link #series()}. + *

This is a shortcut for checking the value of {@link #series()}. * @since 5.0 * @see #is4xxClientError() * @see #is5xxServerError() @@ -529,7 +529,7 @@ public enum HttpStatus { /** - * Return the enum constant of this type with the specified numeric value. + * Return the {@code HttpStatus} enum constant with the specified numeric value. * @param statusCode the numeric value of the enum to be returned * @return the enum constant with the specified numeric value * @throws IllegalArgumentException if this enum has no constant for the specified numeric value @@ -585,18 +585,20 @@ public enum HttpStatus { } /** - * Return the enum constant of this type with the corresponding series. - * @param status a standard HTTP status enum value - * @return the enum constant of this type with the corresponding series + * Return the {@code Series} enum constant for the supplied {@code HttpStatus}. + * @param status a standard HTTP status enum constant + * @return the {@code Series} enum constant for the supplied {@code HttpStatus} + * @deprecated as of 5.3, in favor of invoking {@link HttpStatus#series()} directly */ + @Deprecated public static Series valueOf(HttpStatus status) { return status.series; } /** - * Return the enum constant of this type with the corresponding series. + * Return the {@code Series} enum constant for the supplied status code. * @param statusCode the HTTP status code (potentially non-standard) - * @return the enum constant of this type with the corresponding series + * @return the {@code Series} enum constant for the supplied status code * @throws IllegalArgumentException if this enum has no corresponding constant */ public static Series valueOf(int statusCode) { diff --git a/spring-web/src/test/java/org/springframework/http/HttpStatusTests.java b/spring-web/src/test/java/org/springframework/http/HttpStatusTests.java index 1ee7609d25..cec1f9ad96 100644 --- a/spring-web/src/test/java/org/springframework/http/HttpStatusTests.java +++ b/spring-web/src/test/java/org/springframework/http/HttpStatusTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -27,13 +27,13 @@ import static org.assertj.core.api.Assertions.assertThat; /** * @author Arjen Poutsma */ -public class HttpStatusTests { +class HttpStatusTests { - private Map statusCodes = new LinkedHashMap<>(); + private final Map statusCodes = new LinkedHashMap<>(); @BeforeEach - public void createStatusCodes() { + void createStatusCodes() { statusCodes.put(100, "CONTINUE"); statusCodes.put(101, "SWITCHING_PROTOCOLS"); statusCodes.put(102, "PROCESSING"); @@ -107,7 +107,7 @@ public class HttpStatusTests { @Test - public void fromMapToEnum() { + void fromMapToEnum() { for (Map.Entry entry : statusCodes.entrySet()) { int value = entry.getKey(); HttpStatus status = HttpStatus.valueOf(value); @@ -117,23 +117,25 @@ public class HttpStatusTests { } @Test - public void fromEnumToMap() { + void fromEnumToMap() { for (HttpStatus status : HttpStatus.values()) { - int value = status.value(); - if (value == 302 || value == 413 || value == 414) { + int code = status.value(); + // The following status codes have more than one corresponding HttpStatus enum constant. + if (code == 302 || code == 413 || code == 414) { continue; } - assertThat(statusCodes.containsKey(value)).as("Map has no value for [" + value + "]").isTrue(); - assertThat(status.name()).as("Invalid name for [" + value + "]").isEqualTo(statusCodes.get(value)); + assertThat(statusCodes).as("Map has no value for [" + code + "]").containsKey(code); + assertThat(status.name()).as("Invalid name for [" + code + "]").isEqualTo(statusCodes.get(code)); } } @Test - public void allStatusSeriesShouldMatchExpectations() { - // a series of a status is manually set, make sure it is the correct one + void allStatusSeriesShouldMatchExpectations() { + // The Series of an HttpStatus is set manually, so we make sure it is the correct one. for (HttpStatus status : HttpStatus.values()) { HttpStatus.Series expectedSeries = HttpStatus.Series.valueOf(status.value()); - assertThat(expectedSeries).isEqualTo(status.series()); + assertThat(status.series()).isEqualTo(expectedSeries); } } + }