Skip to content

Commit

Permalink
Merge branch 'developer' of github.com:qiniu/java-sdk into linking
Browse files Browse the repository at this point in the history
  • Loading branch information
Cao Mingming committed Mar 14, 2019
2 parents dc89d31 + da89d79 commit c5698fa
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/qiniu/common/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public String getApiHost() {
/**
* 域名构造器
*/
static class Builder {
public static class Builder {
protected Region region;

public Builder() {
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/qiniu/storage/UploadManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.qiniu.common.QiniuException;
import com.qiniu.http.Client;
import com.qiniu.http.Response;
import com.qiniu.util.IOUtils;
import com.qiniu.util.StringMap;

import java.io.File;
Expand Down Expand Up @@ -86,6 +87,28 @@ public void accept(String key, Object value) {
});
return ret;
}

/**
* 上传字节流,小文件走表单,大文件走分片
* @param inputStream
* @param size
* @param key
* @param token
* @param params
* @param mime
* @param checkCrc
* @return
* @throws QiniuException
* @throws IOException
*/
public Response put(InputStream inputStream, long size, String key, String token, StringMap params,
String mime, boolean checkCrc) throws QiniuException, IOException {
if (size < 0 || size > configuration.putThreshold) {
return put(inputStream, key, token, params, mime);
}
byte[] data = IOUtils.toByteArray(inputStream);
return put(data, key, token, params, mime, checkCrc);
}

/**
* 上传字节数组
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/qiniu/util/IOUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.qiniu.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;

public class IOUtils {

private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

/**
* 输入InputSteam,返回byte[].
* 参考:https://github.com/apache/commons-io/blob/master/src/main/java/org/apache/commons/io/IOUtils.java<br>
* @param input
* @return
* @throws IOException
*/
public static byte[] toByteArray(final InputStream input) throws IOException {
try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int n;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}
}

}
4 changes: 4 additions & 0 deletions src/test/java/test/com/qiniu/TestConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.qiniu.util.Auth;

import test.com.qiniu.storage.FormUploadTest;

public final class TestConfig {

//dummy: ak, sk, ...
Expand Down Expand Up @@ -47,6 +49,8 @@ public static boolean isTravis() {

public static void main(String[] args) {
try {
FormUploadTest t = new FormUploadTest();
t.testFormUploadWithInputStream();
System.out.println("done");
} catch (Exception e) {
e.printStackTrace();
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/test/com/qiniu/storage/FormUploadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -450,6 +451,47 @@ public void testFormLargeSize2() {
}
}
}

/**
* 测试inputStream 表单上传
* 检测reqid是否为Null
* 检测状态码是否为200
*/
@Test
public void testFormUploadWithInputStream() {
testFormUploadWithInputStream(1, -1);
testFormUploadWithInputStream(1, 0);
testFormUploadWithInputStream(1, 1000);
testFormUploadWithInputStream(4 * 1024, 4 * 1024 * 1024);
testFormUploadWithInputStream(5 * 1024, -1);
testFormUploadWithInputStream(5 * 1024, 5 * 1024 * 1024);
}

/**
* 测试inputStream 表单上传
* 检测reqid是否为Null
* 检测状态码是否为200
*/
public void testFormUploadWithInputStream(long kiloSize, long size) {

String token = TestConfig.testAuth.uploadToken(TestConfig.testBucket_z0, TestConfig.testBucket_z0, 3600, null);
System.out.println("token="+token);

try {
File file = TempFile.createFile(kiloSize);
InputStream inputStream = new FileInputStream(file);
System.out.println("length=" + file.length());
System.out.println("size=" + size);
Response response = uploadManager.put(inputStream, size, TestConfig.testBucket_z0, token, null, null, false);
System.out.println("code="+response.statusCode);
System.out.println("reqid="+response.reqId);
System.out.println(response.bodyString());
assertNotNull(response.reqId);
assertEquals(200, response.statusCode);
} catch (Exception e) {
e.printStackTrace();
}
}

class MyRet {
public String hash;
Expand Down
93 changes: 45 additions & 48 deletions src/test/java/test/com/qiniu/streaming/StreamingTest.java
Original file line number Diff line number Diff line change
@@ -1,97 +1,97 @@
package test.com.qiniu.streaming;

import com.google.gson.JsonSyntaxException;
import com.qiniu.common.QiniuException;
import com.qiniu.streaming.StreamingManager;
import com.qiniu.streaming.model.ActivityRecords;
import com.qiniu.streaming.model.StreamAttribute;
import com.qiniu.streaming.model.StreamListing;
import com.qiniu.streaming.model.StreamStatus;
import com.qiniu.util.Auth;
import com.qiniu.util.Json;
import org.junit.Test;
import test.com.qiniu.TestConfig;

import static org.junit.Assert.*;

/**
* Created by bailong on 16/9/22
* Updated by panyuan on 19/3/12
*/
public class StreamingTest {

private Auth auth = TestConfig.testAuth;

private String hub = "pilisdktest";
private String streamKeyPrefix = "pilijava" + System.currentTimeMillis();
private String stream = "javasdk";
private String streamNoExist = "javasdk" + "NoExist";
private String streamKeyPrefix = "javasdk";
private StreamingManager manager = new StreamingManager(auth, hub);


//@Test
/**
* 测试获取不存在的流的信息
* 检测返回状态码是否是612
*/
@Test
public void testGetNoExistStream() {
try {
manager.attribute("nnnoexist");
manager.attribute(streamNoExist);
fail("should not exist");
} catch (QiniuException e) {
e.printStackTrace();
assertEquals(612, e.code());
}
}

// CHECKSTYLE:OFF
//@Test
/**
* 测试创建、禁用、启用、获取流信息、列举
* @throws QiniuException
*/
@Test
public void testStreamOperation() throws QiniuException {
// CHECKSTYLE:ON
String streamKey = streamKeyPrefix + "-a";

manager.create(streamKey);

StreamAttribute attr = manager.attribute(streamKey);
StreamAttribute attr = manager.attribute(stream);
assertEquals(0, attr.disabledTill);
assertNotEquals(0, attr.createdAt);

try {
manager.create(streamKey);
manager.create(stream);
fail("has already existed");
} catch (QiniuException e) {
assertEquals(614, e.code());
}

manager.disableTill(streamKey, -1);
manager.disableTill(stream, -1);

attr = manager.attribute(streamKey);
attr = manager.attribute(stream);
assertEquals(-1, attr.disabledTill);
assertNotEquals(0, attr.updatedAt);

manager.enable(streamKey);
attr = manager.attribute(streamKey);
manager.enable(stream);
attr = manager.attribute(stream);
assertEquals(0, attr.disabledTill);
assertNotEquals(0, attr.updatedAt);

long t = System.currentTimeMillis() / 1000 + 3600;
manager.disableTill(streamKey, t);
attr = manager.attribute(streamKey);
manager.disableTill(stream, t);
attr = manager.attribute(stream);
assertEquals(t, attr.disabledTill);
assertNotEquals(0, attr.updatedAt);

manager.enable(streamKey);
attr = manager.attribute(streamKey);
manager.enable(stream);
attr = manager.attribute(stream);
assertEquals(0, attr.disabledTill);
assertNotEquals(0, attr.updatedAt);

try {
StreamStatus status = manager.status(streamKey);
manager.status(stream);
fail();
} catch (QiniuException e) {
assertEquals(619, e.code());
}

try {
manager.saveAs(streamKey, null, 0, 0);
manager.saveAs(stream, null, 0, 0);
fail();
} catch (QiniuException e) {
assertEquals(619, e.code());
}

ActivityRecords records = manager.history(streamKey, System.currentTimeMillis() / 1000 - 1000, 0);
ActivityRecords records = manager.history(stream, System.currentTimeMillis() / 1000 - 1000, 0);
assertEquals(0, records.items.length);

StreamListing l = manager.listStreams(false, streamKeyPrefix, null);
Expand All @@ -118,34 +118,31 @@ public void testStreamOperation() throws QiniuException {
assertFalse(it.hasNext());
}

//@Test
/**
* 测试saveas
* 检测返回状态码是否是404
* @throws QiniuException
*/
@Test
public void testSaveAs() throws QiniuException {
try {
manager.saveAs("test--sd", "f\"ff.m3u8");
manager.saveAs(streamNoExist, "f\"ff.m3u8");
} catch (QiniuException e) {
// 619 , no data; 612 stream not found, 但请求正常 //
if (e.code() != 619 && e.code() != 612) {
throw e;
}
assertEquals(404, e.response.statusCode);
}
}

//@Test
/**
* 测试创建流
* 检测返回状态码是否为614
* @throws QiniuException
*/
@Test
public void testCreate() throws QiniuException {
try {
String body = String.format("{\"key\":\"%s\"}", "stream\"Key");
System.out.println(body);
Json.decode(body);
fail("json 解析不正确");
} catch (JsonSyntaxException e) {

}
try {
manager.create("streamKey");
manager.create(stream);
} catch (QiniuException e) {
if (e.code() != 614) {
throw e;
}
assertEquals(614, e.code());
}
}

Expand Down

0 comments on commit c5698fa

Please sign in to comment.