< prev index next >

test/jdk/java/lang/StringBuffer/AppendCharSequence.java

Print this page
rev 53655 : imported patch 8218228-The-constructor-StringBuffer-CharSequence-violates-spec-for-negatively-sized-argument

*** 1,7 **** /* ! * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. --- 1,7 ---- /* ! * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.
*** 20,30 **** * or visit www.oracle.com if you need additional information or have any * questions. */ /* @test ! * @bug 4812591 4705328 5019111 * @summary Test append and insert methods with CharSequence params * @key randomness */ import java.util.Random; --- 20,30 ---- * or visit www.oracle.com if you need additional information or have any * questions. */ /* @test ! * @bug 4812591 4705328 5019111 8218228 * @summary Test append and insert methods with CharSequence params * @key randomness */ import java.util.Random;
*** 228,235 **** --- 228,264 ---- StringBuffer sb2 = new StringBuffer(cs); if (!sb.toString().equals(sb2.toString())) { throw new RuntimeException("CharSequence constructor failure"); } } + checkNegativeLenCharSeq(-1); + checkNegativeLenCharSeq(-16); + checkNegativeLenCharSeq(-17); + checkNegativeLenCharSeq(Integer.MIN_VALUE); } + // Test constructing from CharSequence of negative length + private static void checkNegativeLenCharSeq(int len) { + try { + CharSequence seq = new MyNegativeLenCharSeq(len); + StringBuffer sb = new StringBuffer(seq); + } catch (NegativeArraySizeException expected) { + } catch (Throwable exc) { + throw new RuntimeException("Unexpected: " + exc, exc); + } + } + + private static class MyNegativeLenCharSeq implements CharSequence { + int length; + MyNegativeLenCharSeq(int length) { + this.length = length; + } + public char charAt(int i) { + throw new UnsupportedOperationException(); + } + public int length() { return length; } + public CharSequence subSequence(int st, int e) { + throw new UnsupportedOperationException(); + } + public String toString() { return ""; } + } }
< prev index next >