Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions aliyun-java-sdk-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>4.0.5</version>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
Expand Down Expand Up @@ -136,12 +136,6 @@
<classifier>runtime</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ini4j</groupId>
<artifactId>ini4j</artifactId>
<version>0.5.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,7 @@ private <T extends AcsResponse> HttpResponse doRealAction(AcsRequest<T> request,
ex = new ClientException(error.getErrorCode(), error.getErrorMessage(), error.getRequestId(), error.getErrorDescription(), error.getAccessDeniedDetail());
}
}
} catch (SocketTimeoutException exp) {
ex = exp;
} catch (IOException exp) {
} catch (Exception exp) {
ex = exp;
}
context = RetryPolicyContext.builder()
Expand Down Expand Up @@ -435,7 +433,7 @@ private <T extends AcsResponse> HttpResponse doRealAction(AcsRequest<T> request,
throw (ClientException) exp;
} else {
throw new ClientException("SDK.RequestTryOrRetryFailed",
"Some errors occurred. Error message for latest request is " + exp.getMessage(), exp);
"Some errors occurred. Error message for latest request is " + errorMessage, exp);
}
} finally {
if (null != logger) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.aliyuncs.utils;

import org.ini4j.Wini;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -29,14 +28,11 @@ public void testProfile() throws IOException {
+ "\t3\n"
+ "str = #comment\n"
+ "\ttest\n";
Wini ini = new Wini(new StringReader(context));
Map<String, Map<String, String>> iniMap = ProfileUtils.parseFile(new StringReader(context));
Assert.assertEquals(4, ini.size());
Assert.assertEquals(4, iniMap.size());
Assert.assertEquals("false #comment", ini.get("profile1").get("enable"));
Assert.assertEquals("false", iniMap.get("profile1").get("enable"));
Assert.assertEquals(ini.get("profile2").get("region"), iniMap.get("profile2").get("region"));
Assert.assertEquals(ini.get("default").get("default_property"), iniMap.get("default").get("default_property"));
Assert.assertEquals("cn-hangzhou#comment", iniMap.get("profile2").get("region"));
Assert.assertEquals("property2", iniMap.get("default").get("default_property"));
Assert.assertEquals("\n3", iniMap.get("profile3").get("int"));
Assert.assertEquals("\ntest", iniMap.get("profile3").get("str"));

Expand All @@ -54,33 +50,18 @@ public void testProfile() throws IOException {
+ "enable = True\n"
+ "[profile7]\n"
+ "enable =\n";
ini = new Wini(new StringReader(context));
iniMap = ProfileUtils.parseFile(new StringReader(context));
Assert.assertEquals(7, ini.size());
Assert.assertEquals(7, iniMap.size());
Assert.assertEquals(false, ini.get("profile1").get("enable", Boolean.class));
Assert.assertEquals(false, Boolean.parseBoolean(iniMap.get("profile1").get("enable")));
Assert.assertEquals(true, ini.get("profile2").get("enable", Boolean.class));
Assert.assertEquals(true, Boolean.parseBoolean(iniMap.get("profile2").get("enable")));
Assert.assertEquals(false, ini.get("profile3").get("enable", Boolean.class));
Assert.assertEquals(false, Boolean.parseBoolean(iniMap.get("profile3").get("enable")));
Assert.assertEquals(false, ini.get("profile4").get("enable", Boolean.class));
Assert.assertEquals(false, Boolean.parseBoolean(iniMap.get("profile4").get("enable")));
Assert.assertEquals(false, ini.get("profile5").get("enable", Boolean.class));
Assert.assertEquals(false, Boolean.parseBoolean(iniMap.get("profile5").get("enable")));
Assert.assertEquals(true, ini.get("profile6").get("enable", Boolean.class));
Assert.assertEquals(true, Boolean.parseBoolean(iniMap.get("profile6").get("enable")));
Assert.assertEquals(false, ini.get("profile7").get("enable", Boolean.class));
Assert.assertEquals(false, Boolean.parseBoolean(iniMap.get("profile7").get("enable")));
Assert.assertEquals("false", iniMap.get("profile1").get("enable"));
Assert.assertEquals("true", iniMap.get("profile2").get("enable"));
Assert.assertEquals("null", iniMap.get("profile3").get("enable"));
Assert.assertEquals("1", iniMap.get("profile4").get("enable"));
Assert.assertEquals("False", iniMap.get("profile5").get("enable"));
Assert.assertEquals("True", iniMap.get("profile6").get("enable"));
Assert.assertEquals("", iniMap.get("profile7").get("enable"));

context = "[invalid\n"
+ "enable = false\n";
try {
new Wini(new StringReader(context));
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("parse error"));
}
try {
ProfileUtils.parseFile(new StringReader(context));
Assert.fail();
Expand All @@ -92,65 +73,40 @@ public void testProfile() throws IOException {
+ "enable = false\n"
+ "str = \n"
+ "\ttest\n";
;
try {
new Wini(new StringReader(context));
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("parse error"));
}
iniMap = ProfileUtils.parseFile(new StringReader(context));
Assert.assertEquals(0, iniMap.size());

context = "[profile1]\n"
+ "[profile2]\n";
ini = new Wini(new StringReader(context));
iniMap = ProfileUtils.parseFile(new StringReader(context));
Assert.assertEquals(2, ini.size());
Assert.assertEquals(2, iniMap.size());
Assert.assertEquals(ini.get("profile1").size(), iniMap.get("profile1").size());
Assert.assertEquals(ini.get("profile2").size(), iniMap.get("profile2").size());
Assert.assertEquals(0, iniMap.get("profile1").size());
Assert.assertEquals(0, iniMap.get("profile2").size());

context = "enable=true\n"
+ "key=value\n"
+ "str = \n"
+ "\ttest\n";
ini = new Wini(new StringReader(context));
iniMap = ProfileUtils.parseFile(new StringReader(context));
Assert.assertEquals(1, ini.size());
Assert.assertEquals(1, iniMap.size());
Assert.assertEquals(4, ini.get("?").size());
Assert.assertEquals(3, iniMap.get("?").size());
Assert.assertEquals("true", ini.get("?").get("enable"));
Assert.assertEquals("true", iniMap.get("?").get("enable"));
Assert.assertEquals("value", ini.get("?").get("key"));
Assert.assertEquals("value", iniMap.get("?").get("key"));
Assert.assertEquals("\ntest", iniMap.get("?").get("str"));

context = "\ttest\n";
ini = new Wini(new StringReader(context));
iniMap = ProfileUtils.parseFile(new StringReader(context));
Assert.assertEquals(1, ini.size());
Assert.assertEquals(1, iniMap.size());
Assert.assertEquals(1, ini.get("?").size());
Assert.assertEquals(1, iniMap.get("?").size());

context = "[profile1]\n"
+ "key \n";
ini = new Wini(new StringReader(context));
iniMap = ProfileUtils.parseFile(new StringReader(context));
Assert.assertEquals(1, ini.size());
Assert.assertEquals(1, iniMap.size());
Assert.assertNull(ini.get("profile1").get("key"));
Assert.assertNull(iniMap.get("profile1").get("key"));

context = "[profile1]\n"
+ " = value\n";
try {
new Wini(new StringReader(context));
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("parse error"));
}
try {
ProfileUtils.parseFile(new StringReader(context));
Assert.fail();
Expand All @@ -159,4 +115,93 @@ public void testProfile() throws IOException {
}
}

@Test
public void testParseFileFromPath() throws IOException {
String resourcePath = "src/test/resources/configTest.ini";

Map<String, Map<String, String>> iniMap = ProfileUtils.parseFile(resourcePath);

Assert.assertTrue("Should contain client profile", iniMap.containsKey("client"));
Assert.assertTrue("Should contain default profile", iniMap.containsKey("default"));
Assert.assertTrue("Should contain client1 profile", iniMap.containsKey("client1"));
Assert.assertTrue("Should contain client2 profile", iniMap.containsKey("client2"));
Assert.assertTrue("Should contain client3 profile", iniMap.containsKey("client3"));
Assert.assertTrue("Should contain client4 profile", iniMap.containsKey("client4"));

Assert.assertEquals("access_key", iniMap.get("default").get("type"));
Assert.assertEquals("foo", iniMap.get("default").get("access_key_id"));
Assert.assertEquals("bar", iniMap.get("default").get("access_key_secret"));
Assert.assertEquals("cn-hangzhou", iniMap.get("default").get("region_id"));

Assert.assertEquals("ecs_ram_role", iniMap.get("client1").get("type"));
Assert.assertEquals("EcsRamRoleTest", iniMap.get("client1").get("role_name"));

Assert.assertEquals("bearer_token", iniMap.get("client3").get("type"));
Assert.assertEquals("bearer_token", iniMap.get("client3").get("bearer_token"));
}

@Test
public void testReaderCloseException() throws IOException {
String context = "[profile1]\n"
+ "key = value\n";

// 使用一个会在关闭时抛出异常的 Reader
StringReader reader = new StringReader(context) {
@Override
public void close() {
super.close();
// 这里不抛出异常,因为 StringReader.close() 不会抛出 IOException
// 但这个测试确保了 finally 块中的 close() 调用被执行
}
};

Map<String, Map<String, String>> iniMap = ProfileUtils.parseFile(reader);
Assert.assertEquals(1, iniMap.size());
Assert.assertEquals("value", iniMap.get("profile1").get("key"));
}

@Test
public void testPropertyContinuationWithoutCurrentProperty() throws IOException {
String context = "\ttest_continuation\n";

Map<String, Map<String, String>> iniMap = ProfileUtils.parseFile(new StringReader(context));
Assert.assertEquals(1, iniMap.size());
Assert.assertEquals(1, iniMap.get("?").size());
// 当没有当前属性时,currentPropertyBeingRead 为 null
// currentPropertyValue 也为 null,所以结果是 "null\ntest_continuation"
Assert.assertEquals("null\ntest_continuation", iniMap.get("?").get(null));
}

@Test
public void testRemoveTrailingCommentsWithNoMatch() throws IOException {
String context = "[profile1]\n"
+ "key = value_without_comment\n";

Map<String, Map<String, String>> iniMap = ProfileUtils.parseFile(new StringReader(context));
Assert.assertEquals(1, iniMap.size());
Assert.assertEquals("value_without_comment", iniMap.get("profile1").get("key"));
}

@Test
public void testPropertyDefinitionWithoutCurrentProfile() throws IOException {
String context = "global_key = global_value\n"
+ "[profile1]\n"
+ "key = value\n";

Map<String, Map<String, String>> iniMap = ProfileUtils.parseFile(new StringReader(context));
Assert.assertEquals(2, iniMap.size());
Assert.assertEquals("global_value", iniMap.get("?").get("global_key"));
Assert.assertEquals("value", iniMap.get("profile1").get("key"));
}

@Test
public void testPropertyContinuationWithoutCurrentProfile() throws IOException {
String context = "key = value\n"
+ "\tcontinuation\n";

Map<String, Map<String, String>> iniMap = ProfileUtils.parseFile(new StringReader(context));
Assert.assertEquals(1, iniMap.size());
Assert.assertEquals("value\ncontinuation", iniMap.get("?").get("key"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ public void oidcProviderForDefaultCredentialsProviderTest() throws ClientExcepti
GetCallerIdentityResponse response = client.getAcsResponse(request);
Assert.assertNotNull(response);
Assert.assertEquals("AssumedRoleUser", response.getIdentityType());
Assert.assertTrue(response.getArn().contains("oidc-role-for-java-sdk-v1-ci"));
Assert.assertTrue(response.getArn().contains("github-action"));
}
}
Loading