/* * Copyright (c) 2011, 2013, 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 in 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 in 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 Franklin 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.
*/
/* @test * @bug 7006126 8020669 8024788 8019526 * @build BytesAndLines PassThroughFileSystem * @run testng BytesAndLines * @summary Unit test for methods for Files readAllBytes, readAllLines and * and write methods. * @key randomness
*/
// data for text files privatestaticfinal String EN_STRING = "The quick brown fox jumps over the lazy dog"; privatestaticfinal String JA_STRING = "\u65e5\u672c\u8a9e\u6587\u5b57\u5217";
// used for random byte content privatestatic Random RAND = new Random();
/** * Returns a byte[] of the given size with random content
*/ privatebyte[] genBytes(int size) { byte[] arr = newbyte[size];
RAND.nextBytes(arr); return arr;
}
// check expected bytes are read byte[] read = Files.readAllBytes(tmpfile);
assertTrue(Arrays.equals(read, expected), "Bytes read not the same as written");
}
/** * Linux specific test to exercise Files.readAllBytes on /proc. This is * special because file sizes are reported as 0 even though the file * has content.
*/ publicvoid testReadAllBytesOnProcFS() throws IOException { // read from procfs if (System.getProperty("os.name").equals("Linux")) {
Path statFile = Paths.get("/proc/self/stat"); byte[] data = Files.readAllBytes(statFile);
assertTrue(data.length > 0, "Files.readAllBytes('" + statFile + "') failed to read");
}
}
/** * Exercise Files.readAllBytes(Path) on custom file system. This is special * because readAllBytes was originally implemented to use FileChannel * and so may not be supported by custom file system providers.
*/ publicvoid testReadAllBytesOnCustomFS() throws IOException {
Path myfile = PassThroughFileSystem.create().getPath("myfile"); try { int size = 0; while (size <= 1024) { byte[] b1 = genBytes(size);
Files.write(myfile, b1); byte[] b2 = Files.readAllBytes(myfile);
assertTrue(Arrays.equals(b1, b2), "bytes not equal");
size += 512;
}
} finally {
Files.deleteIfExists(myfile);
}
}
/** * Exercise Files.write(Path, byte[], OpenOption...) on various sizes
*/ publicvoid testWriteBytes() throws IOException { int size = 0; while (size < 16*1024) {
testWriteBytes(size, false);
testWriteBytes(size, true);
size += 512;
}
}
/** * Linux specific test to exercise Files.readAllLines(Path) on /proc. This * is special because file sizes are reported as 0 even though the file * has content.
*/ publicvoid testReadAllLinesOnProcFS() throws IOException { if (System.getProperty("os.name").equals("Linux")) {
Path statFile = Paths.get("/proc/self/stat");
List<String> lines = Files.readAllLines(statFile);
assertTrue(lines.size() > 0, "Files.readAllLines('" + statFile + "') failed to read");
}
}
// a sample of malformed sequences
testReadAllLinesMalformedUTF8((byte)0xFF); // one-byte sequence
testReadAllLinesMalformedUTF8((byte)0xC0, (byte)0x80); // invalid first byte
testReadAllLinesMalformedUTF8((byte)0xC2, (byte)0x00); // invalid second byte
}
privatebyte[] encodeAsUTF8(String s) throws CharacterCodingException { // not using s.getBytes here so as to catch unmappable characters
ByteBuffer bb = UTF_8.newEncoder().encode(CharBuffer.wrap(s)); byte[] result = newbyte[bb.limit()];
bb.get(result);
assertTrue(bb.remaining() == 0); return result;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.