3471N/A/*
3471N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3471N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3471N/A *
3471N/A * This code is free software; you can redistribute it and/or modify it
3471N/A * under the terms of the GNU General Public License version 2 only, as
3471N/A * published by the Free Software Foundation.
3471N/A *
3471N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3471N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3471N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3471N/A * version 2 for more details (a copy is included in the LICENSE file that
3471N/A * accompanied this code).
3471N/A *
3471N/A * You should have received a copy of the GNU General Public License version
3471N/A * 2 along with this work; if not, write to the Free Software Foundation,
3471N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3471N/A *
3471N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3471N/A * or visit www.oracle.com if you need additional information or have any
3471N/A * questions.
3471N/A */
3471N/A
3471N/A/* @test
3471N/A * @bug 7006126
3471N/A * @summary Unit test for methods for Files readAllBytes, readAllLines and
3471N/A * and write methods.
3471N/A */
3471N/A
3471N/Aimport java.nio.file.*;
3471N/Aimport static java.nio.file.Files.*;
3471N/Aimport java.io.*;
3471N/Aimport java.util.*;
3471N/Aimport java.nio.charset.*;
3471N/A
3471N/Apublic class BytesAndLines {
3471N/A static final Random rand = new Random();
3471N/A
3471N/A static final Charset US_ASCII = Charset.forName("US-ASCII");
3471N/A
3471N/A public static void main(String[] args) throws IOException {
3471N/A testReadAndWriteBytes();
3471N/A testReadLines();
3471N/A testWriteLines();
3471N/A }
3471N/A
3471N/A /**
3471N/A * Test readAllBytes(Path) and write(Path, byte[], OpenOption...)
3471N/A */
3471N/A static void testReadAndWriteBytes() throws IOException {
3471N/A // exercise methods with various sizes
3471N/A testReadAndWriteBytes(0);
3471N/A for (int i=0; i<100; i++) {
3471N/A testReadAndWriteBytes(rand.nextInt(32000));
3471N/A }
3471N/A
3471N/A // NullPointerException
3471N/A Path file = Paths.get("foo");
3471N/A List<String> lines = Collections.emptyList();
3471N/A try {
3471N/A readAllBytes(null);
3471N/A throw new RuntimeException("NullPointerException expected");
3471N/A } catch (NullPointerException ignore) { }
3471N/A try {
3471N/A write(null, lines, Charset.defaultCharset());
3471N/A throw new RuntimeException("NullPointerException expected");
3471N/A } catch (NullPointerException ignore) { }
3471N/A try {
3471N/A write(file, null, Charset.defaultCharset());
3471N/A throw new RuntimeException("NullPointerException expected");
3471N/A } catch (NullPointerException ignore) { }
3471N/A try {
3471N/A write(file, lines, null);
3471N/A throw new RuntimeException("NullPointerException expected");
3471N/A } catch (NullPointerException ignore) { }
3471N/A try {
3471N/A write(file, lines, Charset.defaultCharset(), (OpenOption[])null);
3471N/A throw new RuntimeException("NullPointerException expected");
3471N/A } catch (NullPointerException ignore) { }
3471N/A try {
3471N/A OpenOption[] opts = { null };
3471N/A write(file, lines, Charset.defaultCharset(), opts);
3471N/A throw new RuntimeException("NullPointerException expected");
3471N/A } catch (NullPointerException ignore) { }
3471N/A }
3471N/A
3471N/A
3471N/A static void testReadAndWriteBytes(int size) throws IOException {
3471N/A Path path = createTempFile("blah", null);
3471N/A try {
3471N/A boolean append = rand.nextBoolean();
3471N/A
3471N/A byte[] b1 = new byte[size];
3471N/A rand.nextBytes(b1);
3471N/A
3471N/A byte[] b2 = (append) ? new byte[size] : new byte[0];
3471N/A rand.nextBytes(b2);
3471N/A
3471N/A // write method should create file if it doesn't exist
3471N/A if (rand.nextBoolean())
3471N/A delete(path);
3471N/A
3471N/A // write bytes to file
3471N/A Path target = write(path, b1);
3471N/A assertTrue(target==path, "Unexpected path");
3471N/A assertTrue(size(path) == b1.length, "Unexpected file size");
3471N/A
3471N/A // append bytes to file (might be 0 bytes)
3471N/A write(path, b2, StandardOpenOption.APPEND);
3471N/A assertTrue(size(path) == b1.length + b2.length, "Unexpected file size");
3471N/A
3471N/A // read entire file
3471N/A byte[] read = readAllBytes(path);
3471N/A
3471N/A // check bytes are correct
3471N/A byte[] expected;
3471N/A if (append) {
3471N/A expected = new byte[b1.length + b2.length];
3471N/A System.arraycopy(b1, 0, expected, 0, b1.length);
3471N/A System.arraycopy(b2, 0, expected, b1.length, b2.length);
3471N/A } else {
3471N/A expected = b1;
3471N/A }
3471N/A assertTrue(Arrays.equals(read, expected),
3471N/A "Bytes read not the same as bytes written");
3471N/A } finally {
3471N/A deleteIfExists(path);
3471N/A }
3471N/A }
3471N/A
3471N/A /**
3471N/A * Test readAllLines(Path,Charset)
3471N/A */
3471N/A static void testReadLines() throws IOException {
3471N/A Path tmpfile = createTempFile("blah", "txt");
3471N/A try {
3471N/A List<String> lines;
3471N/A
3471N/A // zero lines
3471N/A assertTrue(size(tmpfile) == 0, "File should be empty");
3471N/A lines = readAllLines(tmpfile, US_ASCII);
3471N/A assertTrue(lines.isEmpty(), "No line expected");
3471N/A
3471N/A // one line
3471N/A byte[] hi = { (byte)'h', (byte)'i' };
3471N/A write(tmpfile, hi);
3471N/A lines = readAllLines(tmpfile, US_ASCII);
3471N/A assertTrue(lines.size() == 1, "One line expected");
3471N/A assertTrue(lines.get(0).equals("hi"), "'Hi' expected");
3471N/A
3471N/A // two lines using platform's line separator
3471N/A List<String> expected = Arrays.asList("hi", "there");
3471N/A write(tmpfile, expected, US_ASCII);
3471N/A assertTrue(size(tmpfile) > 0, "File is empty");
3471N/A lines = readAllLines(tmpfile, US_ASCII);
3471N/A assertTrue(lines.equals(expected), "Unexpected lines");
3471N/A
3471N/A // MalformedInputException
3471N/A byte[] bad = { (byte)0xff, (byte)0xff };
3471N/A write(tmpfile, bad);
3471N/A try {
3471N/A readAllLines(tmpfile, US_ASCII);
3471N/A throw new RuntimeException("MalformedInputException expected");
3471N/A } catch (MalformedInputException ignore) { }
3471N/A
3471N/A
3471N/A // NullPointerException
3471N/A try {
3471N/A readAllLines(null, US_ASCII);
3471N/A throw new RuntimeException("NullPointerException expected");
3471N/A } catch (NullPointerException ignore) { }
3471N/A try {
3471N/A readAllLines(tmpfile, null);
3471N/A throw new RuntimeException("NullPointerException expected");
3471N/A } catch (NullPointerException ignore) { }
3471N/A
3471N/A } finally {
3471N/A delete(tmpfile);
3471N/A }
3471N/A }
3471N/A
3471N/A /**
3471N/A * Test write(Path,Iterable<? extends CharSequence>,Charset,OpenOption...)
3471N/A */
3471N/A static void testWriteLines() throws IOException {
3471N/A Path tmpfile = createTempFile("blah", "txt");
3471N/A try {
3471N/A // write method should create file if it doesn't exist
3471N/A if (rand.nextBoolean())
3471N/A delete(tmpfile);
3471N/A
3471N/A // zero lines
3471N/A Path result = write(tmpfile, Collections.<String>emptyList(), US_ASCII);
3471N/A assert(size(tmpfile) == 0);
3471N/A assert(result == tmpfile);
3471N/A
3471N/A // two lines
3471N/A List<String> lines = Arrays.asList("hi", "there");
3471N/A write(tmpfile, lines, US_ASCII);
3471N/A List<String> actual = readAllLines(tmpfile, US_ASCII);
3471N/A assertTrue(actual.equals(lines), "Unexpected lines");
3471N/A
3471N/A // append two lines
3471N/A write(tmpfile, lines, US_ASCII, StandardOpenOption.APPEND);
3471N/A List<String> expected = new ArrayList<String>();
3471N/A expected.addAll(lines);
3471N/A expected.addAll(lines);
3471N/A assertTrue(expected.size() == 4, "List should have 4 elements");
3471N/A actual = readAllLines(tmpfile, US_ASCII);
3471N/A assertTrue(actual.equals(expected), "Unexpected lines");
3471N/A
3471N/A // UnmappableCharacterException
3471N/A try {
3471N/A String s = "\u00A0\u00A1";
3471N/A write(tmpfile, Arrays.asList(s), US_ASCII);
3471N/A throw new RuntimeException("UnmappableCharacterException expected");
3471N/A } catch (UnmappableCharacterException ignore) { }
3471N/A
3471N/A // NullPointerException
3471N/A try {
3471N/A write(null, lines, US_ASCII);
3471N/A throw new RuntimeException("NullPointerException expected");
3471N/A } catch (NullPointerException ignore) { }
3471N/A try {
3471N/A write(tmpfile, null, US_ASCII);
3471N/A throw new RuntimeException("NullPointerException expected");
3471N/A } catch (NullPointerException ignore) { }
3471N/A try {
3471N/A write(tmpfile, lines, null);
3471N/A throw new RuntimeException("NullPointerException expected");
3471N/A } catch (NullPointerException ignore) { }
3471N/A try {
3471N/A write(tmpfile, lines, US_ASCII, (OpenOption[])null);
3471N/A throw new RuntimeException("NullPointerException expected");
3471N/A } catch (NullPointerException ignore) { }
3471N/A try {
3471N/A OpenOption[] opts = { (OpenOption)null };
3471N/A write(tmpfile, lines, US_ASCII, opts);
3471N/A throw new RuntimeException("NullPointerException expected");
3471N/A } catch (NullPointerException ignore) { }
3471N/A
3471N/A } finally {
3471N/A delete(tmpfile);
3471N/A }
3471N/A
3471N/A }
3471N/A
3471N/A static void assertTrue(boolean expr, String errmsg) {
3471N/A if (!expr)
3471N/A throw new RuntimeException(errmsg);
3471N/A }
3471N/A}