< prev index next >

test/jdk/java/nio/file/Files/Misc.java

Print this page


   1 /*
   2  * Copyright (c) 2008, 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 4313887 6838333 8005566 8032220
  26  * @summary Unit test for miscellenous methods in java.nio.file.Files
  27  * @library ..
  28  */
  29 
  30 import java.nio.file.*;
  31 import static java.nio.file.Files.*;
  32 import static java.nio.file.LinkOption.*;
  33 import java.nio.file.attribute.*;
  34 import java.io.IOException;
  35 import java.util.*;
  36 

  37 public class Misc {


  38 
  39     public static void main(String[] args) throws IOException {
  40         Path dir = TestUtil.createTemporaryDirectory();
  41         try {
  42             testCreateDirectories(dir);
  43             testIsHidden(dir);
  44             testIsSameFile(dir);

  45             testFileTypeMethods(dir);
  46             testAccessMethods(dir);
  47         } finally {
  48              TestUtil.removeAll(dir);
  49         }
  50     }
  51 
  52     /**
  53      * Tests createDirectories
  54      */
  55     static void testCreateDirectories(Path tmpdir) throws IOException {
  56         // a no-op
  57         createDirectories(tmpdir);
  58 
  59         // create one directory
  60         Path subdir = tmpdir.resolve("a");
  61         createDirectories(subdir);
  62         assertTrue(exists(subdir));
  63 
  64         // create parents


 168                 try {
 169                     assertTrue(isSameFile(thisFile, thatFile));
 170                     assertTrue(isSameFile(thatFile, thisFile));
 171                 } finally {
 172                     TestUtil.deleteUnchecked(thatFile);
 173                 }
 174             }
 175         } finally {
 176             delete(thisFile);
 177         }
 178 
 179         // nulls
 180         try {
 181             isSameFile(thisFile, null);
 182             throw new RuntimeException("NullPointerException expected");
 183         } catch (NullPointerException ignore) { }
 184         try {
 185             isSameFile(null, thatFile);
 186             throw new RuntimeException("NullPointerException expected");
 187         } catch (NullPointerException ignore) { }

































































































 188     }
 189 
 190     /**
 191      * Exercise isRegularFile, isDirectory, isSymbolicLink
 192      */
 193     static void testFileTypeMethods(Path tmpdir) throws IOException {
 194         assertTrue(!isRegularFile(tmpdir));
 195         assertTrue(!isRegularFile(tmpdir, NOFOLLOW_LINKS));
 196         assertTrue(isDirectory(tmpdir));
 197         assertTrue(isDirectory(tmpdir, NOFOLLOW_LINKS));
 198         assertTrue(!isSymbolicLink(tmpdir));
 199 
 200         Path file = createFile(tmpdir.resolve("foo"));
 201         try {
 202             assertTrue(isRegularFile(file));
 203             assertTrue(isRegularFile(file, NOFOLLOW_LINKS));
 204             assertTrue(!isDirectory(file));
 205             assertTrue(!isDirectory(file, NOFOLLOW_LINKS));
 206             assertTrue(!isSymbolicLink(file));
 207 


   1 /*
   2  * Copyright (c) 2008, 2018, 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 4313887 6838333 8005566 8032220 8202285
  26  * @summary Unit test for miscellenous methods in java.nio.file.Files
  27  * @library ..
  28  */
  29 
  30 import java.nio.file.*;
  31 import static java.nio.file.Files.*;
  32 import static java.nio.file.LinkOption.*;
  33 import java.nio.file.attribute.*;
  34 import java.io.IOException;
  35 import java.util.*;
  36 
  37 
  38 public class Misc {
  39     // used for random byte content
  40     private static Random RAND = new Random();
  41 
  42     public static void main(String[] args) throws IOException {
  43         Path dir = TestUtil.createTemporaryDirectory();
  44         try {
  45             testCreateDirectories(dir);
  46             testIsHidden(dir);
  47             testIsSameFile(dir);
  48             testIsSameContent(dir);
  49             testFileTypeMethods(dir);
  50             testAccessMethods(dir);
  51         } finally {
  52              TestUtil.removeAll(dir);
  53         }
  54     }
  55 
  56     /**
  57      * Tests createDirectories
  58      */
  59     static void testCreateDirectories(Path tmpdir) throws IOException {
  60         // a no-op
  61         createDirectories(tmpdir);
  62 
  63         // create one directory
  64         Path subdir = tmpdir.resolve("a");
  65         createDirectories(subdir);
  66         assertTrue(exists(subdir));
  67 
  68         // create parents


 172                 try {
 173                     assertTrue(isSameFile(thisFile, thatFile));
 174                     assertTrue(isSameFile(thatFile, thisFile));
 175                 } finally {
 176                     TestUtil.deleteUnchecked(thatFile);
 177                 }
 178             }
 179         } finally {
 180             delete(thisFile);
 181         }
 182 
 183         // nulls
 184         try {
 185             isSameFile(thisFile, null);
 186             throw new RuntimeException("NullPointerException expected");
 187         } catch (NullPointerException ignore) { }
 188         try {
 189             isSameFile(null, thatFile);
 190             throw new RuntimeException("NullPointerException expected");
 191         } catch (NullPointerException ignore) { }
 192     }
 193 
 194     /**
 195      * Tests isSameContent. The isSameContent is an extension to isSameFile, the
 196      * test therefore is a copy of the testIsSameFile until the last section that
 197      * compares the content.
 198      */
 199     static void testIsSameContent(Path tmpdir) throws IOException {
 200         Path thisFile = tmpdir.resolve("thisFile1");
 201         Path thatFile = tmpdir.resolve("thatFile1");
 202 
 203         /**
 204          * Test: isSameContent for self
 205          */
 206         assertTrue(isSameContent(thisFile, thisFile));
 207 
 208         /**
 209          * Test: Neither files exist
 210          */
 211         try {
 212             isSameContent(thisFile, thatFile);
 213             throw new RuntimeException("IOException not thrown");
 214         } catch (IOException x) {
 215         }
 216         try {
 217             isSameContent(thatFile, thisFile);
 218             throw new RuntimeException("IOException not thrown");
 219         } catch (IOException x) {
 220         }
 221 
 222         createFile(thisFile);
 223         try {
 224             /**
 225              * Test: One file exists
 226              */
 227             try {
 228                 isSameContent(thisFile, thatFile);
 229                 throw new RuntimeException("IOException not thrown");
 230             } catch (IOException x) {
 231             }
 232             try {
 233                 isSameContent(thatFile, thisFile);
 234                 throw new RuntimeException("IOException not thrown");
 235             } catch (IOException x) {
 236             }
 237 
 238             /**
 239              * Test: Both file exists
 240              */
 241             createFile(thatFile);
 242             try {
 243                 assertTrue(!isSameContent(thisFile, thatFile));
 244                 assertTrue(!isSameContent(thatFile, thisFile));
 245             } finally {
 246                 delete(thatFile);
 247             }
 248 
 249             /**
 250              * Test: Symbolic links
 251              */
 252             if (TestUtil.supportsLinks(tmpdir)) {
 253                 createSymbolicLink(thatFile, thisFile);
 254                 try {
 255                     assertTrue(isSameContent(thisFile, thatFile));
 256                     assertTrue(isSameContent(thatFile, thisFile));
 257                 } finally {
 258                     TestUtil.deleteUnchecked(thatFile);
 259                 }
 260             }
 261         } finally {
 262             delete(thisFile);
 263         }
 264 
 265         // nulls
 266         try {
 267             isSameContent(thisFile, null);
 268             throw new RuntimeException("NullPointerException expected");
 269         } catch (NullPointerException ignore) { }
 270         try {
 271             isSameContent(null, thatFile);
 272             throw new RuntimeException("NullPointerException expected");
 273         } catch (NullPointerException ignore) { }
 274 
 275         // compares the contents
 276         byte[] bytes = genBytes(512);
 277         Files.write(thisFile, bytes, StandardOpenOption.CREATE);
 278         Files.write(thatFile, bytes, StandardOpenOption.CREATE);
 279         assertTrue(isSameContent(thisFile, thatFile));
 280     }
 281 
 282     /**
 283      * Returns a byte[] of the given size with random content
 284      */
 285     static private byte[] genBytes(int size) {
 286         byte[] arr = new byte[size];
 287         RAND.nextBytes(arr);
 288         return arr;
 289     }
 290 
 291     /**
 292      * Exercise isRegularFile, isDirectory, isSymbolicLink
 293      */
 294     static void testFileTypeMethods(Path tmpdir) throws IOException {
 295         assertTrue(!isRegularFile(tmpdir));
 296         assertTrue(!isRegularFile(tmpdir, NOFOLLOW_LINKS));
 297         assertTrue(isDirectory(tmpdir));
 298         assertTrue(isDirectory(tmpdir, NOFOLLOW_LINKS));
 299         assertTrue(!isSymbolicLink(tmpdir));
 300 
 301         Path file = createFile(tmpdir.resolve("foo"));
 302         try {
 303             assertTrue(isRegularFile(file));
 304             assertTrue(isRegularFile(file, NOFOLLOW_LINKS));
 305             assertTrue(!isDirectory(file));
 306             assertTrue(!isDirectory(file, NOFOLLOW_LINKS));
 307             assertTrue(!isSymbolicLink(file));
 308 


< prev index next >