summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/io/IOUtilsTestCase.java
blob: d4df3478da82f9f42d41e38de721500e371bd930 (plain) (blame)
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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.io;

import java.io.*;
import java.util.Arrays;
import java.util.List;

/**
 * @author bratseth
 */
public class IOUtilsTestCase extends junit.framework.TestCase {

    public void testCloseNUllDoesNotFail() {
        IOUtils.closeWriter(null);
        IOUtils.closeReader(null);
        IOUtils.closeInputStream(null);
        IOUtils.closeOutputStream(null);
    }

    public void testFileWriter() throws IOException {
        IOUtils.writeFile("temp1.txt", "hello",false);
        assertEquals("hello", IOUtils.readFile(new File("temp1.txt")));
        new File("temp1.txt").delete();
    }

    public void testFileWriterWithoutEncoding() throws IOException {
        BufferedWriter writer=null;
        try {
            writer=IOUtils.createWriter(new File("temp2.txt"),false);
            writer.write("hello");
        }
        finally {
            IOUtils.closeWriter(writer);
        }
        assertEquals("hello", IOUtils.readFile(new File("temp2.txt")));
        new File("temp2.txt").delete();
    }

    public void testFileWriterWithoutEncodingFromFileName() throws IOException {
        BufferedWriter writer=null;
        try {
            writer=IOUtils.createWriter("temp3.txt",false);
            writer.write("hello");
        }
        finally {
            IOUtils.closeWriter(writer);
        }
        assertEquals("hello",IOUtils.readFile(new File("temp3.txt")));
        new File("temp3.txt").delete();
    }

    public void testFileCounting() throws IOException {
        IOUtils.writeFile("temp4.txt","hello\nworld",false);
        assertEquals(2,IOUtils.countLines("temp4.txt"));
        new File("temp4.txt").delete();
    }

    public void testFileCopy() throws IOException {
        IOUtils.writeFile("temp5.txt","hello",false);
        IOUtils.copy(new File("temp5.txt"), new File("temp5copy.txt"));
        assertEquals("hello", IOUtils.readFile(new File("temp5copy.txt")));
        new File("temp5.txt").delete();
        new File("temp5copy.txt").delete();
    }

    public void testFileCopyWithLineCap() throws IOException {
        IOUtils.writeFile("temp6.txt","hello\nyou\nworld",false);
        IOUtils.copy("temp6.txt","temp6copy.txt",2);
        assertEquals("hello\nyou\n", IOUtils.readFile(new File("temp6copy.txt")));
        new File("temp6.txt").delete();
        new File("temp6copy.txt").delete();
    }

    public void testGetLines() throws IOException {
        IOUtils.writeFile("temp7.txt","hello\nworld",false);
        List<String> lines=IOUtils.getLines("temp7.txt");
        assertEquals(2,lines.size());
        assertEquals("hello",lines.get(0));
        assertEquals("world",lines.get(1));
        new File("temp7.txt").delete();
    }

    public void testFileWriterAppend() throws IOException {
        boolean append=true;
        IOUtils.writeFile("temp8.txt", "hello",!append);
        BufferedWriter writer=null;
        try {
            writer=IOUtils.createWriter(new File("temp8.txt"),append);
            writer.write("\nworld");
        }
        finally {
            IOUtils.closeWriter(writer);
        }
        assertEquals("hello\nworld", IOUtils.readFile(new File("temp8.txt")));
        new File("temp8.txt").delete();
    }

    public void testCloseAllReaders() throws IOException {
        StringReader reader1=new StringReader("hello");
        StringReader reader2=new StringReader("world");
        IOUtils.closeAll(Arrays.<Reader>asList(reader1, reader2));
        try {
            reader1.ready();
            fail("Expected exception due to reader closed");
        }
        catch (IOException e) {
            // Expected
        }
        try {
            reader2.ready();
            fail("Expected exception due to reader closed");
        }
        catch (IOException e) {
            // Expected
        }
    }

    public void testDirCopying() throws IOException {
        IOUtils.writeFile("temp1/temp1.txt","hello",false);
        IOUtils.writeFile("temp1/temp2.txt","world",false);
        IOUtils.copyDirectory(new File("temp1"), new File("temp2"));
        assertEquals("hello", IOUtils.readFile(new File("temp2/temp1.txt")));
        assertEquals("world", IOUtils.readFile(new File("temp2/temp2.txt")));
        IOUtils.recursiveDeleteDir(new File("temp1"));
        IOUtils.recursiveDeleteDir(new File("temp2"));
        assertTrue(!new File("temp1").exists());
        assertTrue(!new File("temp2").exists());
    }

    public void testDirCopyingWithFilter() throws IOException {
        IOUtils.writeFile("temp1/temp1.txt","hello",false);
        IOUtils.writeFile("temp1/temp2.txt","world",false);
        IOUtils.writeFile("temp1/temp3.json", "world", false);
        IOUtils.copyDirectory(new File("temp1"), new File("temp2"), -1, new FilenameFilter() {
                    @Override
                    public boolean accept(File dir, String name) {
                        return name.endsWith(".json");
                    }
                });
        assertEquals("world", IOUtils.readFile(new File("temp2/temp3.json")));
        assertFalse(new File("temp2/temp1.txt").exists());
        assertFalse(new File("temp2/temp2.txt").exists());
        IOUtils.recursiveDeleteDir(new File("temp1"));
        IOUtils.recursiveDeleteDir(new File("temp2"));
        assertTrue(!new File("temp1").exists());
        assertTrue(!new File("temp2").exists());
    }

}