1 /* 2 * Copyright (c) 2011, 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 in 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 in 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 Franklin 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 /* @test 25 * @bug 7006126 26 * @summary Unit test for methods for Files readAllBytes, readAllLines and 27 * and write methods. 28 */ 29 30 import java.nio.file.*; 31 import static java.nio.file.Files.*; 32 import java.io.*; 33 import java.util.*; 34 import java.nio.charset.*; 35 36 public class BytesAndLines { 37 static final Random rand = new Random(); 38 39 static final Charset US_ASCII = Charset.forName("US-ASCII"); 40 41 public static void main(String[] args) throws IOException { 42 testReadAndWriteBytes(); 43 testReadLines(); 44 testWriteLines(); 45 } 46 47 /** 48 * Test readAllBytes(Path) and write(Path, byte[], OpenOption...) 49 */ 50 static void testReadAndWriteBytes() throws IOException { 51 // exercise methods with various sizes 52 testReadAndWriteBytes(0); 53 for (int i=0; i<100; i++) { 54 testReadAndWriteBytes(rand.nextInt(32000)); 55 } 56 57 // NullPointerException 58 Path file = Paths.get("foo"); 59 List<String> lines = Collections.emptyList(); 60 try { 61 readAllBytes(null); 62 throw new RuntimeException("NullPointerException expected"); 63 } catch (NullPointerException ignore) { } 64 try { 65 write(null, lines, Charset.defaultCharset()); 66 throw new RuntimeException("NullPointerException expected"); 67 } catch (NullPointerException ignore) { } 68 try { 69 write(file, null, Charset.defaultCharset()); 70 throw new RuntimeException("NullPointerException expected"); 71 } catch (NullPointerException ignore) { } 72 try { 73 write(file, lines, null); 74 throw new RuntimeException("NullPointerException expected"); 75 } catch (NullPointerException ignore) { } 76 try { 77 write(file, lines, Charset.defaultCharset(), (OpenOption[])null); 78 throw new RuntimeException("NullPointerException expected"); 79 } catch (NullPointerException ignore) { } 80 try { 81 OpenOption[] opts = { null }; 82 write(file, lines, Charset.defaultCharset(), opts); 83 throw new RuntimeException("NullPointerException expected"); 84 } catch (NullPointerException ignore) { } 85 } 86 87 88 static void testReadAndWriteBytes(int size) throws IOException { 89 Path path = createTempFile("blah", null); 90 try { 91 boolean append = rand.nextBoolean(); 92 93 byte[] b1 = new byte[size]; 94 rand.nextBytes(b1); 95 96 byte[] b2 = (append) ? new byte[size] : new byte[0]; 97 rand.nextBytes(b2); 98 99 // write method should create file if it doesn't exist 100 if (rand.nextBoolean()) 101 delete(path); 102 103 // write bytes to file 104 Path target = write(path, b1); 105 assertTrue(target==path, "Unexpected path"); 106 assertTrue(size(path) == b1.length, "Unexpected file size"); 107 108 // append bytes to file (might be 0 bytes) 109 write(path, b2, StandardOpenOption.APPEND); 110 assertTrue(size(path) == b1.length + b2.length, "Unexpected file size"); 111 112 // read entire file 113 byte[] read = readAllBytes(path); 114 115 // check bytes are correct 116 byte[] expected; 117 if (append) { 118 expected = new byte[b1.length + b2.length]; 119 System.arraycopy(b1, 0, expected, 0, b1.length); 120 System.arraycopy(b2, 0, expected, b1.length, b2.length); 121 } else { 122 expected = b1; 123 } 124 assertTrue(Arrays.equals(read, expected), 125 "Bytes read not the same as bytes written"); 126 } finally { 127 deleteIfExists(path); 128 } 129 } 130 131 /** 132 * Test readAllLines(Path,Charset) 133 */ 134 static void testReadLines() throws IOException { 135 Path tmpfile = createTempFile("blah", "txt"); 136 try { 137 List<String> lines; 138 139 // zero lines 140 assertTrue(size(tmpfile) == 0, "File should be empty"); 141 lines = readAllLines(tmpfile, US_ASCII); 142 assertTrue(lines.isEmpty(), "No line expected"); 143 144 // one line 145 byte[] hi = { (byte)'h', (byte)'i' }; 146 write(tmpfile, hi); 147 lines = readAllLines(tmpfile, US_ASCII); 148 assertTrue(lines.size() == 1, "One line expected"); 149 assertTrue(lines.get(0).equals("hi"), "'Hi' expected"); 150 151 // two lines using platform's line separator 152 List<String> expected = Arrays.asList("hi", "there"); 153 write(tmpfile, expected, US_ASCII); 154 assertTrue(size(tmpfile) > 0, "File is empty"); 155 lines = readAllLines(tmpfile, US_ASCII); 156 assertTrue(lines.equals(expected), "Unexpected lines"); 157 158 // MalformedInputException 159 byte[] bad = { (byte)0xff, (byte)0xff }; 160 write(tmpfile, bad); 161 try { 162 readAllLines(tmpfile, US_ASCII); 163 throw new RuntimeException("MalformedInputException expected"); 164 } catch (MalformedInputException ignore) { } 165 166 167 // NullPointerException 168 try { 169 readAllLines(null, US_ASCII); 170 throw new RuntimeException("NullPointerException expected"); 171 } catch (NullPointerException ignore) { } 172 try { 173 readAllLines(tmpfile, null); 174 throw new RuntimeException("NullPointerException expected"); 175 } catch (NullPointerException ignore) { } 176 177 } finally { 178 delete(tmpfile); 179 } 180 } 181 182 /** 183 * Test write(Path,Iterable<? extends CharSequence>,Charset,OpenOption...) 184 */ 185 static void testWriteLines() throws IOException { 186 Path tmpfile = createTempFile("blah", "txt"); 187 try { 188 // write method should create file if it doesn't exist 189 if (rand.nextBoolean()) 190 delete(tmpfile); 191 192 // zero lines 193 Path result = write(tmpfile, Collections.<String>emptyList(), US_ASCII); 194 assert(size(tmpfile) == 0); 195 assert(result == tmpfile); 196 197 // two lines 198 List<String> lines = Arrays.asList("hi", "there"); 199 write(tmpfile, lines, US_ASCII); 200 List<String> actual = readAllLines(tmpfile, US_ASCII); 201 assertTrue(actual.equals(lines), "Unexpected lines"); 202 203 // append two lines 204 write(tmpfile, lines, US_ASCII, StandardOpenOption.APPEND); 205 List<String> expected = new ArrayList<String>(); 206 expected.addAll(lines); 207 expected.addAll(lines); 208 assertTrue(expected.size() == 4, "List should have 4 elements"); 209 actual = readAllLines(tmpfile, US_ASCII); 210 assertTrue(actual.equals(expected), "Unexpected lines"); 211 212 // UnmappableCharacterException 213 try { 214 String s = "\u00A0\u00A1"; 215 write(tmpfile, Arrays.asList(s), US_ASCII); 216 throw new RuntimeException("UnmappableCharacterException expected"); 217 } catch (UnmappableCharacterException ignore) { } 218 219 // NullPointerException 220 try { 221 write(null, lines, US_ASCII); 222 throw new RuntimeException("NullPointerException expected"); 223 } catch (NullPointerException ignore) { } 224 try { 225 write(tmpfile, null, US_ASCII); 226 throw new RuntimeException("NullPointerException expected"); 227 } catch (NullPointerException ignore) { } 228 try { 229 write(tmpfile, lines, null); 230 throw new RuntimeException("NullPointerException expected"); 231 } catch (NullPointerException ignore) { } 232 try { 233 write(tmpfile, lines, US_ASCII, (OpenOption[])null); 234 throw new RuntimeException("NullPointerException expected"); 235 } catch (NullPointerException ignore) { } 236 try { 237 OpenOption[] opts = { (OpenOption)null }; 238 write(tmpfile, lines, US_ASCII, opts); 239 throw new RuntimeException("NullPointerException expected"); 240 } catch (NullPointerException ignore) { } 241 242 } finally { 243 delete(tmpfile); 244 } 245 246 } 247 248 static void assertTrue(boolean expr, String errmsg) { 249 if (!expr) 250 throw new RuntimeException(errmsg); 251 } 252 }