Skip to content

Commit

Permalink
chore: remove usage of deprecated ExpectedException JUnit rule (googl…
Browse files Browse the repository at this point in the history
…eapis#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
  • Loading branch information
athakor committed Mar 16, 2020
1 parent 9b33637 commit 212a095
Show file tree
Hide file tree
Showing 12 changed files with 271 additions and 169 deletions.
Expand Up @@ -21,18 +21,15 @@
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;

/** Unit tests for {@link CloudStorageConfiguration}. */
@RunWith(JUnit4.class)
public class CloudStorageConfigurationTest {

@Rule public final ExpectedException thrown = ExpectedException.none();

@Test
public void testBuilder() {
CloudStorageConfiguration config =
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Expand Up @@ -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;

Expand All @@ -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());
Expand Down Expand Up @@ -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/");
}
}

Expand Down
Expand Up @@ -26,18 +26,14 @@
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;

/** Unit tests for {@link CloudStorageFileSystemProvider} late initialization. */
@RunWith(JUnit4.class)
public class CloudStorageLateInitializationTest {

@Rule public final ExpectedException thrown = ExpectedException.none();

private StorageOptions mockOptions;

@Before
Expand Down

0 comments on commit 212a095

Please sign in to comment.