|
5 | 5 | import static org.junit.Assert.fail; |
6 | 6 |
|
7 | 7 | import fi.helsinki.cs.tmc.langs.io.EverythingIsStudentFileStudentFilePolicy; |
| 8 | +import fi.helsinki.cs.tmc.langs.io.StudentFilePolicy; |
8 | 9 | import fi.helsinki.cs.tmc.langs.utils.TestUtils; |
9 | 10 |
|
10 | 11 | import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; |
|
20 | 21 | import java.nio.file.FileVisitor; |
21 | 22 | import java.nio.file.Files; |
22 | 23 | import java.nio.file.Path; |
| 24 | +import java.nio.file.Paths; |
23 | 25 | import java.nio.file.attribute.BasicFileAttributes; |
24 | 26 | import java.util.Enumeration; |
25 | 27 | import java.util.HashMap; |
@@ -77,10 +79,16 @@ public FileVisitResult postVisitDirectory(Path path, IOException ex) |
77 | 79 | } |
78 | 80 |
|
79 | 81 | @Test(expected = FileNotFoundException.class) |
80 | | - public void zipperThrowsExceptionWhenUnzippingNonExistentFile() throws IOException { |
| 82 | + public void zipperThrowsExceptionWhenZippingNonExistentFile() throws IOException { |
81 | 83 | zipper.zip(TEST_ASSETS_DIR.resolve("noSuchDir")); |
82 | 84 | } |
83 | 85 |
|
| 86 | + @Test(expected = IllegalArgumentException.class) |
| 87 | + public void zipperThrowsExceptionWhenZippingRoot() throws IOException { |
| 88 | + // platform-specific root |
| 89 | + zipper.zip(Paths.get("/").toAbsolutePath()); |
| 90 | + } |
| 91 | + |
84 | 92 | @Test |
85 | 93 | public void zipperCorrectlyZipsSingleFile() throws IOException { |
86 | 94 |
|
@@ -137,7 +145,43 @@ public void zipperDetectectsAndObeysTmcnosubmitFiles() throws IOException { |
137 | 145 | expected.close(); |
138 | 146 | actual.close(); |
139 | 147 | Files.deleteIfExists(compressed); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void zipperFollowsStudentPolicy() throws IOException { |
| 152 | + Path uncompressed = TestUtils.getPath(StudentFileAwareUnzipperTest.class, |
| 153 | + "zip_studentpolicy_test_case"); |
| 154 | + |
| 155 | + // Policy: zip every directory and file whose name starts with "include" |
| 156 | + zipper.setStudentFilePolicy(new StudentFilePolicy() { |
| 157 | + @Override |
| 158 | + public boolean isStudentFile(Path path, Path projectRootPath) { |
| 159 | + if (path.equals(projectRootPath)) { |
| 160 | + return true; |
| 161 | + } |
| 162 | + return path.getFileName().toString().startsWith("include"); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public boolean mayDelete(Path file, Path projectRoot) { |
| 167 | + return true; |
| 168 | + } |
| 169 | + }); |
140 | 170 |
|
| 171 | + byte[] zip = zipper.zip(uncompressed); |
| 172 | + Path compressed = Files.createTempFile("testZip", ".zip"); |
| 173 | + Files.write(compressed, zip); |
| 174 | + |
| 175 | + Path referenceZip = TEST_ASSETS_DIR.resolve("zip_studentpolicy_test_case.zip"); |
| 176 | + |
| 177 | + ZipFile expected = new ZipFile(referenceZip.toFile()); |
| 178 | + ZipFile actual = new ZipFile(compressed.toFile()); |
| 179 | + |
| 180 | + assertZipsEqualDecompressed(expected, actual); |
| 181 | + |
| 182 | + expected.close(); |
| 183 | + actual.close(); |
| 184 | + Files.deleteIfExists(compressed); |
141 | 185 | } |
142 | 186 |
|
143 | 187 | private void assertZipsEqualDecompressed(ZipFile expected, ZipFile actual) |
|
0 commit comments