Skip to content

Commit e326ec4

Browse files
committed
Fix tests to use tempN5Location() (full container URI) and disentangle N5FS specifics
1 parent 13053f6 commit e326ec4

7 files changed

Lines changed: 98 additions & 113 deletions

src/test/java/org/janelia/saalfeldlab/n5/s3/AbstractN5AmazonS3BucketRootTest.java

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,47 +28,25 @@
2828
*/
2929
package org.janelia.saalfeldlab.n5.s3;
3030

31+
import com.amazonaws.services.s3.AmazonS3;
3132
import java.io.IOException;
32-
import java.nio.file.Paths;
33-
34-
import com.google.gson.GsonBuilder;
35-
import org.janelia.saalfeldlab.n5.N5Reader;
36-
import org.janelia.saalfeldlab.n5.N5Writer;
33+
import java.net.URI;
34+
import java.net.URISyntaxException;
3735
import org.junit.AfterClass;
3836

39-
import com.amazonaws.services.s3.AmazonS3;
40-
4137
public abstract class AbstractN5AmazonS3BucketRootTest extends AbstractN5AmazonS3Test {
4238

4339
public AbstractN5AmazonS3BucketRootTest(final AmazonS3 s3) {
4440

4541
super(s3);
4642
}
4743

48-
@Override
49-
protected N5Writer createN5Writer() throws IOException {
50-
51-
final String bucketName = tempBucketName();
52-
return new N5AmazonS3Writer(s3, bucketName);
53-
}
54-
55-
@Override protected N5Writer createN5Writer(String location) throws IOException {
56-
57-
return new N5AmazonS3Writer(s3, Paths.get(location).getFileName().toString());
58-
}
59-
60-
@Override protected N5Writer createN5Writer(String location, GsonBuilder gson) throws IOException {
61-
62-
return new N5AmazonS3Writer(s3, Paths.get(location).getFileName().toString(), gson);
63-
64-
}
65-
66-
@Override protected N5Reader createN5Reader(String location, GsonBuilder gson) throws IOException {
67-
68-
return new N5AmazonS3Reader(s3, Paths.get(location).getFileName().toString(), gson);
69-
}
44+
@Override
45+
protected String tempN5Location() throws URISyntaxException {
46+
return new URI("s3", tempBucketName(), "/", null).toString();
47+
}
7048

71-
@AfterClass
49+
@AfterClass
7250
public static void cleanup() throws IOException {
7351

7452
rampDownAfterClass();

src/test/java/org/janelia/saalfeldlab/n5/s3/AbstractN5AmazonS3ContainerPathTest.java

Lines changed: 19 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
import com.amazonaws.services.s3.AmazonS3;
3232
import com.google.gson.GsonBuilder;
33+
import java.net.URI;
34+
import java.net.URISyntaxException;
3335
import org.janelia.saalfeldlab.n5.N5Reader;
3436
import org.janelia.saalfeldlab.n5.N5Writer;
3537
import org.junit.AfterClass;
@@ -47,64 +49,23 @@ public AbstractN5AmazonS3ContainerPathTest(final AmazonS3 s3) {
4749
super(s3);
4850
}
4951

50-
@Override
51-
protected N5Writer createN5Writer() throws IOException {
52-
53-
final String bucketName = tempBucketName();
54-
final String containerPath = tempContainerPath();
55-
return new N5AmazonS3Writer(s3, bucketName, containerPath);
56-
}
57-
58-
@Override protected N5Writer createN5Writer(final String location) throws IOException {
59-
60-
final String bucketName = tempBucketName();
61-
return new N5AmazonS3Writer(s3, bucketName, location);
62-
}
63-
64-
protected N5Writer createN5Writer(final String bucket, final String location) throws IOException {
65-
66-
return new N5AmazonS3Writer(s3, bucket, location);
67-
}
68-
69-
protected N5Reader createN5Reader(final String bucket, final String location) throws IOException {
70-
71-
return new N5AmazonS3Reader(s3, bucket, location);
72-
}
73-
74-
@Override protected N5Writer createN5Writer(final String location, final GsonBuilder gson) throws IOException {
75-
76-
final String bucketName = tempBucketName();
77-
return new N5AmazonS3Writer(s3, bucketName, location, gson);
78-
}
79-
80-
@Override protected N5Reader createN5Reader(final String location, final GsonBuilder gson) throws IOException {
81-
82-
final String bucketName = tempBucketName();
83-
return new N5AmazonS3Reader(s3, bucketName, location, gson);
84-
}
85-
86-
protected N5Writer createN5Writer(final String bucket, final String location, final GsonBuilder gson) throws IOException {
87-
88-
return new N5AmazonS3Writer(s3, bucket, location, gson);
89-
}
90-
91-
protected N5Reader createN5Reader(final String bucket, final String location, final GsonBuilder gson) throws IOException {
92-
93-
return new N5AmazonS3Reader(s3, bucket, location, gson);
94-
}
95-
96-
@Override @Test
97-
public void testRemoveContainer() throws IOException {
98-
99-
final String location = tempN5PathName();
100-
final String tempBucket = tempBucketName();
101-
try (final N5Writer n5 = createN5Writer(tempBucket, location)) {
102-
assertNotNull(createN5Reader(tempBucket, location));
103-
n5.remove();
104-
assertThrows(Exception.class, () -> createN5Reader(tempBucket, location));
105-
}
106-
assertThrows(Exception.class, () -> createN5Reader(tempBucket, location));
107-
}
52+
@Override
53+
protected String tempN5Location() throws URISyntaxException {
54+
return new URI("s3", tempBucketName(), tempContainerPath(), null).toString();
55+
}
56+
57+
@Override
58+
@Test
59+
public void testRemoveContainer() throws IOException, URISyntaxException {
60+
61+
final String location = new URI("s3", tempBucketName(), tempContainerPath(), null).toString();
62+
try (final N5Writer n5 = createN5Writer(location)) {
63+
assertNotNull(createN5Reader(location));
64+
n5.remove();
65+
assertThrows(Exception.class, () -> createN5Reader(location));
66+
}
67+
assertThrows(Exception.class, () -> createN5Reader(location));
68+
}
10869

10970
@AfterClass
11071
public static void cleanup() throws IOException {

src/test/java/org/janelia/saalfeldlab/n5/s3/AbstractN5AmazonS3Test.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,18 @@
2828
*/
2929
package org.janelia.saalfeldlab.n5.s3;
3030

31+
import com.google.gson.GsonBuilder;
3132
import java.io.IOException;
33+
import java.net.URI;
34+
import java.net.URISyntaxException;
35+
import java.nio.file.Path;
3236
import java.nio.file.Paths;
3337

38+
import java.security.SecureRandom;
39+
import java.util.UUID;
3440
import org.janelia.saalfeldlab.n5.AbstractN5Test;
41+
import org.janelia.saalfeldlab.n5.N5Reader;
42+
import org.janelia.saalfeldlab.n5.N5Writer;
3543
import org.junit.Assert;
3644
import org.junit.Test;
3745

@@ -52,14 +60,37 @@ public AbstractN5AmazonS3Test(final AmazonS3 s3) {
5260
AbstractN5AmazonS3Test.s3 = s3;
5361
}
5462

63+
private static final SecureRandom random = new SecureRandom();
64+
private static String generateName(String prefix, String suffix) {
65+
return prefix + Long.toUnsignedString(random.nextLong()) + suffix;
66+
}
67+
5568
protected String tempBucketName() {
5669

57-
return Paths.get(AbstractN5Test.tempN5PathName()).getFileName().toString();
70+
return generateName("n5-test-", "-bucket");
5871
}
5972

6073
protected String tempContainerPath() {
61-
return AbstractN5Test.tempN5PathName();
6274

75+
return generateName("/n5-test-", ".n5");
76+
}
77+
78+
@Override
79+
protected N5Writer createN5Writer(final String location, final GsonBuilder gson) throws IOException, URISyntaxException {
80+
81+
final URI uri = new URI(location);
82+
final String bucketName = uri.getHost();
83+
final String basePath = uri.getPath();
84+
return new N5AmazonS3Writer(s3, bucketName, basePath, gson);
85+
}
86+
87+
@Override
88+
protected N5Reader createN5Reader(final String location, final GsonBuilder gson) throws IOException, URISyntaxException {
89+
90+
final URI uri = new URI(location);
91+
final String bucketName = uri.getHost();
92+
final String basePath = uri.getPath();
93+
return new N5AmazonS3Reader(s3, bucketName, basePath, gson);
6394
}
6495

6596
/**

src/test/java/org/janelia/saalfeldlab/n5/s3/backend/N5AmazonS3BucketRootBackendTest.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,16 @@
2828
*/
2929
package org.janelia.saalfeldlab.n5.s3.backend;
3030

31+
import com.google.gson.GsonBuilder;
3132
import java.io.IOException;
3233

34+
import java.net.URI;
35+
import java.net.URISyntaxException;
36+
import org.janelia.saalfeldlab.n5.N5Reader;
3337
import org.janelia.saalfeldlab.n5.N5Writer;
3438
import org.janelia.saalfeldlab.n5.s3.AbstractN5AmazonS3BucketRootTest;
39+
import org.janelia.saalfeldlab.n5.s3.N5AmazonS3Reader;
40+
import org.janelia.saalfeldlab.n5.s3.N5AmazonS3Writer;
3541

3642
/**
3743
* Initiates testing of the Amazon Web Services S3-based N5 implementation using actual S3 backend.
@@ -46,11 +52,14 @@ public N5AmazonS3BucketRootBackendTest() {
4652
super(BackendS3Factory.getOrCreateS3());
4753
}
4854

49-
@Override
50-
protected N5Writer createN5Writer() throws IOException {
55+
@Override
56+
protected N5Writer createN5Writer(final String location, final GsonBuilder gson) throws IOException, URISyntaxException {
57+
58+
final URI uri = new URI(location);
59+
final String bucketName = uri.getHost();
60+
61+
N5AmazonS3DelayedWriter.sleep();
62+
return new N5AmazonS3DelayedWriter(s3, bucketName, gson, false);
63+
}
5164

52-
N5AmazonS3DelayedWriter.sleep();
53-
final String testBucketName = tempBucketName();
54-
return new N5AmazonS3DelayedWriter(s3, testBucketName);
55-
}
5665
}

src/test/java/org/janelia/saalfeldlab/n5/s3/backend/N5AmazonS3ContainerPathBackendTest.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@
2828
*/
2929
package org.janelia.saalfeldlab.n5.s3.backend;
3030

31+
import com.google.gson.GsonBuilder;
3132
import java.io.IOException;
3233

34+
import java.net.URI;
35+
import java.net.URISyntaxException;
3336
import org.janelia.saalfeldlab.n5.N5Writer;
3437
import org.janelia.saalfeldlab.n5.s3.AbstractN5AmazonS3ContainerPathTest;
38+
import org.janelia.saalfeldlab.n5.s3.N5AmazonS3Writer;
3539

3640
/**
3741
* Initiates testing of the Amazon Web Services S3-based N5 implementation using actual S3 backend.
@@ -46,12 +50,13 @@ public N5AmazonS3ContainerPathBackendTest() {
4650
super(BackendS3Factory.getOrCreateS3());
4751
}
4852

49-
@Override
50-
protected N5Writer createN5Writer() throws IOException {
53+
@Override
54+
protected N5Writer createN5Writer(final String location, final GsonBuilder gson) throws IOException, URISyntaxException {
55+
56+
final URI uri = new URI(location);
57+
final String bucketName = uri.getHost();
58+
final String basePath = uri.getPath();
59+
return new N5AmazonS3DelayedWriter(s3, bucketName, basePath, gson, false);
60+
}
5161

52-
N5AmazonS3DelayedWriter.sleep();
53-
final String bucketName = tempBucketName();
54-
final String containerPath = tempContainerPath();
55-
return new N5AmazonS3DelayedWriter(s3, bucketName, containerPath);
56-
}
5762
}

src/test/java/org/janelia/saalfeldlab/n5/s3/backend/N5AmazonS3DelayedWriter.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ class N5AmazonS3DelayedWriter extends N5AmazonS3Writer {
5555

5656
private static final long delayMsec = 1000;
5757

58-
public N5AmazonS3DelayedWriter(final AmazonS3 s3, final String bucketName) throws IOException {
58+
public N5AmazonS3DelayedWriter(final AmazonS3 s3, final String bucketName, final GsonBuilder gson, final boolean cacheAttributes) throws IOException {
5959

60-
super(s3, bucketName, new GsonBuilder());
60+
super(s3, bucketName, gson, cacheAttributes);
6161
sleep();
6262
}
6363

64-
public N5AmazonS3DelayedWriter(final AmazonS3 s3, final String bucketName, final String containerPath) throws IOException {
64+
public N5AmazonS3DelayedWriter(final AmazonS3 s3, final String bucketName, final String basePath, final GsonBuilder gson, final boolean cacheAttributes) throws IOException {
6565

66-
super(s3, bucketName, containerPath, new GsonBuilder());
67-
sleep();
68-
}
66+
super(s3, bucketName, basePath, gson, cacheAttributes);
67+
sleep();
68+
}
6969

7070
@Override
7171
public void createGroup(final String pathName) throws IOException {
@@ -94,7 +94,7 @@ public <T> void writeBlock(
9494
}
9595

9696
@Override
97-
public boolean deleteBlock(final String pathName, final long[] gridPosition) {
97+
public boolean deleteBlock(final String pathName, final long[] gridPosition) throws IOException {
9898

9999
final boolean ret = super.deleteBlock(pathName, gridPosition);
100100
sleep();

src/test/java/org/janelia/saalfeldlab/n5/s3/mock/N5AmazonS3ContainerPathMockTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
package org.janelia.saalfeldlab.n5.s3.mock;
3030

3131
import com.google.gson.GsonBuilder;
32+
import org.janelia.saalfeldlab.n5.N5Exception;
3233
import org.janelia.saalfeldlab.n5.N5Reader;
3334
import org.janelia.saalfeldlab.n5.N5Writer;
3435
import org.janelia.saalfeldlab.n5.s3.AbstractN5AmazonS3ContainerPathTest;
@@ -58,7 +59,7 @@ public N5AmazonS3ContainerPathMockTest() {
5859
@Override
5960
public void testReaderCreation() throws IOException {
6061

61-
final String containerPath = tempN5PathName();
62+
final String containerPath = tempContainerPath();
6263
final String bucketName = tempBucketName();
6364
try (N5Writer writer = new N5AmazonS3Writer(s3, bucketName, containerPath, new GsonBuilder())) {
6465

@@ -84,14 +85,14 @@ public void testReaderCreation() throws IOException {
8485
writer.createGroup("/");
8586
writer.setAttribute("/", N5Reader.VERSION_KEY,
8687
new N5Reader.Version(N5Reader.VERSION.getMajor() + 1, N5Reader.VERSION.getMinor(), N5Reader.VERSION.getPatch()).toString());
87-
assertThrows("Incompatible version throws error", IOException.class,
88+
assertThrows("Incompatible version throws error", N5Exception.N5IOException.class,
8889
() -> {
8990
new N5AmazonS3Reader(s3, bucketName, containerPath, new GsonBuilder());
9091
});
9192

9293
// non-existent directory should fail
9394
writer.remove("/");
94-
assertThrows("Non-existant location throws error", IOException.class,
95+
assertThrows("Non-existant location throws error", N5Exception.N5IOException.class,
9596
() -> {
9697
final N5Reader test = new N5AmazonS3Reader(s3, bucketName, containerPath, new GsonBuilder());
9798
test.list("/");

0 commit comments

Comments
 (0)