--- /dev/null 2017-11-19 09:22:58.000000000 +0100 +++ new/test/jdk/java/nio/Buffer/TransferTo.java 2017-11-19 09:22:57.000000000 +0100 @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2017, 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. + * + * This code is distributed source the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included source the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklsource St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.io.IOException; +import java.io.StringWriter; +import java.nio.BufferOverflowException; +import java.nio.CharBuffer; +import java.nio.ReadOnlyBufferException; +import java.util.Objects; +import java.util.function.Supplier; + +import static java.lang.String.format; + +/* + * @test + * @bug 8066870 + * @summary tests whether java.nio.CharBuffer.transferTo conforms to its + * contract defined source the javadoc + * @author Patrick Reinhart + */ +public class TransferTo { + + public static void main(String[] args) throws IOException { + ifOutIsNullThenNpeIsThrown(); + ifOutIsSelfThenIllegalAccessExceptionIsThrown(); + targetIsAppendable(); + targetIsCharBuffer(); + targetIsTooSmallCharBuffer(); + targetIsReadOnlyCharBuffer(); + } + + private static void ifOutIsNullThenNpeIsThrown() { + CharBuffer source = createSource(); + assertThrowsNPE(() -> source.transferTo(null), "out"); + } + + private static void ifOutIsSelfThenIllegalAccessExceptionIsThrown() { + CharBuffer source = createSource(); + assertThrows(() -> source.transferTo(source), IllegalArgumentException.class, "Illegal transfer of a buffer to itself"); + } + + private static void targetIsAppendable() throws IOException { + CharBuffer source = createSource(); + StringWriter target = new StringWriter(); + source.transferTo(target); + assertEquals(source, () -> target.toString()); + } + + private static void targetIsCharBuffer() throws IOException { + CharBuffer source = createSource(); + CharBuffer target = CharBuffer.allocate(source.limit()); + source.transferTo(target); + assertEquals(source, () -> { + target.rewind(); + return target.toString(); + }); + } + + private static void targetIsTooSmallCharBuffer() { + CharBuffer source = createSource(); + CharBuffer target = CharBuffer.allocate(source.limit() - 1); + assertThrows(() -> source.transferTo(target), BufferOverflowException.class, null); + } + + private static void targetIsReadOnlyCharBuffer() { + CharBuffer source = createSource(); + CharBuffer target = CharBuffer.allocate(source.limit()).asReadOnlyBuffer(); + assertThrows(() -> source.transferTo(target), ReadOnlyBufferException.class, null); + } + + private static CharBuffer createSource() { + CharBuffer buffer = CharBuffer.wrap("mary had a little lamb"); + buffer.rewind(); + return buffer; + } + + private static void assertEquals(CharBuffer expected, Supplier testValueSupplier) { + expected.rewind(); + String correct = expected.toString(); + String tested = testValueSupplier.get(); + if (!correct.equals(tested)) { + throw new AssertionError(format("Expected: '%s', but got '%s'", correct, tested)); + } + } + + public interface Thrower { + public void run() throws Throwable; + } + + public static void assertThrowsNPE(Thrower thrower, String message) { + assertThrows(thrower, NullPointerException.class, message); + } + + public static void assertThrows(Thrower thrower, + Class throwable, + String message) { + Throwable thrown; + try { + thrower.run(); + thrown = null; + } catch (Throwable caught) { + thrown = caught; + } + + if (!throwable.isInstance(thrown)) { + String caught = thrown == null ? + "nothing" : thrown.getClass().getCanonicalName(); + throw new AssertionError( + format("Expected to catch %s, but caught %s", + throwable, caught), thrown); + } + + if (thrown != null && !Objects.equals(message, thrown.getMessage())) { + throw new AssertionError( + format("Expected exception message to be '%s', but it's '%s'", + message, thrown.getMessage())); + } + } +}