Skip to content

Commit

Permalink
Add tests for Tag
Browse files Browse the repository at this point in the history
  • Loading branch information
sualeh committed Aug 11, 2024
1 parent 3db374f commit e848f90
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 79 deletions.
140 changes: 67 additions & 73 deletions schemacrawler-utility/src/main/java/us/fatehi/utility/html/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@

package us.fatehi.utility.html;

import static java.util.Objects.requireNonNull;
import static us.fatehi.utility.Utility.isBlank;
import static us.fatehi.utility.html.TagOutputFormat.html;
import static us.fatehi.utility.html.TagOutputFormat.tsv;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import static java.util.Objects.requireNonNull;
import static us.fatehi.utility.Utility.isBlank;
import us.fatehi.utility.Color;

public class Tag {
Expand Down Expand Up @@ -79,11 +77,10 @@ protected Tag(
}

public String addAttribute(final String key, final String value) {
if (!isBlank(key) && !isBlank(value)) {
if (!isBlank(key)) {
return attributes.put(key, value);
} else {
return value;
}
return value;
}

public Tag addInnerTag(final Tag tag) {
Expand Down Expand Up @@ -133,66 +130,40 @@ public String toString() {
return getTagName();
}

/**
* Escapes the characters in text for use in HTML.
*
* @param text Text to escape.
* @return HTML-escaped text
*/
private String escapeHtml(final String text) {
final StringBuilder buffer = new StringBuilder(text.length() * 2);
for (int i = 0; i < text.length(); i++) {
final char ch = text.charAt(i);
switch (ch) {
case 62:
buffer.append("&gt;");
break;
case 38:
buffer.append("&amp;");
break;
case 60:
buffer.append("&lt;");
break;
default:
buffer.append(ch);
break;
}
}
return buffer.toString();
}

private void appendAttributes(StringBuilder buffer) {
private void appendAttributes(final StringBuilder buffer) {
for (final Entry<String, String> attribute : attributes.entrySet()) {
buffer
.append(" ")
.append(attribute.getKey())
.append("='")
.append(attribute.getValue())
.append("'");
final String value = attribute.getValue();
buffer.append(" ").append(attribute.getKey());
if (value != null) {
buffer.append("='").append(value).append("'");
}
buffer.append(" ");
}
}

private void appendBgColor(StringBuilder buffer) {
private void appendBgColor(final StringBuilder buffer) {
if (bgColor != null && !bgColor.equals(Color.white)) {
buffer.append(" bgcolor='").append(bgColor).append("'");
}
}

private void appendStyleClass(StringBuilder buffer) {
if (!isBlank(styleClass)) {
buffer.append(" class='").append(styleClass).append("'");
} else if (align != null && align != Alignment.inherit) {
buffer.append(" align='").append(align).append("'");
private void appendClosingTag(final StringBuilder buffer) {
if (emphasizeText) {
buffer.append("</i></b>");
}
if (indent) {
buffer.append("\t");
}
buffer.append("</").append(getTagName()).append(">");
}

private void appendEmphasizedText(StringBuilder buffer) {
private void appendEmphasizedText(final StringBuilder buffer) {
if (emphasizeText) {
buffer.append("<b><i>");
}
}

private void appendInnerTags(StringBuilder buffer) {
private void appendInnerTags(final StringBuilder buffer) {
for (final Tag innerTag : innerTags) {
if (indent) {
buffer.append("\t");
Expand All @@ -201,14 +172,40 @@ private void appendInnerTags(StringBuilder buffer) {
}
}

private void appendClosingTag(StringBuilder buffer) {
if (emphasizeText) {
buffer.append("</i></b>");
private void appendStyleClass(final StringBuilder buffer) {
if (!isBlank(styleClass)) {
buffer.append(" class='").append(styleClass).append("'");
} else if (align != null && align != Alignment.inherit) {
buffer.append(" align='").append(align).append("'");
}
if (indent) {
buffer.append("\t");
}

/**
* Escapes the characters in text for use in HTML.
*
* @param text Text to escape.
* @return HTML-escaped text
*/
private String escapeHtml(final String text) {
final StringBuilder buffer = new StringBuilder(text.length() * 2);
for (int i = 0; i < text.length(); i++) {
final char ch = text.charAt(i);
switch (ch) {
case 62:
buffer.append("&gt;");
break;
case 38:
buffer.append("&amp;");
break;
case 60:
buffer.append("&lt;");
break;
default:
buffer.append(ch);
break;
}
}
buffer.append("</").append(getTagName()).append(">");
return buffer.toString();
}

/**
Expand All @@ -228,12 +225,12 @@ private String toHtmlString() {
buffer.append(">");
appendEmphasizedText(buffer);

if (innerTags.isEmpty()) {
if (indent) {
buffer.append(System.lineSeparator());
}
buffer.append(escapeText ? escapeHtml(text) : text);
} else {
if (indent) {
buffer.append(System.lineSeparator());
}
buffer.append(escapeText ? escapeHtml(text) : text);

if (!innerTags.isEmpty()) {
buffer.append(System.lineSeparator());
appendInnerTags(buffer);
}
Expand Down Expand Up @@ -277,17 +274,15 @@ private String toInnerTagsTsvString() {
* @return Text
*/
private String toPlainTextString() {
if (innerTags.isEmpty()) {
if (characterWidth > 0) {
final String format =
String.format("%%%s%ds", align == Alignment.right ? "" : "-", characterWidth);
return String.format(format, text);
} else {
return text;
}
} else {
if (!innerTags.isEmpty()) {
return toInnerTagsPlainTextString();
}
if (characterWidth > 0) {
final String format =
String.format("%%%s%ds", align == Alignment.right ? "" : "-", characterWidth);
return String.format(format, text);
}
return text;
}

/**
Expand All @@ -298,8 +293,7 @@ private String toPlainTextString() {
private String toTsvString() {
if (innerTags.isEmpty()) {
return text;
} else {
return toInnerTagsTsvString();
}
return toInnerTagsTsvString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@
import static org.hamcrest.Matchers.nullValue;
import static us.fatehi.utility.html.TagBuilder.caption;
import static us.fatehi.utility.html.TagBuilder.span;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import us.fatehi.utility.Color;
import us.fatehi.utility.html.Tag;
import us.fatehi.utility.html.TagOutputFormat;
Expand Down Expand Up @@ -73,7 +71,7 @@ public void caption1() {
assertThat(captionElement.attr("sometag"), is("customvalue"));
assertThat(captionElement.attr("bgcolor"), is("#FF0064"));
assertThat(captionElement.attr("class"), is("class"));
assertThat(captionElement.text(), is("display text"));
assertThat(captionElement.text(), is("display text display text"));
assertThat(captionElement.select("span").text(), is("display text"));

assertThat(caption.render(TagOutputFormat.text), is("display text"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
import static org.hamcrest.Matchers.nullValue;
import static us.fatehi.utility.html.TagBuilder.tableCell;
import static us.fatehi.utility.html.TagBuilder.tableRow;

import org.junit.jupiter.api.Test;

import us.fatehi.utility.html.Tag;
import us.fatehi.utility.html.TagOutputFormat;

Expand Down Expand Up @@ -87,7 +85,7 @@ public void tr() {

assertThat(
row.render(TagOutputFormat.html).replace(System.lineSeparator(), "~"),
is("\t<tr>~\t\t<td>display text</td>~\t\t<td>display text</td>~\t</tr>"));
is("\t<tr>~~\t\t<td>display text</td>~\t\t<td>display text</td>~\t</tr>"));
assertThat(row.render(TagOutputFormat.text), is("display text display text"));
assertThat(row.render(TagOutputFormat.tsv), is("display text\tdisplay text"));
}
Expand Down
Loading

0 comments on commit e848f90

Please sign in to comment.