From 212a09570dc082275dab46d247f4f94d454a834a Mon Sep 17 00:00:00 2001 From: Ajit Thakor <49403056+athakor@users.noreply.github.com> Date: Tue, 17 Mar 2020 00:56:29 +0530 Subject: [PATCH] chore: remove usage of deprecated ExpectedException JUnit rule (#20) * chore: remove usage of deprecated ExpectedException JUnit rule * chore: modified code * fix: fix review changes * fix: review changes * fix: updated CloudStorageFileSystemProviderTest * fix: fix review changes --- .../nio/CloudStorageConfigurationTest.java | 13 +- .../CloudStorageFileAttributeViewTest.java | 17 +- .../CloudStorageFileSystemProviderTest.java | 150 ++++++++++++------ .../nio/CloudStorageFileSystemTest.java | 10 +- .../CloudStorageLateInitializationTest.java | 4 - .../contrib/nio/CloudStoragePathTest.java | 56 ++++--- .../nio/CloudStorageReadChannelTest.java | 91 +++++++---- .../nio/CloudStorageReadFileChannelTest.java | 4 - .../contrib/nio/CloudStorageReadTest.java | 4 - .../nio/CloudStorageWriteChannelTest.java | 39 +++-- .../nio/CloudStorageWriteFileChannelTest.java | 4 - .../storage/contrib/nio/UnixPathTest.java | 48 ++++-- 12 files changed, 271 insertions(+), 169 deletions(-) diff --git a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageConfigurationTest.java b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageConfigurationTest.java index 2546cf2028..b71926981f 100644 --- a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageConfigurationTest.java +++ b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageConfigurationTest.java @@ -21,9 +21,8 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import java.net.SocketTimeoutException; -import org.junit.Rule; +import org.junit.Assert; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -31,8 +30,6 @@ @RunWith(JUnit4.class) public class CloudStorageConfigurationTest { - @Rule public final ExpectedException thrown = ExpectedException.none(); - @Test public void testBuilder() { CloudStorageConfiguration config = @@ -83,8 +80,12 @@ public void testFromMap() { @Test public void testFromMap_badKey_throwsIae() { - thrown.expect(IllegalArgumentException.class); - CloudStorageConfiguration.fromMap(ImmutableMap.of("lol", "/omg")); + try { + CloudStorageConfiguration.fromMap(ImmutableMap.of("lol", "/omg")); + Assert.fail(); + } catch (IllegalArgumentException ex) { + assertThat(ex.getMessage()).isNotNull(); + } } @Test diff --git a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileAttributeViewTest.java b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileAttributeViewTest.java index 238be44507..be601e8885 100644 --- a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileAttributeViewTest.java +++ b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileAttributeViewTest.java @@ -29,10 +29,9 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.FileTime; +import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -42,8 +41,6 @@ public class CloudStorageFileAttributeViewTest { private static final byte[] HAPPY = "(✿◕ ‿◕ )ノ".getBytes(UTF_8); - @Rule public final ExpectedException thrown = ExpectedException.none(); - private Path path; @Before @@ -62,10 +59,14 @@ public void testReadAttributes() throws IOException { @Test public void testReadAttributes_notFound_throwsNoSuchFileException() throws IOException { - CloudStorageFileAttributeView lazyAttributes = - Files.getFileAttributeView(path, CloudStorageFileAttributeView.class); - thrown.expect(NoSuchFileException.class); - lazyAttributes.readAttributes(); + try { + CloudStorageFileAttributeView lazyAttributes = + Files.getFileAttributeView(path, CloudStorageFileAttributeView.class); + lazyAttributes.readAttributes(); + Assert.fail(); + } catch (NoSuchFileException ex) { + assertThat(ex.getMessage()).isNotNull(); + } } @Test diff --git a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProviderTest.java b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProviderTest.java index 5b7401b881..a8b9d041de 100644 --- a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProviderTest.java +++ b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProviderTest.java @@ -51,10 +51,9 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -85,8 +84,6 @@ public class CloudStorageFileSystemProviderTest { private static final String SINGULARITY = "A string"; - @Rule public final ExpectedException thrown = ExpectedException.none(); - @Before public void before() { CloudStorageFileSystemProvider.setStorageOptions(LocalStorageHelper.getOptions()); @@ -132,8 +129,12 @@ public void testReadAllBytes() throws Exception { @Test public void testReadAllBytes_trailingSlash() throws Exception { - thrown.expect(CloudStoragePseudoDirectoryException.class); - Files.readAllBytes(Paths.get(URI.create("gs://bucket/wat/"))); + try { + Files.readAllBytes(Paths.get(URI.create("gs://bucket/wat/"))); + Assert.fail(); + } catch (CloudStoragePseudoDirectoryException ex) { + assertThat(ex.getMessage()).isNotNull(); + } } @Test @@ -189,16 +190,24 @@ public void testNewByteChannelRead_seekBeyondSize_reportsEofOnNextRead() throws @Test public void testNewByteChannelRead_trailingSlash() throws Exception { - Path path = Paths.get(URI.create("gs://bucket/wat/")); - thrown.expect(CloudStoragePseudoDirectoryException.class); - Files.newByteChannel(path); + try { + Path path = Paths.get(URI.create("gs://bucket/wat/")); + Files.newByteChannel(path); + Assert.fail(); + } catch (CloudStoragePseudoDirectoryException ex) { + assertThat(ex.getMessage()).isNotNull(); + } } @Test public void testNewByteChannelRead_notFound() throws Exception { - Path path = Paths.get(URI.create("gs://bucket/wednesday")); - thrown.expect(NoSuchFileException.class); - Files.newByteChannel(path); + try { + Path path = Paths.get(URI.create("gs://bucket/wednesday")); + Files.newByteChannel(path); + Assert.fail(); + } catch (NoSuchFileException ex) { + assertThat(ex.getMessage()).isNotNull(); + } } @Test @@ -233,18 +242,22 @@ public void testNewInputStream() throws Exception { @Test public void testNewInputStream_trailingSlash() throws Exception { Path path = Paths.get(URI.create("gs://bucket/wat/")); - thrown.expect(CloudStoragePseudoDirectoryException.class); try (InputStream input = Files.newInputStream(path)) { input.read(); + Assert.fail(); + } catch (CloudStoragePseudoDirectoryException ex) { + assertThat(ex.getMessage()).isNotNull(); } } @Test public void testNewInputStream_notFound() throws Exception { Path path = Paths.get(URI.create("gs://cry/wednesday")); - thrown.expect(NoSuchFileException.class); try (InputStream input = Files.newInputStream(path)) { input.read(); + Assert.fail(); + } catch (NoSuchFileException ex) { + assertThat(ex.getMessage()).isNotNull(); } } @@ -282,9 +295,13 @@ public void testNewOutputStream_truncateExplicitly() throws Exception { @Test public void testNewOutputStream_trailingSlash() throws Exception { - Path path = Paths.get(URI.create("gs://bucket/wat/")); - thrown.expect(CloudStoragePseudoDirectoryException.class); - Files.newOutputStream(path); + try { + Path path = Paths.get(URI.create("gs://bucket/wat/")); + Files.newOutputStream(path); + Assert.fail(); + } catch (CloudStoragePseudoDirectoryException ex) { + assertThat(ex.getMessage()).isNotNull(); + } } @Test @@ -295,17 +312,24 @@ public void testNewOutputStream_createNew() throws Exception { @Test public void testNewOutputStream_createNew_alreadyExists() throws Exception { - Path path = Paths.get(URI.create("gs://cry/wednesday")); - Files.write(path, SINGULARITY.getBytes(UTF_8)); - thrown.expect(FileAlreadyExistsException.class); - Files.newOutputStream(path, CREATE_NEW); + try { + Path path = Paths.get(URI.create("gs://cry/wednesday")); + Files.write(path, SINGULARITY.getBytes(UTF_8)); + Files.newOutputStream(path, CREATE_NEW); + Assert.fail(); + } catch (FileAlreadyExistsException expected) { + } } @Test public void testWrite_objectNameWithExtraSlashes_throwsIae() throws Exception { - Path path = Paths.get(URI.create("gs://double/slash//yep")); - thrown.expect(IllegalArgumentException.class); - Files.write(path, FILE_CONTENTS, UTF_8); + try { + Path path = Paths.get(URI.create("gs://double/slash//yep")); + Files.write(path, FILE_CONTENTS, UTF_8); + Assert.fail(); + } catch (IllegalArgumentException ex) { + assertThat(ex.getMessage()).isNotNull(); + } } @Test @@ -381,8 +405,12 @@ public void testWriteOnClose() throws Exception { @Test public void testWrite_trailingSlash() throws Exception { - thrown.expect(CloudStoragePseudoDirectoryException.class); - Files.write(Paths.get(URI.create("gs://greenbean/adipose/")), FILE_CONTENTS, UTF_8); + try { + Files.write(Paths.get(URI.create("gs://greenbean/adipose/")), FILE_CONTENTS, UTF_8); + Assert.fail(); + } catch (CloudStoragePseudoDirectoryException ex) { + assertThat(ex.getMessage()).isNotNull(); + } } @Test @@ -463,8 +491,12 @@ public void testDelete() throws Exception { @Test public void testDelete_dotDirNotNormalized_throwsIae() throws Exception { - thrown.expect(IllegalArgumentException.class); - Files.delete(Paths.get(URI.create("gs://love/fly/../passion"))); + try { + Files.delete(Paths.get(URI.create("gs://love/fly/../passion"))); + Assert.fail(); + } catch (IllegalArgumentException ex) { + assertThat(ex.getMessage()).isNotNull(); + } } @Test @@ -485,8 +517,12 @@ public void testDelete_trailingSlash_disablePseudoDirectories() throws Exception @Test public void testDelete_notFound() throws Exception { - thrown.expect(NoSuchFileException.class); - Files.delete(Paths.get(URI.create("gs://loveh/passionehu"))); + try { + Files.delete(Paths.get(URI.create("gs://loveh/passionehu"))); + Assert.fail(); + } catch (NoSuchFileException ex) { + assertThat(ex.getMessage()).isNotNull(); + } } @Test @@ -518,20 +554,26 @@ public void testCopy() throws Exception { @Test public void testCopy_sourceMissing_throwsNoSuchFileException() throws Exception { - thrown.expect(NoSuchFileException.class); - Files.copy( - Paths.get(URI.create("gs://military/fashion.show")), - Paths.get(URI.create("gs://greenbean/adipose"))); + try { + Files.copy( + Paths.get(URI.create("gs://military/fashion.show")), + Paths.get(URI.create("gs://greenbean/adipose"))); + Assert.fail(); + } catch (NoSuchFileException expected) { + } } @Test public void testCopy_targetExists_throwsFileAlreadyExistsException() throws Exception { - Path source = Paths.get(URI.create("gs://military/fashion.show")); - Path target = Paths.get(URI.create("gs://greenbean/adipose")); - Files.write(source, "(✿◕ ‿◕ )ノ".getBytes(UTF_8)); - Files.write(target, "(✿◕ ‿◕ )ノ".getBytes(UTF_8)); - thrown.expect(FileAlreadyExistsException.class); - Files.copy(source, target); + try { + Path source = Paths.get(URI.create("gs://military/fashion.show")); + Path target = Paths.get(URI.create("gs://greenbean/adipose")); + Files.write(source, "(✿◕ ‿◕ )ノ".getBytes(UTF_8)); + Files.write(target, "(✿◕ ‿◕ )ノ".getBytes(UTF_8)); + Files.copy(source, target); + Assert.fail(); + } catch (FileAlreadyExistsException expected) { + } } @Test @@ -552,11 +594,15 @@ public void testCopy_directory_doesNothing() throws Exception { @Test public void testCopy_atomic_throwsUnsupported() throws Exception { - Path source = Paths.get(URI.create("gs://military/fashion.show")); - Path target = Paths.get(URI.create("gs://greenbean/adipose")); - Files.write(source, "(✿◕ ‿◕ )ノ".getBytes(UTF_8)); - thrown.expect(UnsupportedOperationException.class); - Files.copy(source, target, ATOMIC_MOVE); + try { + Path source = Paths.get(URI.create("gs://military/fashion.show")); + Path target = Paths.get(URI.create("gs://greenbean/adipose")); + Files.write(source, "(✿◕ ‿◕ )ノ".getBytes(UTF_8)); + Files.copy(source, target, ATOMIC_MOVE); + Assert.fail(); + } catch (UnsupportedOperationException ex) { + assertThat(ex.getMessage()).isNotNull(); + } } @Test @@ -579,11 +625,15 @@ public void testCreateDirectory() throws Exception { @Test public void testMove_atomicMove_notSupported() throws Exception { - Path source = Paths.get(URI.create("gs://military/fashion.show")); - Path target = Paths.get(URI.create("gs://greenbean/adipose")); - Files.write(source, "(✿◕ ‿◕ )ノ".getBytes(UTF_8)); - thrown.expect(AtomicMoveNotSupportedException.class); - Files.move(source, target, ATOMIC_MOVE); + try { + Path source = Paths.get(URI.create("gs://military/fashion.show")); + Path target = Paths.get(URI.create("gs://greenbean/adipose")); + Files.write(source, "(✿◕ ‿◕ )ノ".getBytes(UTF_8)); + Files.move(source, target, ATOMIC_MOVE); + Assert.fail(); + } catch (AtomicMoveNotSupportedException ex) { + assertThat(ex.getMessage()).isNotNull(); + } } @Test diff --git a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemTest.java b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemTest.java index 9369ea673b..e46ab75211 100644 --- a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemTest.java +++ b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemTest.java @@ -37,10 +37,9 @@ import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.List; +import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -58,8 +57,6 @@ public class CloudStorageFileSystemTest { + "The Heart-ache, and the thousand Natural shocks\n" + "That Flesh is heir to? 'Tis a consummation\n"; - @Rule public ExpectedException thrown = ExpectedException.none(); - @Before public void before() { CloudStorageFileSystemProvider.setStorageOptions(LocalStorageHelper.getOptions()); @@ -266,11 +263,14 @@ public void testDeleteEmptyFolder() throws IOException { @Test public void testDeleteFullFolder() throws IOException { - thrown.expect(CloudStoragePseudoDirectoryException.class); try (FileSystem fs = CloudStorageFileSystem.forBucket("bucket")) { Files.write(fs.getPath("dir/angel"), ALONE.getBytes(UTF_8)); // we cannot delete existing folders if they contain something Files.delete(fs.getPath("dir/")); + Assert.fail(); + } catch (CloudStoragePseudoDirectoryException ex) { + assertThat(ex.getMessage()) + .isEqualTo("Can't perform I/O on pseudo-directories (trailing slash): dir/"); } } diff --git a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageLateInitializationTest.java b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageLateInitializationTest.java index 71c8a08084..0885eb76c1 100644 --- a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageLateInitializationTest.java +++ b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageLateInitializationTest.java @@ -26,9 +26,7 @@ import com.google.cloud.storage.StorageOptions; import java.net.URI; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -36,8 +34,6 @@ @RunWith(JUnit4.class) public class CloudStorageLateInitializationTest { - @Rule public final ExpectedException thrown = ExpectedException.none(); - private StorageOptions mockOptions; @Before diff --git a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStoragePathTest.java b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStoragePathTest.java index 028ed0c01a..5fb43719e3 100644 --- a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStoragePathTest.java +++ b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStoragePathTest.java @@ -29,10 +29,9 @@ import java.nio.file.FileSystems; import java.nio.file.Path; import java.nio.file.ProviderMismatchException; +import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -40,8 +39,6 @@ @RunWith(JUnit4.class) public class CloudStoragePathTest { - @Rule public final ExpectedException thrown = ExpectedException.none(); - @Before public void before() { CloudStorageFileSystemProvider.setStorageOptions(LocalStorageHelper.getOptions()); @@ -66,8 +63,10 @@ public void testCreate_preservesTrailingSlash() throws IOException { @Test public void testGetGcsFilename_empty_notAllowed() throws IOException { try (CloudStorageFileSystem fs = CloudStorageFileSystem.forBucket("doodle")) { - thrown.expect(IllegalArgumentException.class); fs.getPath("").getBlobId(); + Assert.fail(); + } catch (IllegalArgumentException ex) { + assertThat(ex.getMessage()).isEqualTo("Object names cannot be empty."); } } @@ -90,8 +89,10 @@ public void testGetGcsFilename_overrideStripPrefixSlash_doesntStripPrefixSlash() @Test public void testGetGcsFilename_extraSlashes_throwsIae() throws IOException { try (CloudStorageFileSystem fs = CloudStorageFileSystem.forBucket("doodle")) { - thrown.expect(IllegalArgumentException.class); fs.getPath("a//b").getBlobId().getName(); + Assert.fail(); + } catch (IllegalArgumentException ex) { + assertThat(ex.getMessage()).isNotNull(); } } @@ -106,8 +107,10 @@ public void testGetGcsFilename_overridepermitEmptyPathComponents() throws IOExce @Test public void testGetGcsFilename_freaksOutOnExtraSlashesAndDotDirs() throws IOException { try (CloudStorageFileSystem fs = CloudStorageFileSystem.forBucket("doodle")) { - thrown.expect(IllegalArgumentException.class); fs.getPath("a//b/..").getBlobId().getName(); + Assert.fail(); + } catch (IllegalArgumentException ex) { + assertThat(ex.getMessage()).isNotNull(); } } @@ -134,16 +137,18 @@ public void testGetName() throws IOException { @Test public void testGetName_negative_throwsIae() throws IOException { try (CloudStorageFileSystem fs = CloudStorageFileSystem.forBucket("doodle")) { - thrown.expect(IllegalArgumentException.class); fs.getPath("angel").getName(-1); + Assert.fail(); + } catch (IllegalArgumentException expected) { } } @Test public void testGetName_overflow_throwsIae() throws IOException { try (CloudStorageFileSystem fs = CloudStorageFileSystem.forBucket("doodle")) { - thrown.expect(IllegalArgumentException.class); fs.getPath("angel").getName(1); + Assert.fail(); + } catch (IllegalArgumentException expected) { } } @@ -203,9 +208,10 @@ public void testToRealPath_hasDotDir_throwsIae() throws IOException { try (CloudStorageFileSystem fs = CloudStorageFileSystem.forBucket("doodle")) { fs.getPath("a/hi./b").toRealPath(); fs.getPath("a/.hi/b").toRealPath(); - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("dot-dir"); fs.getPath("a/./b").toRealPath(); + Assert.fail(); + } catch (IllegalArgumentException ex) { + assertThat(ex.getMessage()).isNotNull(); } } @@ -214,18 +220,22 @@ public void testToRealPath_hasDotDotDir_throwsIae() throws IOException { try (CloudStorageFileSystem fs = CloudStorageFileSystem.forBucket("doodle")) { fs.getPath("a/hi../b").toRealPath(); fs.getPath("a/..hi/b").toRealPath(); - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("dot-dir"); fs.getPath("a/../b").toRealPath(); + Assert.fail(); + } catch (IllegalArgumentException ex) { + assertThat(ex.getMessage()) + .contains("I/O not allowed on dot-dirs or extra slashes when !permitEmptyPathComponents"); } } @Test public void testToRealPath_extraSlashes_throwsIae() throws IOException { try (CloudStorageFileSystem fs = CloudStorageFileSystem.forBucket("doodle")) { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("extra slashes"); fs.getPath("a//b").toRealPath(); + Assert.fail(); + } catch (IllegalArgumentException ex) { + assertThat(ex.getMessage()) + .contains("I/O not allowed on dot-dirs or extra slashes when !permitEmptyPathComponents"); } } @@ -320,8 +330,10 @@ public void testRelativize() throws IOException { @Test public void testRelativize_providerMismatch() throws IOException { try (CloudStorageFileSystem gcs = CloudStorageFileSystem.forBucket("doodle")) { - thrown.expect(ProviderMismatchException.class); gcs.getPath("/etc").relativize(FileSystems.getDefault().getPath("/dog")); + Assert.fail(); + } catch (ProviderMismatchException ex) { + assertThat(ex.getMessage()).contains("Not a Cloud Storage path"); } } @@ -329,8 +341,10 @@ public void testRelativize_providerMismatch() throws IOException { @SuppressWarnings("ReturnValueIgnored") // testing that an Exception is thrown public void testRelativize_providerMismatch2() throws IOException { try (CloudStorageFileSystem gcs = CloudStorageFileSystem.forBucket("doodle")) { - thrown.expect(ProviderMismatchException.class); gcs.getPath("/dog").relativize(FileSystems.getDefault().getPath("/etc")); + Assert.fail(); + } catch (ProviderMismatchException ex) { + assertThat(ex.getMessage()).contains("Not a Cloud Storage path"); } } @@ -345,8 +359,10 @@ public void testResolve() throws IOException { @Test public void testResolve_providerMismatch() throws IOException { try (CloudStorageFileSystem gcs = CloudStorageFileSystem.forBucket("doodle")) { - thrown.expect(ProviderMismatchException.class); gcs.getPath("etc").resolve(FileSystems.getDefault().getPath("/dog")); + Assert.fail(); + } catch (ProviderMismatchException ex) { + assertThat(ex.getMessage()).contains("Not a Cloud Storage path"); } } @@ -449,8 +465,10 @@ public void testRelativize_willWorkWithRecursiveCopy() throws IOException { public void testToFile_unsupported() throws IOException { try (CloudStorageFileSystem fs = CloudStorageFileSystem.forBucket("doodle")) { Path path = fs.getPath("/lol"); - thrown.expect(UnsupportedOperationException.class); path.toFile(); + Assert.fail(); + } catch (UnsupportedOperationException ex) { + assertThat(ex.getMessage()).isEqualTo("GCS objects aren't available locally"); } } diff --git a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageReadChannelTest.java b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageReadChannelTest.java index fbed31aea5..c5681366a8 100644 --- a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageReadChannelTest.java +++ b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageReadChannelTest.java @@ -35,10 +35,9 @@ import java.nio.channels.ClosedChannelException; import java.nio.channels.NonWritableChannelException; import javax.net.ssl.SSLHandshakeException; +import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.mockito.ArgumentCaptor; @@ -47,8 +46,6 @@ @RunWith(JUnit4.class) public class CloudStorageReadChannelTest { - @Rule public final ExpectedException thrown = ExpectedException.none(); - private CloudStorageReadChannel chan; private final Storage gcsStorage = mock(Storage.class); @@ -125,39 +122,53 @@ public void testReadRetrySSLHandshake() throws IOException { @Test public void testReadRetryEventuallyGivesUp() throws IOException { - ByteBuffer buffer = ByteBuffer.allocate(1); - when(gcsChannel.read(eq(buffer))) - .thenThrow( - new StorageException( - new IOException( - "Connection closed prematurely: bytesRead = 33554432, Content-Length = 41943040"))) - .thenThrow( - new StorageException( - new IOException( - "Connection closed prematurely: bytesRead = 33554432, Content-Length = 41943040"))) - .thenReturn(1); - assertThat(chan.position()).isEqualTo(0L); - thrown.expect(StorageException.class); - chan.read(buffer); + try { + ByteBuffer buffer = ByteBuffer.allocate(1); + when(gcsChannel.read(eq(buffer))) + .thenThrow( + new StorageException( + new IOException( + "Connection closed prematurely: bytesRead = 33554432, Content-Length = 41943040"))) + .thenThrow( + new StorageException( + new IOException( + "Connection closed prematurely: bytesRead = 33554432, Content-Length = 41943040"))) + .thenReturn(1); + assertThat(chan.position()).isEqualTo(0L); + chan.read(buffer); + Assert.fail(); + } catch (StorageException ex) { + assertThat(ex.getMessage()).isNotNull(); + } } @Test public void testRead_whenClosed_throwsCce() throws IOException { - when(gcsChannel.isOpen()).thenReturn(false); - thrown.expect(ClosedChannelException.class); - chan.read(ByteBuffer.allocate(1)); + try { + when(gcsChannel.isOpen()).thenReturn(false); + chan.read(ByteBuffer.allocate(1)); + Assert.fail(); + } catch (ClosedChannelException expected) { + } } @Test public void testWrite_throwsNonWritableChannelException() throws IOException { - thrown.expect(NonWritableChannelException.class); - chan.write(ByteBuffer.allocate(1)); + try { + chan.write(ByteBuffer.allocate(1)); + Assert.fail(); + } catch (NonWritableChannelException expected) { + } } @Test public void testTruncate_throwsNonWritableChannelException() throws IOException { - thrown.expect(NonWritableChannelException.class); - chan.truncate(0); + try { + chan.truncate(0); + Assert.fail(); + } catch (NonWritableChannelException ex) { + assertThat(ex.getClass()).isEqualTo(NonWritableChannelException.class); + } } @Test @@ -179,23 +190,35 @@ public void testSize() throws IOException { @Test public void testSize_whenClosed_throwsCce() throws IOException { - when(gcsChannel.isOpen()).thenReturn(false); - thrown.expect(ClosedChannelException.class); - chan.size(); + try { + when(gcsChannel.isOpen()).thenReturn(false); + chan.size(); + Assert.fail(); + } catch (ClosedChannelException ex) { + assertThat(ex.getClass()).isEqualTo(ClosedChannelException.class); + } } @Test public void testPosition_whenClosed_throwsCce() throws IOException { - when(gcsChannel.isOpen()).thenReturn(false); - thrown.expect(ClosedChannelException.class); - chan.position(); + try { + when(gcsChannel.isOpen()).thenReturn(false); + chan.position(); + Assert.fail(); + } catch (ClosedChannelException ex) { + assertThat(ex.getClass()).isEqualTo(ClosedChannelException.class); + } } @Test public void testSetPosition_whenClosed_throwsCce() throws IOException { - when(gcsChannel.isOpen()).thenReturn(false); - thrown.expect(ClosedChannelException.class); - chan.position(0); + try { + when(gcsChannel.isOpen()).thenReturn(false); + chan.position(0); + Assert.fail(); + } catch (ClosedChannelException ex) { + assertThat(ex.getClass()).isEqualTo(ClosedChannelException.class); + } } @Test diff --git a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageReadFileChannelTest.java b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageReadFileChannelTest.java index 9ffff6bf3f..2774474b2c 100644 --- a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageReadFileChannelTest.java +++ b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageReadFileChannelTest.java @@ -24,9 +24,7 @@ import java.nio.channels.FileChannel; import java.nio.channels.SeekableByteChannel; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -103,8 +101,6 @@ public SeekableByteChannel truncate(long size) throws IOException { } } - @Rule public final ExpectedException thrown = ExpectedException.none(); - private CloudStorageReadFileChannel fileChannel; private SeekableByteChannel readChannel; private ByteBuffer data; diff --git a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageReadTest.java b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageReadTest.java index 34370bef26..af84ddea01 100644 --- a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageReadTest.java +++ b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageReadTest.java @@ -32,9 +32,7 @@ import java.nio.file.StandardOpenOption; import java.util.Arrays; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -55,8 +53,6 @@ public class CloudStorageReadTest { // Large enough value that we write more than one "chunk", for interesting behavior. private static final int repeat = 10000; - @Rule public ExpectedException thrown = ExpectedException.none(); - @Before public void before() { CloudStorageFileSystemProvider.setStorageOptions(LocalStorageHelper.getOptions()); diff --git a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageWriteChannelTest.java b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageWriteChannelTest.java index 283cdd8a08..aa388fbbac 100644 --- a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageWriteChannelTest.java +++ b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageWriteChannelTest.java @@ -30,10 +30,9 @@ import java.nio.ByteBuffer; import java.nio.channels.ClosedChannelException; import java.nio.channels.NonReadableChannelException; +import org.junit.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -41,8 +40,6 @@ @RunWith(JUnit4.class) public class CloudStorageWriteChannelTest { - @Rule public final ExpectedException thrown = ExpectedException.none(); - private final WriteChannel gcsChannel = mock(WriteChannel.class); private final CloudStorageWriteChannel chan = new CloudStorageWriteChannel(gcsChannel); @@ -53,8 +50,11 @@ public void before() { @Test public void testRead_throwsNonReadableChannelException() throws IOException { - thrown.expect(NonReadableChannelException.class); - chan.read(ByteBuffer.allocate(1)); + try { + chan.read(ByteBuffer.allocate(1)); + Assert.fail(); + } catch (NonReadableChannelException expected) { + } } @Test @@ -74,9 +74,12 @@ public void testWrite() throws IOException { @Test public void testWrite_whenClosed_throwsCce() throws IOException { - when(gcsChannel.isOpen()).thenReturn(false); - thrown.expect(ClosedChannelException.class); - chan.write(ByteBuffer.allocate(1)); + try { + when(gcsChannel.isOpen()).thenReturn(false); + chan.write(ByteBuffer.allocate(1)); + Assert.fail(); + } catch (ClosedChannelException expected) { + } } @Test @@ -99,16 +102,22 @@ public void testSize() throws IOException { @Test public void testSize_whenClosed_throwsCce() throws IOException { - when(gcsChannel.isOpen()).thenReturn(false); - thrown.expect(ClosedChannelException.class); - chan.size(); + try { + when(gcsChannel.isOpen()).thenReturn(false); + chan.size(); + Assert.fail(); + } catch (ClosedChannelException expected) { + } } @Test public void testPosition_whenClosed_throwsCce() throws IOException { - when(gcsChannel.isOpen()).thenReturn(false); - thrown.expect(ClosedChannelException.class); - chan.position(); + try { + when(gcsChannel.isOpen()).thenReturn(false); + chan.position(); + Assert.fail(); + } catch (ClosedChannelException expected) { + } } @Test diff --git a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageWriteFileChannelTest.java b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageWriteFileChannelTest.java index 62807c2069..7dd105e74c 100644 --- a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageWriteFileChannelTest.java +++ b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageWriteFileChannelTest.java @@ -24,9 +24,7 @@ import java.nio.channels.FileChannel; import java.nio.channels.SeekableByteChannel; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -103,8 +101,6 @@ public SeekableByteChannel truncate(long size) throws IOException { } } - @Rule public final ExpectedException thrown = ExpectedException.none(); - private CloudStorageWriteFileChannel fileChannel; private SeekableByteChannel writeChannel; private ByteBuffer data; diff --git a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/UnixPathTest.java b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/UnixPathTest.java index e9d485746d..ebf87999fe 100644 --- a/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/UnixPathTest.java +++ b/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/UnixPathTest.java @@ -21,9 +21,8 @@ import com.google.common.testing.EqualsTester; import com.google.common.testing.NullPointerTester; -import org.junit.Rule; +import org.junit.Assert; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -31,8 +30,6 @@ @RunWith(JUnit4.class) public class UnixPathTest { - @Rule public final ExpectedException thrown = ExpectedException.none(); - @Test public void testNormalize() { assertThat(p(".").normalize()).isEqualTo(p("")); @@ -133,8 +130,12 @@ public void testRelativize() { @Test public void testRelativize_absoluteMismatch_notAllowed() { - thrown.expect(IllegalArgumentException.class); - p("/a/b/").relativize(p("")); + try { + p("/a/b/").relativize(p("")); + Assert.fail(); + } catch (IllegalArgumentException ex) { + assertThat(ex.getMessage()).isEqualTo("'other' is different type of Path"); + } } @Test @@ -264,32 +265,47 @@ public void testSubpath_empty_returnsEmpty() { @Test public void testSubpath_root_throwsIae() { - thrown.expect(IllegalArgumentException.class); - p("/").subpath(0, 1); + try { + p("/").subpath(0, 1); + Assert.fail(); + } catch (IllegalArgumentException expected) { + } } @Test public void testSubpath_negativeIndex_throwsIae() { - thrown.expect(IllegalArgumentException.class); - p("/eins/zwei/drei/vier").subpath(-1, 1); + try { + p("/eins/zwei/drei/vier").subpath(-1, 1); + Assert.fail(); + } catch (IllegalArgumentException expected) { + } } @Test public void testSubpath_notEnoughElements_throwsIae() { - thrown.expect(IllegalArgumentException.class); - p("/eins/zwei/drei/vier").subpath(0, 5); + try { + p("/eins/zwei/drei/vier").subpath(0, 5); + Assert.fail(); + } catch (IllegalArgumentException expected) { + } } @Test public void testSubpath_beginAboveEnd_throwsIae() { - thrown.expect(IllegalArgumentException.class); - p("/eins/zwei/drei/vier").subpath(1, 0); + try { + p("/eins/zwei/drei/vier").subpath(1, 0); + Assert.fail(); + } catch (IllegalArgumentException expected) { + } } @Test public void testSubpath_beginAndEndEqual_throwsIae() { - thrown.expect(IllegalArgumentException.class); - p("/eins/zwei/drei/vier").subpath(0, 0); + try { + p("/eins/zwei/drei/vier").subpath(0, 0); + Assert.fail(); + } catch (IllegalArgumentException expected) { + } } @Test