BytesAndLines.java 12.6 KB
Newer Older
1
/*
2
 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * 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
25
 * @bug 7006126 8020669 8024788 8019526
26
 * @build BytesAndLines PassThroughFileSystem
27
 * @run testng BytesAndLines
28 29 30 31
 * @summary Unit test for methods for Files readAllBytes, readAllLines and
 *     and write methods.
 */

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.OpenOption;
import static java.nio.file.StandardOpenOption.*;
import java.nio.charset.Charset;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.MalformedInputException;
import java.nio.charset.UnmappableCharacterException;
import static java.nio.charset.StandardCharsets.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.io.IOException;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static org.testng.Assert.*;

@Test(groups = "unit")
58 59
public class BytesAndLines {

60 61 62 63 64 65 66 67 68 69 70 71 72 73
    // data for text files
    private static final String EN_STRING = "The quick brown fox jumps over the lazy dog";
    private static final String JA_STRING = "\u65e5\u672c\u8a9e\u6587\u5b57\u5217";

    // used for random byte content
    private static Random RAND = new Random();

    // file used by most tests
    private Path tmpfile;

    @BeforeClass
    void setup() throws IOException {
        tmpfile = Files.createTempFile("blah", null);
    }
74

75 76 77
    @AfterClass
    void cleanup() throws IOException {
        Files.deleteIfExists(tmpfile);
78 79 80
    }

    /**
81
     * Returns a byte[] of the given size with random content
82
     */
83 84 85 86 87
    private byte[] genBytes(int size) {
        byte[] arr = new byte[size];
        RAND.nextBytes(arr);
        return arr;
    }
88

89 90 91 92
    /**
     * Exercise NullPointerException
     */
    public void testNulls() {
93
        Path file = Paths.get("foo");
94
        byte[] bytes = new byte[100];
95
        List<String> lines = Collections.emptyList();
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

        checkNullPointerException(() -> Files.readAllBytes(null));

        checkNullPointerException(() -> Files.write(null, bytes));
        checkNullPointerException(() -> Files.write(file, (byte[])null));
        checkNullPointerException(() -> Files.write(file, bytes, (OpenOption[])null));
        checkNullPointerException(() -> Files.write(file, bytes, new OpenOption[] { null } ));

        checkNullPointerException(() -> Files.readAllLines(null));
        checkNullPointerException(() -> Files.readAllLines(file, (Charset)null));
        checkNullPointerException(() -> Files.readAllLines(null, Charset.defaultCharset()));

        checkNullPointerException(() -> Files.write(null, lines));
        checkNullPointerException(() -> Files.write(file, (List<String>)null));
        checkNullPointerException(() -> Files.write(file, lines, (OpenOption[])null));
        checkNullPointerException(() -> Files.write(file, lines, new OpenOption[] { null } ));
        checkNullPointerException(() -> Files.write(null, lines, Charset.defaultCharset()));
        checkNullPointerException(() -> Files.write(file, null, Charset.defaultCharset()));
        checkNullPointerException(() -> Files.write(file, lines, (Charset)null));
        checkNullPointerException(() -> Files.write(file, lines, Charset.defaultCharset(), (OpenOption[])null));
        checkNullPointerException(() -> Files.write(file, lines, Charset.defaultCharset(), new OpenOption[] { null } ));
    }

    private void checkNullPointerException(Callable<?> c) {
120
        try {
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
            c.call();
            fail("NullPointerException expected");
        } catch (NullPointerException ignore) {
        } catch (Exception e) {
            fail(e + " not expected");
        }
    }

    /**
     * Exercise Files.readAllBytes(Path) on varied file sizes
     */
    public void testReadAllBytes() throws IOException {
        int size = 0;
        while (size <= 16*1024) {
            testReadAllBytes(size);
            size += 512;
        }
    }
139

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    private void testReadAllBytes(int size) throws IOException {
        // write bytes to file (random content)
        byte[] expected = genBytes(size);
        Files.write(tmpfile, expected);

        // 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.
     */
    public void testReadAllBytesOnProcFS() throws IOException {
156 157
        // read from procfs
        if (System.getProperty("os.name").equals("Linux")) {
158 159
            Path statFile = Paths.get("/proc/self/stat");
            byte[] data = Files.readAllBytes(statFile);
160 161
            assertTrue(data.length > 0, "Files.readAllBytes('" + statFile + "') failed to read");
        }
162 163
    }

164 165 166 167 168 169 170
    /**
     * 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.
     */
    public void testReadAllBytesOnCustomFS() throws IOException {
        Path myfile = PassThroughFileSystem.create().getPath("myfile");
171
        try {
172 173 174 175 176 177 178
            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;
179 180
            }
        } finally {
181
            Files.deleteIfExists(myfile);
182 183 184 185
        }
    }

    /**
186
     * Exercise Files.write(Path, byte[], OpenOption...) on various sizes
187
     */
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    public void testWriteBytes() throws IOException {
        int size = 0;
        while (size < 16*1024) {
            testWriteBytes(size, false);
            testWriteBytes(size, true);
            size += 512;
        }
    }

    private void testWriteBytes(int size, boolean append) throws IOException {
        byte[] bytes = genBytes(size);
        Path result = Files.write(tmpfile, bytes);
        assertTrue(result == tmpfile);
        if (append) {
            Files.write(tmpfile, bytes, APPEND);
            assertTrue(Files.size(tmpfile) == size*2);
        }

        byte[] expected;
        if (append) {
            expected = new byte[size << 1];
            System.arraycopy(bytes, 0, expected, 0, bytes.length);
            System.arraycopy(bytes, 0, expected, bytes.length, bytes.length);
        } else {
            expected = bytes;
        }

        byte[] read = Files.readAllBytes(tmpfile);
        assertTrue(Arrays.equals(read, expected), "Bytes read not the same as written");
    }
218

219 220 221 222 223 224 225
    /**
     * Exercise Files.readAllLines(Path, Charset)
     */
    public void testReadAllLines() throws IOException {
        // zero lines
        Files.write(tmpfile, new byte[0]);
        List<String> lines = Files.readAllLines(tmpfile, US_ASCII);
226 227
            assertTrue(lines.isEmpty(), "No line expected");

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
        // one line
        byte[] hi = { (byte)'h', (byte)'i' };
        Files.write(tmpfile, hi);
        lines = Files.readAllLines(tmpfile, US_ASCII);
        assertTrue(lines.size() == 1, "One line expected");
        assertTrue(lines.get(0).equals("hi"), "'Hi' expected");

        // two lines using platform's line separator
        List<String> expected = Arrays.asList("hi", "there");
        Files.write(tmpfile, expected, US_ASCII);
        assertTrue(Files.size(tmpfile) > 0, "File is empty");
        lines = Files.readAllLines(tmpfile, US_ASCII);
        assertTrue(lines.equals(expected), "Unexpected lines");

        // MalformedInputException
        byte[] bad = { (byte)0xff, (byte)0xff };
        Files.write(tmpfile, bad);
        try {
            Files.readAllLines(tmpfile, US_ASCII);
            fail("MalformedInputException expected");
        } catch (MalformedInputException ignore) { }
    }
250

251 252 253 254 255 256 257 258 259 260
    /**
     * 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.
     */
    public void 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");
261 262 263 264
        }
    }

    /**
265
     * Exercise Files.readAllLines(Path)
266
     */
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    public void testReadAllLinesUTF8() throws IOException {
        Files.write(tmpfile, encodeAsUTF8(EN_STRING + "\n" + JA_STRING));

        List<String> lines = Files.readAllLines(tmpfile);
        assertTrue(lines.size() == 2, "Read " + lines.size() + " lines instead of 2");
        assertTrue(lines.get(0).equals(EN_STRING));
        assertTrue(lines.get(1).equals(JA_STRING));

        // 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
    }

    private byte[] 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 = new byte[bb.limit()];
        bb.get(result);
        assertTrue(bb.remaining() == 0);
        return result;
    }

    private void testReadAllLinesMalformedUTF8(byte... bytes) throws IOException {
        Files.write(tmpfile, bytes);
292
        try {
293 294 295 296
            Files.readAllLines(tmpfile);
            fail("MalformedInputException expected");
        } catch (MalformedInputException ignore) { }
    }
297

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    /**
     * Exercise Files.write(Path, Iterable<? extends CharSequence>, Charset, OpenOption...)
     */
    public void testWriteLines() throws IOException {
        // zero lines
        Path result = Files.write(tmpfile, Collections.<String>emptyList(), US_ASCII);
        assert(Files.size(tmpfile) == 0);
        assert(result == tmpfile);

        // two lines
        List<String> lines = Arrays.asList("hi", "there");
        Files.write(tmpfile, lines, US_ASCII);
        List<String> actual = Files.readAllLines(tmpfile, US_ASCII);
        assertTrue(actual.equals(lines), "Unexpected lines");

        // append two lines
        Files.write(tmpfile, lines, US_ASCII, APPEND);
        List<String> expected = new ArrayList<>();
        expected.addAll(lines);
        expected.addAll(lines);
        assertTrue(expected.size() == 4, "List should have 4 elements");
        actual = Files.readAllLines(tmpfile, US_ASCII);
        assertTrue(actual.equals(expected), "Unexpected lines");

        // UnmappableCharacterException
        try {
            String s = "\u00A0\u00A1";
            Files.write(tmpfile, Arrays.asList(s), US_ASCII);
            fail("UnmappableCharacterException expected");
        } catch (UnmappableCharacterException ignore) { }
328 329
    }

330 331 332 333 334 335 336 337
    /**
     * Exercise Files.write(Path, Iterable<? extends CharSequence>, OpenOption...)
     */
    public void testWriteLinesUTF8() throws IOException {
        List<String> lines = Arrays.asList(EN_STRING, JA_STRING);
        Files.write(tmpfile, lines);
        List<String> actual = Files.readAllLines(tmpfile, UTF_8);
        assertTrue(actual.equals(lines), "Unexpected lines");
338 339
    }
}