Author: mattsicker
Date: Mon May 26 22:00:51 2014
New Revision: 1597650
URL: http://svn.apache.org/r1597650
Log:
Add builder class to HtmlLayout and update unit tests.
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/HtmlLayout.java
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/HtmlLayoutTest.java
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/HtmlLayout.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/HtmlLayout.java?rev=1597650&r1=1597649&r2=1597650&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/HtmlLayout.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/HtmlLayout.java
Mon May 26 22:00:51 2014
@@ -32,6 +32,7 @@ import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
+import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.util.Charsets;
import org.apache.logging.log4j.core.util.Constants;
@@ -324,7 +325,7 @@ public final class HtmlLayout extends Ab
public static HtmlLayout createLayout(
@PluginAttribute(value = "locationInfo", defaultBooleanValue =
false) final boolean locationInfo,
@PluginAttribute(value = "title", defaultStringValue =
DEFAULT_TITLE) final String title,
- @PluginAttribute(value = "contentType", defaultStringValue =
DEFAULT_CONTENT_TYPE) String contentType,
+ @PluginAttribute("contentType") String contentType,
@PluginAttribute(value = "charset", defaultStringValue = "UTF-8")
final Charset charset,
@PluginAttribute("fontSize") String fontSize,
@PluginAttribute(value = "fontName", defaultStringValue =
DEFAULT_FONT_FAMILY) final String font) {
@@ -343,7 +344,75 @@ public final class HtmlLayout extends Ab
* @return an HTML Layout.
*/
public static HtmlLayout createDefaultLayout() {
- return new HtmlLayout(false, DEFAULT_TITLE, DEFAULT_CONTENT_TYPE + ";
charset=UTF-8", Charsets.UTF_8,
- DEFAULT_FONT_FAMILY, FontSize.SMALL.getFontSize(),
FontSize.SMALL.larger().getFontSize());
+ return newBuilder().build();
+ }
+
+ @PluginBuilderFactory
+ public static Builder newBuilder() {
+ return new Builder();
+ }
+
+ public static class Builder implements
org.apache.logging.log4j.core.util.Builder<HtmlLayout> {
+
+ @PluginAttribute(value = "locationInfo", defaultBooleanValue = false)
+ private boolean locationInfo = false;
+
+ @PluginAttribute(value = "title", defaultStringValue = DEFAULT_TITLE)
+ private String title = DEFAULT_TITLE;
+
+ @PluginAttribute(value = "contentType")
+ private String contentType = null; // defer default value in order to
use specified charset
+
+ @PluginAttribute(value = "charset", defaultStringValue = "UTF-8")
+ private Charset charset = Charsets.UTF_8;
+
+ @PluginAttribute(value = "fontSize", defaultStringValue = "SMALL")
+ private FontSize fontSize = FontSize.SMALL;
+
+ @PluginAttribute(value = "fontName", defaultStringValue =
DEFAULT_FONT_FAMILY)
+ private String fontName = DEFAULT_FONT_FAMILY;
+
+ private Builder() {
+ }
+
+ public Builder withLocationInfo(final boolean locationInfo) {
+ this.locationInfo = locationInfo;
+ return this;
+ }
+
+ public Builder withTitle(final String title) {
+ this.title = title;
+ return this;
+ }
+
+ public Builder withContentType(final String contentType) {
+ this.contentType = contentType;
+ return this;
+ }
+
+ public Builder withCharset(final Charset charset) {
+ this.charset = charset;
+ return this;
+ }
+
+ public Builder withFontSize(final FontSize fontSize) {
+ this.fontSize = fontSize;
+ return this;
+ }
+
+ public Builder withFontName(final String fontName) {
+ this.fontName = fontName;
+ return this;
+ }
+
+ @Override
+ public HtmlLayout build() {
+ // TODO: extract charset from content-type
+ if (contentType == null) {
+ contentType = DEFAULT_CONTENT_TYPE + "; charset=" + charset;
+ }
+ return new HtmlLayout(locationInfo, title, contentType, charset,
fontName, fontSize.getFontSize(),
+ fontSize.larger().getFontSize());
+ }
}
}
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/HtmlLayoutTest.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/HtmlLayoutTest.java?rev=1597650&r1=1597649&r2=1597650&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/HtmlLayoutTest.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/HtmlLayoutTest.java
Mon May 26 22:00:51 2014
@@ -27,12 +27,6 @@ import org.apache.logging.log4j.core.Bas
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
-import org.apache.logging.log4j.core.config.DefaultConfiguration;
-import org.apache.logging.log4j.core.config.Node;
-import org.apache.logging.log4j.core.config.plugins.PluginFactory;
-import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
-import org.apache.logging.log4j.core.config.plugins.util.PluginType;
-import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.util.Charsets;
import org.apache.logging.log4j.test.appender.ListAppender;
import org.junit.AfterClass;
@@ -76,14 +70,17 @@ public class HtmlLayoutTest {
@Test
public void testContentType() {
- final HtmlLayout layout = HtmlLayout.createLayout(true, null,
"text/html; charset=UTF-16", null, "small",
- null);
+ final HtmlLayout layout = HtmlLayout.newBuilder()
+ .withContentType("text/html; charset=UTF-16")
+ .build();
assertEquals("text/html; charset=UTF-16", layout.getContentType());
+ // TODO: make sure this following bit works as well
+// assertEquals(Charset.forName("UTF-16"), layout.getCharset());
}
@Test
public void testDefaultCharset() {
- final HtmlLayout layout = HtmlLayout.createLayout(true, null, null,
null, "small", null);
+ final HtmlLayout layout = HtmlLayout.createDefaultLayout();
assertEquals(Charsets.UTF_8, layout.getCharset());
}
@@ -106,17 +103,8 @@ public class HtmlLayoutTest {
root.removeAppender(appender);
}
// set up appender
- // note: yes, this is a bit of a roundabout way to constructing the
layout, but without builder classes, this
- // is the most reliable way to get the default values for everything
- // TODO: this could probably be tested more easily using a config file
- final Node node = new Node();
- node.getAttributes().put("locationInfo",
Boolean.toString(includeLocation));
- final HtmlLayout layout =
- new PluginBuilder<HtmlLayout>(new
PluginType<HtmlLayout>(HtmlLayout.class, "HtmlLayout", false, false))
- .withFactoryMethodAnnotatedBy(PluginFactory.class)
- .withConfiguration(new DefaultConfiguration())
- .withConfigurationNode(node)
- .forLogEvent(new Log4jLogEvent())
+ final HtmlLayout layout = HtmlLayout.newBuilder()
+ .withLocationInfo(includeLocation)
.build();
final ListAppender appender = new ListAppender("List", null, layout,
true, false);
appender.start();