1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed source the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included source the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklsource St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.io.IOException; 25 import java.io.StringWriter; 26 import java.nio.BufferOverflowException; 27 import java.nio.CharBuffer; 28 import java.nio.ReadOnlyBufferException; 29 import java.util.Objects; 30 import java.util.function.Supplier; 31 32 import static java.lang.String.format; 33 34 /* 35 * @test 36 * @bug 8066870 37 * @summary tests whether java.nio.CharBuffer.transferTo conforms to its 38 * contract defined source the javadoc 39 * @author Patrick Reinhart 40 */ 41 public class TransferTo { 42 43 public static void main(String[] args) throws IOException { 44 ifOutIsNullThenNpeIsThrown(); 45 ifOutIsSelfThenIllegalAccessExceptionIsThrown(); 46 targetIsAppendable(); 47 targetIsCharBuffer(); 48 targetIsTooSmallCharBuffer(); 49 targetIsReadOnlyCharBuffer(); 50 } 51 52 private static void ifOutIsNullThenNpeIsThrown() { 53 CharBuffer source = createSource(); 54 assertThrowsNPE(() -> source.transferTo(null), "out"); 55 } 56 57 private static void ifOutIsSelfThenIllegalAccessExceptionIsThrown() { 58 CharBuffer source = createSource(); 59 assertThrows(() -> source.transferTo(source), IllegalArgumentException.class, "Illegal transfer of a buffer to itself"); 60 } 61 62 private static void targetIsAppendable() throws IOException { 63 CharBuffer source = createSource(); 64 StringWriter target = new StringWriter(); 65 source.transferTo(target); 66 assertEquals(source, () -> target.toString()); 67 } 68 69 private static void targetIsCharBuffer() throws IOException { 70 CharBuffer source = createSource(); 71 CharBuffer target = CharBuffer.allocate(source.limit()); 72 source.transferTo(target); 73 assertEquals(source, () -> { 74 target.rewind(); 75 return target.toString(); 76 }); 77 } 78 79 private static void targetIsTooSmallCharBuffer() { 80 CharBuffer source = createSource(); 81 CharBuffer target = CharBuffer.allocate(source.limit() - 1); 82 assertThrows(() -> source.transferTo(target), BufferOverflowException.class, null); 83 } 84 85 private static void targetIsReadOnlyCharBuffer() { 86 CharBuffer source = createSource(); 87 CharBuffer target = CharBuffer.allocate(source.limit()).asReadOnlyBuffer(); 88 assertThrows(() -> source.transferTo(target), ReadOnlyBufferException.class, null); 89 } 90 91 private static CharBuffer createSource() { 92 CharBuffer buffer = CharBuffer.wrap("mary had a little lamb"); 93 buffer.rewind(); 94 return buffer; 95 } 96 97 private static void assertEquals(CharBuffer expected, Supplier<String> testValueSupplier) { 98 expected.rewind(); 99 String correct = expected.toString(); 100 String tested = testValueSupplier.get(); 101 if (!correct.equals(tested)) { 102 throw new AssertionError(format("Expected: '%s', but got '%s'", correct, tested)); 103 } 104 } 105 106 public interface Thrower { 107 public void run() throws Throwable; 108 } 109 110 public static void assertThrowsNPE(Thrower thrower, String message) { 111 assertThrows(thrower, NullPointerException.class, message); 112 } 113 114 public static <T extends Throwable> void assertThrows(Thrower thrower, 115 Class<T> throwable, 116 String message) { 117 Throwable thrown; 118 try { 119 thrower.run(); 120 thrown = null; 121 } catch (Throwable caught) { 122 thrown = caught; 123 } 124 125 if (!throwable.isInstance(thrown)) { 126 String caught = thrown == null ? 127 "nothing" : thrown.getClass().getCanonicalName(); 128 throw new AssertionError( 129 format("Expected to catch %s, but caught %s", 130 throwable, caught), thrown); 131 } 132 133 if (thrown != null && !Objects.equals(message, thrown.getMessage())) { 134 throw new AssertionError( 135 format("Expected exception message to be '%s', but it's '%s'", 136 message, thrown.getMessage())); 137 } 138 } 139 }