Clover Coverage Report - Atlassian Core
Coverage timestamp: Sun Nov 30 2008 18:33:35 CST
43   143   16   2.69
0   115   0.37   3.2
16     1  
5    
 
 
  TestFileUtils       Line # 10 35 8 100% 1.0
  TestFileUtils.StubInputStream       Line # 88 3 2 60% 0.6
  TestFileUtils.StubOutputStream       Line # 104 2 2 75% 0.75
  TestFileUtils.ExceptionalInputStream       Line # 119 2 2 50% 0.5
  TestFileUtils.ExceptionalOutputStream       Line # 132 1 2 66.7% 0.6666667
 
  (8)
 
1    package com.atlassian.core.util;
2   
3    import junit.framework.TestCase;
4   
5    import java.io.*;
6   
7    import org.apache.commons.io.IOUtils;
8    import org.apache.commons.io.output.ByteArrayOutputStream;
9   
 
10    public class TestFileUtils extends TestCase
11    {
12    private final static String MAVEN_TARGET_DIR = "target";
13   
 
14  1 toggle public void testCopyFile() throws Exception
15    {
16  1 File file1 = new File(MAVEN_TARGET_DIR + "/file1.txt");
17  1 File file2 = new File(MAVEN_TARGET_DIR + "/file2.txt");
18  1 FileUtils.saveTextFile("Test content", file1);
19  1 FileUtils.saveTextFile("To be overwritten", file2);
20   
21  1 FileUtils.copyFile(file1, file2);
22   
23  1 assertEquals("Test content", IOUtils.toString(new FileInputStream(file2)));
24    }
25   
 
26  1 toggle public void testCopy() throws Exception
27    {
28  1 InputStream in = new ByteArrayInputStream("Test content".getBytes());
29  1 OutputStream out = new ByteArrayOutputStream();
30  1 FileUtils.copy(in, out);
31  1 assertEquals("Test content", out.toString());
32    }
33   
 
34  1 toggle public void testShutdownInputStream() throws Exception
35    {
36  1 StubInputStream in = new StubInputStream();
37  1 FileUtils.shutdownStream(in);
38  1 assertTrue(in.wasClosed);
39    }
40   
 
41  1 toggle public void testShutdownInputStreamWithException() throws Exception
42    {
43  1 ExceptionalInputStream in = new ExceptionalInputStream();
44  1 FileUtils.shutdownStream(in); // shouldn't throw an exception
45    }
46   
 
47  1 toggle public void testShutdownOutputStream() throws Exception
48    {
49  1 StubOutputStream out = new StubOutputStream();
50  1 FileUtils.shutdownStream(out);
51  1 assertTrue(out.wasClosed);
52    }
53   
 
54  1 toggle public void testShutdownOutputStreamWithException() throws Exception
55    {
56  1 ExceptionalOutputStream out = new ExceptionalOutputStream();
57  1 FileUtils.shutdownStream(out); // shouldn't throw an exception
58    }
59   
 
60  1 toggle public void testSaveTestFile() throws IOException
61    {
62  1 File file = new File(MAVEN_TARGET_DIR + File.separator + "foo.tmp");
63  1 FileUtils.saveTextFile("Hello World", file);
64  1 assertEquals("Hello World", IOUtils.toString(new FileInputStream(file)));
65   
66    // overwrite the existing text in the file
67  1 FileUtils.saveTextFile("Goodbye Moon", file);
68  1 assertEquals("Goodbye Moon", IOUtils.toString(new FileInputStream(file)));
69    }
70   
 
71  1 toggle public void testMoveDir() throws Exception
72    {
73  1 final File baseDir = new File(MAVEN_TARGET_DIR, "TestFileUtils-testMoveDir");
74  1 FileUtils.deleteDir(baseDir);
75  1 File oldDir = new File(baseDir, "old");
76  1 oldDir.mkdirs();
77  1 FileUtils.saveTextFile("Test content", new File(oldDir, "test.txt"));
78   
79  1 File newDir = new File(baseDir, "new");
80  1 assertTrue(FileUtils.moveDir(oldDir, newDir));
81   
82  1 assertTrue("directory has been moved", newDir.exists());
83  1 assertTrue("old directory no longer exists", !oldDir.exists());
84  1 assertEquals("Test content should be copied", "Test content",
85    IOUtils.toString(new FileInputStream(new File(newDir, "test.txt"))));
86    }
87   
 
88    private static class StubInputStream extends InputStream
89    {
90    public boolean wasClosed = false;
91   
 
92  0 toggle public int read()
93    {
94  0 return 0;
95    }
96   
 
97  1 toggle public void close() throws IOException
98    {
99  1 wasClosed = true;
100  1 super.close();
101    }
102    }
103   
 
104    private static class StubOutputStream extends OutputStream
105    {
106    public boolean wasClosed = false;
107   
 
108  0 toggle public void write(int b)
109    {
110    }
111   
 
112  1 toggle public void close() throws IOException
113    {
114  1 wasClosed = true;
115  1 super.close();
116    }
117    }
118   
 
119    private static class ExceptionalInputStream extends InputStream
120    {
 
121  0 toggle public int read()
122    {
123  0 return 0;
124    }
125   
 
126  1 toggle public void close() throws IOException
127    {
128  1 throw new IOException();
129    }
130    }
131   
 
132    private static class ExceptionalOutputStream extends OutputStream
133    {
 
134  0 toggle public void write(int b)
135    {
136    }
137   
 
138  1 toggle public void close() throws IOException
139    {
140  1 throw new IOException();
141    }
142    }
143    }