|
| 1 | +package com.qiniu.test; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileOutputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.UUID; |
| 8 | + |
| 9 | +import junit.framework.Assert; |
| 10 | + |
| 11 | +import org.json.JSONException; |
| 12 | +import org.json.JSONObject; |
| 13 | + |
| 14 | +import com.qiniu.auth.JSONObjectRet; |
| 15 | +import com.qiniu.io.IO; |
| 16 | +import com.qiniu.resumableio.ResumableIO; |
| 17 | + |
| 18 | +import android.content.Context; |
| 19 | +import android.net.Uri; |
| 20 | +import android.test.AndroidTestCase; |
| 21 | +import android.test.suitebuilder.annotation.MediumTest; |
| 22 | +import android.test.suitebuilder.annotation.SmallTest; |
| 23 | +import android.util.Log; |
| 24 | + |
| 25 | +public class UploadTest extends AndroidTestCase { |
| 26 | + private String uptoken = "acmKu7Hie1OQ3t31bAovR6JORFX72MMpTicc2xje:n6I4SuMAxBgZ6o3qh8z1bMWqKow=:eyJzY29wZSI6ImFhYTUiLCJyZXR1cm5Cb2R5Ijoie1wiaGFzaFwiOlwiJChldGFnKVwiLFwia2V5XCI6XCIkKGtleSlcIixcImZuYW1lXCI6XCIgJChmbmFtZSkgXCIsXCJmc2l6ZVwiOlwiJChmc2l6ZSlcIixcImF2aW5mb1wiOlwiJChhdmluZm8pXCIsXCJ4OnRlc3RfbmFtZVwiOlwiJCh4OnRlc3RfbmFtZSlcIixcIm5vbnh0ZXN05Lit5paHX25hbWVcIjpcIiQobm9ueHRlc3TkuK3mlodfbmFtZSlcIn0iLCJkZWFkbGluZSI6MTQzMjg2ODA3NH0="; |
| 27 | + |
| 28 | + private File file; |
| 29 | + |
| 30 | + private boolean uploading; |
| 31 | + private boolean success; |
| 32 | + private JSONObjectRet jsonRet; |
| 33 | + private JSONObject resp; |
| 34 | + private Exception e; |
| 35 | + |
| 36 | + private Context context; |
| 37 | + private Uri uri; |
| 38 | + private com.qiniu.io.PutExtra extra; |
| 39 | + private String key = IO.UNDEFINED_KEY; |
| 40 | + |
| 41 | + private com.qiniu.resumableio.PutExtra rextra; |
| 42 | + |
| 43 | + public void setUp() throws Exception { |
| 44 | + key = UUID.randomUUID().toString(); |
| 45 | + uploading = true; |
| 46 | + success = false; |
| 47 | + |
| 48 | + extra = new com.qiniu.io.PutExtra(); |
| 49 | + extra.params = new HashMap<String, String>(); |
| 50 | + extra.params.put("x:a", "测试中文信息"); |
| 51 | + |
| 52 | + rextra = new com.qiniu.resumableio.PutExtra(); |
| 53 | + rextra.params = new HashMap<String, String>(); |
| 54 | + rextra.params.put("x:a", "测试中文信息"); |
| 55 | + |
| 56 | + context = this.getContext(); |
| 57 | + jsonRet = new JSONObjectRet() { |
| 58 | + @Override |
| 59 | + public void onProcess(long current, long total) { |
| 60 | + Log.d("UploadTest", current + "/" + total); |
| 61 | + // Assert.assertEquals(file.length(), total); // 内部实现原因,可能不相等 |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void onSuccess(JSONObject res) { |
| 66 | + uploading = false; |
| 67 | + success = true; |
| 68 | + resp = res; |
| 69 | + Log.d("UploadTest", "上传成功! " + resp.toString()); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void onFailure(Exception ex) { |
| 74 | + uploading = false; |
| 75 | + success = false; |
| 76 | + e = ex; |
| 77 | + Log.d("UploadTest", "上传失败! " + ex.getMessage()); |
| 78 | + } |
| 79 | + }; |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + public void tearDown() throws Exception { |
| 84 | + if(file != null){ |
| 85 | + file.delete(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @SmallTest |
| 90 | + public void testS() throws IOException, JSONException { |
| 91 | + file = createFile(0.2, ".test"); |
| 92 | + uri = Uri.fromFile(file); |
| 93 | + IO.putFile(context, uptoken, key, uri, extra, jsonRet); |
| 94 | + sleepLimit(60); |
| 95 | + successCheck(); |
| 96 | + } |
| 97 | + |
| 98 | + @MediumTest |
| 99 | + public void testM() throws IOException, JSONException { |
| 100 | + file = createFile(4, ".test"); |
| 101 | + uri = Uri.fromFile(file); |
| 102 | + IO.putFile(context, uptoken, key, uri, extra, jsonRet); |
| 103 | + sleepLimit(60 * 5); |
| 104 | + successCheck(); |
| 105 | + } |
| 106 | + |
| 107 | + @SmallTest |
| 108 | + public void testRS() throws IOException, JSONException { |
| 109 | + file = createFile(0.2, ".test"); |
| 110 | + uri = Uri.fromFile(file); |
| 111 | + ResumableIO.putFile(context, uptoken, key, uri, rextra, jsonRet); |
| 112 | + sleepLimit(60); |
| 113 | + successCheck(); |
| 114 | + } |
| 115 | + |
| 116 | + @MediumTest |
| 117 | + public void testRM() throws IOException, JSONException { |
| 118 | + file = createFile(4, ".test"); |
| 119 | + uri = Uri.fromFile(file); |
| 120 | + ResumableIO.putFile(context, uptoken, key, uri, rextra, jsonRet); |
| 121 | + sleepLimit(60 * 5); |
| 122 | + successCheck(); |
| 123 | + } |
| 124 | + |
| 125 | + @MediumTest |
| 126 | + public void testRL() throws IOException, JSONException { |
| 127 | + file = createFile(8.6, ".test"); |
| 128 | + uri = Uri.fromFile(file); |
| 129 | + ResumableIO.putFile(context, uptoken, key, uri, rextra, jsonRet); |
| 130 | + sleepLimit(60 * 5); |
| 131 | + successCheck(); |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + private void successCheck() throws JSONException{ |
| 136 | + Assert.assertTrue(success); |
| 137 | + Assert.assertNotNull(resp.optString("hash")); |
| 138 | + Assert.assertEquals(file.length(), resp.getLong("fsize")); |
| 139 | + } |
| 140 | + |
| 141 | + private void sleepLimit(int limit){ |
| 142 | + int t = 5; |
| 143 | + while(uploading && t < limit){ |
| 144 | + try { |
| 145 | + t += 5; |
| 146 | + Thread.sleep(1000 * 5); |
| 147 | + } catch (InterruptedException e) { |
| 148 | + |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private File createFile(double fileSize, String suf) throws IOException { |
| 154 | + FileOutputStream fos = null; |
| 155 | + try{ |
| 156 | + long size = (long)(1024 * 1024 * fileSize); |
| 157 | + File f = File.createTempFile("qiniu_", suf); |
| 158 | + f.createNewFile(); |
| 159 | + fos = new FileOutputStream(f); |
| 160 | + byte [] b = getByte(); |
| 161 | + long s = 0; |
| 162 | + while(s < size){ |
| 163 | + int l = (int)Math.min(b.length, size - s); |
| 164 | + fos.write(b, 0, l); |
| 165 | + s += l; |
| 166 | + } |
| 167 | + fos.flush(); |
| 168 | + return f; |
| 169 | + }finally{ |
| 170 | + if(fos != null){ |
| 171 | + try { |
| 172 | + fos.close(); |
| 173 | + } catch (IOException e) { |
| 174 | + e.printStackTrace(); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private byte[] getByte(){ |
| 181 | + byte [] b = new byte[1024 * 4]; |
| 182 | + b[0] = 'A'; |
| 183 | + for(int i=1; i < 1024 * 4 ; i++){ |
| 184 | + b[i] = 'b'; |
| 185 | + } |
| 186 | + b[1024 * 4 - 2] = '\r'; |
| 187 | + b[1024 * 4 - 1] = '\n'; |
| 188 | + return b; |
| 189 | + } |
| 190 | + |
| 191 | +} |
0 commit comments