Repository: logging-log4j2 Updated Branches: refs/heads/master 1a19f75dc -> d953cbfa6
LOG4J2-1434 added unit test to verify that StringBuilder capacity is reset to at most MAX on each call to AbstractStringLayout.getStringBuilder() Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/d953cbfa Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/d953cbfa Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/d953cbfa Branch: refs/heads/master Commit: d953cbfa6a922061dbc4537f58d766debdc75b2e Parents: 1a19f75 Author: rpopma <rpo...@apache.org> Authored: Sun Jun 19 13:56:53 2016 +0900 Committer: rpopma <rpo...@apache.org> Committed: Sun Jun 19 13:56:53 2016 +0900 ---------------------------------------------------------------------- .../core/layout/AbstractStringLayoutTest.java | 61 ++++++++++++++++++++ 1 file changed, 61 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d953cbfa/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/AbstractStringLayoutTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/AbstractStringLayoutTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/AbstractStringLayoutTest.java new file mode 100644 index 0000000..f213a31 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/AbstractStringLayoutTest.java @@ -0,0 +1,61 @@ +package org.apache.logging.log4j.core.layout;/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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. + */ + +import java.nio.charset.Charset; + +import org.apache.logging.log4j.core.LogEvent; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests AbstractStringLayout. + */ +public class AbstractStringLayoutTest { + static class ConcreteStringLayout extends AbstractStringLayout { + public static int DEFAULT_STRING_BUILDER_SIZE = AbstractStringLayout.DEFAULT_STRING_BUILDER_SIZE; + public static int MAX_STRING_BUILDER_SIZE = AbstractStringLayout.MAX_STRING_BUILDER_SIZE; + + public ConcreteStringLayout() { + super(Charset.defaultCharset()); + } + + public static StringBuilder getStringBuilder() { + return AbstractStringLayout.getStringBuilder(); + } + + @Override + public String toSerializable(final LogEvent event) { + return null; + } + } + + @Test + public void testGetStringBuilderCapacityRestrictedToMax() throws Exception { + final int LARGE = 4096; + final String largeMessage = new String(new char[LARGE]); + final StringBuilder sb = ConcreteStringLayout.getStringBuilder(); + assertEquals("initial capacity", ConcreteStringLayout.DEFAULT_STRING_BUILDER_SIZE, sb.capacity()); + sb.append(largeMessage); + assertEquals("capacity=msg length", LARGE, sb.capacity()); + assertEquals("capacity=msg length", LARGE, sb.length()); + + final StringBuilder next = ConcreteStringLayout.getStringBuilder(); + assertEquals("max capacity", ConcreteStringLayout.MAX_STRING_BUILDER_SIZE, next.capacity()); + assertEquals("empty, ready for use", 0, sb.length()); + } +} \ No newline at end of file