Implemented StatusCode bean. Closes #5
This commit is contained in:
parent
12936688ae
commit
95f4c9714e
6 changed files with 306 additions and 15 deletions
149
src/main/java/net/thauvin/erik/httpstatus/StatusCode.java
Normal file
149
src/main/java/net/thauvin/erik/httpstatus/StatusCode.java
Normal file
|
@ -0,0 +1,149 @@
|
|||
/*
|
||||
* StatusCode.java
|
||||
*
|
||||
* Copyright (c) 2015-2021, Erik C. Thauvin (erik@thauvin.net)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* Neither the name of this project nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package net.thauvin.erik.httpstatus;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* The <code>StatusCode</code> class implements methods to check the class of a HTTP status code.
|
||||
*
|
||||
* @author <a href="mailto:erik@thauvin.net" target="_blank">Erik C. Thauvin</a>
|
||||
*/
|
||||
public class StatusCode implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
private int code;
|
||||
|
||||
/**
|
||||
* Creates a new statusCode object.
|
||||
*/
|
||||
public StatusCode() {
|
||||
// Default construtor.
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new StatusCode object.
|
||||
*
|
||||
* @param code The status code.
|
||||
*/
|
||||
public StatusCode(final int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the status code.
|
||||
*/
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the reason for the status code.
|
||||
*
|
||||
* @return The reason, or <code>null</code>.
|
||||
*/
|
||||
public String getReason() {
|
||||
return Reasons.getReasonPhrase(code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the status code is a client error.
|
||||
*
|
||||
* @return <code>true</code> if the status code is a client error, <code>false</code> otherwise.
|
||||
*/
|
||||
public boolean isClientError() {
|
||||
return code >= 400 && code < 500;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the status code is a client or server error.
|
||||
*
|
||||
* @return <code>true</code> if the status code is an error, <code>false</code> otherwise.
|
||||
*/
|
||||
public boolean isError() {
|
||||
return code >= 400 && code < 600;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the status code is informational.
|
||||
*
|
||||
* @return <code>true</code> if the status code is informational, <code>false</code> otherwise.
|
||||
*/
|
||||
public boolean isInfo() {
|
||||
return code >= 100 && code < 200;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the status code is a redirect.
|
||||
*
|
||||
* @return <code>true</code> if the status code is a redirect, <code>false</code> otherwise.
|
||||
*/
|
||||
public boolean isRedirect() {
|
||||
return code >= 300 && code < 400;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the status code is a server error.
|
||||
*
|
||||
* @return <code>true</code> if the status code is a server error, <code>false</code> otherwise.
|
||||
*/
|
||||
public boolean isServerError() {
|
||||
return code >= 500 && code < 600;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the status code is a (<code>OK</code>) success.
|
||||
*
|
||||
* @return <code>true</code> if the status code is a success, <code>false</code> otherwise.
|
||||
*/
|
||||
public boolean isSuccess() {
|
||||
return code >= 200 && code < 300;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the status code is valid.
|
||||
*
|
||||
* @return <code>true</code> if the status code is valid, <code>false</code> otherwise.
|
||||
*/
|
||||
public boolean isValid() {
|
||||
return code >= 100 && code < 600;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the status code.
|
||||
*
|
||||
* @param code The HTTP status code.
|
||||
*/
|
||||
public void setCode(final int code) {
|
||||
this.code = code;
|
||||
}
|
||||
}
|
|
@ -79,6 +79,4 @@ public class ReasonTag extends XmlSupport {
|
|||
public void setCode(final int statusCode) {
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* StatusCodeTest.java
|
||||
*
|
||||
* Copyright (c) 2015-2021, Erik C. Thauvin (erik@thauvin.net)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* Neither the name of this project nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package net.thauvin.erik.httpstatus;
|
||||
|
||||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertFalse;
|
||||
import static org.testng.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* StatusCode Tests.
|
||||
*
|
||||
* @author <a href="mailto:erik@thauvin.net" target="_blank">Erik C. Thauvin</a>
|
||||
*/
|
||||
@SuppressFBWarnings("CE_CLASS_ENVY")
|
||||
public class StatusCodeTest {
|
||||
@Test
|
||||
void testStatusCode() {
|
||||
final StatusCode statusCode = new StatusCode(100);
|
||||
|
||||
assertEquals(statusCode.getCode(), 100, "100 is 100");
|
||||
assertTrue(statusCode.isInfo(), "100 is informational");
|
||||
|
||||
statusCode.setCode(200);
|
||||
assertEquals(statusCode.getCode(), 200, "200 is 200");
|
||||
assertTrue(statusCode.isSuccess(), "200 is OK");
|
||||
|
||||
statusCode.setCode(300);
|
||||
assertTrue(statusCode.isRedirect(), "300 is redirect");
|
||||
|
||||
statusCode.setCode(400);
|
||||
assertTrue(statusCode.isClientError(), "400 is client error");
|
||||
assertTrue(statusCode.isError(), "400 is error");
|
||||
|
||||
statusCode.setCode(500);
|
||||
assertTrue(statusCode.isServerError(), "500 is server error");
|
||||
assertTrue(statusCode.isError(), "500 is error");
|
||||
assertEquals(statusCode.getReason(), Reasons.getReasonPhrase(500), "500 reason phrase");
|
||||
assertTrue(statusCode.isValid(), "500 is valid");
|
||||
|
||||
statusCode.setCode(600);
|
||||
assertFalse(statusCode.isValid(), "600 is invalid()");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue