aboutsummaryrefslogtreecommitdiffstats
path: root/util/src/test/java/org/aspectj/util/FileUtilTest.java
blob: f5466e2cff66dffdae09657c82500237e39b2827 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/* *******************************************************************
 * Copyright (c) 1999-2001 Xerox Corporation,
 *               2002 Palo Alto Research Center, Incorporated (PARC).
 * All rights reserved.
 * This program and the accompanying materials are made available
 * under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Xerox/PARC     initial implementation
 * ******************************************************************/

package org.aspectj.util;

//import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.StringBufferInputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import junit.framework.TestCase;
import junit.textui.TestRunner;

/**
 *
 */
@SuppressWarnings("deprecation")
public class FileUtilTest extends TestCase {
	public static final String[] NONE = new String[0];
	public static boolean log = false;

	public static void main(String[] args) {
		TestRunner.main(new String[] { "org.aspectj.util.FileUtilTest" });
	}

	public static void assertSame(String prefix, String[] lhs, String[] rhs) { // XXX cheap diff
		String srcPaths = LangUtil.arrayAsList(lhs).toString();
		String destPaths = LangUtil.arrayAsList(rhs).toString();
		if (!srcPaths.equals(destPaths)) {
			log("expected: " + srcPaths);
			log("  actual: " + destPaths);
			assertTrue(prefix + " expected=" + srcPaths + " != actual=" + destPaths, false);
		}
	}

	/**
	 * Verify that dir contains files with names, and return the names of other files in dir.
	 *
	 * @return the contents of dir after excluding files or NONE if none
	 * @throws AssertionFailedError if any names are not in dir
	 */
	public static String[] dirContains(File dir, final String[] filenames) {
		final ArrayList<String> sought = new ArrayList<>(LangUtil.arrayAsList(filenames));
		FilenameFilter filter = new FilenameFilter() {
			public boolean accept(File d, String name) {
				return !sought.remove(name);
			}
		};
		// remove any found from sought and return remainder
		String[] found = dir.list(filter);
		if (0 < sought.size()) {
			assertTrue("found " + LangUtil.arrayAsList(dir.list()).toString() + " expected " + sought, false);
		}
		return (found.length == 0 ? NONE : found);
	}

	/** @return sorted String[] of all paths to all files/dirs under dir */
	public static String[] dirPaths(File dir) {
		return dirPaths(dir, new String[0]);
	}

	/**
	 * Get a sorted String[] of all paths to all files/dirs under dir. Files with names starting with "." are ignored, as are
	 * directory paths containing "CVS". The directory prefix of the path is stripped. Thus, given directory:
	 *
	 * <pre>
	 * path/to
	 *   .cvsignore
	 *   CVS/
	 *     Root
	 *     Repository
	 *   Base.java
	 *   com/
	 *     parc/
	 *       messages.properties
	 *   org/
	 *     aspectj/
	 *       Version.java
	 * </pre>
	 *
	 * a call
	 *
	 * <pre>
	 * dirPaths(new File(&quot;path/to&quot;), new String[0]);
	 * </pre>
	 *
	 * returns
	 *
	 * <pre>
	 * { &quot;Base.java&quot;, &quot;com/parc/messages.properties&quot;, &quot;org/aspectj/Version.java&quot; }
	 * </pre>
	 *
	 * while the call
	 *
	 * <pre>
	 * dirPaths(new File(&quot;path/to&quot;), new String[] { &quot;.java&quot; });
	 * </pre>
	 *
	 * returns
	 *
	 * <pre>
	 * { &quot;Base.java&quot;, &quot;org/aspectj/Version.java&quot; }
	 * </pre>
	 *
	 * @param dir the File path to the directory to inspect
	 * @param suffixes if not empty, require files returned to have this suffix
	 * @return sorted String[] of all paths to all files under dir ending with one of the listed suffixes but not starting with "."
	 */
	public static String[] dirPaths(File dir, String[] suffixes) {
		ArrayList<String> result = new ArrayList<>();
		doDirPaths(dir, result);
		// if suffixes required, remove those without suffixes
		if (!LangUtil.isEmpty(suffixes)) {
			for (ListIterator<String> iter = result.listIterator(); iter.hasNext();) {
				String path = iter.next().toString();
				boolean hasSuffix = false;
				for (int i = 0; !hasSuffix && (i < suffixes.length); i++) {
					hasSuffix = path.endsWith(suffixes[i]);
				}
				if (!hasSuffix) {
					iter.remove();
				}
			}
		}
		Collections.sort(result);
		// trim prefix
		final String prefix = dir.getPath();
		final int len = prefix.length() + 1; // plus directory separator
		String[] ra = result.toArray(new String[0]);
		for (int i = 0; i < ra.length; i++) {
			// assertTrue(ra[i].startsWith(prefix));
			assertTrue(ra[i], ra[i].length() > len);
			ra[i] = ra[i].substring(len);
		}
		return ra;
	}

	/**
	 * @param dir the File to read - ignored if null, not a directory, or has "CVS" in its path
	 * @param useSuffix if true, then use dir as suffix to path
	 */
	private static void doDirPaths(File dir, ArrayList<String> paths) {
		if ((null == dir) || !dir.canRead() || (dir.getPath().contains("CVS"))) {
			return;
		}
		File[] files = dir.listFiles();
		for (File file : files) {
			String path = file.getPath();
			if (!file.getName().startsWith(".")) {
				if (file.isFile()) {
					paths.add(path);
				} else if (file.isDirectory()) {
					doDirPaths(file, paths);
				} else {
					log("not file or dir: " + dir + "/" + path);
				}
			}
		}
	}

	/** Print s if logging is enabled */
	private static void log(String s) {
		if (log) {
			System.err.println(s);
		}
	}

	/** List of File files or directories to delete when exiting */
	final ArrayList<File> tempFiles;

	public FileUtilTest(String s) {
		super(s);
		tempFiles = new ArrayList<>();
	}

	public void tearDown() {
		for (ListIterator<File> iter = tempFiles.listIterator(); iter.hasNext();) {
			File dir = iter.next();
			log("removing " + dir);
			FileUtil.deleteContents(dir);
			dir.delete();
			iter.remove();
		}
	}

	public void testNotIsFileIsDirectory() {
		File noSuchFile = new File("foo");
		assertTrue(!noSuchFile.isFile());
		assertTrue(!noSuchFile.isDirectory());
	}

	public void testGetBestFile() {
		assertNull(FileUtil.getBestFile((String[]) null));
		assertNull(FileUtil.getBestFile(new String[0]));
		assertNull(FileUtil.getBestFile(new String[] { "!" }));
		File f = FileUtil.getBestFile(new String[] { "." });
		assertNotNull(f);
		f = FileUtil.getBestFile(new String[] { "!", "." });
		assertNotNull(f);
		assertTrue(f.canRead());
		boolean setProperty = false;
		try {
			System.setProperty("bestfile", ".");
			setProperty = true;
		} catch (Throwable t) {
			// ignore Security, etc.
		}
		if (setProperty) {
			f = FileUtil.getBestFile(new String[] { "sp:bestfile" });
			assertNotNull(f);
			assertTrue(f.canRead());
		}
	}

	public void testCopyFiles() {
		// bad input
		Class<?> iaxClass = IllegalArgumentException.class;

		checkCopyFiles(null, null, iaxClass, false);

		File noSuchFile = new File("foo");
		checkCopyFiles(noSuchFile, null, iaxClass, false);
		checkCopyFiles(noSuchFile, noSuchFile, iaxClass, false);

		File tempDir = FileUtil.getTempDir("testCopyFiles");
		tempFiles.add(tempDir);
		File fromFile = new File(tempDir, "fromFile");
		String err = FileUtil.writeAsString(fromFile, "contents of from file");
		assertTrue(err, null == err);
		checkCopyFiles(fromFile, null, iaxClass, false);
		checkCopyFiles(fromFile, fromFile, iaxClass, false);

		// file-file
		File toFile = new File(tempDir, "toFile");
		checkCopyFiles(fromFile, toFile, null, true);

		// file-dir
		File toDir = new File(tempDir, "toDir");
		assertTrue(toDir.mkdirs());
		checkCopyFiles(fromFile, toDir, null, true);

		// dir-dir
		File fromDir = new File(tempDir, "fromDir");
		assertTrue(fromDir.mkdirs());
		checkCopyFiles(fromFile, fromDir, null, false);
		File toFile2 = new File(fromDir, "toFile2");
		checkCopyFiles(fromFile, toFile2, null, false);
		checkCopyFiles(fromDir, toDir, null, true);
	}

	void checkCopyFiles(File from, File to, Class exceptionClass, boolean clean) {
		try {
			FileUtil.copyFile(from, to);
			assertTrue(null == exceptionClass);
			if (to.isFile()) {
				assertTrue(from.length() == to.length()); // XXX cheap test
			} else if (!from.isDirectory()) {
				File toFile = new File(to, from.getName());
				assertTrue(from.length() == toFile.length());
			} else {
				// from is a dir and to is a dir, toDir should be created, and have the
				// same contents as fromDir.
				assertTrue(to.exists());
				assertTrue(from.listFiles().length == to.listFiles().length);
			}
		} catch (Throwable t) {
			assertTrue(null != exceptionClass);
			assertTrue(exceptionClass.isAssignableFrom(t.getClass()));
		} finally {
			if (clean && (null != to) && (to.exists())) {
				if (to.isDirectory()) {
					FileUtil.deleteContents(to);
				}
				to.delete();
			}
		}
	}

	public void testDirCopySubdirs() throws IOException {
		File srcDir = new File("src");
		File destDir = FileUtil.getTempDir("testDirCopySubdirs");
		tempFiles.add(destDir);
		FileUtil.copyDir(srcDir, destDir);
		assertSame("testDirCopySubdirs", dirPaths(srcDir), dirPaths(destDir));
	}

	public void testDirCopySubdirsSuffix() throws IOException {
		File srcDir = new File("src");
		File destDir = FileUtil.getTempDir("testDirCopySubdirsSuffix");
		tempFiles.add(destDir);
		FileUtil.copyDir(srcDir, destDir, ".java", ".aj");

		String[] sources = dirPaths(srcDir, new String[] { ".java" });
		for (int i = 0; i < sources.length; i++) {
			sources[i] = sources[i].substring(0, sources[i].length() - 4);
		}
		String[] sinks = dirPaths(destDir, new String[] { ".aj" });
		for (int i = 0; i < sinks.length; i++) {
			sinks[i] = sinks[i].substring(0, sinks[i].length() - 2);
		}
		assertSame("testDirCopySubdirs", sources, sinks);
	}

	public void testGetURL() {
		String[] args = new String[] { ".", "../util/testdata", "../lib/test/aspectjrt.jar" };
		for (String arg : args) {
			checkGetURL(arg);
		}
	}

	/**
	 * Method checkGetURL.
	 *
	 * @param string
	 * @param uRL
	 */
	private void checkGetURL(String arg) {
		assertTrue(null != arg);
		File f = new File(arg);
		URL url = FileUtil.getFileURL(f);
		assertTrue(null != url);
		log("url       " + url);
		if (!f.exists()) {
			log("not exist        " + f);
		} else if (f.isDirectory()) {
			log("directory        " + f);
		} else {
			log("     file        " + f);
			InputStream in = null;
			try {
				in = url.openStream();
			} catch (IOException e) {
				assertTrue("IOException: " + e, false);
			} finally {
				if (null != in) {
					try {
						in.close();
					} catch (IOException e) {
					}
				}
			}
		}
	}

	public void testGetTempDir() {
		boolean pass = true;
		boolean delete = true;
		checkGetTempDir("parent", null, pass, delete);
		checkGetTempDir(null, "child", pass, delete);
		tempFiles.add(checkGetTempDir("parent", "child", pass, !delete).getParentFile());
		tempFiles.add(checkGetTempDir("parent", "child", pass, !delete).getParentFile());
		tempFiles.add(checkGetTempDir("parent", "child", pass, !delete).getParentFile());
	}

	File checkGetTempDir(String parent, String child, boolean ok, boolean delete) {
		File parentDir = FileUtil.getTempDir(parent);
		assertTrue("unable to create " + parent, null != parentDir);
		File dir = FileUtil.makeNewChildDir(parentDir, child);
		log("parent=" + parent + " child=" + child + " -> " + dir);
		assertTrue("dir: " + dir, ok == (dir.canWrite() && dir.isDirectory()));
		if (delete) {
			dir.delete();
			parentDir.delete();
		}
		return dir;
	}

	public void testRandomFileString() {
		ArrayList<String> results = new ArrayList<>();
		for (int i = 0; i < 1000; i++) {
			String s = FileUtil.randomFileString();
			if (results.contains(s)) {
				log("warning: got duplicate at iteration " + i);
			}
			results.add(s);
			// System.err.print(" " + s);
			// if (0 == (i % 5)) {
			// System.err.println("");
			// }
		}
	}

	public void testNormalizedPath() {
		File tempFile = null;
		try {
			tempFile = File.createTempFile("FileUtilTest_testNormalizedPath", "tmp");
			tempFiles.add(tempFile);
		} catch (IOException e) {
			log("aborting test - unable to create temp file");
			return;
		}
		File parentDir = tempFile.getParentFile();
		String tempFilePath = FileUtil.normalizedPath(tempFile, parentDir);
		assertEquals(tempFile.getName(), tempFilePath);
	}

	public void testFileToClassName() {

		File basedir = new File("/base/dir"); // never created
		File classFile = new File(basedir, "foo/Bar.class");
		assertEquals("foo.Bar", FileUtil.fileToClassName(basedir, classFile));

		classFile = new File(basedir, "foo\\Bar.class");
		assertEquals("foo.Bar", FileUtil.fileToClassName(basedir, classFile));

		assertEquals("Bar", FileUtil.fileToClassName(null, classFile));

		classFile = new File("/home/classes/org/aspectj/lang/JoinPoint.class");
		assertEquals("org.aspectj.lang.JoinPoint", FileUtil.fileToClassName(null, classFile));

		classFile = new File("/home/classes/com/sun/tools/Javac.class");
		assertEquals("com.sun.tools.Javac", FileUtil.fileToClassName(null, classFile));
	}

	public void testDeleteContents() {
		File tempDir = FileUtil.getTempDir("testDeleteContents");
		tempFiles.add(tempDir);
		File f = new File(tempDir, "foo");
		f.mkdirs();
		File g = new File(f, "bar");
		g.mkdirs();
		File h = new File(g, "bash");
		h.mkdirs();
		int d = FileUtil.deleteContents(f);
		assertTrue(0 == d);
		assertTrue(0 == f.list().length);
		f.delete();
		assertTrue(!f.exists());
	}

	public void testLineSeek() {
		File tempDir = FileUtil.getTempDir("testLineSeek");
		tempFiles.add(tempDir);
		File file = new File(tempDir, "testLineSeek");
		String path = file.getPath();
		String contents = "0123456789" + LangUtil.EOL;
		contents += contents;
		FileUtil.writeAsString(file, contents);
		tempFiles.add(file);
		List<String> sourceList = new ArrayList<>();
		sourceList.add(file.getPath());

		final ArrayList<String> errors = new ArrayList<>();
		final PrintStream errorSink = new PrintStream(System.err, true) {
			public void println(String error) {
				errors.add(error);
			}
		};
		for (int i = 0; i < 10; i++) {
			List<String> result = FileUtil.lineSeek("" + i, sourceList, true, errorSink);
			assertEquals(2, result.size());
			assertEquals(path + ":1:" + i, result.get(0));
			assertEquals(path + ":2:" + i, result.get(1));
			if (!LangUtil.isEmpty(errors)) { // XXX prefer fast-fail?
				assertTrue("errors: " + errors, false);
			}
		}

	}

	public void testLineSeekMore() {
		final int MAX = 3; // 1..10
		File tempDir = FileUtil.getTempDir("testLineSeekMore");
		tempFiles.add(tempDir);
		final String prefix = new File(tempDir, "testLineSeek").getPath();
		// setup files 0..MAX with 2*MAX lines
		String[] sources = new String[MAX];
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < sources.length; i++) {
			sources[i] = new File(prefix + i).getPath();
			sb.append("not matched");
			sb.append(LangUtil.EOL);
			sb.append("0123456789");
			sb.append(LangUtil.EOL);
		}
		final String contents = sb.toString();
		for (String source : sources) {
			File file = new File(source);
			FileUtil.writeAsString(file, contents);
			tempFiles.add(file);
		}
		// now test
		final ArrayList<String> errors = new ArrayList<>();
		final PrintStream errorSink = new PrintStream(System.err, true) {
			public void println(String error) {
				errors.add(error);
			}
		};
		List<String> sourceList = new ArrayList<>(Arrays.asList(sources));
		sourceList = Collections.unmodifiableList(sourceList);
		for (int k = 0; k < sources.length; k++) {
			List<String> result = FileUtil.lineSeek("" + k, sourceList, true, errorSink);
			// number k found in every other line of every file at index k
			Iterator<String> iter = result.iterator();
			for (int i = 0; i < MAX; i++) { // for each file
				for (int j = 1; j < (MAX + 1); j++) { // for every other line
					assertTrue(iter.hasNext());
					assertEquals(prefix + i + ":" + 2 * j + ":" + k, iter.next());
				}
			}
			if (!LangUtil.isEmpty(errors)) { // XXX prefer fast-fail?
				assertTrue("errors: " + errors, false);
			}
		}
	}

	public void testDirCopyNoSubdirs() throws IOException {
		String[] srcFiles = new String[] { "one.java", "two.java", "three.java" };
		String[] destFiles = new String[] { "three.java", "four.java", "five.java" };
		String[] allFiles = new String[] { "one.java", "two.java", "three.java", "four.java", "five.java" };
		File srcDir = makeTempDir("FileUtilUT_srcDir", srcFiles);
		File destDir = makeTempDir("FileUtilUT_destDir", destFiles);
		assertTrue(null != srcDir);
		assertTrue(null != destDir);
		assertTrue(NONE == dirContains(srcDir, srcFiles));
		assertTrue(NONE == dirContains(destDir, destFiles));

		FileUtil.copyDir(srcDir, destDir);
		String[] resultOne = dirContains(destDir, allFiles);
		FileUtil.copyDir(srcDir, destDir);
		String[] resultTwo = dirContains(destDir, allFiles);

		assertTrue(NONE == resultOne);
		assertTrue(NONE == resultTwo);
	}

	public void testDirCopyNoSubdirsWithSuffixes() throws IOException {
		String[] srcFiles = new String[] { "one.java", "two.java", "three.java" };
		String[] destFiles = new String[] { "three.java", "four.java", "five.java" };
		String[] allFiles = new String[] { "one.aj", "two.aj", "three.aj", "three.java", "four.java", "five.java" };
		File srcDir = makeTempDir("FileUtilUT_srcDir", srcFiles);
		File destDir = makeTempDir("FileUtilUT_destDir", destFiles);
		assertTrue(null != srcDir);
		assertTrue(null != destDir);
		assertTrue(NONE == dirContains(srcDir, srcFiles));
		assertTrue(NONE == dirContains(destDir, destFiles));

		FileUtil.copyDir(srcDir, destDir, ".java", ".aj");
		FileUtil.copyDir(srcDir, destDir, ".java", ".aj");

		assertTrue(NONE == dirContains(destDir, allFiles));
		assertTrue(NONE == dirContains(destDir, allFiles));
	}

	public void testDirCopySubdirsSuffixRoundTrip() throws IOException {
		final File srcDir = new File("src");
		final File one = FileUtil.getTempDir("testDirCopySubdirsSuffixRoundTrip_1");
		final File two = FileUtil.getTempDir("testDirCopySubdirsSuffixRoundTrip_2");
		FileUtil.copyDir(srcDir, one); // no selection
		FileUtil.copyDir(two, one, ".java", ".aj"); // only .java files
		FileUtil.copyDir(one, two, ".aj", ".java");

		FileUtil.deleteContents(one);
		one.delete();
		FileUtil.deleteContents(two);
		two.delete();
	}

	/**
	 * Create temp dir at loc containing temp files files. Result is registered for deletion on cleanup.
	 */
	File makeTempDir(String loc, String[] filenames) throws IOException {
		File d = new File(loc);
		d.mkdirs();
		assertTrue(d.exists());
		tempFiles.add(d);
		assertTrue(d.canWrite());
		for (String filename : filenames) {
			File f = new File(d, filename);
			assertTrue(filename, f.createNewFile());
		}
		return d;
	}

	public void testPipeEmpty() {
		checkPipe("");
	}

	public void testPipeMin() {
		checkPipe("0");
	}

	public void testPipe() {
		String str = "The quick brown fox jumped over the lazy dog";
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < 4096; i++) {
			sb.append(str);
		}
		checkPipe(sb.toString());
	}

	void checkPipe(String data) {
		StringBufferInputStream in = new StringBufferInputStream(data);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		FileUtil.Pipe pipe = new FileUtil.Pipe(in, out, 100l, true, true);
		pipe.run();
		assertTrue(data.equals(out.toString()));
		assertTrue(null == pipe.getThrown());
		assertEquals("totalWritten", data.length(), pipe.totalWritten());
	}

	public void testPipeThrown() {
		final String data = "The quick brown fox jumped over the lazy dog";
		final IOException thrown = new IOException("test");
		StringBufferInputStream in = new StringBufferInputStream(data);
		OutputStream out = new OutputStream() {
			public void write(int b) throws IOException {
				throw thrown;
			}
		};

		FileUtil.Pipe pipe = new FileUtil.Pipe(in, out, 100l, true, true);
		pipe.run();
		assertEquals("totalWritten", 0, pipe.totalWritten());
		assertTrue(thrown == pipe.getThrown());
	}

	public void xtestPipeHalt() { // this test periodically fails on the build machine -
									// disabling till we have time to figure out why
		final long MAX = 1000000;
		InputStream in = new InputStream() {
			long max = 0;

			public int read() throws IOException {
				if (max++ > MAX) {
					throw new IOException("test failed");
				}
				return 1;
			}

		};
		final int minWritten = 20;
		class Flag {
			boolean hit;
		}
		final Flag flag = new Flag();
		OutputStream out = new OutputStream() {
			long max = 0;

			public void write(int b) throws IOException {
				if (max++ > MAX) {
					throw new IOException("test failed");
				} else if (max > minWritten) {
					if (!flag.hit) {
						flag.hit = true;
					}
				}
			}
		};
		class Result {
			long totalWritten;
			Throwable thrown;
			boolean set;
		}
		final Result result = new Result();
		FileUtil.Pipe pipe = new FileUtil.Pipe(in, out, 100l, true, true) {
			protected void completing(long totalWritten, Throwable thrown) {
				result.totalWritten = totalWritten;
				result.thrown = thrown;
				result.set = true;
			}
		};
		// start it up
		new Thread(pipe).start();
		// wait for minWritten input
		while (!flag.hit) {
			try {
				Thread.sleep(5l);
			} catch (InterruptedException e) {
				// ignore
			}
		}
		// halt
		assertTrue(pipe.halt(true, true));
		assertTrue(result.set);
		assertTrue("Expected null but result.thrown = " + result.thrown, null == result.thrown);
		assertTrue(null == pipe.getThrown());
		assertEquals("total written", result.totalWritten, pipe.totalWritten());
		if (minWritten > pipe.totalWritten()) {
			assertTrue("written: " + pipe.totalWritten(), false);
		}
	}

}