|
20 | 20 | package com.aliyun.oss.integrationtests; |
21 | 21 |
|
22 | 22 | import java.io.File; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
23 | 25 |
|
24 | 26 | import junit.framework.Assert; |
25 | 27 |
|
@@ -171,4 +173,170 @@ public void testUploadFileWithCheckpointFile() { |
171 | 173 | } |
172 | 174 | } |
173 | 175 |
|
| 176 | + static class RangeInfo { |
| 177 | + RangeInfo(long start, long end) { |
| 178 | + this.start = start; |
| 179 | + this.end = end; |
| 180 | + } |
| 181 | + public long start; // start index; |
| 182 | + public long end; // end index; |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + public void testDownloadFileWithRange() { |
| 187 | + final String key = "obj-download-file-range-cp"; |
| 188 | + |
| 189 | + try { |
| 190 | + File file = createSampleFile(key, 1024 * 500); |
| 191 | + |
| 192 | + // upload file |
| 193 | + UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key); |
| 194 | + uploadFileRequest.setUploadFile(file.getAbsolutePath()); |
| 195 | + uploadFileRequest.setTaskNum(10); |
| 196 | + uploadFileRequest.setEnableCheckpoint(true); |
| 197 | + ObjectMetadata objMetadata = new ObjectMetadata(); |
| 198 | + objMetadata.addUserMetadata("prop", "propval"); |
| 199 | + uploadFileRequest.setObjectMetadata(objMetadata); |
| 200 | + |
| 201 | + UploadFileResult uploadRes = ossClient.uploadFile(uploadFileRequest); |
| 202 | + Assert.assertEquals(uploadRes.getMultipartUploadResult().getBucketName(), bucketName); |
| 203 | + Assert.assertEquals(uploadRes.getMultipartUploadResult().getKey(), key); |
| 204 | + |
| 205 | + //range |
| 206 | + List<RangeInfo> rangeInfo = new ArrayList<RangeInfo>(); |
| 207 | + List<RangeInfo> expectRangeInfo = new ArrayList<RangeInfo>(); |
| 208 | + // case normal range |
| 209 | + rangeInfo.add(new RangeInfo(20, 200*1024 + 100)); |
| 210 | + expectRangeInfo.add(new RangeInfo(20, 200*1024 + 100)); |
| 211 | + |
| 212 | + rangeInfo.add(new RangeInfo(100, 101)); |
| 213 | + expectRangeInfo.add(new RangeInfo(100, 101)); |
| 214 | + |
| 215 | + rangeInfo.add(new RangeInfo(200, 200)); |
| 216 | + expectRangeInfo.add(new RangeInfo(200, 200)); |
| 217 | + |
| 218 | + //to end |
| 219 | + rangeInfo.add(new RangeInfo(102402, -1)); |
| 220 | + expectRangeInfo.add(new RangeInfo(102402, file.length() -1)); |
| 221 | + |
| 222 | + //end > size |
| 223 | + rangeInfo.add(new RangeInfo(102402, file.length() + 20)); |
| 224 | + expectRangeInfo.add(new RangeInfo(102402, file.length() -1)); |
| 225 | + |
| 226 | + //start < 0, end > 0 && end < size |
| 227 | + rangeInfo.add(new RangeInfo(-1, 300*1024 + 100)); |
| 228 | + expectRangeInfo.add(new RangeInfo(0, 300*1024 + 100)); |
| 229 | + |
| 230 | + //start < 0, end > size |
| 231 | + rangeInfo.add(new RangeInfo(-1, file.length())); |
| 232 | + expectRangeInfo.add(new RangeInfo(0, file.length() -1)); |
| 233 | + |
| 234 | + //start < 0, end < 0 |
| 235 | + rangeInfo.add(new RangeInfo(-1, -1)); |
| 236 | + expectRangeInfo.add(new RangeInfo(0, file.length() -1)); |
| 237 | + |
| 238 | + //start >= size |
| 239 | + rangeInfo.add(new RangeInfo(file.length(), file.length() + 20)); |
| 240 | + expectRangeInfo.add(new RangeInfo(0, file.length() -1)); |
| 241 | + |
| 242 | + //start > end |
| 243 | + rangeInfo.add(new RangeInfo(1024, 100)); |
| 244 | + expectRangeInfo.add(new RangeInfo(0, file.length() -1)); |
| 245 | + |
| 246 | + Assert.assertEquals(rangeInfo.size(), expectRangeInfo.size()); |
| 247 | + Assert.assertEquals(true, rangeInfo.size() > 0); |
| 248 | + |
| 249 | + for (int i = 0; i < rangeInfo.size(); i++) { |
| 250 | + // download file |
| 251 | + String filePathNew = key + "-" + i +"-new.txt"; |
| 252 | + long start = rangeInfo.get(i).start; |
| 253 | + long end = rangeInfo.get(i).end; |
| 254 | + DownloadFileRequest downloadFileRequest = new DownloadFileRequest(bucketName, key); |
| 255 | + downloadFileRequest.setDownloadFile(filePathNew); |
| 256 | + downloadFileRequest.setTaskNum(10); |
| 257 | + downloadFileRequest.setEnableCheckpoint(true); |
| 258 | + downloadFileRequest.setRange(start, end); |
| 259 | + |
| 260 | + DownloadFileResult downloadRes = ossClient.downloadFile(downloadFileRequest); |
| 261 | + |
| 262 | + ObjectMetadata objMeta = downloadRes.getObjectMetadata(); |
| 263 | + //Assert.assertEquals(objMeta.getContentLength(), downloadFileRequest.getPartSize()); |
| 264 | + Assert.assertEquals(objMeta.getObjectType(), "Multipart"); |
| 265 | + Assert.assertEquals(objMeta.getUserMetadata().get("prop"), "propval"); |
| 266 | + |
| 267 | + File fileNew = new File(filePathNew); |
| 268 | + long expectStart = expectRangeInfo.get(i).start; |
| 269 | + long expectEnd = expectRangeInfo.get(i).end; |
| 270 | + Assert.assertTrue("comparte file", compareFileWithRange(file.getAbsolutePath(), expectStart, expectEnd, fileNew.getAbsolutePath())); |
| 271 | + fileNew.delete(); |
| 272 | + } |
| 273 | + ossClient.deleteObject(bucketName, key); |
| 274 | + } catch (Throwable e) { |
| 275 | + e.printStackTrace(); |
| 276 | + Assert.fail(e.getMessage()); |
| 277 | + ossClient.deleteBucket(bucketName); |
| 278 | + } |
| 279 | + } |
| 280 | + |
| 281 | + @Test |
| 282 | + public void testDownloadEmptyFileWithoutCheckpoint() { |
| 283 | + final String key = "obj-download-empty-file-wcp"; |
| 284 | + |
| 285 | + try { |
| 286 | + File file = createSampleFile(key, 0); |
| 287 | + Assert.assertEquals(0, file.length()); |
| 288 | + |
| 289 | + // upload file |
| 290 | + UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key); |
| 291 | + uploadFileRequest.setUploadFile(file.getAbsolutePath()); |
| 292 | + uploadFileRequest.setTaskNum(10); |
| 293 | + ObjectMetadata objMetadata = new ObjectMetadata(); |
| 294 | + objMetadata.addUserMetadata("prop", "propval"); |
| 295 | + uploadFileRequest.setObjectMetadata(objMetadata); |
| 296 | + |
| 297 | + UploadFileResult uploadRes = ossClient.uploadFile(uploadFileRequest); |
| 298 | + Assert.assertEquals(uploadRes.getMultipartUploadResult().getBucketName(), bucketName); |
| 299 | + Assert.assertEquals(uploadRes.getMultipartUploadResult().getKey(), key); |
| 300 | + |
| 301 | + // download file |
| 302 | + String filePathNew = key + "-new.txt"; |
| 303 | + DownloadFileRequest downloadFileRequest = new DownloadFileRequest(bucketName, key); |
| 304 | + downloadFileRequest.setDownloadFile(filePathNew); |
| 305 | + downloadFileRequest.setTaskNum(10); |
| 306 | + |
| 307 | + DownloadFileResult downloadRes = ossClient.downloadFile(downloadFileRequest); |
| 308 | + |
| 309 | + ObjectMetadata objMeta = downloadRes.getObjectMetadata(); |
| 310 | + Assert.assertEquals(objMeta.getContentLength(), 0); |
| 311 | + Assert.assertEquals(objMeta.getObjectType(), "Multipart"); |
| 312 | + Assert.assertEquals(objMeta.getUserMetadata().get("prop"), "propval"); |
| 313 | + |
| 314 | + File fileNew = new File(filePathNew); |
| 315 | + Assert.assertEquals(0, fileNew.length()); |
| 316 | + fileNew.delete(); |
| 317 | + |
| 318 | + //download file with range |
| 319 | + filePathNew = key + "range-new.txt"; |
| 320 | + downloadFileRequest = new DownloadFileRequest(bucketName, key); |
| 321 | + downloadFileRequest.setDownloadFile(filePathNew); |
| 322 | + downloadFileRequest.setTaskNum(10); |
| 323 | + downloadFileRequest.setRange(0, 0); |
| 324 | + downloadRes = ossClient.downloadFile(downloadFileRequest); |
| 325 | + |
| 326 | + objMeta = downloadRes.getObjectMetadata(); |
| 327 | + Assert.assertEquals(objMeta.getContentLength(), 0); |
| 328 | + Assert.assertEquals(objMeta.getObjectType(), "Multipart"); |
| 329 | + Assert.assertEquals(objMeta.getUserMetadata().get("prop"), "propval"); |
| 330 | + |
| 331 | + fileNew = new File(filePathNew); |
| 332 | + Assert.assertEquals(0, fileNew.length()); |
| 333 | + fileNew.delete(); |
| 334 | + |
| 335 | + ossClient.deleteObject(bucketName, key); |
| 336 | + } catch (Throwable e) { |
| 337 | + e.printStackTrace(); |
| 338 | + Assert.fail(e.getMessage()); |
| 339 | + ossClient.deleteBucket(bucketName); |
| 340 | + } |
| 341 | + } |
174 | 342 | } |
0 commit comments