|
53 | 53 | import java.nio.channels.FileChannel; |
54 | 54 | import java.nio.channels.ReadableByteChannel; |
55 | 55 | import java.nio.channels.SeekableByteChannel; |
| 56 | +import java.nio.file.FileAlreadyExistsException; |
56 | 57 | import java.nio.file.FileSystem; |
57 | 58 | import java.nio.file.FileVisitResult; |
58 | 59 | import java.nio.file.Files; |
@@ -1108,6 +1109,63 @@ public void testListObject() throws IOException { |
1108 | 1109 | assertThat(objects.size()).isEqualTo(1); |
1109 | 1110 | } |
1110 | 1111 |
|
| 1112 | + @Test(expected = FileAlreadyExistsException.class) |
| 1113 | + public void testCopy_replaceFile_withoutOption() throws IOException { |
| 1114 | + CloudStorageFileSystem fs = getTestBucket(); |
| 1115 | + String uuid = UUID.randomUUID().toString(); |
| 1116 | + |
| 1117 | + CloudStoragePath foo = fs.getPath(uuid, "foo.txt"); |
| 1118 | + CloudStoragePath bar = fs.getPath(uuid, "bar.txt"); |
| 1119 | + |
| 1120 | + try { |
| 1121 | + Files.createFile(foo); |
| 1122 | + Files.createFile(bar); |
| 1123 | + |
| 1124 | + Files.copy(foo, bar); |
| 1125 | + } finally { |
| 1126 | + Files.deleteIfExists(foo); |
| 1127 | + Files.deleteIfExists(bar); |
| 1128 | + } |
| 1129 | + } |
| 1130 | + |
| 1131 | + @Test |
| 1132 | + public void testCopy_replaceFile_withOption() throws IOException { |
| 1133 | + CloudStorageFileSystem fs = getTestBucket(); |
| 1134 | + String uuid = UUID.randomUUID().toString(); |
| 1135 | + |
| 1136 | + CloudStoragePath foo = fs.getPath(uuid, "foo.txt"); |
| 1137 | + CloudStoragePath bar = fs.getPath(uuid, "bar.txt"); |
| 1138 | + |
| 1139 | + try { |
| 1140 | + Files.createFile(foo); |
| 1141 | + Files.createFile(bar); |
| 1142 | + |
| 1143 | + Files.copy(foo, bar, StandardCopyOption.REPLACE_EXISTING); |
| 1144 | + } finally { |
| 1145 | + Files.deleteIfExists(foo); |
| 1146 | + Files.deleteIfExists(bar); |
| 1147 | + } |
| 1148 | + } |
| 1149 | + |
| 1150 | + @Test(expected = NoSuchFileException.class) |
| 1151 | + public void testCopy_replaceFile_withOption_srcDoesNotExist() throws IOException { |
| 1152 | + CloudStorageFileSystem fs = getTestBucket(); |
| 1153 | + String uuid = UUID.randomUUID().toString(); |
| 1154 | + |
| 1155 | + CloudStoragePath foo = fs.getPath(uuid, "foo.txt"); |
| 1156 | + CloudStoragePath bar = fs.getPath(uuid, "bar.txt"); |
| 1157 | + |
| 1158 | + try { |
| 1159 | + // explicitly do not create foo |
| 1160 | + Files.createFile(bar); |
| 1161 | + |
| 1162 | + Files.copy(foo, bar); |
| 1163 | + } finally { |
| 1164 | + Files.deleteIfExists(foo); |
| 1165 | + Files.deleteIfExists(bar); |
| 1166 | + } |
| 1167 | + } |
| 1168 | + |
1111 | 1169 | private CloudStorageFileSystem getTestBucket() throws IOException { |
1112 | 1170 | // in typical usage we use the single-argument version of forBucket |
1113 | 1171 | // and rely on the user being logged into their project with the |
|
0 commit comments