test/java/io/pathNames/General.java

Print this page




  22  */
  23 
  24 /*
  25    @summary Common definitions for general exhaustive pathname tests
  26    @author  Mark Reinhold
  27  */
  28 
  29 import java.io.*;
  30 import java.util.*;
  31 import java.nio.file.*;
  32 
  33 
  34 public class General {
  35 
  36     public static boolean debug = false;
  37 
  38     private static boolean win32 = (File.separatorChar == '\\');
  39 
  40     private static int gensymCounter = 0;
  41 




  42 
  43     /* Generate a filename unique to this run */
  44     protected static String gensym() {
  45         return "x." + ++gensymCounter;
  46     }
  47 
























  48 
  49     /**
  50      * Find a file in the given subdirectory, or descend into further
  51      * subdirectories, if any, if no file is found here.  Return null if no
  52      * file can be found anywhere beneath the given subdirectory.
  53      * @param  dir     Directory at which we started
  54      * @param  subdir  Subdirectory that we're exploring
  55      * @param  dl      Listing of subdirectory
  56      */
  57     private static String findSomeFile(String dir, String subdir, String[] dl) {
  58         for (int i = 0; i < dl.length; i++) {
  59             File f = new File(subdir, dl[i]);
  60             File df = new File(dir, f.getPath());
  61             if (Files.isRegularFile(df.toPath(), LinkOption.NOFOLLOW_LINKS)) {
  62                 return f.getPath();
  63             }
  64         }
  65         for (int i = 0; i < dl.length; i++) {
  66             File f = (subdir.length() == 0) ? new File(dl[i])
  67                                             : new File(subdir, dl[i]);


 197         return s.substring(0, n + 1);
 198     }
 199 
 200 
 201     /** Concatenate two paths, trimming slashes as needed */
 202     private static String pathConcat(String a, String b) {
 203         if (a.length() == 0) return b;
 204         if (b.length() == 0) return a;
 205         if (isSlash(a.charAt(a.length() - 1))
 206             || isSlash(b.charAt(0))
 207             || (win32 && (a.charAt(a.length() - 1) == ':'))) {
 208             return a + b;
 209         } else {
 210             return a + File.separatorChar + b;
 211         }
 212     }
 213 
 214 
 215 
 216     /** Hash table of input pathnames, used to detect duplicates */
 217     private static Hashtable checked = new Hashtable();
 218 
 219     /**
 220      * Check the given pathname.  Its canonical pathname should be the given
 221      * answer.  If the path names a file that exists and is readable, then
 222      * FileInputStream and RandomAccessFile should both be able to open it.
 223      */
 224     public static void check(String answer, String path) throws IOException {
 225         String ans = trimTrailingSlashes(answer);
 226         if (path.length() == 0) return;
 227         if (checked.get(path) != null) {
 228             System.err.println("DUP " + path);
 229             return;
 230         }
 231         checked.put(path, path);
 232 
 233         String cpath;
 234         try {
 235             File f = new File(path);
 236             cpath = f.getCanonicalPath();
 237             if (f.exists() && f.isFile() && f.canRead()) {


 255             }
 256         }
 257     }
 258 
 259 
 260 
 261     /*
 262      * The following three mutually-recursive methods generate and check a tree
 263      * of filenames of arbitrary depth.  Each method has (at least) these
 264      * arguments:
 265      *
 266      *     int depth         Remaining tree depth
 267      *     boolean create    Controls whether test files and directories
 268      *                       will be created as needed
 269      *     String ans        Expected answer for the check method (above)
 270      *     String ask        Input pathname to be passed to the check method
 271      */
 272 
 273 
 274     /** Check a single slash case, plus its children */
 275     public static void checkSlash(int depth, boolean create,
 276                                   String ans, String ask, String slash)
 277         throws Exception
 278     {
 279         check(ans, ask + slash);
 280         checkNames(depth, create,
 281                    ans.endsWith(File.separator) ? ans : ans + File.separator,
 282                    ask + slash);
 283     }
 284 
 285 
 286     /** Check slash cases for the given ask string */
 287     public static void checkSlashes(int depth, boolean create,
 288                                     String ans, String ask)
 289         throws Exception
 290     {
 291         check(ans, ask);
 292         if (depth == 0) return;
 293 
 294         checkSlash(depth, create, ans, ask, "/");
 295         checkSlash(depth, create, ans, ask, "//");




  22  */
  23 
  24 /*
  25    @summary Common definitions for general exhaustive pathname tests
  26    @author  Mark Reinhold
  27  */
  28 
  29 import java.io.*;
  30 import java.util.*;
  31 import java.nio.file.*;
  32 
  33 
  34 public class General {
  35 
  36     public static boolean debug = false;
  37 
  38     private static boolean win32 = (File.separatorChar == '\\');
  39 
  40     private static int gensymCounter = 0;
  41 
  42     protected static final String userDir = System.getProperty("user.dir");
  43 
  44     protected static String baseDir = null;
  45     protected static String relative = null;
  46 
  47     /* Generate a filename unique to this run */
  48     private static String gensym() {
  49         return "x." + ++gensymCounter;
  50     }
  51 
  52     /**
  53      * Create files and folders in the test working directory.
  54      * The purpose is to make sure the test will not go out of
  55      * its user dir when walking the file tree.
  56      *
  57      * @param  depth    The number of directory levels to be created under
  58      *                  the user directory. It should be the maximum value
  59      *                  of the depths passed to checkNames method (including
  60      *                  direct or indirect calling) in a whole test.
  61      */
  62     protected static void initTestData(int depth) throws IOException {
  63         File parent = new File(userDir);
  64         for (int i = 0; i < depth; i++) {
  65             File tmp = new File(parent, gensym());
  66             tmp.createNewFile();
  67             tmp = new File(parent, gensym());
  68             if (tmp.mkdir())
  69                 parent = tmp;
  70             else
  71                 throw new IOException("Fail to create directory, " + tmp);
  72         }
  73         baseDir = parent.getAbsolutePath();
  74         relative = baseDir.substring(userDir.length() + 1);
  75     }
  76 
  77     /**
  78      * Find a file in the given subdirectory, or descend into further
  79      * subdirectories, if any, if no file is found here.  Return null if no
  80      * file can be found anywhere beneath the given subdirectory.
  81      * @param  dir     Directory at which we started
  82      * @param  subdir  Subdirectory that we're exploring
  83      * @param  dl      Listing of subdirectory
  84      */
  85     private static String findSomeFile(String dir, String subdir, String[] dl) {
  86         for (int i = 0; i < dl.length; i++) {
  87             File f = new File(subdir, dl[i]);
  88             File df = new File(dir, f.getPath());
  89             if (Files.isRegularFile(df.toPath(), LinkOption.NOFOLLOW_LINKS)) {
  90                 return f.getPath();
  91             }
  92         }
  93         for (int i = 0; i < dl.length; i++) {
  94             File f = (subdir.length() == 0) ? new File(dl[i])
  95                                             : new File(subdir, dl[i]);


 225         return s.substring(0, n + 1);
 226     }
 227 
 228 
 229     /** Concatenate two paths, trimming slashes as needed */
 230     private static String pathConcat(String a, String b) {
 231         if (a.length() == 0) return b;
 232         if (b.length() == 0) return a;
 233         if (isSlash(a.charAt(a.length() - 1))
 234             || isSlash(b.charAt(0))
 235             || (win32 && (a.charAt(a.length() - 1) == ':'))) {
 236             return a + b;
 237         } else {
 238             return a + File.separatorChar + b;
 239         }
 240     }
 241 
 242 
 243 
 244     /** Hash table of input pathnames, used to detect duplicates */
 245     private static Hashtable<String, String> checked = new Hashtable<>();
 246 
 247     /**
 248      * Check the given pathname.  Its canonical pathname should be the given
 249      * answer.  If the path names a file that exists and is readable, then
 250      * FileInputStream and RandomAccessFile should both be able to open it.
 251      */
 252     public static void check(String answer, String path) throws IOException {
 253         String ans = trimTrailingSlashes(answer);
 254         if (path.length() == 0) return;
 255         if (checked.get(path) != null) {
 256             System.err.println("DUP " + path);
 257             return;
 258         }
 259         checked.put(path, path);
 260 
 261         String cpath;
 262         try {
 263             File f = new File(path);
 264             cpath = f.getCanonicalPath();
 265             if (f.exists() && f.isFile() && f.canRead()) {


 283             }
 284         }
 285     }
 286 
 287 
 288 
 289     /*
 290      * The following three mutually-recursive methods generate and check a tree
 291      * of filenames of arbitrary depth.  Each method has (at least) these
 292      * arguments:
 293      *
 294      *     int depth         Remaining tree depth
 295      *     boolean create    Controls whether test files and directories
 296      *                       will be created as needed
 297      *     String ans        Expected answer for the check method (above)
 298      *     String ask        Input pathname to be passed to the check method
 299      */
 300 
 301 
 302     /** Check a single slash case, plus its children */
 303     private static void checkSlash(int depth, boolean create,
 304                                   String ans, String ask, String slash)
 305         throws Exception
 306     {
 307         check(ans, ask + slash);
 308         checkNames(depth, create,
 309                    ans.endsWith(File.separator) ? ans : ans + File.separator,
 310                    ask + slash);
 311     }
 312 
 313 
 314     /** Check slash cases for the given ask string */
 315     public static void checkSlashes(int depth, boolean create,
 316                                     String ans, String ask)
 317         throws Exception
 318     {
 319         check(ans, ask);
 320         if (depth == 0) return;
 321 
 322         checkSlash(depth, create, ans, ask, "/");
 323         checkSlash(depth, create, ans, ask, "//");