Compare commits
3 commits
ec75fc4690
...
f596469b5b
Author | SHA1 | Date | |
---|---|---|---|
f596469b5b | |||
7ee45b34a4 | |||
96497f7f27 |
13 changed files with 94 additions and 75 deletions
2
.github/workflows/bld.yml
vendored
2
.github/workflows/bld.yml
vendored
|
@ -11,7 +11,7 @@ jobs:
|
|||
|
||||
strategy:
|
||||
matrix:
|
||||
java-version: [17, 21, 22]
|
||||
java-version: [17, 21, 23]
|
||||
|
||||
steps:
|
||||
- name: Checkout source repository
|
||||
|
|
|
@ -7,9 +7,9 @@
|
|||
<!-- BEST PRACTICES -->
|
||||
<rule ref="category/java/bestpractices.xml">
|
||||
<exclude name="AvoidPrintStackTrace"/>
|
||||
<exclude name="JUnit4TestShouldUseTestAnnotation"/>
|
||||
<exclude name="JUnitTestContainsTooManyAsserts"/>
|
||||
<exclude name="GuardLogStatement"/>
|
||||
<exclude name="UnitTestContainsTooManyAsserts"/>
|
||||
<exclude name="UnitTestShouldUseTestAnnotation"/>
|
||||
</rule>
|
||||
|
||||
<rule ref="category/java/bestpractices.xml/MissingOverride">
|
||||
|
|
|
@ -2,6 +2,6 @@ bld.downloadExtensionJavadoc=false
|
|||
bld.downloadExtensionSources=true
|
||||
bld.downloadLocation=
|
||||
bld.extension-jacoco=com.uwyn.rife2:bld-jacoco-report:0.9.8
|
||||
bld.extension-pmd=com.uwyn.rife2:bld-pmd:1.1.5
|
||||
bld.extension-pmd=com.uwyn.rife2:bld-pmd:1.1.7
|
||||
bld.repositories=MAVEN_CENTRAL,RIFE2_RELEASES,MAVEN_LOCAL,RIFE2_SNAPSHOTS
|
||||
bld.version=2.1.0
|
||||
|
|
|
@ -76,8 +76,8 @@ public class HttpStatusBuild extends Project {
|
|||
.include(dependency("jakarta.el", "jakarta.el-api", version(6, 0, 1)));
|
||||
scope(test)
|
||||
.include(dependency("org.assertj", "assertj-core", version(3, 26, 3)))
|
||||
.include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 11, 0)))
|
||||
.include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 11, 0)));
|
||||
.include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 11, 3)))
|
||||
.include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 11, 3)));
|
||||
|
||||
jarOperation().manifestAttribute(Attributes.Name.MAIN_CLASS, pkg + '.' + "Reasons");
|
||||
|
||||
|
|
|
@ -48,7 +48,9 @@ import java.io.IOException;
|
|||
*/
|
||||
public class CauseTag extends XmlSupport {
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* Prints the cause (if any) for the current HTTP Status Error Code.
|
||||
*
|
||||
* @throws IOException If an error occurs while writing the output.
|
||||
*/
|
||||
@Override
|
||||
public void doTag() throws IOException {
|
||||
|
|
|
@ -47,7 +47,9 @@ import java.io.IOException;
|
|||
*/
|
||||
public class CodeTag extends SimpleTagSupport {
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* Writes the HTTP Status Error Code to the current JspWriter.
|
||||
*
|
||||
* @throws IOException If an I/O error occurs.
|
||||
*/
|
||||
@Override
|
||||
public void doTag() throws IOException {
|
||||
|
|
|
@ -47,7 +47,9 @@ import java.io.IOException;
|
|||
*/
|
||||
public class MessageTag extends XmlSupport {
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* Writes the error message associated with the current HTTP Status Error Code.
|
||||
*
|
||||
* @throws IOException If an I/O error occurs.
|
||||
*/
|
||||
@Override
|
||||
public void doTag() throws IOException {
|
||||
|
|
|
@ -51,7 +51,7 @@ public class ReasonTag extends XmlSupport {
|
|||
private int statusCode = -1;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* Writes the Reason Phrase for the current (or specified) HTTP Status Error Code.
|
||||
*/
|
||||
@Override
|
||||
public void doTag() {
|
||||
|
|
|
@ -33,10 +33,9 @@
|
|||
package net.thauvin.erik.httpstatus;
|
||||
|
||||
import net.thauvin.erik.httpstatus.taglibs.CauseTag;
|
||||
import org.assertj.core.api.AutoCloseableSoftAssertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Implements the CauseTagTest class.
|
||||
*
|
||||
|
@ -49,9 +48,11 @@ class CauseTagTest {
|
|||
var message = "This is the cause";
|
||||
var tag = new CauseTag();
|
||||
|
||||
assertThat(tag.getCause(new Exception(message))).as("has cause").isEqualTo(message);
|
||||
assertThat(tag.getCause(new Exception())).as("no cause").isNull();
|
||||
assertThat(tag.getCause(null)).as("null").isNull();
|
||||
assertThat(tag.getCause(new Exception(""))).as("empty").isEmpty();
|
||||
try (var softly = new AutoCloseableSoftAssertions()) {
|
||||
softly.assertThat(tag.getCause(new Exception(message))).as("has cause").isEqualTo(message);
|
||||
softly.assertThat(tag.getCause(new Exception())).as("no cause").isNull();
|
||||
softly.assertThat(tag.getCause(null)).as("null").isNull();
|
||||
softly.assertThat(tag.getCause(new Exception(""))).as("empty").isEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,55 +50,55 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
* @since 1.0
|
||||
*/
|
||||
class ReasonsMainTest {
|
||||
private final static PrintStream originalOut = System.out;
|
||||
private final static ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
||||
private final static ByteArrayOutputStream OUTPUT_STREAM = new ByteArrayOutputStream();
|
||||
private final static PrintStream SYSTEM_OUT = System.out;
|
||||
|
||||
@AfterAll
|
||||
public static void restoreStreams() {
|
||||
System.setOut(originalOut);
|
||||
System.setOut(SYSTEM_OUT);
|
||||
}
|
||||
|
||||
@BeforeAll
|
||||
public static void setUpStreams() {
|
||||
System.setOut(new PrintStream(outContent));
|
||||
System.setOut(new PrintStream(OUTPUT_STREAM));
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void resetStreams() {
|
||||
outContent.reset();
|
||||
OUTPUT_STREAM.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMain() {
|
||||
Reasons.main("401");
|
||||
assertThat(outContent.toString()).contains(Reasons.getReasonPhrase("401")).as("401");
|
||||
assertThat(outContent.toString()).doesNotContain("500").as("401 no 500");
|
||||
assertThat(OUTPUT_STREAM.toString()).contains(Reasons.getReasonPhrase("401")).as("401");
|
||||
assertThat(OUTPUT_STREAM.toString()).doesNotContain("500").as("401 no 500");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMainAll() {
|
||||
Reasons.main();
|
||||
assertThat(outContent.toString()).contains(Reasons.getReasonPhrase(301)).as("301");
|
||||
assertThat(outContent.toString()).contains(Reasons.getReasonPhrase(404)).as("404");
|
||||
assertThat(OUTPUT_STREAM.toString()).contains(Reasons.getReasonPhrase(301)).as("301");
|
||||
assertThat(OUTPUT_STREAM.toString()).contains(Reasons.getReasonPhrase(404)).as("404");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMainArgs() {
|
||||
Reasons.main("500", "302");
|
||||
assertThat(outContent.toString()).contains(Reasons.getReasonPhrase("500")).as("500 (302)");
|
||||
assertThat(outContent.toString()).contains(Reasons.getReasonPhrase("302")).as("(500) 302");
|
||||
assertThat(outContent.toString()).doesNotContain("404").as("500/302 not 404");
|
||||
assertThat(OUTPUT_STREAM.toString()).contains(Reasons.getReasonPhrase("500")).as("500 (302)");
|
||||
assertThat(OUTPUT_STREAM.toString()).contains(Reasons.getReasonPhrase("302")).as("(500) 302");
|
||||
assertThat(OUTPUT_STREAM.toString()).doesNotContain("404").as("500/302 not 404");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMainArgsClass() {
|
||||
Reasons.main("2xx");
|
||||
assertThat(outContent.toString()).contains(Reasons.getReasonPhrase("200")).as("2xx");
|
||||
assertThat(OUTPUT_STREAM.toString()).contains(Reasons.getReasonPhrase("200")).as("2xx");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMainInvalid() {
|
||||
Reasons.main("aaa");
|
||||
assertThat(outContent.toString()).as("invalid argument: aaa").isEmpty();
|
||||
assertThat(OUTPUT_STREAM.toString()).as("invalid argument: aaa").isEmpty();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,9 +50,9 @@ class ReasonsTest {
|
|||
void testGetReasonPhrase() {
|
||||
var bundle = ResourceBundle.getBundle(Reasons.BUNDLE_BASENAME);
|
||||
for (var key : bundle.keySet()) {
|
||||
assertThat(Reasons.getReasonPhrase(key)).as("getReasonPhrase(" + key + ')').isEqualTo(bundle.getString(key));
|
||||
assertThat(Reasons.getReasonPhrase(Integer.parseInt(key)))
|
||||
.as("getReasonPhrase(int: " + key + ')').isEqualTo(bundle.getString(key));
|
||||
assertThat(Reasons.getReasonPhrase(key)).as("getReasonPhrase(%s)", key).isEqualTo(bundle.getString(key));
|
||||
assertThat(Reasons.getReasonPhrase(Integer.parseInt(key))).as("getReasonPhrase(%s)", key)
|
||||
.isEqualTo(bundle.getString(key));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
|
||||
package net.thauvin.erik.httpstatus;
|
||||
|
||||
import org.assertj.core.api.AutoCloseableSoftAssertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ResourceBundle;
|
||||
|
@ -49,37 +50,47 @@ class StatusCodeTest {
|
|||
void testStatusCode() {
|
||||
var bundle = ResourceBundle.getBundle(Reasons.BUNDLE_BASENAME);
|
||||
var statusCode = new StatusCode();
|
||||
for (var key : bundle.keySet()) {
|
||||
int code = Integer.parseInt(key);
|
||||
statusCode.setCode(code);
|
||||
assertThat(statusCode.getCode()).as("is not " + code).isEqualTo(code);
|
||||
assertThat(statusCode.isInfo()).as(code + " is info").isEqualTo(code >= 100 && code < 200);
|
||||
assertThat(statusCode.isSuccess()).as(code + " is ok").isEqualTo(code >= 200 && code < 300);
|
||||
assertThat(statusCode.isRedirect()).as(code + " is redirect").isEqualTo(code >= 300 && code < 400);
|
||||
assertThat(statusCode.isClientError()).as(code + " is client error").isEqualTo(code >= 400 && code < 500);
|
||||
assertThat(statusCode.isServerError()).as(code + " is server error").isEqualTo(code >= 500 && code < 600);
|
||||
assertThat(statusCode.isError()).as(code + " is error").isEqualTo(code >= 400 && code < 600);
|
||||
assertThat(statusCode.isValid()).as(code + " is valid").isTrue();
|
||||
|
||||
assertThat(statusCode.getReason()).as(code + " reason phrase is not valid")
|
||||
.isEqualTo(Reasons.getReasonPhrase(code));
|
||||
try (var softly = new AutoCloseableSoftAssertions()) {
|
||||
for (var key : bundle.keySet()) {
|
||||
int code = Integer.parseInt(key);
|
||||
|
||||
statusCode.setCode(code);
|
||||
softly.assertThat(statusCode.getCode()).as("is not %s", code).isEqualTo(code);
|
||||
softly.assertThat(statusCode.isInfo()).as("%s is info", code).isEqualTo(code >= 100 && code < 200);
|
||||
softly.assertThat(statusCode.isSuccess()).as("%s is ok", code).isEqualTo(code >= 200 && code < 300);
|
||||
softly.assertThat(statusCode.isRedirect()).as("%s is redirect", code)
|
||||
.isEqualTo(code >= 300 && code < 400);
|
||||
softly.assertThat(statusCode.isClientError()).as("%s is client error", code)
|
||||
.isEqualTo(code >= 400 && code < 500);
|
||||
softly.assertThat(statusCode.isServerError()).as("%s is server error", code)
|
||||
.isEqualTo(code >= 500 && code < 600);
|
||||
softly.assertThat(statusCode.isError()).as("%s is error", code).isEqualTo(code >= 400 && code < 600);
|
||||
softly.assertThat(statusCode.isValid()).as("%s is valid", code).isTrue();
|
||||
|
||||
softly.assertThat(statusCode.getReason()).as("%s reason phrase is not valid", code)
|
||||
.isEqualTo(Reasons.getReasonPhrase(code));
|
||||
}
|
||||
}
|
||||
|
||||
int[] unknowns = {0, 99, 600};
|
||||
for (var code : unknowns) {
|
||||
statusCode.setCode(code);
|
||||
assertThat(statusCode.getCode()).as("is not " + code).isEqualTo(code);
|
||||
assertThat(statusCode.isInfo()).as(code + " is info").isFalse();
|
||||
assertThat(statusCode.isSuccess()).as(code + " is ok").isFalse();
|
||||
assertThat(statusCode.isRedirect()).as(code + " is redirect").isFalse();
|
||||
assertThat(statusCode.isClientError()).as(code + " is client error").isFalse();
|
||||
assertThat(statusCode.isServerError()).as(code + " is server error").isFalse();
|
||||
assertThat(statusCode.isError()).as(code + " is error").isFalse();
|
||||
assertThat(statusCode.isValid()).as(code + " is invalid").isFalse();
|
||||
assertThat(statusCode.getReason()).as(code + " reason phrase is not null.").isNull();
|
||||
try (var softly = new AutoCloseableSoftAssertions()) {
|
||||
int[] unknowns = {0, 99, 600};
|
||||
|
||||
for (var code : unknowns) {
|
||||
statusCode.setCode(code);
|
||||
softly.assertThat(statusCode.getCode()).as("is not %s", code).isEqualTo(code);
|
||||
softly.assertThat(statusCode.isInfo()).as("%s is info", code).isFalse();
|
||||
softly.assertThat(statusCode.isSuccess()).as("%s is ok", code).isFalse();
|
||||
softly.assertThat(statusCode.isRedirect()).as("%s is redirect", code).isFalse();
|
||||
softly.assertThat(statusCode.isClientError()).as("%s is client error", code).isFalse();
|
||||
softly.assertThat(statusCode.isServerError()).as("%s is server error", code).isFalse();
|
||||
softly.assertThat(statusCode.isError()).as("%s is error", code).isFalse();
|
||||
softly.assertThat(statusCode.isValid()).as("%s is invalid", code).isFalse();
|
||||
softly.assertThat(statusCode.getReason()).as("%s reason phrase is not null.", code).isNull();
|
||||
}
|
||||
}
|
||||
|
||||
statusCode = new StatusCode(900);
|
||||
assertThat(statusCode.getCode()).as("is not 900").isEqualTo(900);
|
||||
assertThat(statusCode.getCode()).as("is not %s", statusCode.getCode()).isEqualTo(900);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
|
||||
package net.thauvin.erik.httpstatus;
|
||||
|
||||
import org.assertj.core.api.AutoCloseableSoftAssertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -56,44 +57,44 @@ class UtilsTest {
|
|||
"according the "encoding" parameter & value.");
|
||||
}
|
||||
|
||||
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
|
||||
@Test
|
||||
void testOutWrite() throws IOException {
|
||||
try (var sw = new StringWriter()) {
|
||||
Utils.outWrite(sw, null, "default", false);
|
||||
assertThat(sw.toString()).isEqualTo("default").as("outWrite(default)");
|
||||
try (var sw = new StringWriter(); var softly = new AutoCloseableSoftAssertions()) {
|
||||
var defaultValue = "default";
|
||||
Utils.outWrite(sw, null, defaultValue, false);
|
||||
softly.assertThat(sw.toString()).as("outWrite(default)").isEqualTo(defaultValue);
|
||||
|
||||
sw.getBuffer().setLength(0);
|
||||
Utils.outWrite(sw, "", "default", false);
|
||||
assertThat(sw.toString()).isEqualTo("").as("outWrite(value empty)");
|
||||
Utils.outWrite(sw, "", defaultValue, false);
|
||||
softly.assertThat(sw.toString()).as("outWrite(value empty)").isEmpty();
|
||||
|
||||
sw.getBuffer().setLength(0);
|
||||
Utils.outWrite(sw, null, null, true);
|
||||
assertThat(sw.toString()).isEqualTo("").as("outWrite(null)");
|
||||
softly.assertThat(sw.toString()).as("outWrite(null)").isEmpty();
|
||||
|
||||
sw.getBuffer().setLength(0);
|
||||
Utils.outWrite(sw, "value", "default", false);
|
||||
assertThat(sw.toString()).isEqualTo("value").as("outWrite(value)");
|
||||
Utils.outWrite(sw, "value", defaultValue, false);
|
||||
softly.assertThat(sw.toString()).as("outWrite(value)").isEqualTo("value");
|
||||
|
||||
sw.getBuffer().setLength(0);
|
||||
Utils.outWrite(sw, "wan't", "default", true);
|
||||
assertThat(sw.toString()).isEqualTo("wan't").as("outWrite(wan't)");
|
||||
Utils.outWrite(sw, "wan't", defaultValue, true);
|
||||
softly.assertThat(sw.toString()).as("outWrite(wan't)").isEqualTo("wan't");
|
||||
|
||||
sw.getBuffer().setLength(0);
|
||||
Utils.outWrite(sw, null, "1 & 1", true);
|
||||
assertThat(sw.toString()).isEqualTo("1 & 1").as("outWrite(1 & 1)");
|
||||
softly.assertThat(sw.toString()).as("outWrite(1 & 1)").isEqualTo("1 & 1");
|
||||
|
||||
sw.getBuffer().setLength(0);
|
||||
Utils.outWrite(sw, "", "default", true);
|
||||
assertThat(sw.toString()).isEqualTo("").as("outWrite(value empty).as(xml)");
|
||||
Utils.outWrite(sw, "", defaultValue, true);
|
||||
softly.assertThat(sw.toString()).as("outWrite(value empty).as(xml)").isEmpty();
|
||||
|
||||
sw.getBuffer().setLength(0);
|
||||
Utils.outWrite(sw, null, "", true);
|
||||
assertThat(sw.toString()).isEqualTo("").as("outWrite(default empty)");
|
||||
softly.assertThat(sw.toString()).as("outWrite(default empty)").isEmpty();
|
||||
|
||||
sw.getBuffer().setLength(0);
|
||||
Utils.outWrite(sw, null, null, true);
|
||||
assertThat(sw.toString()).isEqualTo("").as("outWrite(null).as(xml)");
|
||||
softly.assertThat(sw.toString()).as("outWrite(null).as(xml)").isEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue