Added more tests
This commit is contained in:
parent
1aa2bc2681
commit
05c1bd3f2a
7 changed files with 121 additions and 52 deletions
|
@ -140,7 +140,7 @@ publishing {
|
||||||
from(components["java"])
|
from(components["java"])
|
||||||
pom {
|
pom {
|
||||||
name.set("RIFE2 Template Renderers")
|
name.set("RIFE2 Template Renderers")
|
||||||
description.set("Template Renderers for the RIFE2 framework")
|
description.set("Template Renderers for the RIFE2 web framework")
|
||||||
url.set("https://github.com/rife2/rife2-template-renderers")
|
url.set("https://github.com/rife2/rife2-template-renderers")
|
||||||
licenses {
|
licenses {
|
||||||
license {
|
license {
|
||||||
|
|
|
@ -40,6 +40,6 @@ public class EncodeHtmlEntities implements ValueRenderer {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String render(Template template, String valueId, String differentiator) {
|
public String render(Template template, String valueId, String differentiator) {
|
||||||
return RenderUtils.toHtmlEntities(template.getValueOrAttribute(differentiator));
|
return RenderUtils.htmlEntities(template.getValueOrAttribute(differentiator));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,6 @@ public class EncodeJs implements ValueRenderer {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String render(Template template, String valueId, String differentiator) {
|
public String render(Template template, String valueId, String differentiator) {
|
||||||
return RenderUtils.encodeJS(template.getValueOrAttribute(differentiator));
|
return RenderUtils.encodeJs(template.getValueOrAttribute(differentiator));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,7 +53,7 @@ public final class RenderUtils {
|
||||||
* @return the abbreviated String
|
* @return the abbreviated String
|
||||||
*/
|
*/
|
||||||
public static String abbreviate(String src, int max, String marker) {
|
public static String abbreviate(String src, int max, String marker) {
|
||||||
if (src == null || src.isBlank()) {
|
if (src == null || src.isBlank() || marker == null) {
|
||||||
return src;
|
return src;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,7 +85,7 @@ public final class RenderUtils {
|
||||||
* @param src the source String
|
* @param src the source String
|
||||||
* @return the encoded String
|
* @return the encoded String
|
||||||
*/
|
*/
|
||||||
public static String encodeJS(String src) {
|
public static String encodeJs(String src) {
|
||||||
if (src == null || src.isBlank()) {
|
if (src == null || src.isBlank()) {
|
||||||
return src;
|
return src;
|
||||||
}
|
}
|
||||||
|
@ -172,6 +172,34 @@ public final class RenderUtils {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a text string to HTML decimal entities.
|
||||||
|
*
|
||||||
|
* @param src the String to convert
|
||||||
|
* @return the converted String
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("PMD.AvoidReassigningLoopVariables")
|
||||||
|
public static String htmlEntities(String src) {
|
||||||
|
if (src == null || src.isEmpty()) {
|
||||||
|
return src;
|
||||||
|
}
|
||||||
|
|
||||||
|
var len = src.length();
|
||||||
|
var sb = new StringBuilder(len * 6);
|
||||||
|
|
||||||
|
// https://stackoverflow.com/a/6766497/8356718
|
||||||
|
int codePoint;
|
||||||
|
for (var i = 0; i < len; i++) {
|
||||||
|
codePoint = src.codePointAt(i);
|
||||||
|
// Skip over the second char in a surrogate pair
|
||||||
|
if (codePoint > 0xffff) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
sb.append(String.format("&#%s;", codePoint));
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Masks characters in a String.
|
* Masks characters in a String.
|
||||||
*
|
*
|
||||||
|
@ -182,6 +210,10 @@ public final class RenderUtils {
|
||||||
* @return the masked String
|
* @return the masked String
|
||||||
*/
|
*/
|
||||||
public static String mask(String src, String mask, int unmasked, boolean fromStart) {
|
public static String mask(String src, String mask, int unmasked, boolean fromStart) {
|
||||||
|
if (src == null || src.isEmpty()) {
|
||||||
|
return src;
|
||||||
|
}
|
||||||
|
|
||||||
var len = src.length();
|
var len = src.length();
|
||||||
var buff = new StringBuilder(len);
|
var buff = new StringBuilder(len);
|
||||||
if (unmasked > 0 && unmasked < len) {
|
if (unmasked > 0 && unmasked < len) {
|
||||||
|
@ -205,6 +237,10 @@ public final class RenderUtils {
|
||||||
* @return The normalized String
|
* @return The normalized String
|
||||||
*/
|
*/
|
||||||
public static String normalize(String src) {
|
public static String normalize(String src) {
|
||||||
|
if (src == null || src.isBlank()) {
|
||||||
|
return src;
|
||||||
|
}
|
||||||
|
|
||||||
var normalized = Normalizer.normalize(src.trim(), Normalizer.Form.NFD);
|
var normalized = Normalizer.normalize(src.trim(), Normalizer.Form.NFD);
|
||||||
var sb = new StringBuilder(normalized.length());
|
var sb = new StringBuilder(normalized.length());
|
||||||
boolean space = false;
|
boolean space = false;
|
||||||
|
@ -344,33 +380,6 @@ public final class RenderUtils {
|
||||||
return new String(buff, 0, offset);
|
return new String(buff, 0, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts a text string to HTML decimal entities.
|
|
||||||
*
|
|
||||||
* @param src the String to convert
|
|
||||||
* @return the converted String
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("PMD.AvoidReassigningLoopVariables")
|
|
||||||
public static String toHtmlEntities(String src) {
|
|
||||||
if (src == null || src.isEmpty()) {
|
|
||||||
return src;
|
|
||||||
}
|
|
||||||
|
|
||||||
var len = src.length();
|
|
||||||
var sb = new StringBuilder(len * 6);
|
|
||||||
|
|
||||||
// https://stackoverflow.com/a/6766497/8356718
|
|
||||||
for (var i = 0; i < len; i++) {
|
|
||||||
var codePoint = src.codePointAt(i);
|
|
||||||
// Skip over the second char in a surrogate pair
|
|
||||||
if (codePoint > 0xffff) {
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
sb.append(String.format("&#%s;", codePoint));
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the formatted server uptime.
|
* Returns the formatted server uptime.
|
||||||
*
|
*
|
||||||
|
|
|
@ -41,7 +41,8 @@ class TestEncode {
|
||||||
void testEncodeHtmlEntities() {
|
void testEncodeHtmlEntities() {
|
||||||
var t = TemplateFactory.HTML.get("encodeHtmlEntities");
|
var t = TemplateFactory.HTML.get("encodeHtmlEntities");
|
||||||
t.setAttribute(TestCase.FOO, "john@doe.com");
|
t.setAttribute(TestCase.FOO, "john@doe.com");
|
||||||
assertThat(t.getContent()).isEqualTo("<a href=\"mailto:john@doe.com\">Email</a>");
|
assertThat(t.getContent()).isEqualTo(
|
||||||
|
"<a href=\"mailto:john@doe.com\">Email</a>");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -66,18 +67,19 @@ class TestEncode {
|
||||||
// Encode
|
// Encode
|
||||||
var bean = new ValueBean(TestCase.SAMPLE_TEXT);
|
var bean = new ValueBean(TestCase.SAMPLE_TEXT);
|
||||||
t.setBean(bean);
|
t.setBean(bean);
|
||||||
assertThat(t.getContent()).isEqualTo(bean.getValue() + ": " + rot13);
|
assertThat(t.getContent()).as("encode").isEqualTo(bean.getValue() + ": " + rot13);
|
||||||
|
|
||||||
// Decode
|
// Decode
|
||||||
t.setValue("value", rot13);
|
t.setValue("value", rot13);
|
||||||
assertThat(t.getContent()).isEqualTo(rot13 + ": " + TestCase.SAMPLE_TEXT);
|
assertThat(t.getContent()).as("decode").isEqualTo(rot13 + ": " + TestCase.SAMPLE_TEXT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testEncodeUnicode() {
|
void testEncodeUnicode() {
|
||||||
var t = TemplateFactory.TXT.get("encodeUnicode");
|
var t = TemplateFactory.TXT.get("encodeUnicode");
|
||||||
t.setAttribute(TestCase.FOO, TestCase.SAMPLE_TEXT);
|
t.setAttribute(TestCase.FOO, TestCase.SAMPLE_TEXT);
|
||||||
assertThat(t.getContent()).isEqualTo("\\u0054\\u0068\\u0069\\u0073\\u0020\\u0069\\u0073\\u0020\\u0061\\u0020\\u0074\\u0065\\u0073\\u0074\\u002E");
|
assertThat(t.getContent()).isEqualTo(
|
||||||
|
"\\u0054\\u0068\\u0069\\u0073\\u0020\\u0069\\u0073\\u0020\\u0061\\u0020\\u0074\\u0065\\u0073\\u0074\\u002E");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -27,12 +27,11 @@ class TestFormat {
|
||||||
void testAbbreviate() {
|
void testAbbreviate() {
|
||||||
var t = TemplateFactory.HTML.get("abbreviate");
|
var t = TemplateFactory.HTML.get("abbreviate");
|
||||||
t.setAttribute(TestCase.FOO, TestCase.SAMPLE_TEXT);
|
t.setAttribute(TestCase.FOO, TestCase.SAMPLE_TEXT);
|
||||||
System.out.println(t.getContent());
|
assertThat(t.getContent()).as("activate.html").endsWith("…").hasSize(12);
|
||||||
assertThat(t.getContent()).as("max=12").endsWith("…").hasSize(12);
|
|
||||||
|
|
||||||
t = TemplateFactory.TXT.get("abbreviate");
|
t = TemplateFactory.TXT.get("abbreviate");
|
||||||
t.setAttribute(TestCase.FOO, TestCase.SAMPLE_TEXT);
|
t.setAttribute(TestCase.FOO, TestCase.SAMPLE_TEXT);
|
||||||
assertThat(t.getContent()).as("max=8").endsWith("...").hasSize(8);
|
assertThat(t.getContent()).as("activate.txt").endsWith("...").hasSize(8);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -58,12 +57,6 @@ class TestFormat {
|
||||||
t = TemplateFactory.TXT.get("mask");
|
t = TemplateFactory.TXT.get("mask");
|
||||||
t.setAttribute(TestCase.FOO, foo);
|
t.setAttribute(TestCase.FOO, foo);
|
||||||
assertThat(t.getContent()).as("mask.txt").isEqualTo("***************");
|
assertThat(t.getContent()).as("mask.txt").isEqualTo("***************");
|
||||||
|
|
||||||
assertThat(RenderUtils.mask(foo, "?", 4, false)).as("mask=?")
|
|
||||||
.isEqualTo("???????????1053");
|
|
||||||
|
|
||||||
assertThat(RenderUtils.mask(foo, "-", 22, false)).as("unmasked=22")
|
|
||||||
.isEqualTo("---------------");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -97,23 +90,25 @@ class TestFormat {
|
||||||
@Test
|
@Test
|
||||||
void testUptime() {
|
void testUptime() {
|
||||||
var t = TemplateFactory.TXT.get("uptime");
|
var t = TemplateFactory.TXT.get("uptime");
|
||||||
assertThat(t.getContent()).matches("0 minute\n0 minuto\n0 minute");
|
assertThat(t.getContent()).as("uptime.txt").isEqualTo("0 minute\n0 minuto\n0 minute");
|
||||||
|
|
||||||
t = TemplateFactory.HTML.get("uptime");
|
t = TemplateFactory.HTML.get("uptime");
|
||||||
t.setAttribute(Uptime.class.getName(), 547800300076L);
|
t.setAttribute(Uptime.class.getName(), 547800300076L);
|
||||||
assertThat(t.getContent()).isEqualTo("17 années, 4 mois, 2 semaines, 1 jour, 6 heures, 45 minutes");
|
assertThat(t.getContent()).as("uptime.html")
|
||||||
|
.isEqualTo("17 années, 4 mois, 2 semaines, 1 jour, 6 heures, 45 minutes");
|
||||||
t.setAttribute(Uptime.class.getName(), 120000L);
|
t.setAttribute(Uptime.class.getName(), 120000L);
|
||||||
assertThat(t.getContent()).matches("2 minutes");
|
assertThat(t.getContent()).as("uptime.html: 2 min").isEqualTo("2 minutes");
|
||||||
|
|
||||||
t = TemplateFactory.JSON.get("uptime");
|
t = TemplateFactory.JSON.get("uptime");
|
||||||
t.setAttribute(Uptime.class.getName(), 5999964460000L);
|
t.setAttribute(Uptime.class.getName(), 5999964460000L);
|
||||||
assertThat(t.getContent()).isEqualTo("190 years 3 months 4 days 47 minutes");
|
assertThat(t.getContent()).as("uptime.json")
|
||||||
|
.isEqualTo("190 years 3 months 4 days 47 minutes");
|
||||||
t.setAttribute(Uptime.class.getName(), 34822860000L);
|
t.setAttribute(Uptime.class.getName(), 34822860000L);
|
||||||
assertThat(t.getContent()).isEqualTo("1 year 1 month 1 week 1 day 1 hour 1 minute");
|
assertThat(t.getContent()).as("uptime.json: 1 year...")
|
||||||
|
.isEqualTo("1 year 1 month 1 week 1 day 1 hour 1 minute");
|
||||||
|
|
||||||
t = TemplateFactory.TXT.get("uptime2");
|
t = TemplateFactory.TXT.get("uptime2");
|
||||||
t.setAttribute(Uptime.class.getName(), 547800388076L);
|
t.setAttribute(Uptime.class.getName(), 547800388076L);
|
||||||
assertThat(t.getContent()).matches("17YRS-4MOS-2WKS-1D-6H-46M");
|
assertThat(t.getContent()).as("uptime2.txt").isEqualTo("17YRS-4MOS-2WKS-1D-6H-46M");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
63
lib/src/test/java/rife/render/TestRenderUtils.java
Normal file
63
lib/src/test/java/rife/render/TestRenderUtils.java
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2023 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package rife.render;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
class TestRenderUtils {
|
||||||
|
static final String SAMPLE_GERMAN = "Möchten Sie ein paar Äpfel?";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testAbbreviate() {
|
||||||
|
assertThat(RenderUtils.abbreviate(TestCase.SAMPLE_TEXT, 9, "")).as("max=9")
|
||||||
|
.isEqualTo("This is a");
|
||||||
|
|
||||||
|
assertThat(RenderUtils.abbreviate(TestCase.SAMPLE_TEXT, 0, "")).as("max=0").isEmpty();
|
||||||
|
|
||||||
|
assertThat(RenderUtils.abbreviate(TestCase.SAMPLE_TEXT, -1, "")).as("max=-1")
|
||||||
|
.isEqualTo(TestCase.SAMPLE_TEXT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testMask() {
|
||||||
|
var foo = "4342256562440179";
|
||||||
|
|
||||||
|
assertThat(RenderUtils.mask(foo, "?", 4, false)).as("mask=?")
|
||||||
|
.isEqualTo("????????????0179");
|
||||||
|
|
||||||
|
assertThat(RenderUtils.mask(foo, "-", 22, true)).as("unmasked=22")
|
||||||
|
.isEqualTo("----------------");
|
||||||
|
|
||||||
|
assertThat(RenderUtils.mask(foo, "•", -1, false)).as("mask=•")
|
||||||
|
.isEqualTo("••••••••••••••••");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testNormalize() {
|
||||||
|
assertThat(RenderUtils.normalize(SAMPLE_GERMAN)).isEqualTo("mochten-sie-ein-paar-apfel");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRot13() {
|
||||||
|
var encoded = "Zöpugra Fvr rva cnne Äcsry?";
|
||||||
|
assertThat(RenderUtils.rot13(SAMPLE_GERMAN)).as("encode").isEqualTo(encoded);
|
||||||
|
assertThat(RenderUtils.rot13(encoded)).as("decode").isEqualTo(SAMPLE_GERMAN);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue