Skip to content

Commit be624f5

Browse files
committed
Merge pull request #49 from qiniu/develop
Release v6.0.3
2 parents 6b06f30 + e24831c commit be624f5

File tree

9 files changed

+257
-29
lines changed

9 files changed

+257
-29
lines changed

.gitignore

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1-
# Gradle
2-
build/
3-
.gradle
1+
# built application files
2+
*.apk
3+
*.ap_
4+
5+
# files for the dex VM
6+
*.dex
7+
8+
# Java class files
9+
*.class
410

5-
#Java
6-
.class
11+
# generated files
12+
bin/
13+
gen/
714

8-
#Android
9-
.apk
10-
.dex
15+
# Local configuration file (sdk path, etc)
16+
local.properties
17+
18+
# Eclipse project files
19+
.classpath
20+
.project
21+
.settings
22+
23+
# Gradle
24+
.gradle
25+
build/

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ before_script:
3232
- adb shell input keyevent 82 &
3333

3434
script:
35-
- ./gradlew build
35+
#- ./gradlew build
36+
- ./gradlew connectedInstrumentTest

AndroidManifest.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
<activity android:name=".demo.MyResumableActivity"
1919
android:theme="@android:style/Theme.Light.NoTitleBar"
2020
android:label="@string/app_name_resumable">
21-
2221
</activity>
22+
23+
<uses-library android:name="android.test.runner" />
2324
</application>
25+
26+
<uses-permission android:name="android.permission.RUN_INSTRUMENTATION" />
27+
<instrumentation android:name="android.test.InstrumentationTestRunner"
28+
android:targetPackage="com.qiniu"
29+
android:label="Test for sdk"/>
2430
</manifest>

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
## CHANGE LOG
22

3+
4+
### v6.0.3
5+
2014-07-11 issue [#49](https://github.com/qiniu/android-sdk/pull/49)
6+
7+
- [#45] block count 计数修正
8+
- [#47] file Uri 修正
9+
- [#48] 调整上传默认为upload.qiniu.com
10+
311
### v6.0.2
412
2014-04-15 issue [#43](https://github.com/qiniu/android-sdk/pull/43)
513

build.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,18 @@ android {
2525
}
2626

2727
instrumentTest.setRoot('tests')
28+
instrumentTest {
29+
manifest.srcFile 'AndroidManifest.xml'
30+
java.srcDirs = ['tests/src']
31+
res.srcDirs = ['res']
32+
assets.srcDirs = ['assets']
33+
resources.srcDirs = ['tests/src']
34+
}
35+
2836
}
2937

3038
dependencies {
3139
compile fileTree(dir: 'libs', include: '*.jar')
3240
}
3341
}
42+
// if don't find sdk, please set the local.properties

src/com/qiniu/conf/Conf.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
public class Conf {
44
public static final String USER_AGENT = "qiniu android-sdk v6.0.0";
5-
public static final String UP_HOST = "http://up.qiniu.com";
5+
public static final String UP_HOST = "http://upload.qiniu.com";
66
}

src/com/qiniu/io/IO.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,15 @@ public void onFailure(Exception ex) {
9494
*/
9595
public void putFile(Context mContext, String key, Uri uri, PutExtra extra, final JSONObjectRet ret) {
9696
if (!uri.toString().startsWith("file")) uri = convertFileUri(mContext, uri);
97-
try {
98-
File file = new File(new URI(uri.toString()));
99-
if (file.exists()) {
100-
putAndClose(key, InputStreamAt.fromFile(file), extra, ret);
101-
return;
102-
}
103-
ret.onFailure(new Exception("file not exist: " + uri.toString()));
104-
} catch (URISyntaxException e) {
105-
e.printStackTrace();
106-
ret.onFailure(e);
97+
98+
File file = new File(uri.getEncodedPath());
99+
if (file.exists()) {
100+
putAndClose(key, InputStreamAt.fromFile(file), extra, ret);
101+
return;
107102
}
103+
ret.onFailure(new Exception("file not exist: " + uri.toString()));
108104
}
105+
109106
public void putFile(String key, File file, PutExtra extra, JSONObjectRet callback) {
110107
putAndClose(key, InputStreamAt.fromFile(file), extra, callback);
111108
}

src/com/qiniu/resumableio/ResumableIO.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void onFailure(Exception ex) {
6363
}
6464

6565
public int put(final String key, final InputStreamAt input, final PutExtra extra, final JSONObjectRet ret) {
66-
final int blkCount = (int) (input.length() / BLOCK_SIZE) + 1;
66+
final int blkCount = (int) ((input.length() + BLOCK_SIZE - 1) / BLOCK_SIZE);
6767
if (extra.processes == null) extra.processes = new PutRet[blkCount];
6868
extra.totalSize = input.length();
6969
final int[] success = new int[] {0};
@@ -149,14 +149,13 @@ public int putFile(String key, File file, PutExtra extra, final JSONObjectRet re
149149

150150
public int putFile(Context mContext, String key, Uri uri, PutExtra extra, final JSONObjectRet ret) {
151151
if (!uri.toString().startsWith("file")) uri = convertFileUri(mContext, uri);
152-
try {
153-
File file = new File(new URI(uri.toString()));
154-
if (file.exists()) return putAndClose(key, InputStreamAt.fromFile(file), extra, ret);
155-
ret.onFailure(new Exception("file not exist: " + uri.toString()));
156-
} catch (URISyntaxException e) {
157-
e.printStackTrace();
158-
ret.onFailure(e);
152+
153+
File file = new File(uri.getEncodedPath());
154+
if (file.exists()) {
155+
return putAndClose(key, InputStreamAt.fromFile(file), extra, ret);
159156
}
157+
ret.onFailure(new Exception("file not exist: " + uri.toString()));
158+
160159
return -1;
161160
}
162161

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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

Comments
 (0)