|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.storage.it; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import com.google.cloud.WriteChannel; |
| 22 | +import com.google.cloud.storage.Blob; |
| 23 | +import com.google.cloud.storage.BlobInfo; |
| 24 | +import com.google.cloud.storage.BucketInfo; |
| 25 | +import com.google.cloud.storage.DataGenerator; |
| 26 | +import com.google.cloud.storage.PackagePrivateMethodWorkarounds; |
| 27 | +import com.google.cloud.storage.Storage; |
| 28 | +import com.google.cloud.storage.TransportCompatibility.Transport; |
| 29 | +import com.google.cloud.storage.it.runner.StorageITRunner; |
| 30 | +import com.google.cloud.storage.it.runner.annotations.Backend; |
| 31 | +import com.google.cloud.storage.it.runner.annotations.CrossRun; |
| 32 | +import com.google.cloud.storage.it.runner.annotations.Inject; |
| 33 | +import com.google.cloud.storage.it.runner.registry.Generator; |
| 34 | +import java.io.IOException; |
| 35 | +import java.util.Optional; |
| 36 | +import org.junit.Test; |
| 37 | +import org.junit.runner.RunWith; |
| 38 | + |
| 39 | +@RunWith(StorageITRunner.class) |
| 40 | +@CrossRun( |
| 41 | + backends = {Backend.PROD}, |
| 42 | + transports = {Transport.HTTP, Transport.GRPC}) |
| 43 | +public final class ITWriteChannelTest { |
| 44 | + |
| 45 | + @Inject public Storage storage; |
| 46 | + @Inject public BucketInfo bucket; |
| 47 | + @Inject public Generator generator; |
| 48 | + |
| 49 | + @Test |
| 50 | + public void writeChannel_isOpen_onConstruction() throws IOException { |
| 51 | + BlobInfo info = BlobInfo.newBuilder(bucket, generator.randomObjectName()).build(); |
| 52 | + try (WriteChannel writer = storage.writer(info)) { |
| 53 | + assertThat(writer.isOpen()).isTrue(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void writeChannel_createsObjectEvenIfWriteNeverCalled() throws IOException { |
| 59 | + BlobInfo info = BlobInfo.newBuilder(bucket, generator.randomObjectName()).build(); |
| 60 | + WriteChannel w; |
| 61 | + try (WriteChannel writer = storage.writer(info)) { |
| 62 | + w = writer; |
| 63 | + assertThat(writer.isOpen()).isTrue(); |
| 64 | + } |
| 65 | + |
| 66 | + Optional<BlobInfo> internalInfo = |
| 67 | + PackagePrivateMethodWorkarounds.maybeGetBlobInfoFunction().apply(w); |
| 68 | + |
| 69 | + assertThat(internalInfo.isPresent()).isTrue(); |
| 70 | + |
| 71 | + Blob blob = storage.get(info.getBlobId()); |
| 72 | + assertThat(blob).isNotNull(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void writeChannel_openAfterWriteSmallerThanBlockSize() throws IOException { |
| 77 | + BlobInfo info = BlobInfo.newBuilder(bucket, generator.randomObjectName()).build(); |
| 78 | + WriteChannel w; |
| 79 | + try (WriteChannel writer = storage.writer(info)) { |
| 80 | + w = writer; |
| 81 | + assertThat(writer.isOpen()).isTrue(); |
| 82 | + |
| 83 | + int write = writer.write(DataGenerator.base64Characters().genByteBuffer(10)); |
| 84 | + assertThat(write).isEqualTo(10); |
| 85 | + |
| 86 | + assertThat(writer.isOpen()).isTrue(); |
| 87 | + } |
| 88 | + |
| 89 | + assertThat(w.isOpen()).isFalse(); |
| 90 | + } |
| 91 | +} |
0 commit comments