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 public void testCopyFile() throws Exception
15 {
16 File file1 = new File(MAVEN_TARGET_DIR + "/file1.txt");
17 File file2 = new File(MAVEN_TARGET_DIR + "/file2.txt");
18 FileUtils.saveTextFile("Test content", file1);
19 FileUtils.saveTextFile("To be overwritten", file2);
20
21 FileUtils.copyFile(file1, file2);
22
23 assertEquals("Test content", IOUtils.toString(new FileInputStream(file2)));
24 }
25
26 public void testCopy() throws Exception
27 {
28 InputStream in = new ByteArrayInputStream("Test content".getBytes());
29 OutputStream out = new ByteArrayOutputStream();
30 FileUtils.copy(in, out);
31 assertEquals("Test content", out.toString());
32 }
33
34 public void testShutdownInputStream() throws Exception
35 {
36 StubInputStream in = new StubInputStream();
37 FileUtils.shutdownStream(in);
38 assertTrue(in.wasClosed);
39 }
40
41 public void testShutdownInputStreamWithException() throws Exception
42 {
43 ExceptionalInputStream in = new ExceptionalInputStream();
44 FileUtils.shutdownStream(in);
45 }
46
47 public void testShutdownOutputStream() throws Exception
48 {
49 StubOutputStream out = new StubOutputStream();
50 FileUtils.shutdownStream(out);
51 assertTrue(out.wasClosed);
52 }
53
54 public void testShutdownOutputStreamWithException() throws Exception
55 {
56 ExceptionalOutputStream out = new ExceptionalOutputStream();
57 FileUtils.shutdownStream(out);
58 }
59
60 public void testSaveTestFile() throws IOException
61 {
62 File file = new File(MAVEN_TARGET_DIR + File.separator + "foo.tmp");
63 FileUtils.saveTextFile("Hello World", file);
64 assertEquals("Hello World", IOUtils.toString(new FileInputStream(file)));
65
66
67 FileUtils.saveTextFile("Goodbye Moon", file);
68 assertEquals("Goodbye Moon", IOUtils.toString(new FileInputStream(file)));
69 }
70
71 public void testMoveDir() throws Exception
72 {
73 final File baseDir = new File(MAVEN_TARGET_DIR, "TestFileUtils-testMoveDir");
74 FileUtils.deleteDir(baseDir);
75 File oldDir = new File(baseDir, "old");
76 oldDir.mkdirs();
77 FileUtils.saveTextFile("Test content", new File(oldDir, "test.txt"));
78
79 File newDir = new File(baseDir, "new");
80 assertTrue(FileUtils.moveDir(oldDir, newDir));
81
82 assertTrue("directory has been moved", newDir.exists());
83 assertTrue("old directory no longer exists", !oldDir.exists());
84 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 public int read()
93 {
94 return 0;
95 }
96
97 public void close() throws IOException
98 {
99 wasClosed = true;
100 super.close();
101 }
102 }
103
104 private static class StubOutputStream extends OutputStream
105 {
106 public boolean wasClosed = false;
107
108 public void write(int b)
109 {
110 }
111
112 public void close() throws IOException
113 {
114 wasClosed = true;
115 super.close();
116 }
117 }
118
119 private static class ExceptionalInputStream extends InputStream
120 {
121 public int read()
122 {
123 return 0;
124 }
125
126 public void close() throws IOException
127 {
128 throw new IOException();
129 }
130 }
131
132 private static class ExceptionalOutputStream extends OutputStream
133 {
134 public void write(int b)
135 {
136 }
137
138 public void close() throws IOException
139 {
140 throw new IOException();
141 }
142 }
143 }