View Javadoc

1   package com.atlassian.core.util.zip;
2   
3   
4   import java.io.File;
5   import java.io.IOException;
6   import java.io.InputStream;
7   import java.util.Enumeration;
8   import java.util.zip.ZipEntry;
9   import java.util.zip.ZipFile;
10  
11  import com.google.common.base.Optional;
12  
13  import org.apache.commons.io.IOUtils;
14  import org.hamcrest.Description;
15  import org.hamcrest.Matcher;
16  import org.hamcrest.TypeSafeMatcher;
17  
18  
19  /**
20   * Zip matchers contains convenient hamcrest matchers to assert content of zip archives.
21   */
22  public class ZipMatchers
23  {
24  
25      public static Matcher<File> hasEntry(final String name, final String content)
26      {
27          return new TypeSafeMatcher<File>()
28          {
29              private File archiveFile;
30  
31              @Override
32              public boolean matchesSafely(final File file)
33              {
34                  archiveFile = file;
35  
36                  final Optional<ZipEntry> zipEntry = findZipEntryByName(file, name);
37                  return zipEntry.isPresent() && entryHasContent(file, zipEntry.get(), content);
38              }
39  
40              @Override
41              public void describeTo(final Description description)
42              {
43                  description.appendText("\n entry with name:{" + name + "} content:{" + content+"}");
44              }
45  
46              @Override
47              public void describeMismatchSafely(final File item, final Description description)
48              {
49                  description.appendText("\n got entries");
50                  try
51                  {
52                      final ZipFile zipFile = new ZipFile(item);
53                      final Enumeration<? extends ZipEntry> entries = zipFile.entries();
54                      while(entries.hasMoreElements())
55                      {
56                          final ZipEntry zipEntry = entries.nextElement();
57                          description.appendText("\n Entry with name {" + zipEntry.getName() + "} and content {" +
58                                  IOUtils.toString(zipFile.getInputStream(zipEntry)) + "}");
59                      }
60                  }
61                  catch (final IOException e)
62                  {
63                      throw new RuntimeException(e);
64                  }
65              }
66          };
67      }
68  
69      private static boolean entryHasContent(final File file, final ZipEntry zipEntry, final String content)
70      {
71          try
72          {
73              final ZipFile zipFile = new ZipFile(file);
74              final InputStream inputStream = zipFile.getInputStream(zipEntry);
75              return content.equals(IOUtils.toString(inputStream, "UTF-8"));
76          }
77          catch (final IOException e)
78          {
79              throw new RuntimeException(e);
80          }
81      }
82  
83      private static Optional<ZipEntry> findZipEntryByName(final File archive, final String entryName)
84      {
85          try
86          {
87              final ZipFile zipFile = new ZipFile(archive);
88              final Enumeration<? extends ZipEntry> entries = zipFile.entries();
89              while(entries.hasMoreElements())
90              {
91                  final ZipEntry zipEntry = entries.nextElement();
92                  final String zipEntryName = zipEntry.getName();
93                  if(zipEntryName.equals(entryName))
94                  {
95                      return Optional.of(zipEntry);
96                  }
97              }
98              return Optional.absent();
99          }
100         catch (final IOException e)
101         {
102             throw new RuntimeException(e);
103         }
104     }
105 
106     public static Matcher<File> hasEntryWithName(final String name)
107     {
108         return new TypeSafeMatcher<File>()
109         {
110             private File archiveFile;
111 
112             @Override
113             public boolean matchesSafely(final File file)
114             {
115                 archiveFile = file;
116                 try
117                 {
118                     final ZipFile zipFile = new ZipFile(file);
119                     final Enumeration<? extends ZipEntry> entries = zipFile.entries();
120                     while(entries.hasMoreElements())
121                     {
122                         final ZipEntry zipEntry = entries.nextElement();
123                         final String zipEntryName = zipEntry.getName();
124                         if(zipEntryName.equals(name))
125                         {
126                             return true;
127                         }
128                     }
129                     return false;
130                 }
131                 catch (final IOException e)
132                 {
133                     throw new RuntimeException(e);
134                 }
135             }
136 
137             @Override
138             public void describeTo(final Description description)
139             {
140                 description.appendText("\n entry named: "+name);
141             }
142 
143             @Override
144             public void describeMismatchSafely(final File item, final Description description)
145             {
146                 description.appendText("not present in archive, entries in archive: ");
147                 try
148                 {
149                     final ZipFile zipFile = new ZipFile(item);
150                     final Enumeration<? extends ZipEntry> entries = zipFile.entries();
151                     while(entries.hasMoreElements())
152                     {
153                         final ZipEntry zipEntry = entries.nextElement();
154                         description.appendText("\n  {" + zipEntry.getName() + "}");
155                     }
156                 }
157                 catch (final IOException e)
158                 {
159                     throw new RuntimeException(e);
160                 }
161             }
162         };
163     }
164     
165 }