1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.filter.test.internal; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.io.FileInputStream; |
24 |
|
import java.io.IOException; |
25 |
|
import java.io.InputStream; |
26 |
|
import java.util.Enumeration; |
27 |
|
import java.util.HashMap; |
28 |
|
import java.util.Map; |
29 |
|
|
30 |
|
import junit.framework.AssertionFailedError; |
31 |
|
|
32 |
|
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; |
33 |
|
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; |
34 |
|
import org.apache.commons.compress.archivers.zip.ZipFile; |
35 |
|
import org.apache.commons.io.FileUtils; |
36 |
|
import org.apache.commons.io.IOUtils; |
37 |
|
import org.junit.Assert; |
38 |
|
|
39 |
|
|
40 |
|
@version |
41 |
|
@since |
42 |
|
|
|
|
| 0% |
Uncovered Elements: 50 (50) |
Complexity: 8 |
Complexity Density: 0.18 |
|
43 |
|
public class ZIPFileAssertComparator implements FileAssertComparator |
44 |
|
{ |
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 3 |
Complexity Density: 0.43 |
|
45 |
0 |
public static boolean isZip(File file) throws IOException... |
46 |
|
{ |
47 |
0 |
final byte[] signature = new byte[12]; |
48 |
|
|
49 |
0 |
int signatureLength; |
50 |
0 |
try (FileInputStream stream = new FileInputStream(file)) { |
51 |
0 |
stream.mark(signature.length); |
52 |
0 |
signatureLength = stream.read(signature); |
53 |
|
} |
54 |
|
|
55 |
0 |
return ZipArchiveInputStream.matches(signature, signatureLength); |
56 |
|
} |
57 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 2 |
Complexity Density: 0.17 |
|
58 |
0 |
private static Map<String, byte[]> unzip(File filename) throws IOException... |
59 |
|
{ |
60 |
0 |
Map<String, byte[]> zipContent = new HashMap<String, byte[]>(); |
61 |
|
|
62 |
0 |
ZipFile zipFile = new ZipFile(filename); |
63 |
|
|
64 |
0 |
try { |
65 |
0 |
Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); |
66 |
0 |
while (entries.hasMoreElements()) { |
67 |
0 |
ZipArchiveEntry entry = entries.nextElement(); |
68 |
|
|
69 |
0 |
InputStream inputStream = zipFile.getInputStream(entry); |
70 |
0 |
try { |
71 |
0 |
zipContent.put(entry.getName(), IOUtils.toByteArray(inputStream)); |
72 |
|
} finally { |
73 |
0 |
inputStream.close(); |
74 |
|
} |
75 |
|
} |
76 |
|
} finally { |
77 |
0 |
zipFile.close(); |
78 |
|
} |
79 |
|
|
80 |
0 |
return zipContent; |
81 |
|
} |
82 |
|
|
83 |
|
|
84 |
|
@link |
85 |
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 2 |
Complexity Density: 0.12 |
|
86 |
0 |
public void assertEquals(String message, File expected, File actual)... |
87 |
|
{ |
88 |
0 |
Assert.assertNotNull(expected); |
89 |
0 |
Assert.assertNotNull(actual); |
90 |
|
|
91 |
0 |
Assert.assertTrue("Expected file does not exist [" + expected.getAbsolutePath() + "]", expected.exists()); |
92 |
0 |
Assert.assertTrue("Actual file does not exist [" + actual.getAbsolutePath() + "]", actual.exists()); |
93 |
|
|
94 |
0 |
Assert.assertTrue("Expected file not readable", expected.canRead()); |
95 |
0 |
Assert.assertTrue("Actual file not readable", actual.canRead()); |
96 |
|
|
97 |
0 |
try { |
98 |
0 |
Map<String, byte[]> expectedMap = unzip(expected); |
99 |
0 |
Map<String, byte[]> actualMap = unzip(actual); |
100 |
|
|
101 |
0 |
for (Map.Entry<String, byte[]> expectedEntry : expectedMap.entrySet()) { |
102 |
0 |
byte[] actualContent = actualMap.get(expectedEntry.getKey()); |
103 |
|
|
104 |
0 |
Assert.assertNotNull("Entry [" + expectedEntry.getKey() + "] not present", actualContent); |
105 |
|
|
106 |
0 |
FileAssertComparator fileAssertComparator = FileAssert.getComparator(expectedEntry.getKey()); |
107 |
|
|
108 |
0 |
fileAssertComparator.assertEquals("Entry [" + expectedEntry.getKey() + "] has different content", |
109 |
|
expectedEntry.getValue(), actualContent); |
110 |
|
} |
111 |
|
|
112 |
0 |
Assert.assertEquals("Too much entries", expectedMap.size(), actualMap.size()); |
113 |
|
} catch (IOException e) { |
114 |
0 |
throw new AssertionFailedError(e.toString()); |
115 |
|
} |
116 |
|
} |
117 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
118 |
0 |
@Override... |
119 |
|
public void assertEquals(String message, byte[] expected, byte[] actual) throws IOException |
120 |
|
{ |
121 |
0 |
File actualFile = File.createTempFile("actual", ".actual"); |
122 |
|
|
123 |
0 |
try { |
124 |
0 |
FileUtils.writeByteArrayToFile(actualFile, actual); |
125 |
|
|
126 |
0 |
File expectedFile = File.createTempFile("expected", ".expected"); |
127 |
|
|
128 |
0 |
try { |
129 |
0 |
FileUtils.writeByteArrayToFile(expectedFile, expected); |
130 |
|
|
131 |
0 |
assertEquals(message, expectedFile, actualFile); |
132 |
|
} finally { |
133 |
0 |
expectedFile.delete(); |
134 |
|
} |
135 |
|
} finally { |
136 |
0 |
actualFile.delete(); |
137 |
|
} |
138 |
|
} |
139 |
|
} |