LOG4J2-1434 added more tests 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/13b453ce Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/13b453ce Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/13b453ce Branch: refs/heads/LOG4J-1181 Commit: 13b453ce57518f5c1886a5761a7e9ca7b66a2544 Parents: d953cbf Author: rpopma <rpo...@apache.org> Authored: Sun Jun 19 14:03:33 2016 +0900 Committer: rpopma <rpo...@apache.org> Committed: Sun Jun 19 14:03:33 2016 +0900 ---------------------------------------------------------------------- .../core/layout/AbstractStringLayoutTest.java | 23 +++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/13b453ce/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 index f213a31..fcd4ae1 100644 --- 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 @@ -46,16 +46,27 @@ public class AbstractStringLayoutTest { @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 int MEDIUM = ConcreteStringLayout.DEFAULT_STRING_BUILDER_SIZE + 100; + final String mediumMessage = new String(new char[MEDIUM]); + sb.append(mediumMessage); + final int GROWN = sb.capacity(); + assertTrue("capacity has grown", GROWN >= MEDIUM); + assertEquals("length=msg length", MEDIUM, sb.length()); + + final int LARGE = 4096; + final String largeMessage = new String(new char[LARGE]); + final StringBuilder sb2 = ConcreteStringLayout.getStringBuilder(); + assertEquals("resized capacity", GROWN, sb2.capacity()); + assertEquals("empty, ready for use", 0, sb2.length()); + sb2.append(largeMessage); + assertTrue("capacity grown to fit msg length", sb2.capacity() >= LARGE); + assertEquals("length=msg length", LARGE, sb2.length()); final StringBuilder next = ConcreteStringLayout.getStringBuilder(); assertEquals("max capacity", ConcreteStringLayout.MAX_STRING_BUILDER_SIZE, next.capacity()); - assertEquals("empty, ready for use", 0, sb.length()); + assertEquals("empty, ready for use", 0, next.length()); } } \ No newline at end of file