|
| 1 | +package dev.zarr.zarrjava.store; |
| 2 | + |
| 3 | +import dev.zarr.zarrjava.Utils; |
| 4 | +import dev.zarr.zarrjava.ZarrException; |
| 5 | +import dev.zarr.zarrjava.core.Array; |
| 6 | +import dev.zarr.zarrjava.core.Group; |
| 7 | +import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; |
| 8 | +import org.apache.commons.compress.archivers.zip.ZipFile; |
| 9 | +import org.junit.jupiter.api.Assertions; |
| 10 | +import org.junit.jupiter.api.BeforeAll; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.params.ParameterizedTest; |
| 13 | +import org.junit.jupiter.params.provider.CsvSource; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.nio.file.Files; |
| 17 | +import java.nio.file.Path; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.zip.ZipEntry; |
| 21 | + |
| 22 | +import static dev.zarr.zarrjava.Utils.unzipFile; |
| 23 | + |
| 24 | +public class BufferedZipStoreTest extends WritableStoreTest { |
| 25 | + |
| 26 | + Path testGroupDir = TESTOUTPUT.resolve("testZipStore.zip"); |
| 27 | + |
| 28 | + @BeforeAll |
| 29 | + void writeTestGroup() throws ZarrException, IOException { |
| 30 | + Path sourceDir = TESTOUTPUT.resolve("testZipStore"); |
| 31 | + FilesystemStore fsStore = new FilesystemStore(sourceDir); |
| 32 | + writeTestGroupV3(fsStore.resolve(), true); |
| 33 | + Utils.zipFile(sourceDir, testGroupDir); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + StoreHandle storeHandleWithData() { |
| 38 | + return new BufferedZipStore(testGroupDir).resolve("zarr.json"); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testOpenZipStore() throws ZarrException, IOException { |
| 43 | + BufferedZipStore zipStore = new BufferedZipStore(testGroupDir); |
| 44 | + assertIsTestGroupV3(Group.open(zipStore.resolve()), true); |
| 45 | + } |
| 46 | + |
| 47 | + @ParameterizedTest |
| 48 | + @CsvSource({"false", "true",}) |
| 49 | + public void testWriteZipStore(boolean flushOnWrite) throws ZarrException, IOException { |
| 50 | + Path path = TESTOUTPUT.resolve("testWriteZipStore" + (flushOnWrite ? "Flush" : "NoFlush") + ".zip"); |
| 51 | + BufferedZipStore zipStore = new BufferedZipStore(path, flushOnWrite); |
| 52 | + writeTestGroupV3(zipStore.resolve(), true); |
| 53 | + if (!flushOnWrite) zipStore.flush(); |
| 54 | + |
| 55 | + BufferedZipStore zipStoreRead = new BufferedZipStore(path); |
| 56 | + assertIsTestGroupV3(Group.open(zipStoreRead.resolve()), true); |
| 57 | + |
| 58 | + Path unzippedPath = TESTOUTPUT.resolve("testWriteZipStoreUnzipped" + (flushOnWrite ? "Flush" : "NoFlush")); |
| 59 | + |
| 60 | + unzipFile(path, unzippedPath); |
| 61 | + FilesystemStore fsStore = new FilesystemStore(unzippedPath); |
| 62 | + assertIsTestGroupV3(Group.open(fsStore.resolve()), true); |
| 63 | + } |
| 64 | + |
| 65 | + @ParameterizedTest |
| 66 | + @CsvSource({"false", "true",}) |
| 67 | + public void testZipStoreWithComment(boolean flushOnWrite) throws ZarrException, IOException { |
| 68 | + Path path = TESTOUTPUT.resolve("testZipStoreWithComment" + (flushOnWrite ? "Flush" : "NoFlush") + ".zip"); |
| 69 | + String comment = "{\"ome\": { \"version\": \"XX.YY\" }}"; |
| 70 | + BufferedZipStore zipStore = new BufferedZipStore(path, comment, flushOnWrite); |
| 71 | + writeTestGroupV3(zipStore.resolve(), true); |
| 72 | + if (!flushOnWrite) zipStore.flush(); |
| 73 | + |
| 74 | + try (java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(path.toFile())) { |
| 75 | + String retrievedComment = zipFile.getComment(); |
| 76 | + Assertions.assertEquals(comment, retrievedComment, "ZIP archive comment does not match expected value."); |
| 77 | + } |
| 78 | + |
| 79 | + Assertions.assertEquals(comment, new BufferedZipStore(path).getArchiveComment(), "ZIP archive comment from store does not match expected value."); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Test that ZipStore meets requirements for underlying store of Zipped OME-Zarr |
| 84 | + * |
| 85 | + * @see <a href="https://ngff.openmicroscopy.org/rfc/9/index.html">RFC-9: Zipped OME-Zarr</a> |
| 86 | + */ |
| 87 | + @Test |
| 88 | + public void testZipStoreRequirements() throws ZarrException, IOException { |
| 89 | + Path path = TESTOUTPUT.resolve("testZipStoreRequirements.zip"); |
| 90 | + BufferedZipStore zipStore = new BufferedZipStore(path); |
| 91 | + |
| 92 | + dev.zarr.zarrjava.v3.Group group = dev.zarr.zarrjava.v3.Group.create(zipStore.resolve()); |
| 93 | + Array array = group.createArray("a1", b -> b |
| 94 | + .withShape(1024, 1024) |
| 95 | + .withDataType(dev.zarr.zarrjava.v3.DataType.UINT32) |
| 96 | + .withChunkShape(512, 512) |
| 97 | + ); |
| 98 | + array.write(ucar.ma2.Array.factory(ucar.ma2.DataType.UINT, new int[]{1024, 1024}, testDataInt()), true); |
| 99 | + |
| 100 | + dev.zarr.zarrjava.v3.Group g1 = group.createGroup("g1"); |
| 101 | + g1.createGroup("g1_1").createGroup("g1_1_1"); |
| 102 | + g1.createGroup("g1_2"); |
| 103 | + group.createGroup("g2").createGroup("g2_1"); |
| 104 | + group.createGroup("g3"); |
| 105 | + |
| 106 | + zipStore.flush(); |
| 107 | + |
| 108 | + try (ZipFile zip = new ZipFile(path.toFile())) { |
| 109 | + ArrayList<ZipArchiveEntry> entries = Collections.list(zip.getEntries()); |
| 110 | + |
| 111 | + // no compression |
| 112 | + for (ZipArchiveEntry e : entries) { |
| 113 | + Assertions.assertEquals(ZipEntry.STORED, e.getMethod(), "Entry " + e.getName() + " is compressed"); |
| 114 | + } |
| 115 | + |
| 116 | + // correct order of zarr.json files |
| 117 | + String[] expectedFirstEntries = new String[]{ |
| 118 | + "zarr.json", |
| 119 | + "a1/zarr.json", |
| 120 | + "g1/zarr.json", |
| 121 | + "g2/zarr.json", |
| 122 | + "g3/zarr.json", |
| 123 | + "g1/g1_1/zarr.json", |
| 124 | + "g1/g1_2/zarr.json", |
| 125 | + "g2/g2_1/zarr.json", |
| 126 | + "g1/g1_1/g1_1_1/zarr.json" |
| 127 | + }; |
| 128 | + String[] actualFirstEntries = entries.stream() |
| 129 | + .map(ZipArchiveEntry::getName) |
| 130 | + .limit(expectedFirstEntries.length) |
| 131 | + .toArray(String[]::new); |
| 132 | + |
| 133 | + Assertions.assertArrayEquals(expectedFirstEntries, actualFirstEntries, "zarr.json files are not in the expected breadth-first order"); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @ParameterizedTest |
| 138 | + @CsvSource({"false", "true",}) |
| 139 | + public void testZipStoreV2(boolean flushOnWrite) throws ZarrException, IOException { |
| 140 | + Path path = TESTOUTPUT.resolve("testZipStoreV2" + (flushOnWrite ? "Flush" : "NoFlush") + ".zip"); |
| 141 | + BufferedZipStore zipStore = new BufferedZipStore(path, flushOnWrite); |
| 142 | + writeTestGroupV2(zipStore.resolve(), true); |
| 143 | + if (!flushOnWrite) zipStore.flush(); |
| 144 | + |
| 145 | + BufferedZipStore zipStoreRead = new BufferedZipStore(path); |
| 146 | + assertIsTestGroupV2(Group.open(zipStoreRead.resolve()), true); |
| 147 | + |
| 148 | + Path unzippedPath = TESTOUTPUT.resolve("testZipStoreV2Unzipped"); |
| 149 | + |
| 150 | + unzipFile(path, unzippedPath); |
| 151 | + FilesystemStore fsStore = new FilesystemStore(unzippedPath); |
| 152 | + assertIsTestGroupV2(Group.open(fsStore.resolve()), true); |
| 153 | + } |
| 154 | + |
| 155 | + |
| 156 | + @Override |
| 157 | + Store writableStore() { |
| 158 | + Path path = TESTOUTPUT.resolve("writableStore.ZIP"); |
| 159 | + if (Files.exists(path)) { |
| 160 | + try{ |
| 161 | + Files.delete(path); |
| 162 | + }catch (IOException e) { |
| 163 | + throw new RuntimeException("Failed to delete existing test ZIP store at: " + path.toAbsolutePath(), e); |
| 164 | + } |
| 165 | + } |
| 166 | + return new BufferedZipStore(TESTOUTPUT.resolve("writableStore.ZIP"), true); |
| 167 | + } |
| 168 | +} |
0 commit comments