File tree Expand file tree Collapse file tree 2 files changed +28
-1
lines changed
main/java/com/google/cloud/storage/contrib/nio/testing
test/java/com/google/cloud/storage/contrib/nio/testing Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -413,7 +413,10 @@ public RewriteResponse openRewrite(RewriteRequest rewriteRequest) throws Storage
413
413
// if this is a new file, set generation to 1, else increment the existing generation
414
414
long generation = 1 ;
415
415
if (metadata .containsKey (destKey )) {
416
- generation = metadata .get (destKey ).getGeneration () + 1 ;
416
+ Long storedGeneration = metadata .get (destKey ).getGeneration ();
417
+ if (null != storedGeneration ) {
418
+ generation = storedGeneration + 1 ;
419
+ }
417
420
}
418
421
419
422
checkGeneration (destKey , generationMatch );
Original file line number Diff line number Diff line change @@ -156,4 +156,28 @@ public void testStorageOptionIsSerializable_customOptions() throws Exception {
156
156
assertThat (ois .readObject ()).isEqualTo (storageOptions );
157
157
}
158
158
}
159
+
160
+ @ Test
161
+ public void testCopyOperationOverwritesExistingFile () {
162
+ String bucket = "bucket" ;
163
+ String original = "original" ;
164
+ String replacement = "replacement" ;
165
+ byte [] originalContent = "original content" .getBytes ();
166
+ byte [] replacementContent = "replacement content" .getBytes ();
167
+
168
+ localStorageService .create (BlobInfo .newBuilder (bucket , original ).build (), originalContent );
169
+ localStorageService .create (
170
+ BlobInfo .newBuilder (bucket , replacement ).build (), replacementContent );
171
+
172
+ final Storage .CopyRequest request =
173
+ Storage .CopyRequest .newBuilder ()
174
+ .setSource (BlobId .of (bucket , replacement ))
175
+ .setTarget (BlobId .of (bucket , original ))
176
+ .build ();
177
+
178
+ localStorageService .copy (request ).getResult ();
179
+
180
+ assertThat (localStorageService .readAllBytes (BlobId .of (bucket , original )))
181
+ .isEqualTo (replacementContent );
182
+ }
159
183
}
You can’t perform that action at this time.
0 commit comments