JAL-3130 adapted getdown src. attempt 2. first attempt failed due to cp'ed .git files
[jalview.git] / getdown / src / getdown / core / src / test / java / com / threerings / getdown / util / FileUtilTest.java
1 //
2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
5
6 package com.threerings.getdown.util;
7
8 import java.io.File;
9 import java.io.IOException;
10 import java.io.StringReader;
11 import java.util.List;
12
13 import org.junit.*;
14 import org.junit.rules.TemporaryFolder;
15
16 import static org.junit.Assert.*;
17
18 /**
19  * Tests {@link FileUtil}.
20  */
21 public class FileUtilTest
22 {
23     @Test public void testReadLines () throws IOException
24     {
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));
31         }
32     }
33
34     @Test public void shouldCopyFile () throws IOException
35     {
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());
41     }
42
43     @Test public void shouldRecursivelyWalkOverFilesAndFolders () throws IOException
44     {
45         _folder.newFile("a.txt");
46         new File(_folder.newFolder("b"), "b.txt").createNewFile();
47
48         class CountingVisitor implements FileUtil.Visitor {
49             int fileCount = 0;
50             @Override public void visit(File file) {
51                 fileCount++;
52             }
53         }
54         CountingVisitor visitor = new CountingVisitor();
55         FileUtil.walkTree(_folder.getRoot(), visitor);
56         assertEquals(3, visitor.fileCount);
57     }
58
59     @Rule public TemporaryFolder _folder = new TemporaryFolder();
60 }