Skip to content

Commit 261bc10

Browse files
committed
add ZipStore tests
1 parent 9520858 commit 261bc10

File tree

3 files changed

+191
-3
lines changed

3 files changed

+191
-3
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package dev.zarr.zarrjava.store;
2+
3+
import javax.annotation.Nonnull;
4+
import javax.annotation.Nullable;
5+
import java.nio.ByteBuffer;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
import java.util.stream.Stream;
9+
10+
public class ZipStore implements Store, Store.ListableStore {
11+
@Nonnull
12+
private final Path path;
13+
14+
public ZipStore(@Nonnull Path path) {
15+
this.path = path;
16+
}
17+
18+
public ZipStore(@Nonnull String path) {
19+
this.path = Paths.get(path);
20+
}
21+
22+
23+
@Override
24+
public Stream<String> list(String[] keys) {
25+
return Stream.empty();
26+
}
27+
28+
@Override
29+
public boolean exists(String[] keys) {
30+
return false;
31+
}
32+
33+
@Nullable
34+
@Override
35+
public ByteBuffer get(String[] keys) {
36+
return null;
37+
}
38+
39+
@Nullable
40+
@Override
41+
public ByteBuffer get(String[] keys, long start) {
42+
return null;
43+
}
44+
45+
@Nullable
46+
@Override
47+
public ByteBuffer get(String[] keys, long start, long end) {
48+
return null;
49+
}
50+
51+
@Override
52+
public void set(String[] keys, ByteBuffer bytes) {
53+
54+
}
55+
56+
@Override
57+
public void delete(String[] keys) {
58+
59+
}
60+
61+
@Nonnull
62+
@Override
63+
public StoreHandle resolve(String... keys) {
64+
return new StoreHandle(this, keys);
65+
}
66+
67+
@Override
68+
public String toString() {
69+
return this.path.toUri().toString().replaceAll("\\/$", "");
70+
}
71+
72+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package dev.zarr.zarrjava;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.util.zip.ZipEntry;
7+
import java.util.zip.ZipOutputStream;
8+
9+
public class Utils {
10+
11+
static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException {
12+
if (fileToZip.isHidden()) {
13+
return;
14+
}
15+
if (fileToZip.isDirectory()) {
16+
if (fileName.endsWith("/")) {
17+
zipOut.putNextEntry(new ZipEntry(fileName));
18+
zipOut.closeEntry();
19+
} else {
20+
zipOut.putNextEntry(new ZipEntry(fileName + "/"));
21+
zipOut.closeEntry();
22+
}
23+
File[] children = fileToZip.listFiles();
24+
for (File childFile : children) {
25+
zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);
26+
}
27+
return;
28+
}
29+
FileInputStream fis = new FileInputStream(fileToZip);
30+
ZipEntry zipEntry = new ZipEntry(fileName);
31+
zipOut.putNextEntry(zipEntry);
32+
byte[] bytes = new byte[1024];
33+
int length;
34+
while ((length = fis.read(bytes)) >= 0) {
35+
zipOut.write(bytes, 0, length);
36+
}
37+
fis.close();
38+
}
39+
40+
}

src/test/java/dev/zarr/zarrjava/ZarrStoreTest.java

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
package dev.zarr.zarrjava;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import dev.zarr.zarrjava.store.FilesystemStore;
5-
import dev.zarr.zarrjava.store.HttpStore;
6-
import dev.zarr.zarrjava.store.S3Store;
4+
import dev.zarr.zarrjava.core.Attributes;
5+
import dev.zarr.zarrjava.store.*;
76
import dev.zarr.zarrjava.v3.*;
87
import org.junit.jupiter.api.Assertions;
98
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.params.ParameterizedTest;
10+
import org.junit.jupiter.params.provider.MethodSource;
1011
import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider;
1112
import software.amazon.awssdk.regions.Region;
1213
import software.amazon.awssdk.services.s3.S3Client;
1314

15+
import java.io.File;
16+
import java.io.FileOutputStream;
1417
import java.io.IOException;
1518
import java.nio.file.Files;
19+
import java.nio.file.Path;
20+
import java.util.Arrays;
21+
import java.util.stream.Stream;
22+
import java.util.zip.ZipOutputStream;
1623

24+
import static dev.zarr.zarrjava.Utils.zipFile;
1725
import static dev.zarr.zarrjava.v3.Node.makeObjectMapper;
1826

1927
public class ZarrStoreTest extends ZarrTest {
@@ -78,4 +86,72 @@ public void testHttpStore() throws IOException, ZarrException {
7886

7987
Assertions.assertArrayEquals(new long[]{1, 4096, 4096, 2048}, array.metadata().shape);
8088
}
89+
90+
@Test
91+
public void testZipStore() throws ZarrException, IOException {
92+
Path sourceDir = TESTOUTPUT.resolve("testZipStore");
93+
Path targetDir = TESTOUTPUT.resolve("testZipStore.zip");
94+
FilesystemStore fsStore = new FilesystemStore(sourceDir);
95+
writeTestGroupV3(fsStore, true);
96+
97+
FileOutputStream fos = new FileOutputStream(targetDir.toFile());
98+
ZipOutputStream zipOut = new ZipOutputStream(fos);
99+
100+
File fileToZip = new File(sourceDir.toUri());
101+
zipFile(fileToZip, fileToZip.getName(), zipOut);
102+
zipOut.close();
103+
fos.close();
104+
105+
ZipStore zipStore = new ZipStore(targetDir);
106+
assertIsTestGroupV3(Group.open(zipStore.resolve()), true);
107+
}
108+
109+
static Stream<Store> localStores() {
110+
return Stream.of(
111+
// new ConcurrentMemoryStore(),
112+
new FilesystemStore(TESTOUTPUT.resolve("testLocalStoresFS")),
113+
new ZipStore(TESTOUTPUT.resolve("testLocalStoresZIP.zip"))
114+
);
115+
}
116+
117+
@ParameterizedTest
118+
@MethodSource("localStores")
119+
public void testLocalStores(Store store) throws IOException, ZarrException {
120+
boolean useParallel = true;
121+
Group group = writeTestGroupV3(store, useParallel);
122+
assertIsTestGroupV3(group, useParallel);
123+
}
124+
125+
int[] testData(){
126+
int[] testData = new int[1024 * 1024];
127+
Arrays.setAll(testData, p -> p);
128+
return testData;
129+
}
130+
131+
Group writeTestGroupV3(Store store, boolean useParallel) throws ZarrException, IOException {
132+
StoreHandle storeHandle = store.resolve();
133+
134+
Group group = Group.create(storeHandle);
135+
Array array = group.createArray("array", b -> b
136+
.withShape(1024, 1024)
137+
.withDataType(DataType.UINT32)
138+
.withChunkShape(5, 5)
139+
);
140+
array.write(ucar.ma2.Array.factory(ucar.ma2.DataType.UINT, new int[]{1024, 1024}, testData()), useParallel);
141+
group.createGroup("subgroup");
142+
group.setAttributes(new Attributes(b -> b.set("some", "value")));
143+
return group;
144+
}
145+
146+
void assertIsTestGroupV3(Group group, boolean useParallel) throws ZarrException {
147+
Stream<dev.zarr.zarrjava.core.Node> nodes = group.list();
148+
Assertions.assertEquals(2, nodes.count());
149+
Array array = (Array) group.get("array");
150+
Assertions.assertNotNull(array);
151+
ucar.ma2.Array result = array.read(useParallel);
152+
Assertions.assertArrayEquals(testData(), (int[]) result.get1DJavaArray(ucar.ma2.DataType.UINT));
153+
Attributes attrs = group.metadata().attributes;
154+
Assertions.assertNotNull(attrs);
155+
Assertions.assertEquals("value", attrs.getString("some"));
156+
}
81157
}

0 commit comments

Comments
 (0)