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 } 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); 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); | 1 /* 2 * Copyright (c) 2011, 2013 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 8020669 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 } 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 // read from procfs 87 if (System.getProperty("os.name").equals("Linux")) { 88 // Refer to the Linux proc(5) man page for details about /proc/self/stat file 89 // procfs reports it to be zero sized, even though data can be read from it 90 String statFile = "/proc/self/stat"; 91 Path pathStat = Paths.get(statFile); 92 byte[] data = Files.readAllBytes(pathStat); 93 assertTrue(data.length > 0, "Files.readAllBytes('" + statFile + "') failed to read"); 94 } 95 } 96 97 98 static void testReadAndWriteBytes(int size) throws IOException { 99 Path path = createTempFile("blah", null); 100 try { 101 boolean append = rand.nextBoolean(); 102 103 byte[] b1 = new byte[size]; 104 rand.nextBytes(b1); 105 106 byte[] b2 = (append) ? new byte[size] : new byte[0]; 107 rand.nextBytes(b2); 108 109 // write method should create file if it doesn't exist 110 if (rand.nextBoolean()) 111 delete(path); 112 113 // write bytes to file 114 Path target = write(path, b1); 166 assertTrue(lines.equals(expected), "Unexpected lines"); 167 168 // MalformedInputException 169 byte[] bad = { (byte)0xff, (byte)0xff }; 170 write(tmpfile, bad); 171 try { 172 readAllLines(tmpfile, US_ASCII); 173 throw new RuntimeException("MalformedInputException expected"); 174 } catch (MalformedInputException ignore) { } 175 176 177 // NullPointerException 178 try { 179 readAllLines(null, US_ASCII); 180 throw new RuntimeException("NullPointerException expected"); 181 } catch (NullPointerException ignore) { } 182 try { 183 readAllLines(tmpfile, null); 184 throw new RuntimeException("NullPointerException expected"); 185 } catch (NullPointerException ignore) { } 186 187 // read from procfs 188 if (System.getProperty("os.name").equals("Linux")) { 189 // Refer to the Linux proc(5) man page for details about /proc/self/status file 190 // procfs reports this file to be zero sized, even though data can be read from it 191 String statusFile = "/proc/self/status"; 192 Path pathStatus = Paths.get(statusFile); 193 lines = Files.readAllLines(pathStatus, US_ASCII); 194 assertTrue(lines.size() > 0, "Files.readAllLines('" + pathStatus + "') failed to read"); 195 } 196 197 } finally { 198 delete(tmpfile); 199 } 200 } 201 202 /** 203 * Test write(Path,Iterable<? extends CharSequence>,Charset,OpenOption...) 204 */ 205 static void testWriteLines() throws IOException { 206 Path tmpfile = createTempFile("blah", "txt"); 207 try { 208 // write method should create file if it doesn't exist 209 if (rand.nextBoolean()) 210 delete(tmpfile); 211 212 // zero lines 213 Path result = write(tmpfile, Collections.<String>emptyList(), US_ASCII); 214 assert(size(tmpfile) == 0); 215 assert(result == tmpfile); |