test/java/nio/file/Files/BytesAndLines.java

Print this page




   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 {


 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 }


   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         testReadProcFs();
  46     }
  47 
  48     /**
  49      * Test readAllBytes(Path) and write(Path, byte[], OpenOption...)
  50      */
  51     static void testReadAndWriteBytes() throws IOException {
  52         // exercise methods with various sizes
  53         testReadAndWriteBytes(0);
  54         for (int i=0; i<100; i++) {
  55             testReadAndWriteBytes(rand.nextInt(32000));
  56         }
  57 
  58         // NullPointerException
  59         Path file = Paths.get("foo");
  60         List<String> lines = Collections.emptyList();
  61         try {
  62             readAllBytes(null);
  63             throw new RuntimeException("NullPointerException expected");
  64         } catch (NullPointerException ignore) { }
  65         try {


 226                 write(tmpfile, null, US_ASCII);
 227                 throw new RuntimeException("NullPointerException expected");
 228             } catch (NullPointerException ignore) { }
 229             try {
 230                 write(tmpfile, lines, null);
 231                 throw new RuntimeException("NullPointerException expected");
 232             } catch (NullPointerException ignore) { }
 233             try {
 234                 write(tmpfile, lines, US_ASCII, (OpenOption[])null);
 235                 throw new RuntimeException("NullPointerException expected");
 236             } catch (NullPointerException ignore) { }
 237             try {
 238                 OpenOption[] opts = { (OpenOption)null };
 239                 write(tmpfile, lines, US_ASCII, opts);
 240                 throw new RuntimeException("NullPointerException expected");
 241             } catch (NullPointerException ignore) { }
 242 
 243         } finally {
 244             delete(tmpfile);
 245         }
 246     }
 247 
 248     /**
 249      * Test how readAllBytes(Path) and readAllLines(Path,Charset) read from procfs
 250      */
 251     static void testReadProcFs() throws IOException {
 252         if (System.getProperty("os.name").equals("Linux")) {
 253 
 254             // Refer to the Linux proc(5) man page for details about /proc/self/stat and
 255             // /proc/self/status files
 256             //
 257             // procfs reports these files to be zero sized, even though data can be read from them
 258 
 259             String statFile = "/proc/self/stat";
 260             Path pathStat = Paths.get(statFile);
 261             byte[] data = Files.readAllBytes(pathStat);
 262             assertTrue(data.length > 0, "Files.readAllBytes('" + statFile + "') failed to read");
 263 
 264             String statusFile = "/proc/self/status";
 265             Path pathStatus = Paths.get(statusFile);
 266             List<String> lines = Files.readAllLines(pathStatus, US_ASCII);
 267             assertTrue(lines.size() > 0, "Files.readAllLines('" + pathStatus + "') failed to read");
 268         }
 269     }
 270 
 271     static void assertTrue(boolean expr, String errmsg) {
 272         if (!expr)
 273             throw new RuntimeException(errmsg);
 274     }
 275 }