Path: blob/master/test/jdk/java/util/StringJoiner/StringJoinerOomUtf16Test.java
41149 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/**23* @test24* @bug 826523725* @summary tests StringJoiner OOME when joining sub-max-length Strings26* @modules java.base/jdk.internal.util27* @requires vm.bits == "64" & os.maxMemory > 4G28* @run testng/othervm -Xmx4g -XX:+CompactStrings StringJoinerOomUtf16Test29*/3031import org.testng.annotations.Test;3233import static jdk.internal.util.ArraysSupport.SOFT_MAX_ARRAY_LENGTH;34import static org.testng.Assert.fail;3536import java.util.StringJoiner;373839@Test(groups = {"unit","string","util","libs"})40public class StringJoinerOomUtf16Test {4142// the sum of lengths of the following two strings is way less than43// SOFT_MAX_ARRAY_LENGTH, but the byte[] array holding the UTF16 representation44// would need to be bigger than Integer.MAX_VALUE...45private static final String HALF_MAX_LATIN1_STRING =46"*".repeat(SOFT_MAX_ARRAY_LENGTH >> 1);47private static final String OVERFLOW_UTF16_STRING =48"\u017D".repeat(((Integer.MAX_VALUE - SOFT_MAX_ARRAY_LENGTH) >> 1) + 1);4950public void OOM1() {51try {52new StringJoiner("")53.add(HALF_MAX_LATIN1_STRING)54.add(OVERFLOW_UTF16_STRING)55.toString();56fail("Should have thrown OutOfMemoryError");57} catch (OutOfMemoryError ex) {58System.out.println("Expected: " + ex);59}60}6162public void OOM2() {63try {64new StringJoiner(HALF_MAX_LATIN1_STRING)65.add("")66.add(OVERFLOW_UTF16_STRING)67.toString();68fail("Should have thrown OutOfMemoryError");69} catch (OutOfMemoryError ex) {70System.out.println("Expected: " + ex);71}72}7374public void OOM3() {75try {76new StringJoiner(OVERFLOW_UTF16_STRING)77.add("")78.add(HALF_MAX_LATIN1_STRING)79.toString();80fail("Should have thrown OutOfMemoryError");81} catch (OutOfMemoryError ex) {82System.out.println("Expected: " + ex);83}84}8586public void OOM4() {87try {88new StringJoiner("", HALF_MAX_LATIN1_STRING, OVERFLOW_UTF16_STRING)89.toString();90fail("Should have thrown OutOfMemoryError");91} catch (OutOfMemoryError ex) {92System.out.println("Expected: " + ex);93}94}95}96979899