This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch 8.5.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 5159b4966a1da42a6868f958822b7f4881d3333c Author: Mark Thomas <ma...@apache.org> AuthorDate: Wed Jan 3 10:13:33 2024 +0000 Refactor tests that require large heap to new category These tests are still disabled by default but: - no not appear as skipped tests in a standard test run - can be enabled with a build property rather than a code change --- test/org/apache/tomcat/util/buf/TestByteChunk.java | 58 +++++++++++---------- .../tomcat/util/buf/TestByteChunkLargeHeap.java | 60 ++++++++++++++++++++++ test/org/apache/tomcat/util/buf/TestCharChunk.java | 35 ++++--------- ...tCharChunk.java => TestCharChunkLargeHeap.java} | 47 ++--------------- 4 files changed, 104 insertions(+), 96 deletions(-) diff --git a/test/org/apache/tomcat/util/buf/TestByteChunk.java b/test/org/apache/tomcat/util/buf/TestByteChunk.java index 618cd66a01..ce8234230e 100644 --- a/test/org/apache/tomcat/util/buf/TestByteChunk.java +++ b/test/org/apache/tomcat/util/buf/TestByteChunk.java @@ -16,17 +16,17 @@ */ package org.apache.tomcat.util.buf; -import java.io.IOException; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.io.UnsupportedEncodingException; -import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; -import org.apache.tomcat.util.buf.ByteChunk.ByteOutputChannel; - /** * Test cases for {@link ByteChunk}. */ @@ -144,33 +144,39 @@ public class TestByteChunk { } - @Ignore // Requires a 6GB heap (on markt's desktop - YMMV) @Test - public void testAppend() throws Exception { - ByteChunk bc = new ByteChunk(); - bc.setByteOutputChannel(new Sink()); - // Defaults to no limit + public void testSerialization() throws Exception { + String data = "Hello world!"; + byte[] bytes = data.getBytes(StandardCharsets.UTF_8); - byte data[] = new byte[32 * 1024 * 1024]; + ByteChunk bcIn = new ByteChunk(); + bcIn.setBytes(bytes, 0, bytes.length); + bcIn.setCharset(StandardCharsets.UTF_8); - for (int i = 0; i < 100; i++) { - bc.append(data, 0, data.length); - } - - Assert.assertEquals(AbstractChunk.ARRAY_MAX_SIZE, bc.getBuffer().length); - } + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(bcIn); + oos.close(); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ObjectInputStream ois = new ObjectInputStream(bais); + ByteChunk bcOut = (ByteChunk) ois.readObject(); - public class Sink implements ByteOutputChannel { + Assert.assertArrayEquals(bytes, bcOut.getBytes()); + Assert.assertEquals(bcIn.getCharset(), bcOut.getCharset()); + } - @Override - public void realWriteBytes(byte[] cbuf, int off, int len) throws IOException { - // NO-OP - } - @Override - public void realWriteBytes(ByteBuffer from) throws IOException { - // NO-OP - } + @Test + public void testToString() { + ByteChunk bc = new ByteChunk(); + Assert.assertNull(bc.toString()); + byte[] data = new byte[8]; + bc.setBytes(data, 0, data.length); + Assert.assertNotNull(bc.toString()); + bc.recycle(); + // toString() should behave consistently for new ByteChunk and + // immediately after a call to recycle(). + Assert.assertNull(bc.toString()); } } diff --git a/test/org/apache/tomcat/util/buf/TestByteChunkLargeHeap.java b/test/org/apache/tomcat/util/buf/TestByteChunkLargeHeap.java new file mode 100644 index 0000000000..e0b0f2f605 --- /dev/null +++ b/test/org/apache/tomcat/util/buf/TestByteChunkLargeHeap.java @@ -0,0 +1,60 @@ +/* + * 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. + */ +package org.apache.tomcat.util.buf; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.tomcat.util.buf.ByteChunk.ByteOutputChannel; + +/** + * Test cases for {@link ByteChunk} that require a large heap. + */ +public class TestByteChunkLargeHeap { + + @Test + public void testAppend() throws Exception { + ByteChunk bc = new ByteChunk(); + bc.setByteOutputChannel(new Sink()); + // Defaults to no limit + + byte data[] = new byte[32 * 1024 * 1024]; + + for (int i = 0; i < 100; i++) { + bc.append(data, 0, data.length); + } + + Assert.assertEquals(AbstractChunk.ARRAY_MAX_SIZE, bc.getBuffer().length); + } + + + public static class Sink implements ByteOutputChannel { + + @Override + public void realWriteBytes(byte[] cbuf, int off, int len) throws IOException { + // NO-OP + } + + @Override + public void realWriteBytes(ByteBuffer from) throws IOException { + // NO-OP + } + } +} diff --git a/test/org/apache/tomcat/util/buf/TestCharChunk.java b/test/org/apache/tomcat/util/buf/TestCharChunk.java index 36f67bb0ee..9b517b9f6b 100644 --- a/test/org/apache/tomcat/util/buf/TestCharChunk.java +++ b/test/org/apache/tomcat/util/buf/TestCharChunk.java @@ -16,14 +16,9 @@ */ package org.apache.tomcat.util.buf; -import java.io.IOException; - import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; -import org.apache.tomcat.util.buf.CharChunk.CharOutputChannel; - /** * Test cases for {@link CharChunk}. */ @@ -69,28 +64,16 @@ public class TestCharChunk { } - @Ignore // Requires an 11GB heap (on markt's desktop - YMMV) @Test - public void testAppend() throws Exception { + public void testToString() { CharChunk cc = new CharChunk(); - cc.setCharOutputChannel(new Sink()); - // Defaults to no limit - - char data[] = new char[32 * 1024 * 1024]; - - for (int i = 0; i < 100; i++) { - cc.append(data, 0, data.length); - } - - Assert.assertEquals(AbstractChunk.ARRAY_MAX_SIZE, cc.getBuffer().length); - } - - - public class Sink implements CharOutputChannel { - - @Override - public void realWriteChars(char[] cbuf, int off, int len) throws IOException { - // NO-OP - } + Assert.assertNull(cc.toString()); + char[] data = new char[8]; + cc.setChars(data, 0, data.length); + Assert.assertNotNull(cc.toString()); + cc.recycle(); + // toString() should behave consistently for new ByteChunk and + // immediately after a call to recycle(). + Assert.assertNull(cc.toString()); } } diff --git a/test/org/apache/tomcat/util/buf/TestCharChunk.java b/test/org/apache/tomcat/util/buf/TestCharChunkLargeHeap.java similarity index 51% copy from test/org/apache/tomcat/util/buf/TestCharChunk.java copy to test/org/apache/tomcat/util/buf/TestCharChunkLargeHeap.java index 36f67bb0ee..fd6820b563 100644 --- a/test/org/apache/tomcat/util/buf/TestCharChunk.java +++ b/test/org/apache/tomcat/util/buf/TestCharChunkLargeHeap.java @@ -19,7 +19,6 @@ package org.apache.tomcat.util.buf; import java.io.IOException; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; import org.apache.tomcat.util.buf.CharChunk.CharOutputChannel; @@ -27,49 +26,8 @@ import org.apache.tomcat.util.buf.CharChunk.CharOutputChannel; /** * Test cases for {@link CharChunk}. */ -public class TestCharChunk { +public class TestCharChunkLargeHeap { - @Test - public void testEndsWith() { - CharChunk cc = new CharChunk(); - Assert.assertFalse(cc.endsWith("test")); - cc.setChars("xxtestxx".toCharArray(), 2, 4); - Assert.assertTrue(cc.endsWith("")); - Assert.assertTrue(cc.endsWith("t")); - Assert.assertTrue(cc.endsWith("st")); - Assert.assertTrue(cc.endsWith("test")); - Assert.assertFalse(cc.endsWith("x")); - Assert.assertFalse(cc.endsWith("xxtest")); - } - - - @Test - public void testIndexOf_String() { - char[] chars = "Hello\u00a0world".toCharArray(); - final int len = chars.length; - - CharChunk cc = new CharChunk(); - cc.setChars(chars, 0, len); - - Assert.assertEquals(0, cc.indexOf("Hello", 0, "Hello".length(), 0)); - Assert.assertEquals(2, cc.indexOf("ll", 0, 2, 0)); - Assert.assertEquals(2, cc.indexOf("Hello", 2, 2, 0)); - - Assert.assertEquals(7, cc.indexOf("o", 0, 1, 5)); - - // Does work outside of 0-127 (unlike ByteChunk) - Assert.assertEquals(5, cc.indexOf("\u00a0", 0, 1, 0)); - - cc.setChars(chars, 6, 5); - Assert.assertEquals(1, cc.indexOf("o", 0, 1, 0)); - - cc.setChars(chars, 6, 2); - Assert.assertEquals(0, cc.indexOf("wo", 0, 1, 0)); - Assert.assertEquals(-1, cc.indexOf("d", 0, 1, 0)); - } - - - @Ignore // Requires an 11GB heap (on markt's desktop - YMMV) @Test public void testAppend() throws Exception { CharChunk cc = new CharChunk(); @@ -86,11 +44,12 @@ public class TestCharChunk { } - public class Sink implements CharOutputChannel { + public static class Sink implements CharOutputChannel { @Override public void realWriteChars(char[] cbuf, int off, int len) throws IOException { // NO-OP } } + } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org