Skip to content

Commit fb08e35

Browse files
committed
add testcase.
1 parent 0fdfef4 commit fb08e35

34 files changed

+5091
-87
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.aliyun.oss;
21+
22+
import com.aliyun.oss.common.auth.RequestSigner;
23+
import com.aliyun.oss.common.comm.RequestMessage;
24+
import junit.framework.Assert;
25+
import org.junit.Test;
26+
27+
import java.util.LinkedList;
28+
import java.util.List;
29+
30+
public class OSSClientConfigurationTest {
31+
32+
@Test
33+
public void testSetSignerHandlers() {
34+
35+
ClientConfiguration configuration = new ClientConfiguration();
36+
try {
37+
configuration.setSignerHandlers(null);
38+
} catch (Exception e) {
39+
// expected exception.
40+
}
41+
42+
try {
43+
List<RequestSigner> singers = new LinkedList<RequestSigner>();
44+
RequestSigner requestSigner = new RequestSigner() {
45+
@Override
46+
public void sign(RequestMessage request) throws ClientException {
47+
}
48+
};
49+
singers.add(requestSigner);
50+
configuration.setSignerHandlers(singers);
51+
Assert.assertEquals(1, configuration.getSignerHandlers().size());
52+
} catch (Exception e) {
53+
e.printStackTrace();
54+
Assert.fail(e.getMessage());
55+
}
56+
}
57+
58+
@Test
59+
public void testTimeoutClientEnable() {
60+
ClientBuilderConfiguration configuration = new ClientBuilderConfiguration();
61+
Assert.assertFalse(configuration.isRequestTimeoutEnabled());
62+
63+
configuration.setRequestTimeoutEnabled(true);
64+
OSS ossClient = new OSSClientBuilder()
65+
.build("test-endpoint","test-accessId", "test-accessKey", configuration);
66+
Assert.assertTrue(configuration.isRequestTimeoutEnabled());
67+
}
68+
69+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.aliyun.oss.common;
21+
22+
import com.aliyun.oss.ClientErrorCode;
23+
import com.aliyun.oss.ClientException;
24+
import com.aliyun.oss.InconsistentException;
25+
import com.aliyun.oss.OSSException;
26+
import com.aliyun.oss.ServiceException;
27+
import com.aliyun.oss.common.utils.ExceptionFactory;
28+
import org.apache.http.HttpHost;
29+
import org.apache.http.client.ClientProtocolException;
30+
import org.apache.http.conn.HttpHostConnectException;
31+
import org.junit.Assert;
32+
import org.junit.Test;
33+
34+
import java.net.ConnectException;
35+
36+
37+
public class ExceptionTest {
38+
@Test
39+
public void testClientException() {
40+
ClientException exception = new ClientException();
41+
Assert.assertNull(exception.getErrorMessage());
42+
43+
Throwable cause = new Throwable("test error message");
44+
exception = new ClientException(cause);
45+
Assert.assertNull(exception.getErrorMessage());
46+
47+
exception = new ClientException("test err msg", "test err code", "test request id");
48+
Assert.assertEquals("test err msg", exception.getErrorMessage());
49+
Assert.assertEquals("test err code", exception.getErrorCode());
50+
Assert.assertEquals("test request id", exception.getRequestId());
51+
52+
Assert.assertNotNull(exception.getMessage());
53+
}
54+
55+
@Test
56+
public void testInconsistentException() {
57+
InconsistentException exception = new InconsistentException(null, null, null);
58+
exception.setClientChecksum(new Long(123));
59+
exception.setServerChecksum(new Long(123));
60+
exception.setRequestId("test request id");
61+
62+
Assert.assertEquals(exception.getClientChecksum(), new Long(123));
63+
Assert.assertEquals(exception.getServerChecksum(), new Long(123));
64+
Assert.assertEquals(exception.getRequestId(), "test request id");
65+
}
66+
67+
@Test
68+
public void testServiceException() {
69+
ServiceException exception = new ServiceException("test err msg");
70+
Assert.assertEquals("test err msg", exception.getErrorMessage());
71+
72+
exception = new ServiceException(new Throwable("test"));
73+
Assert.assertNull(exception.getErrorMessage());
74+
75+
exception = new ServiceException("test err msg", "test err code", "test request id", "test host id");
76+
Assert.assertEquals("test err msg", exception.getErrorMessage());
77+
Assert.assertEquals("test err code", exception.getErrorCode());
78+
Assert.assertEquals("test request id", exception.getRequestId());
79+
Assert.assertEquals("test host id", exception.getHostId());
80+
81+
exception.setRawResponseError("test response err");
82+
Assert.assertEquals("test response err", exception.getRawResponseError());
83+
Assert.assertTrue(exception.getRawResponseError().contains("test response err"));
84+
}
85+
86+
@Test
87+
public void testOSSException() {
88+
OSSException exception = new OSSException("test err msg");
89+
Assert.assertEquals("test err msg", exception.getErrorMessage());
90+
91+
exception = new OSSException("test err msg", new Throwable("test"));
92+
Assert.assertEquals("test err msg", exception.getErrorMessage());
93+
94+
exception = new OSSException("test err msg", "test err code", "test request id", "test host id",
95+
"test header", "test resource type", "test method", new Throwable("test cause"));
96+
Assert.assertEquals("test err msg", exception.getErrorMessage());
97+
Assert.assertEquals("test err code", exception.getErrorCode());
98+
Assert.assertEquals("test request id", exception.getRequestId());
99+
Assert.assertEquals("test host id", exception.getHostId());
100+
101+
Assert.assertEquals("test header", exception.getHeader());
102+
Assert.assertEquals("test resource type", exception.getResourceType());
103+
Assert.assertEquals("test method", exception.getMethod());
104+
}
105+
106+
@Test
107+
public void testExceptionFactory() {
108+
ExceptionFactory factory = new ExceptionFactory();
109+
ClientProtocolException protocolException = new ClientProtocolException("");
110+
ClientException clientException = factory.createNetworkException(protocolException);
111+
Assert.assertEquals(ClientErrorCode.UNKNOWN, clientException.getErrorCode());
112+
}
113+
114+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.aliyun.oss.common;
21+
22+
import com.aliyun.oss.common.utils.AuthUtils;
23+
import com.aliyun.oss.common.utils.VersionInfoUtils;
24+
import com.aliyun.oss.internal.Mimetypes;
25+
import com.aliyun.oss.internal.OSSConstants;
26+
import com.aliyun.oss.internal.RequestParameters;
27+
import com.aliyun.oss.internal.SignParameters;
28+
import com.aliyun.oss.model.LocationConstraint;
29+
import org.junit.Assert;
30+
import org.junit.Test;
31+
32+
import java.io.ByteArrayInputStream;
33+
import java.io.IOException;
34+
import java.io.InputStream;
35+
36+
public class NoCreationClassTest {
37+
@Test
38+
public void testNoCreationClass() {
39+
// update coverage
40+
RequestParameters requestParameters = new RequestParameters();
41+
OSSConstants ossConstants = new OSSConstants();
42+
LocationConstraint locationConstraint = new LocationConstraint();
43+
SignParameters signParameters = new SignParameters();
44+
AuthUtils authUtils = new AuthUtils();
45+
VersionInfoUtils versionInfoUtils = new VersionInfoUtils();
46+
}
47+
48+
@Test
49+
public void testMimetypesClass() {
50+
String content = "" +
51+
"xdoc application/xdoc\n" +
52+
"#xogg application/xogg\n" +
53+
"\n" +
54+
"xpdf \n" +
55+
"";
56+
try {
57+
Mimetypes mime = Mimetypes.getInstance();
58+
InputStream input = new ByteArrayInputStream(content.getBytes());
59+
mime.loadMimetypes(input);
60+
Assert.assertEquals(mime.getMimetype("test.xdoc"), "application/xdoc");
61+
Assert.assertEquals(mime.getMimetype("test.xogg"), "application/octet-stream");
62+
} catch (IOException e) {
63+
Assert.fail(e.getMessage());
64+
}
65+
66+
}
67+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.aliyun.oss.common.auth;
21+
22+
import junit.framework.Assert;
23+
import org.junit.Test;
24+
25+
public class HmacSHA1SignatureTest {
26+
@Test
27+
public void testHmacSignature() {
28+
HmacSHA1Signature signature = new HmacSHA1Signature();
29+
Assert.assertEquals("HmacSHA1", signature.getAlgorithm());
30+
Assert.assertEquals("1", signature.getVersion());
31+
}
32+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.aliyun.oss.common.auth;
21+
22+
import org.junit.Assert;
23+
import org.junit.Test;
24+
25+
public class PublicKeyTest {
26+
@Test
27+
public void testPublicKey() {
28+
com.aliyuncs.ram.model.v20150501.ListPublicKeysResponse.PublicKey listPublicKey = new com.aliyuncs.ram.model.v20150501.ListPublicKeysResponse.PublicKey();
29+
PublicKey publicKey = new PublicKey(listPublicKey);
30+
Assert.assertNull(publicKey.getPublicKeyId());
31+
Assert.assertNull(publicKey.getPublicKeySpec());
32+
Assert.assertNull(publicKey.getStatus());
33+
Assert.assertNull(publicKey.getCreateDate());
34+
35+
com.aliyuncs.ram.model.v20150501.UploadPublicKeyResponse.PublicKey uploadPublicKey = new com.aliyuncs.ram.model.v20150501.UploadPublicKeyResponse.PublicKey();
36+
publicKey = new PublicKey(uploadPublicKey);
37+
Assert.assertNull(publicKey.getPublicKeyId());
38+
Assert.assertNull(publicKey.getPublicKeySpec());
39+
Assert.assertNull(publicKey.getStatus());
40+
Assert.assertNull(publicKey.getCreateDate());
41+
}
42+
43+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.aliyun.oss.common.comm;
21+
22+
import junit.framework.Assert;
23+
import org.junit.Test;
24+
25+
public class IdleConnectionReaperTest {
26+
@Test
27+
public void testIdleConnectionReaperMethod() {
28+
IdleConnectionReaper.registerConnectionManager(null);
29+
Assert.assertTrue(IdleConnectionReaper.shutdown());
30+
Assert.assertFalse(IdleConnectionReaper.shutdown());
31+
Assert.assertEquals(0, IdleConnectionReaper.size());
32+
}
33+
}

0 commit comments

Comments
 (0)