Path: blob/master/test/jdk/java/io/Reader/ReaderBulkReadContract.java
41149 views
/*1* Copyright (c) 2015, 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*/2223import java.io.BufferedReader;24import java.io.ByteArrayInputStream;25import java.io.CharArrayReader;26import java.io.File;27import java.io.FileReader;28import java.io.FileWriter;29import java.io.IOException;30import java.io.InputStreamReader;31import java.io.LineNumberReader;32import java.io.PipedReader;33import java.io.PipedWriter;34import java.io.PushbackReader;35import java.io.Reader;36import java.io.StringReader;37import java.io.UncheckedIOException;38import java.util.Arrays;39import java.util.Collections;40import java.util.Iterator;41import java.util.LinkedList;42import java.util.List;43import java.util.concurrent.ConcurrentHashMap;44import java.util.function.Function;4546import static java.lang.String.format;4748/*49* @test50* @bug 802968951* @summary checks the bounds part of the contract of java.io.Reader.read(char[], int, int):52*53* 0 <= off <= off+len <= cbuf.length54*55* for publicly exported subtypes of java.io.Reader56*/57public class ReaderBulkReadContract {5859public static void main(String[] args) throws IOException {60ReaderBulkReadContract t = new ReaderBulkReadContract();61t.test();62}6364private void test() throws IOException {65Iterator<Object[]> args = args();66while (args.hasNext()) {67Object[] a = args.next();68Reader r = (Reader) a[0];69int size = (int) a[1];70int off = (int) a[2];71int len = (int) a[3];72try {73read(r, size, off, len);74} finally {75r.close();76}77}78}7980private Iterator<Object[]> args() {8182Integer[] lens = {Integer.MIN_VALUE, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, Integer.MAX_VALUE};83Integer[] offs = {Integer.MIN_VALUE, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, Integer.MAX_VALUE};84Integer[] sizes = {0, 1, 2, 3, 4, 5};85String[] contents = {"", "a", "ab"};8687List<Function<String, Reader>> fs = Arrays.asList(88(String s) -> new BufferedReader(new StringReader(s)),89(String s) -> new LineNumberReader(new StringReader(s)),90(String s) -> new CharArrayReader(s.toCharArray()),91(String s) -> new InputStreamReader(new ByteArrayInputStream(s.getBytes())),92(String s) -> newFileReader(s),93(String s) -> new PushbackReader(new StringReader(s)),94(String s) -> newPipedReader(s),95(String s) -> new StringReader(s)96);9798// The easiest way to produce a cartesian product from a small fixed number of sets99List<Object[]> tuples = Collections.synchronizedList(new LinkedList<>());100for (Integer len : lens)101for (Integer off : offs)102for (String s : contents)103for (Integer size : sizes)104for (Function<String, Reader> f : fs)105tuples.add(new Object[]{f.apply(s), size, off, len});106107return tuples.iterator();108}109110private void read(Reader r, int size, int off, int len) throws IOException {111IndexOutOfBoundsException ex = null;112try {113r.read(new char[size], off, len);114} catch (IndexOutOfBoundsException e) {115ex = e;116}117118boolean incorrectBounds = off < 0 || len < 0 || len > size - off;119boolean exceptionThrown = ex != null;120121if (incorrectBounds != exceptionThrown) { // incorrectBounds iff exceptionThrown122throw new AssertionError(format("r=%s, size=%s, off=%s, len=%s, incorrectBounds=%s, exceptionThrown=%s",123r, size, off, len, incorrectBounds, exceptionThrown));124}125}126127private static PipedReader newPipedReader(String contents) {128try (PipedWriter w = new PipedWriter()) {129PipedReader r = new PipedReader(w);130w.write(contents);131return r;132} catch (IOException e) {133throw new UncheckedIOException(e);134}135}136137private FileReader newFileReader(String contents) {138try {139// To not create an enormous amount of files140File f = cache.computeIfAbsent(contents,141ReaderBulkReadContract::createTempFileWithContents);142return new FileReader(f);143} catch (IOException e) {144throw new UncheckedIOException(e);145}146}147148private static File createTempFileWithContents(String contents) {149try {150File testDir = new File(System.getProperty("test.dir", "."));151File file = File.createTempFile("ReaderContract", "", testDir);152try (FileWriter w = new FileWriter(file)) {153w.write(contents);154}155return file;156} catch (IOException e) {157throw new UncheckedIOException(e);158}159}160161//162// To avoid myriads of tiny files a cache is used.163// ConcurrentHashMap.computeIfAbsent promises a crucial thing:164//165// ...The entire method invocation is performed atomically, so the166// function is applied at most once per key...167// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~168//169private final ConcurrentHashMap<String, File> cache = new ConcurrentHashMap<>();170}171172173