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
32
33 public class General {
34
35 public static boolean debug = false;
36
37 private static boolean win32 = (File.separatorChar == '\\');
38
39 private static int gensymCounter = 0;
40
41
42 /* Generate a filename unique to this run */
43 private static String gensym() {
44 return "x." + ++gensymCounter;
45 }
46
47
48 /**
49 * Find a file in the given subdirectory, or descend into further
50 * subdirectories, if any, if no file is found here. Return null if no
51 * file can be found anywhere beneath the given subdirectory.
52 * @param dir Directory at which we started
53 * @param subdir Subdirectory that we're exploring
54 * @param dl Listing of subdirectory
55 */
56 private static String findSomeFile(String dir, String subdir, String[] dl) {
57 for (int i = 0; i < dl.length; i++) {
58 File f = new File(subdir, dl[i]);
59 File df = new File(dir, f.getPath());
60 if (df.exists() && df.isFile()) {
61 return f.getPath();
62 }
63 }
110 return nf.getName();
111 }
112 return null;
113 }
114
115
116 /**
117 * Construct a string that names a subdirectory of the given directory.
118 * If create is true, then create a subdirectory if none is found, and
119 * throw an exception if that is not possible; otherwise, return null if
120 * no subdirectory can be found.
121 */
122 private static String findSomeDir(String dir, boolean create) {
123 File d = new File(dir);
124 String[] dl = d.list();
125 if (dl == null) {
126 throw new RuntimeException("Can't list " + dir);
127 }
128 for (int i = 0; i < dl.length; i++) {
129 File f = new File(d, dl[i]);
130 if (f.isDirectory() && f.canRead()) {
131 String[] dl2 = f.list();
132 if (dl2.length >= 250) {
133 /* Heuristic to avoid scanning huge directories */
134 continue;
135 }
136 return dl[i];
137 }
138 }
139 if (create) {
140 File sd = new File(d, gensym());
141 if (sd.mkdir()) return sd.getName();
142 }
143 return null;
144 }
145
146
147 /** Construct a string that does not name a file in the given directory */
148 private static String findNon(String dir) {
149 File d = new File(dir);
150 String[] x = new String[] { "foo", "bar", "baz" };
151 for (int i = 0; i < x.length; i++) {
152 File f = new File(d, x[i]);
260 /*
261 * The following three mutually-recursive methods generate and check a tree
262 * of filenames of arbitrary depth. Each method has (at least) these
263 * arguments:
264 *
265 * int depth Remaining tree depth
266 * boolean create Controls whether test files and directories
267 * will be created as needed
268 * String ans Expected answer for the check method (above)
269 * String ask Input pathname to be passed to the check method
270 */
271
272
273 /** Check a single slash case, plus its children */
274 public static void checkSlash(int depth, boolean create,
275 String ans, String ask, String slash)
276 throws Exception
277 {
278 check(ans, ask + slash);
279 checkNames(depth, create,
280 ans.endsWith(File.separator) ? ans : ans + File.separator,
281 ask + slash);
282 }
283
284
285 /** Check slash cases for the given ask string */
286 public static void checkSlashes(int depth, boolean create,
287 String ans, String ask)
288 throws Exception
289 {
290 check(ans, ask);
291 if (depth == 0) return;
292
293 checkSlash(depth, create, ans, ask, "/");
294 checkSlash(depth, create, ans, ask, "//");
295 checkSlash(depth, create, ans, ask, "///");
296 if (win32) {
297 checkSlash(depth, create, ans, ask, "\\");
298 checkSlash(depth, create, ans, ask, "\\\\");
299 checkSlash(depth, create, ans, ask, "\\/");
300 checkSlash(depth, create, ans, ask, "/\\");
301 checkSlash(depth, create, ans, ask, "\\\\\\");
302 }
303 }
304
305
306 /** Check name cases for the given ask string */
307 public static void checkNames(int depth, boolean create,
308 String ans, String ask)
309 throws Exception
310 {
311 int d = depth - 1;
312 File f = new File(ans);
313 String n;
314
315 /* Normal name */
316 if (f.exists()) {
317 if (f.isDirectory() && f.canRead()) {
318 if ((n = findSomeFile(ans, create)) != null)
319 checkSlashes(d, create, ans + n, ask + n);
320 if ((n = findSomeDir(ans, create)) != null)
321 checkSlashes(d, create, ans + n, ask + n);
322 }
323 n = findNon(ans);
324 checkSlashes(d, create, ans + n, ask + n);
325 } else {
326 n = "foo" + depth;
327 checkSlashes(d, create, ans + n, ask + n);
328 }
329
330 /* "." */
331 checkSlashes(d, create, trimTrailingSlashes(ans), ask + ".");
332
333 /* ".." */
334 if ((n = f.getParent()) != null) {
335 String n2;
336 if (win32
337 && ((n2 = f.getParentFile().getParent()) != null)
|
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
32
33 public class General {
34
35 public static boolean debug = false;
36
37 private static boolean win32 = (File.separatorChar == '\\');
38
39 private static int gensymCounter = 0;
40
41
42 /* Generate a filename unique to this run */
43 protected static String gensym() {
44 return "x." + ++gensymCounter;
45 }
46
47
48 /**
49 * Find a file in the given subdirectory, or descend into further
50 * subdirectories, if any, if no file is found here. Return null if no
51 * file can be found anywhere beneath the given subdirectory.
52 * @param dir Directory at which we started
53 * @param subdir Subdirectory that we're exploring
54 * @param dl Listing of subdirectory
55 */
56 private static String findSomeFile(String dir, String subdir, String[] dl) {
57 for (int i = 0; i < dl.length; i++) {
58 File f = new File(subdir, dl[i]);
59 File df = new File(dir, f.getPath());
60 if (df.exists() && df.isFile()) {
61 return f.getPath();
62 }
63 }
110 return nf.getName();
111 }
112 return null;
113 }
114
115
116 /**
117 * Construct a string that names a subdirectory of the given directory.
118 * If create is true, then create a subdirectory if none is found, and
119 * throw an exception if that is not possible; otherwise, return null if
120 * no subdirectory can be found.
121 */
122 private static String findSomeDir(String dir, boolean create) {
123 File d = new File(dir);
124 String[] dl = d.list();
125 if (dl == null) {
126 throw new RuntimeException("Can't list " + dir);
127 }
128 for (int i = 0; i < dl.length; i++) {
129 File f = new File(d, dl[i]);
130 if (f.isDirectory()) {
131 String[] dl2 = f.list();
132 if (dl2 == null || dl2.length >= 250) {
133 /* Heuristic to avoid scanning huge directories */
134 continue;
135 }
136 return dl[i];
137 }
138 }
139 if (create) {
140 File sd = new File(d, gensym());
141 if (sd.mkdir()) return sd.getName();
142 }
143 return null;
144 }
145
146
147 /** Construct a string that does not name a file in the given directory */
148 private static String findNon(String dir) {
149 File d = new File(dir);
150 String[] x = new String[] { "foo", "bar", "baz" };
151 for (int i = 0; i < x.length; i++) {
152 File f = new File(d, x[i]);
260 /*
261 * The following three mutually-recursive methods generate and check a tree
262 * of filenames of arbitrary depth. Each method has (at least) these
263 * arguments:
264 *
265 * int depth Remaining tree depth
266 * boolean create Controls whether test files and directories
267 * will be created as needed
268 * String ans Expected answer for the check method (above)
269 * String ask Input pathname to be passed to the check method
270 */
271
272
273 /** Check a single slash case, plus its children */
274 public static void checkSlash(int depth, boolean create,
275 String ans, String ask, String slash)
276 throws Exception
277 {
278 check(ans, ask + slash);
279 checkNames(depth, create,
280 ans,
281 ask);
282 }
283
284
285 /** Check slash cases for the given ask string */
286 public static void checkSlashes(int depth, boolean create,
287 String ans, String ask)
288 throws Exception
289 {
290 check(ans, ask);
291 if (depth == 0) return;
292
293 checkSlash(depth, create, ans, ask, "/");
294 checkSlash(depth, create, ans, ask, "//");
295 checkSlash(depth, create, ans, ask, "///");
296 if (win32) {
297 checkSlash(depth, create, ans, ask, "\\");
298 checkSlash(depth, create, ans, ask, "\\\\");
299 checkSlash(depth, create, ans, ask, "\\/");
300 checkSlash(depth, create, ans, ask, "/\\");
301 checkSlash(depth, create, ans, ask, "\\\\\\");
302 }
303 }
304
305
306 /** Check name cases for the given ask string */
307 public static void checkNames(int depth, boolean create,
308 String ans, String ask)
309 throws Exception
310 {
311 ans = ans.endsWith(File.separator) ? ans : ans + File.separator;
312 ask = ask.endsWith(File.separator) ? ask : ask + File.separator;
313
314 int d = depth - 1;
315 File f = new File(ans);
316 String n;
317
318 /* Normal name */
319 if (f.exists()) {
320 if (f.isDirectory() && f.list() != null) {
321 if ((n = findSomeFile(ans, create)) != null)
322 checkSlashes(d, create, ans + n, ask + n);
323 if ((n = findSomeDir(ans, create)) != null)
324 checkSlashes(d, create, ans + n, ask + n);
325 }
326 n = findNon(ans);
327 checkSlashes(d, create, ans + n, ask + n);
328 } else {
329 n = "foo" + depth;
330 checkSlashes(d, create, ans + n, ask + n);
331 }
332
333 /* "." */
334 checkSlashes(d, create, trimTrailingSlashes(ans), ask + ".");
335
336 /* ".." */
337 if ((n = f.getParent()) != null) {
338 String n2;
339 if (win32
340 && ((n2 = f.getParentFile().getParent()) != null)
|