Skip to content

Commit

Permalink
make the tag library compatible with Bootstrap v4 #1008
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshikawaa committed Oct 14, 2020
1 parent 4aae4e0 commit 7751c7a
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
package org.terasoluna.gfw.common.message;

import static org.terasoluna.gfw.common.message.StandardResultMessageType.DANGER;
import static org.terasoluna.gfw.common.message.StandardResultMessageType.DARK;
import static org.terasoluna.gfw.common.message.StandardResultMessageType.ERROR;
import static org.terasoluna.gfw.common.message.StandardResultMessageType.INFO;
import static org.terasoluna.gfw.common.message.StandardResultMessageType.LIGHT;
import static org.terasoluna.gfw.common.message.StandardResultMessageType.PRIMARY;
import static org.terasoluna.gfw.common.message.StandardResultMessageType.SECONDARY;
import static org.terasoluna.gfw.common.message.StandardResultMessageType.SUCCESS;
import static org.terasoluna.gfw.common.message.StandardResultMessageType.WARN;
import static org.terasoluna.gfw.common.message.StandardResultMessageType.WARNING;
Expand Down Expand Up @@ -237,6 +241,42 @@ public static ResultMessages danger() {
return new ResultMessages(DANGER);
}

/**
* factory method for primary messages.
* @return error messages
* @since 5.7.0
*/
public static ResultMessages primary() {
return new ResultMessages(PRIMARY);
}

/**
* factory method for secondary messages.
* @return error messages
* @since 5.7.0
*/
public static ResultMessages secondary() {
return new ResultMessages(SECONDARY);
}

/**
* factory method for light messages.
* @return error messages
* @since 5.7.0
*/
public static ResultMessages light() {
return new ResultMessages(LIGHT);
}

/**
* factory method for dark messages.
* @return error messages
* @since 5.7.0
*/
public static ResultMessages dark() {
return new ResultMessages(DARK);
}

/**
* Returns {@link Iterator} instance that iterates over a list of {@link ResultMessage}
* @see java.lang.Iterable#iterator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
* <li><code>warn</code>(Deprecated from 5.0.0, will be removed in the future)</li>
* <li><code>warning(Added from 5.0.0)</code></li>
* <li><code>error</code></li>
* <li><code>danger</code></li>
* <li><code>primary(Added from 5.7.0)</code></li>
* <li><code>secondary(Added from 5.7.0)</code></li>
* <li><code>light(Added from 5.7.0)</code></li>
* <li><code>dark(Added from 5.7.0)</code></li>
* </ul>
* The level of <code>danger</code> is as same as <code>error</code> and <code>danger</code> is usually used as alias for
* <code>error</code>.
Expand Down Expand Up @@ -54,7 +59,27 @@ public enum StandardResultMessageType implements ResultMessageType {
/**
* message type is <code>danger</code>.
*/
DANGER("danger");
DANGER("danger"),
/**
* message type is <code>primary</code>.
* @since 5.7.0
*/
PRIMARY("primary"),
/**
* message type is <code>secondary</code>.
* @since 5.7.0
*/
SECONDARY("secondary"),
/**
* message type is <code>light</code>.
* @since 5.7.0
*/
LIGHT("light"),
/**
* message type is <code>dark</code>.
* @since 5.7.0
*/
DARK("dark");

/**
* message type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;
import static org.terasoluna.gfw.common.message.StandardResultMessageType.DANGER;
import static org.terasoluna.gfw.common.message.StandardResultMessageType.DARK;
import static org.terasoluna.gfw.common.message.StandardResultMessageType.ERROR;
import static org.terasoluna.gfw.common.message.StandardResultMessageType.INFO;
import static org.terasoluna.gfw.common.message.StandardResultMessageType.LIGHT;
import static org.terasoluna.gfw.common.message.StandardResultMessageType.PRIMARY;
import static org.terasoluna.gfw.common.message.StandardResultMessageType.SECONDARY;
import static org.terasoluna.gfw.common.message.StandardResultMessageType.SUCCESS;
import static org.terasoluna.gfw.common.message.StandardResultMessageType.WARN;
import static org.terasoluna.gfw.common.message.StandardResultMessageType.WARNING;
Expand Down Expand Up @@ -258,6 +262,54 @@ public void issue24_testWarning() {
assertThat(messages.getList(), is(Arrays.asList(msg1, msg2)));
}

@Test
public void testPrimary() {
ResultMessage msg1 = ResultMessage.fromCode("foo", "aa");
ResultMessage msg2 = ResultMessage.fromCode("bar", "bb");

ResultMessages messages = ResultMessages.primary().add("foo", "aa").add(
"bar", "bb");

assertThat(messages.getType(), is((ResultMessageType) PRIMARY));
assertThat(messages.getList(), is(Arrays.asList(msg1, msg2)));
}

@Test
public void testSecondary() {
ResultMessage msg1 = ResultMessage.fromCode("foo", "aa");
ResultMessage msg2 = ResultMessage.fromCode("bar", "bb");

ResultMessages messages = ResultMessages.secondary().add("foo", "aa")
.add("bar", "bb");

assertThat(messages.getType(), is((ResultMessageType) SECONDARY));
assertThat(messages.getList(), is(Arrays.asList(msg1, msg2)));
}

@Test
public void testLight() {
ResultMessage msg1 = ResultMessage.fromCode("foo", "aa");
ResultMessage msg2 = ResultMessage.fromCode("bar", "bb");

ResultMessages messages = ResultMessages.light().add("foo", "aa").add(
"bar", "bb");

assertThat(messages.getType(), is((ResultMessageType) LIGHT));
assertThat(messages.getList(), is(Arrays.asList(msg1, msg2)));
}

@Test
public void testDark() {
ResultMessage msg1 = ResultMessage.fromCode("foo", "aa");
ResultMessage msg2 = ResultMessage.fromCode("bar", "bb");

ResultMessages messages = ResultMessages.dark().add("foo", "aa").add(
"bar", "bb");

assertThat(messages.getType(), is((ResultMessageType) DARK));
assertThat(messages.getList(), is(Arrays.asList(msg1, msg2)));
}

@Test
public void testSerialization() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,30 @@ public void issue24_testWarning() {
assertThat(StandardResultMessageType.WARNING.toString(), is("warning"));
}

@Test
public void testPrimary() {
assertThat(StandardResultMessageType.PRIMARY.getType(), is("primary"));
assertThat(StandardResultMessageType.PRIMARY.toString(), is("primary"));
}

@Test
public void testSecondary() {
assertThat(StandardResultMessageType.SECONDARY.getType(), is(
"secondary"));
assertThat(StandardResultMessageType.SECONDARY.toString(), is(
"secondary"));
}

@Test
public void testLight() {
assertThat(StandardResultMessageType.LIGHT.getType(), is("light"));
assertThat(StandardResultMessageType.LIGHT.toString(), is("light"));
}

@Test
public void testDark() {
assertThat(StandardResultMessageType.DARK.getType(), is("dark"));
assertThat(StandardResultMessageType.DARK.toString(), is("dark"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ public class PaginationTag extends RequestContextAwareTag {
*/
private String innerElementClass = PaginationInfo.DEFAULT_INNER_CLASS;

/**
* Value of "class" attribute of anchor tag of pagination
* @since 5.7.0
*/
private String anchorClass = PaginationInfo.DEFAULT_A_CLASS;

/**
* Display text of the link of back to the top
*/
Expand Down Expand Up @@ -183,6 +189,10 @@ protected void writeAnchor(TagWriter tagWriter, String href,
String value) throws JspException {
if (StringUtils.hasText(href)) {
tagWriter.startTag(PaginationInfo.A_ELM);
if (StringUtils.hasText(anchorClass)) {
tagWriter.writeAttribute(PaginationInfo.CLASS_ATTR,
anchorClass);
}
tagWriter.writeAttribute(PaginationInfo.HREF_ATTR, href);
tagWriter.appendValue(value);
tagWriter.endTag(true);
Expand Down Expand Up @@ -393,6 +403,7 @@ public void release() {
this.outerElementClass = null;
this.innerElement = null;
this.innerElementClass = null;
this.anchorClass = null;
this.firstLinkText = null;
this.previousLinkText = null;
this.nextLinkText = null;
Expand Down Expand Up @@ -509,6 +520,15 @@ public void setInnerElementClass(String innerElementClass) {
this.innerElementClass = innerElementClass;
}

/**
* Set value of "class" attribute of anchor tag of pagination <br>
* @param anchorClass Value of "class" attribute of anchor tag of pagination
* @since 5.7.0
*/
public void setAnchorClass(String anchorClass) {
this.anchorClass = anchorClass;
}

/**
* Set display text of the link of back to the top <br>
* @param firstLinkText Display text of the link of back to the top
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public void testRelease01() throws Exception {
nullValue()));
assertThat(ReflectionTestUtils.getField(tag, "innerElementClass"), is(
nullValue()));
assertThat(ReflectionTestUtils.getField(tag, "anchorClass"), is(
nullValue()));
assertThat(ReflectionTestUtils.getField(tag, "firstLinkText"), is(
nullValue()));
assertThat(ReflectionTestUtils.getField(tag, "previousLinkText"), is(
Expand Down Expand Up @@ -593,6 +595,7 @@ public void testDoStartTagInternal16() throws Exception {
* <pre>
* -outer tag class change.
* -inner tag class change.
* -anchor tag class change.
* </pre>
*/
@Test
Expand All @@ -610,11 +613,12 @@ public void testDoStartTagInternal17() throws Exception {
// customize
tag.setOuterElementClass("all");
tag.setInnerElementClass("item");
tag.setAnchorClass("link");

int ret = tag.doStartTagInternal();

assertThat(ret, is(TagSupport.EVAL_BODY_INCLUDE));
String expected = "<ul class=\"all\"><li class=\"disabled\"><a href=\"javascript:void(0)\">&lt;&lt;</a></li><li class=\"disabled\"><a href=\"javascript:void(0)\">&lt;</a></li><li class=\"active\"><a href=\"javascript:void(0)\">1</a></li><li class=\"item\"><a href=\"?page=1&size=10\">2</a></li><li class=\"item\"><a href=\"?page=2&size=10\">3</a></li><li class=\"item\"><a href=\"?page=3&size=10\">4</a></li><li class=\"item\"><a href=\"?page=4&size=10\">5</a></li><li class=\"item\"><a href=\"?page=5&size=10\">6</a></li><li class=\"item\"><a href=\"?page=6&size=10\">7</a></li><li class=\"item\"><a href=\"?page=7&size=10\">8</a></li><li class=\"item\"><a href=\"?page=8&size=10\">9</a></li><li class=\"item\"><a href=\"?page=9&size=10\">10</a></li><li class=\"item\"><a href=\"?page=1&size=10\">&gt;</a></li><li class=\"item\"><a href=\"?page=99&size=10\">&gt;&gt;</a></li></ul>";
String expected = "<ul class=\"all\"><li class=\"disabled\"><a class=\"link\" href=\"javascript:void(0)\">&lt;&lt;</a></li><li class=\"disabled\"><a class=\"link\" href=\"javascript:void(0)\">&lt;</a></li><li class=\"active\"><a class=\"link\" href=\"javascript:void(0)\">1</a></li><li class=\"item\"><a class=\"link\" href=\"?page=1&size=10\">2</a></li><li class=\"item\"><a class=\"link\" href=\"?page=2&size=10\">3</a></li><li class=\"item\"><a class=\"link\" href=\"?page=3&size=10\">4</a></li><li class=\"item\"><a class=\"link\" href=\"?page=4&size=10\">5</a></li><li class=\"item\"><a class=\"link\" href=\"?page=5&size=10\">6</a></li><li class=\"item\"><a class=\"link\" href=\"?page=6&size=10\">7</a></li><li class=\"item\"><a class=\"link\" href=\"?page=7&size=10\">8</a></li><li class=\"item\"><a class=\"link\" href=\"?page=8&size=10\">9</a></li><li class=\"item\"><a class=\"link\" href=\"?page=9&size=10\">10</a></li><li class=\"item\"><a class=\"link\" href=\"?page=1&size=10\">&gt;</a></li><li class=\"item\"><a class=\"link\" href=\"?page=99&size=10\">&gt;&gt;</a></li></ul>";
assertThat(getOutput(), is(expected));
}

Expand All @@ -624,6 +628,7 @@ public void testDoStartTagInternal17() throws Exception {
* <pre>
* -outer tag class change.
* -inner tag class change.
* -anchor tag class change.
* </pre>
*/
@Test
Expand All @@ -641,11 +646,12 @@ public void testDoStartTagInternal17_2() throws Exception {
// customize
tag.setOuterElementClass("all");
tag.setInnerElementClass("item");
tag.setAnchorClass("link");

int ret = tag.doStartTagInternal();

assertThat(ret, is(TagSupport.EVAL_BODY_INCLUDE));
String expected = "<ul class=\"all\"><li class=\"item\"><a href=\"?page=0&size=10\">&lt;&lt;</a></li><li class=\"item\"><a href=\"?page=98&size=10\">&lt;</a></li><li class=\"item\"><a href=\"?page=90&size=10\">91</a></li><li class=\"item\"><a href=\"?page=91&size=10\">92</a></li><li class=\"item\"><a href=\"?page=92&size=10\">93</a></li><li class=\"item\"><a href=\"?page=93&size=10\">94</a></li><li class=\"item\"><a href=\"?page=94&size=10\">95</a></li><li class=\"item\"><a href=\"?page=95&size=10\">96</a></li><li class=\"item\"><a href=\"?page=96&size=10\">97</a></li><li class=\"item\"><a href=\"?page=97&size=10\">98</a></li><li class=\"item\"><a href=\"?page=98&size=10\">99</a></li><li class=\"active\"><a href=\"javascript:void(0)\">100</a></li><li class=\"disabled\"><a href=\"javascript:void(0)\">&gt;</a></li><li class=\"disabled\"><a href=\"javascript:void(0)\">&gt;&gt;</a></li></ul>";
String expected = "<ul class=\"all\"><li class=\"item\"><a class=\"link\" href=\"?page=0&size=10\">&lt;&lt;</a></li><li class=\"item\"><a class=\"link\" href=\"?page=98&size=10\">&lt;</a></li><li class=\"item\"><a class=\"link\" href=\"?page=90&size=10\">91</a></li><li class=\"item\"><a class=\"link\" href=\"?page=91&size=10\">92</a></li><li class=\"item\"><a class=\"link\" href=\"?page=92&size=10\">93</a></li><li class=\"item\"><a class=\"link\" href=\"?page=93&size=10\">94</a></li><li class=\"item\"><a class=\"link\" href=\"?page=94&size=10\">95</a></li><li class=\"item\"><a class=\"link\" href=\"?page=95&size=10\">96</a></li><li class=\"item\"><a class=\"link\" href=\"?page=96&size=10\">97</a></li><li class=\"item\"><a class=\"link\" href=\"?page=97&size=10\">98</a></li><li class=\"item\"><a class=\"link\" href=\"?page=98&size=10\">99</a></li><li class=\"active\"><a class=\"link\" href=\"javascript:void(0)\">100</a></li><li class=\"disabled\"><a class=\"link\" href=\"javascript:void(0)\">&gt;</a></li><li class=\"disabled\"><a class=\"link\" href=\"javascript:void(0)\">&gt;&gt;</a></li></ul>";
assertThat(getOutput(), is(expected));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ public int getEnd() {
*/
public static final String DEFAULT_INNER_CLASS = "";

/**
* Value of "class" attribute of anchor tag of pagination
* @since 5.7.0
*/
public static final String DEFAULT_A_CLASS = "";

/**
* Display text of the link of back to the top
*/
Expand Down

0 comments on commit 7751c7a

Please sign in to comment.