@Override public boolean isOpen() { synchronized (this) { return channel.isOpen(); } }
private void checkOpen() throws ClosedChannelException { if (!channel.isOpen()) { throw new ClosedChannelException(); } }
@Test public void testRead_whenClosed_throwsCce() throws IOException { when(gcsChannel.isOpen()).thenReturn(false); thrown.expect(ClosedChannelException.class); chan.read(ByteBuffer.allocate(1)); }
@Test public void testSize() throws IOException { assertThat(chan.size()).isEqualTo(42L); verify(gcsChannel).isOpen(); verifyZeroInteractions(gcsChannel); }
@Test public void testPosition_whenClosed_throwsCce() throws IOException { when(gcsChannel.isOpen()).thenReturn(false); thrown.expect(ClosedChannelException.class); chan.position(); }
@Test public void testSetPosition_whenClosed_throwsCce() throws IOException { when(gcsChannel.isOpen()).thenReturn(false); thrown.expect(ClosedChannelException.class); chan.position(0); }
@Test public void testSize_whenClosed_throwsCce() throws IOException { when(gcsChannel.isOpen()).thenReturn(false); thrown.expect(ClosedChannelException.class); chan.size(); }
@Test public void testRead() throws IOException { ByteBuffer buffer = ByteBuffer.allocate(1); when(gcsChannel.read(eq(buffer))).thenReturn(1); assertThat(chan.position()).isEqualTo(0L); assertThat(chan.read(buffer)).isEqualTo(1); assertThat(chan.position()).isEqualTo(1L); verify(gcsChannel).read(any(ByteBuffer.class)); verify(gcsChannel, times(3)).isOpen(); }
@Before public void before() throws IOException { when(metadata.getSize()).thenReturn(42L); when(metadata.getGeneration()).thenReturn(2L); when(gcsStorage.get( file, Storage.BlobGetOption.fields(Storage.BlobField.GENERATION, Storage.BlobField.SIZE))) .thenReturn(metadata); when(gcsStorage.reader(file, Storage.BlobSourceOption.generationMatch(2L))) .thenReturn(gcsChannel); when(gcsChannel.isOpen()).thenReturn(true); chan = CloudStorageReadChannel.create( gcsStorage, file, 0, 1, CloudStorageConfiguration.DEFAULT, ""); verify(gcsStorage) .get( eq(file), eq(Storage.BlobGetOption.fields(Storage.BlobField.GENERATION, Storage.BlobField.SIZE))); verify(gcsStorage).reader(eq(file), eq(Storage.BlobSourceOption.generationMatch(2L))); }
@Test public void testSetPosition() throws IOException { assertThat(chan.position()).isEqualTo(0L); assertThat(chan.size()).isEqualTo(42L); chan.position(1L); assertThat(chan.position()).isEqualTo(1L); assertThat(chan.size()).isEqualTo(42L); verify(gcsChannel).seek(1); verify(gcsChannel, times(5)).isOpen(); }
@Test public void testReader() { EasyMock.replay(storageRpcMock); initializeService(); ReadChannel channel = storage.reader(BUCKET_NAME1, BLOB_NAME1); assertNotNull(channel); assertTrue(channel.isOpen()); }
@Test public void testReaderWithOptions() throws IOException { byte[] result = new byte[DEFAULT_CHUNK_SIZE]; EasyMock.expect( storageRpcMock.read(BLOB_INFO2.toPb(), BLOB_SOURCE_OPTIONS, 0, DEFAULT_CHUNK_SIZE)) .andReturn(Tuple.of("etag", result)); EasyMock.replay(storageRpcMock); initializeService(); ReadChannel channel = storage.reader( BUCKET_NAME1, BLOB_NAME2, BLOB_SOURCE_GENERATION, BLOB_SOURCE_METAGENERATION); assertNotNull(channel); assertTrue(channel.isOpen()); channel.read(ByteBuffer.allocate(42)); }
@Test public void testReaderWithDecryptionKey() throws IOException { byte[] result = new byte[DEFAULT_CHUNK_SIZE]; EasyMock.expect( storageRpcMock.read(BLOB_INFO2.toPb(), ENCRYPTION_KEY_OPTIONS, 0, DEFAULT_CHUNK_SIZE)) .andReturn(Tuple.of("etag", result)) .times(2); EasyMock.replay(storageRpcMock); initializeService(); ReadChannel channel = storage.reader(BUCKET_NAME1, BLOB_NAME2, BlobSourceOption.decryptionKey(KEY)); assertNotNull(channel); assertTrue(channel.isOpen()); channel.read(ByteBuffer.allocate(42)); channel = storage.reader(BUCKET_NAME1, BLOB_NAME2, BlobSourceOption.decryptionKey(BASE64_KEY)); assertNotNull(channel); assertTrue(channel.isOpen()); channel.read(ByteBuffer.allocate(42)); }
@Test public void testReaderWithOptionsFromBlobId() throws IOException { byte[] result = new byte[DEFAULT_CHUNK_SIZE]; EasyMock.expect( storageRpcMock.read( BLOB_INFO1.getBlobId().toPb(), BLOB_SOURCE_OPTIONS, 0, DEFAULT_CHUNK_SIZE)) .andReturn(Tuple.of("etag", result)); EasyMock.replay(storageRpcMock); initializeService(); ReadChannel channel = storage.reader( BLOB_INFO1.getBlobId(), BLOB_SOURCE_GENERATION_FROM_BLOB_ID, BLOB_SOURCE_METAGENERATION); assertNotNull(channel); assertTrue(channel.isOpen()); channel.read(ByteBuffer.allocate(42)); }
@Override public boolean isOpen() { synchronized (this) { return channel.isOpen(); } }
private void checkOpen() throws ClosedChannelException { if (!channel.isOpen()) { throw new ClosedChannelException(); } }