| 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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (43) |
Complexity: 8 |
Complexity Density: 0.23 |
|
| 10 |
|
public class TestFileUtils extends TestCase |
| 11 |
|
{ |
| 12 |
|
private final static String MAVEN_TARGET_DIR = "target"; |
| 13 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1
PASS
|
|
| 14 |
1
|
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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1
PASS
|
|
| 26 |
1
|
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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1
PASS
|
|
| 34 |
1
|
public void testShutdownInputStream() throws Exception... |
| 35 |
|
{ |
| 36 |
1
|
StubInputStream in = new StubInputStream(); |
| 37 |
1
|
FileUtils.shutdownStream(in); |
| 38 |
1
|
assertTrue(in.wasClosed); |
| 39 |
|
} |
| 40 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1
PASS
|
|
| 41 |
1
|
public void testShutdownInputStreamWithException() throws Exception... |
| 42 |
|
{ |
| 43 |
1
|
ExceptionalInputStream in = new ExceptionalInputStream(); |
| 44 |
1
|
FileUtils.shutdownStream(in); |
| 45 |
|
} |
| 46 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1
PASS
|
|
| 47 |
1
|
public void testShutdownOutputStream() throws Exception... |
| 48 |
|
{ |
| 49 |
1
|
StubOutputStream out = new StubOutputStream(); |
| 50 |
1
|
FileUtils.shutdownStream(out); |
| 51 |
1
|
assertTrue(out.wasClosed); |
| 52 |
|
} |
| 53 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1
PASS
|
|
| 54 |
1
|
public void testShutdownOutputStreamWithException() throws Exception... |
| 55 |
|
{ |
| 56 |
1
|
ExceptionalOutputStream out = new ExceptionalOutputStream(); |
| 57 |
1
|
FileUtils.shutdownStream(out); |
| 58 |
|
} |
| 59 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1
PASS
|
|
| 60 |
1
|
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 |
|
|
| 67 |
1
|
FileUtils.saveTextFile("Goodbye Moon", file); |
| 68 |
1
|
assertEquals("Goodbye Moon", IOUtils.toString(new FileInputStream(file))); |
| 69 |
|
} |
| 70 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1
PASS
|
|
| 71 |
1
|
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 |
|
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 88 |
|
private static class StubInputStream extends InputStream |
| 89 |
|
{ |
| 90 |
|
public boolean wasClosed = false; |
| 91 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 92 |
0
|
public int read()... |
| 93 |
|
{ |
| 94 |
0
|
return 0; |
| 95 |
|
} |
| 96 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 97 |
1
|
public void close() throws IOException... |
| 98 |
|
{ |
| 99 |
1
|
wasClosed = true; |
| 100 |
1
|
super.close(); |
| 101 |
|
} |
| 102 |
|
} |
| 103 |
|
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 104 |
|
private static class StubOutputStream extends OutputStream |
| 105 |
|
{ |
| 106 |
|
public boolean wasClosed = false; |
| 107 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 108 |
0
|
public void write(int b)... |
| 109 |
|
{ |
| 110 |
|
} |
| 111 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 112 |
1
|
public void close() throws IOException... |
| 113 |
|
{ |
| 114 |
1
|
wasClosed = true; |
| 115 |
1
|
super.close(); |
| 116 |
|
} |
| 117 |
|
} |
| 118 |
|
|
|
|
|
| 50% |
Uncovered Elements: 2 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 119 |
|
private static class ExceptionalInputStream extends InputStream |
| 120 |
|
{ |
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 121 |
0
|
public int read()... |
| 122 |
|
{ |
| 123 |
0
|
return 0; |
| 124 |
|
} |
| 125 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 126 |
1
|
public void close() throws IOException... |
| 127 |
|
{ |
| 128 |
1
|
throw new IOException(); |
| 129 |
|
} |
| 130 |
|
} |
| 131 |
|
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
| 132 |
|
private static class ExceptionalOutputStream extends OutputStream |
| 133 |
|
{ |
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 134 |
0
|
public void write(int b)... |
| 135 |
|
{ |
| 136 |
|
} |
| 137 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 138 |
1
|
public void close() throws IOException... |
| 139 |
|
{ |
| 140 |
1
|
throw new IOException(); |
| 141 |
|
} |
| 142 |
|
} |
| 143 |
|
} |