1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.filter.test.internal

File ZIPFileAssertComparator.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

2
44
4
1
139
88
8
0.18
11
4
2

Classes

Class Line # Actions
ZIPFileAssertComparator 43 44 0% 8 50
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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 $Id: 325a42f84b480130fcf64a02d3410f4f52b73206 $
41    * @since 6.2M1
42    */
 
43    public class ZIPFileAssertComparator implements FileAssertComparator
44    {
 
45  0 toggle 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   
 
58  0 toggle 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    * Asserts that two ZIP files are equal. If they are not, an {@link AssertionError} without a message is thrown.
85    */
 
86  0 toggle 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   
 
118  0 toggle @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    }