|
| 1 | +package com.qiniu.storage; |
| 2 | + |
| 3 | +import com.qiniu.TestConfig; |
| 4 | +import com.qiniu.common.QiniuException; |
| 5 | +import com.qiniu.common.Zone; |
| 6 | +import com.qiniu.http.Response; |
| 7 | +import com.qiniu.storage.model.BatchStatus; |
| 8 | +import com.qiniu.storage.model.FileInfo; |
| 9 | +import com.qiniu.storage.model.FileListing; |
| 10 | +import com.qiniu.util.StringMap; |
| 11 | +import com.qiniu.util.StringUtils; |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +import static org.junit.Assert.*; |
| 15 | + |
| 16 | +@SuppressWarnings("ConstantConditions") |
| 17 | +public class AutoZoneBucketTest { |
| 18 | + private BucketManager bucketManager = new BucketManager(TestConfig.testAuth, new Configuration(Zone.autoZone())); |
| 19 | + private BucketManager dummyBucketManager = new BucketManager(TestConfig.dummyAuth, |
| 20 | + new Configuration(Zone.autoZone())); |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testBuckets() { |
| 24 | + try { |
| 25 | + String[] buckets = bucketManager.buckets(); |
| 26 | + assertTrue(StringUtils.inStringArray(TestConfig.bucket, buckets)); |
| 27 | + } catch (QiniuException e) { |
| 28 | + fail(e.getMessage()); |
| 29 | + } |
| 30 | + |
| 31 | + try { |
| 32 | + dummyBucketManager.buckets(); |
| 33 | + fail(); |
| 34 | + } catch (QiniuException e) { |
| 35 | + assertEquals(401, e.code()); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testList() { |
| 41 | + try { |
| 42 | + FileListing l = bucketManager.listFiles(TestConfig.bucket, null, null, 2, null); |
| 43 | + assertNotNull(l.items[0]); |
| 44 | + assertNotNull(l.marker); |
| 45 | + } catch (QiniuException e) { |
| 46 | + e.printStackTrace(); |
| 47 | + assertEquals("", e.error()); |
| 48 | + assertEquals("", e.getMessage()); |
| 49 | + fail(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testListUseDelimiter() { |
| 55 | + try { |
| 56 | + bucketManager.copy(TestConfig.bucket, TestConfig.key, TestConfig.bucket, "test/", true); |
| 57 | + bucketManager.copy(TestConfig.bucket, TestConfig.key, TestConfig.bucket, "test/1", true); |
| 58 | + bucketManager.copy(TestConfig.bucket, TestConfig.key, TestConfig.bucket, "test/2", true); |
| 59 | + bucketManager.copy(TestConfig.bucket, TestConfig.key, TestConfig.bucket, "test/3/", true); |
| 60 | + FileListing l = bucketManager.listFiles(TestConfig.bucket, "test/", null, 10, "/"); |
| 61 | + assertEquals(3, l.items.length); |
| 62 | + assertEquals(2, l.commonPrefixes.length); |
| 63 | + |
| 64 | + } catch (QiniuException e) { |
| 65 | + e.printStackTrace(); |
| 66 | + assertEquals("", e.error()); |
| 67 | + assertEquals("", e.getMessage()); |
| 68 | + fail(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testListIterator() { |
| 74 | + BucketManager.FileListIterator it = bucketManager |
| 75 | + .createFileListIterator(TestConfig.bucket, "", 20, null); |
| 76 | + |
| 77 | + assertTrue(it.hasNext()); |
| 78 | + FileInfo[] items0 = it.next(); |
| 79 | + assertNotNull(items0[0]); |
| 80 | + |
| 81 | + while (it.hasNext()) { |
| 82 | + FileInfo[] items = it.next(); |
| 83 | + if (items.length > 1) { |
| 84 | + assertNotNull(items[0]); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testStat() { |
| 91 | + try { |
| 92 | + FileInfo info = bucketManager.stat(TestConfig.bucket, TestConfig.key); |
| 93 | + assertEquals("FmYW4RYti94tr4ncaKzcTJz9M4Y9", info.hash); |
| 94 | + } catch (QiniuException e) { |
| 95 | + e.printStackTrace(); |
| 96 | + fail(); |
| 97 | + } |
| 98 | + |
| 99 | + try { |
| 100 | + bucketManager.stat(TestConfig.bucket, "noFile"); |
| 101 | + fail(); |
| 102 | + } catch (QiniuException e) { |
| 103 | + assertEquals(612, e.code()); |
| 104 | + } |
| 105 | + |
| 106 | + try { |
| 107 | + bucketManager.stat(TestConfig.bucket, null); |
| 108 | + fail(); |
| 109 | + } catch (QiniuException e) { |
| 110 | + assertEquals(612, e.code()); |
| 111 | + } |
| 112 | + |
| 113 | + try { |
| 114 | + bucketManager.stat("noBucket", "noFile"); |
| 115 | + fail(); |
| 116 | + } catch (QiniuException e) { |
| 117 | + assertEquals(631, e.code()); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testDelete() { |
| 123 | + try { |
| 124 | + bucketManager.delete(TestConfig.bucket, "del"); |
| 125 | + fail(); |
| 126 | + } catch (QiniuException e) { |
| 127 | + assertEquals(612, e.code()); |
| 128 | + } |
| 129 | + |
| 130 | + try { |
| 131 | + bucketManager.delete(TestConfig.bucket, null); |
| 132 | + fail(); |
| 133 | + } catch (QiniuException e) { |
| 134 | + assertEquals(612, e.code()); |
| 135 | + } |
| 136 | + |
| 137 | + try { |
| 138 | + bucketManager.delete("noBucket", null); |
| 139 | + fail(); |
| 140 | + } catch (QiniuException e) { |
| 141 | + assertEquals(631, e.code()); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void testRename() { |
| 147 | + String key = "renameFrom" + Math.random(); |
| 148 | + try { |
| 149 | + bucketManager.copy(TestConfig.bucket, TestConfig.key, TestConfig.bucket, key); |
| 150 | + String key2 = "renameTo" + key; |
| 151 | + bucketManager.rename(TestConfig.bucket, key, key2); |
| 152 | + bucketManager.delete(TestConfig.bucket, key2); |
| 153 | + } catch (QiniuException e) { |
| 154 | + e.printStackTrace(); |
| 155 | + fail(e.getMessage()); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void testCopy() { |
| 161 | + String key = "copyTo" + Math.random(); |
| 162 | + try { |
| 163 | + bucketManager.copy(TestConfig.bucket, TestConfig.key, TestConfig.bucket, key); |
| 164 | + bucketManager.delete(TestConfig.bucket, key); |
| 165 | + } catch (QiniuException e) { |
| 166 | + e.printStackTrace(); |
| 167 | + fail(); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + public void testChangeMime() { |
| 173 | + try { |
| 174 | + bucketManager.changeMime(TestConfig.bucket, "java-sdk.html", "text.html"); |
| 175 | + } catch (QiniuException e) { |
| 176 | + e.printStackTrace(); |
| 177 | + fail(); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + public void testPrefetch() { |
| 183 | + try { |
| 184 | + bucketManager.prefetch(TestConfig.bucket, "java-sdk.html"); |
| 185 | + } catch (QiniuException e) { |
| 186 | + e.printStackTrace(); |
| 187 | + fail(); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + public void testFetch() { |
| 193 | + try { |
| 194 | + bucketManager.fetch("http://developer.qiniu.com/docs/v6/sdk/java-sdk.html", |
| 195 | + TestConfig.bucket, "fetch.html"); |
| 196 | + } catch (QiniuException e) { |
| 197 | + fail(); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + |
| 202 | + @Test |
| 203 | + public void testBatchCopy() { |
| 204 | + String key = "copyTo" + Math.random(); |
| 205 | + BucketManager.Batch ops = new BucketManager.Batch(). |
| 206 | + copy(TestConfig.bucket, TestConfig.key, TestConfig.bucket, key); |
| 207 | + try { |
| 208 | + Response r = bucketManager.batch(ops); |
| 209 | + BatchStatus[] bs = r.jsonToObject(BatchStatus[].class); |
| 210 | + assertEquals(200, bs[0].code); |
| 211 | + } catch (QiniuException e) { |
| 212 | + e.printStackTrace(); |
| 213 | + fail(); |
| 214 | + } |
| 215 | + ops = new BucketManager.Batch().delete(TestConfig.bucket, key); |
| 216 | + try { |
| 217 | + Response r = bucketManager.batch(ops); |
| 218 | + BatchStatus[] bs = r.jsonToObject(BatchStatus[].class); |
| 219 | + assertEquals(200, bs[0].code); |
| 220 | + } catch (QiniuException e) { |
| 221 | + e.printStackTrace(); |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + @Test |
| 226 | + public void testBatchMove() { |
| 227 | + String key = "moveFrom" + Math.random(); |
| 228 | + try { |
| 229 | + bucketManager.copy(TestConfig.bucket, TestConfig.key, TestConfig.bucket, key); |
| 230 | + } catch (QiniuException e) { |
| 231 | + e.printStackTrace(); |
| 232 | + fail(); |
| 233 | + } |
| 234 | + String key2 = key + "to"; |
| 235 | + StringMap x = new StringMap().put(key, key2); |
| 236 | + BucketManager.Batch ops = new BucketManager.Batch().move(TestConfig.bucket, |
| 237 | + key, TestConfig.bucket, key2); |
| 238 | + try { |
| 239 | + Response r = bucketManager.batch(ops); |
| 240 | + BatchStatus[] bs = r.jsonToObject(BatchStatus[].class); |
| 241 | + assertEquals(200, bs[0].code); |
| 242 | + } catch (QiniuException e) { |
| 243 | + e.printStackTrace(); |
| 244 | + fail(); |
| 245 | + } |
| 246 | + try { |
| 247 | + bucketManager.delete(TestConfig.bucket, key2); |
| 248 | + } catch (QiniuException e) { |
| 249 | + e.printStackTrace(); |
| 250 | + fail(); |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + @Test |
| 255 | + public void testBatchRename() { |
| 256 | + String key = "rename" + Math.random(); |
| 257 | + try { |
| 258 | + bucketManager.copy(TestConfig.bucket, TestConfig.key, TestConfig.bucket, key); |
| 259 | + } catch (QiniuException e) { |
| 260 | + e.printStackTrace(); |
| 261 | + fail(); |
| 262 | + } |
| 263 | + String key2 = key + "to"; |
| 264 | + BucketManager.Batch ops = new BucketManager.Batch().rename(TestConfig.bucket, key, key2); |
| 265 | + try { |
| 266 | + Response r = bucketManager.batch(ops); |
| 267 | + BatchStatus[] bs = r.jsonToObject(BatchStatus[].class); |
| 268 | + assertEquals(200, bs[0].code); |
| 269 | + } catch (QiniuException e) { |
| 270 | + e.printStackTrace(); |
| 271 | + fail(); |
| 272 | + } |
| 273 | + try { |
| 274 | + bucketManager.delete(TestConfig.bucket, key2); |
| 275 | + } catch (QiniuException e) { |
| 276 | + e.printStackTrace(); |
| 277 | + fail(); |
| 278 | + } |
| 279 | + } |
| 280 | + |
| 281 | + @Test |
| 282 | + public void testBatchStat() { |
| 283 | + String[] array = {"java-sdk.html"}; |
| 284 | + BucketManager.Batch ops = new BucketManager.Batch().stat(TestConfig.bucket, array); |
| 285 | + try { |
| 286 | + Response r = bucketManager.batch(ops); |
| 287 | + BatchStatus[] bs = r.jsonToObject(BatchStatus[].class); |
| 288 | + assertEquals(200, bs[0].code); |
| 289 | + } catch (QiniuException e) { |
| 290 | + e.printStackTrace(); |
| 291 | + fail(); |
| 292 | + } |
| 293 | + } |
| 294 | + |
| 295 | + @Test |
| 296 | + public void testBatch() { |
| 297 | + String[] array = {"java-sdk.html"}; |
| 298 | + String key = "copyFrom" + Math.random(); |
| 299 | + |
| 300 | + String key1 = "moveFrom" + Math.random(); |
| 301 | + String key2 = "moveTo" + Math.random(); |
| 302 | + |
| 303 | + String key3 = "moveFrom" + Math.random(); |
| 304 | + String key4 = "moveTo" + Math.random(); |
| 305 | + |
| 306 | + try { |
| 307 | + bucketManager.copy(TestConfig.bucket, TestConfig.key, TestConfig.bucket, key1); |
| 308 | + bucketManager.copy(TestConfig.bucket, TestConfig.key, TestConfig.bucket, key3); |
| 309 | + } catch (QiniuException e) { |
| 310 | + e.printStackTrace(); |
| 311 | + fail(); |
| 312 | + } |
| 313 | + |
| 314 | + BucketManager.Batch ops = new BucketManager.Batch() |
| 315 | + .copy(TestConfig.bucket, TestConfig.key, TestConfig.bucket, key) |
| 316 | + .move(TestConfig.bucket, key1, TestConfig.bucket, key2) |
| 317 | + .rename(TestConfig.bucket, key3, key4) |
| 318 | + .stat(TestConfig.bucket, array) |
| 319 | + .stat(TestConfig.bucket, array[0]); |
| 320 | + try { |
| 321 | + Response r = bucketManager.batch(ops); |
| 322 | + BatchStatus[] bs = r.jsonToObject(BatchStatus[].class); |
| 323 | + for (BatchStatus b : bs) { |
| 324 | + assertEquals(200, b.code); |
| 325 | + } |
| 326 | + } catch (QiniuException e) { |
| 327 | + e.printStackTrace(); |
| 328 | + fail(); |
| 329 | + } |
| 330 | + |
| 331 | + BucketManager.Batch opsDel = new BucketManager.Batch().delete(TestConfig.bucket, |
| 332 | + key, key1, key2, key3, key4); |
| 333 | + |
| 334 | + try { |
| 335 | + bucketManager.batch(opsDel); |
| 336 | + } catch (QiniuException e) { |
| 337 | + e.printStackTrace(); |
| 338 | + fail(); |
| 339 | + } |
| 340 | + } |
| 341 | +} |
0 commit comments