Skip to content

Commit

Permalink
change test json lib
Browse files Browse the repository at this point in the history
  • Loading branch information
wujinhu committed Dec 6, 2017
1 parent 80f53f7 commit 9fcf1f7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.8.2</version>
<version>2.9.0</version>
<packaging>jar</packaging>
<name>Aliyun OSS SDK for Java</name>
<description>The Aliyun OSS SDK for Java used for accessing Aliyun Object Storage Service</description>
Expand All @@ -30,10 +30,10 @@
<version>1.1</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import java.util.Date;

import junit.framework.Assert;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.PropertyUtils;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.junit.Test;

import com.aliyun.oss.HttpMethod;
Expand Down Expand Up @@ -264,22 +264,20 @@ public void testGeneratePresignedUrlWithProcess() {
}
}

private static ImageInfo getImageInfo(final String bucket, final String image) throws IOException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
private static ImageInfo getImageInfo(final String bucket, final String image) throws IOException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, JSONException {
GetObjectRequest request = new GetObjectRequest(bucketName, image);
request.setProcess("image/info");
OSSObject ossObject = ossClient.getObject(request);

String jsonStr = IOUtils.readStreamAsString(ossObject.getObjectContent(), "UTF-8");
ossObject.getObjectContent().close();

JSONObject jsonObject = JSONObject.fromObject(jsonStr);
Object bean = JSONObject.toBean(jsonObject);

long height = Long.parseLong((String) PropertyUtils.getProperty(PropertyUtils.getProperty(bean, "ImageHeight"), "value"));
long width = Long.parseLong((String) PropertyUtils.getProperty(PropertyUtils.getProperty(bean, "ImageWidth"), "value"));
long size = Long.parseLong((String) PropertyUtils.getProperty(PropertyUtils.getProperty(bean, "FileSize"), "value"));
String format = (String) PropertyUtils.getProperty(PropertyUtils.getProperty(bean, "Format"), "value");

JSONObject jsonObject = new JSONObject(jsonStr);

long height = jsonObject.getJSONObject("ImageHeight").getLong("value");
long width = jsonObject.getJSONObject("ImageWidth").getLong("value");
long size = jsonObject.getJSONObject("FileSize").getLong("value");
String format = jsonObject.getJSONObject("Format").getString("value");
return new ImageInfo(height, width, size, format);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import junit.framework.Assert;

import org.codehaus.jettison.json.JSONException;
import org.junit.Ignore;
import org.junit.Test;

Expand Down Expand Up @@ -72,7 +73,7 @@ public class SecurityTokenTest {

@SuppressWarnings("deprecation")
@Test
public void testBucketOperationsWithToken() {
public void testBucketOperationsWithToken() throws JSONException {
List<String> actions = new ArrayList<String>();
actions.add("oss:ListBuckets");
List<String> resources = new ArrayList<String>();
Expand Down Expand Up @@ -417,7 +418,7 @@ public void testBucketOperationsWithToken() {
}

@Test
public void testObjectOperationsWithToken() {
public void testObjectOperationsWithToken() throws JSONException {
List<String> actions = new ArrayList<String>();
List<String> resources = new ArrayList<String>();

Expand Down
13 changes: 7 additions & 6 deletions src/test/java/com/aliyun/oss/integrationtests/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
import javax.net.ssl.X509TrustManager;

import junit.framework.Assert;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
Expand All @@ -65,6 +63,9 @@
import com.aliyun.oss.model.InitiateMultipartUploadRequest;
import com.aliyun.oss.model.InitiateMultipartUploadResult;
import com.aliyun.oss.model.PartETag;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

@SuppressWarnings("deprecation")
public class TestUtils {
Expand Down Expand Up @@ -342,21 +343,21 @@ public void setSecurityToken(String securityToken) {
}
}

public static OSSClient createSessionClient(List<String> actions, List<String> resources) {
public static OSSClient createSessionClient(List<String> actions, List<String> resources) throws JSONException {
String tokenPolicy = jsonizeTokenPolicy(actions, resources, true);
StsToken token = getStsToken("", "", 3600, tokenPolicy);
return new OSSClient(OSS_TEST_ENDPOINT, token.accessKeyId, token.secretAccessKey, token.securityToken,
new ClientConfiguration().setSupportCname(false));
}

public static String jsonizeTokenPolicy(List<String> actions, List<String> resources, boolean allow) {
public static String jsonizeTokenPolicy(List<String> actions, List<String> resources, boolean allow) throws JSONException {
JSONObject stmtJsonObject = new JSONObject();
stmtJsonObject.put("Action", actions);
stmtJsonObject.put("Resource", resources);
stmtJsonObject.put("Effect", allow ? "Allow" : "Deny");

JSONArray stmtJsonArray = new JSONArray();
stmtJsonArray.add(0, stmtJsonObject);
stmtJsonArray.put(0, stmtJsonObject);

JSONObject policyJsonObject = new JSONObject();
policyJsonObject.put("Version", "1");
Expand Down Expand Up @@ -401,7 +402,7 @@ public static StsToken getStsToken(String grantor, String grantee,
System.out.println(line);
}

JSONObject tokenJsonObject = JSONObject.fromObject(tokenString.toString());
JSONObject tokenJsonObject = new JSONObject(tokenString.toString());
JSONObject credsJsonObjson = tokenJsonObject.getJSONObject("Credentials");
String accessKeyId = credsJsonObjson.getString("AccessKeyId");
String secretAccessKey = credsJsonObjson.getString("AccessKeySecret");
Expand Down

0 comments on commit 9fcf1f7

Please sign in to comment.