2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
6 package com.threerings.getdown.util;
9 import java.io.IOException;
10 import java.io.StringReader;
11 import java.util.List;
14 import org.junit.rules.TemporaryFolder;
16 import static org.junit.Assert.*;
19 * Tests {@link FileUtil}.
21 public class FileUtilTest
23 @Test public void testReadLines () throws IOException
25 String data = "This is a test\nof a file with\na few lines\n";
26 List<String> lines = FileUtil.readLines(new StringReader(data));
27 String[] linesBySplit = data.split("\n");
28 assertEquals(linesBySplit.length, lines.size());
29 for (int ii = 0; ii < lines.size(); ii++) {
30 assertEquals(linesBySplit[ii], lines.get(ii));
34 @Test public void shouldCopyFile () throws IOException
36 File source = _folder.newFile("source.txt");
37 File target = new File(_folder.getRoot(), "target.txt");
38 assertFalse(target.exists());
39 FileUtil.copy(source, target);
40 assertTrue(target.exists());
43 @Test public void shouldRecursivelyWalkOverFilesAndFolders () throws IOException
45 _folder.newFile("a.txt");
46 new File(_folder.newFolder("b"), "b.txt").createNewFile();
48 class CountingVisitor implements FileUtil.Visitor {
50 @Override public void visit(File file) {
54 CountingVisitor visitor = new CountingVisitor();
55 FileUtil.walkTree(_folder.getRoot(), visitor);
56 assertEquals(3, visitor.fileCount);
59 @Rule public TemporaryFolder _folder = new TemporaryFolder();