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 = "anEC5u_72gw1kZPSy3Dsq1lo_DPXyvuPDaj4ePkN:zmaikrTu1lgLb8DTvKQbuFZ5ai0=:eyJzY29wZSI6ImFuZHJvaWRzZGsiLCJyZXR1cm5Cb2R5Ijoie1wiaGFzaFwiOlwiJChldGFnKVwiLFwia2V5XCI6XCIkKGtleSlcIixcImZuYW1lXCI6XCIgJChmbmFtZSkgXCIsXCJmc2l6ZVwiOlwiJChmc2l6ZSlcIixcIm1pbWVUeXBlXCI6XCIkKG1pbWVUeXBlKVwiLFwieDphXCI6XCIkKHg6YSlcIn0iLCJkZWFkbGluZSI6MTQ2NjIyMjcwMX0=" ;
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+ System .out .println ("上传成功! " + resp .toString ());
71+ }
72+
73+ @ Override
74+ public void onFailure (Exception ex ) {
75+ uploading = false ;
76+ success = false ;
77+ e = ex ;
78+ Log .d ("UploadTest" , "上传失败! " + ex .getMessage ());
79+ System .out .println ("上传失败! " + ex .getMessage ());
80+ }
81+ };
82+ }
83+
84+
85+ public void tearDown () throws Exception {
86+ if (file != null ){
87+ file .delete ();
88+ }
89+ }
90+
91+ @ SmallTest
92+ public void testS () throws IOException , JSONException {
93+ file = createFile (0.2 , ".test" );
94+ uri = Uri .fromFile (file );
95+ IO .putFile (context , uptoken , key , uri , extra , jsonRet );
96+ sleepLimit (60 );
97+ successCheck ();
98+ }
99+
100+ @ MediumTest
101+ public void testM () throws IOException , JSONException {
102+ file = createFile (4 , "--—— 地 方.test" );
103+ uri = Uri .fromFile (file );
104+ IO .putFile (context , uptoken , key , uri , extra , jsonRet );
105+ sleepLimit (60 * 5 );
106+ successCheck ();
107+ }
108+
109+ @ SmallTest
110+ public void testRS () throws IOException , JSONException {
111+ file = createFile (0.2 , ".test" );
112+ uri = Uri .fromFile (file );
113+ ResumableIO .putFile (context , uptoken , key , uri , rextra , jsonRet );
114+ sleepLimit (60 );
115+ successCheck ();
116+ }
117+
118+ @ MediumTest
119+ public void testRM () throws IOException , JSONException {
120+ file = createFile (4 , ".test" );
121+ uri = Uri .fromFile (file );
122+ ResumableIO .putFile (context , uptoken , key , uri , rextra , jsonRet );
123+ sleepLimit (60 * 5 );
124+ successCheck ();
125+ }
126+
127+ @ MediumTest
128+ public void testRL () throws IOException , JSONException {
129+ file = createFile (8.6 , ".test" );
130+ uri = Uri .fromFile (file );
131+ ResumableIO .putFile (context , uptoken , key , uri , rextra , jsonRet );
132+ sleepLimit (60 * 5 );
133+ successCheck ();
134+ }
135+
136+
137+ private void successCheck () throws JSONException {
138+ Assert .assertTrue (success );
139+ Assert .assertNotNull (resp .optString ("hash" ));
140+ Assert .assertEquals (file .length (), resp .getLong ("fsize" ));
141+ }
142+
143+ private void sleepLimit (int limit ){
144+ int t = 5 ;
145+ while (uploading && t < limit ){
146+ try {
147+ t += 5 ;
148+ Thread .sleep (1000 * 5 );
149+ } catch (InterruptedException e ) {
150+
151+ }
152+ }
153+ }
154+
155+ private File createFile (double fileSize , String suf ) throws IOException {
156+ FileOutputStream fos = null ;
157+ try {
158+ long size = (long )(1024 * 1024 * fileSize );
159+ File f = File .createTempFile ("qiniu_" , suf );
160+ f .createNewFile ();
161+ fos = new FileOutputStream (f );
162+ byte [] b = getByte ();
163+ long s = 0 ;
164+ while (s < size ){
165+ int l = (int )Math .min (b .length , size - s );
166+ fos .write (b , 0 , l );
167+ s += l ;
168+ }
169+ fos .flush ();
170+ return f ;
171+ }finally {
172+ if (fos != null ){
173+ try {
174+ fos .close ();
175+ } catch (IOException e ) {
176+ e .printStackTrace ();
177+ }
178+ }
179+ }
180+ }
181+
182+ private byte [] getByte (){
183+ byte [] b = new byte [1024 * 4 ];
184+ b [0 ] = 'A' ;
185+ for (int i =1 ; i < 1024 * 4 ; i ++){
186+ b [i ] = 'b' ;
187+ }
188+ b [1024 * 4 - 2 ] = '\r' ;
189+ b [1024 * 4 - 1 ] = '\n' ;
190+ return b ;
191+ }
192+
193+ }
0 commit comments