Added tests for the cause tag
This commit is contained in:
parent
2ff4128bc9
commit
9a2647e7f4
2 changed files with 72 additions and 6 deletions
|
@ -52,15 +52,24 @@ public class CauseTag extends XmlSupport {
|
||||||
@Override
|
@Override
|
||||||
public void doTag() throws IOException {
|
public void doTag() throws IOException {
|
||||||
final PageContext pageContext = (PageContext) getJspContext();
|
final PageContext pageContext = (PageContext) getJspContext();
|
||||||
@SuppressWarnings("PMD.CloseResource") final JspWriter out = pageContext.getOut();
|
final JspWriter out = pageContext.getOut();
|
||||||
|
|
||||||
final Throwable cause = pageContext.getErrorData().getThrowable().getCause();
|
final Throwable cause = pageContext.getErrorData().getThrowable().getCause();
|
||||||
|
|
||||||
String message = defaultValue;
|
Utils.outWrite(out, getCause(cause), defaultValue, escapeXml);
|
||||||
if (cause != null && cause.getLocalizedMessage() != null) {
|
|
||||||
message = cause.getLocalizedMessage();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Utils.outWrite(out, message, defaultValue, escapeXml);
|
/**
|
||||||
|
* Returns the cause's localized message or default value.
|
||||||
|
*
|
||||||
|
* @param cause The cause.
|
||||||
|
* @return The cause or {@code null}.
|
||||||
|
*/
|
||||||
|
public String getCause(Throwable cause) {
|
||||||
|
if (cause != null && cause.getLocalizedMessage() != null) {
|
||||||
|
return cause.getLocalizedMessage();
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
57
src/test/java/net/thauvin/erik/httpstatus/CauseTagTest.java
Normal file
57
src/test/java/net/thauvin/erik/httpstatus/CauseTagTest.java
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
/*
|
||||||
|
* CauseTagTest.java
|
||||||
|
*
|
||||||
|
* Copyright 2023 sErik 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 net.thauvin.erik.httpstatus.taglibs.CauseTag;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements the CauseTagTest class.
|
||||||
|
*
|
||||||
|
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
|
||||||
|
* @since 1.0
|
||||||
|
*/
|
||||||
|
class CauseTagTest {
|
||||||
|
@Test
|
||||||
|
void causeTest() {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue