diff --git a/aliyun-java-sdk-core/pom.xml b/aliyun-java-sdk-core/pom.xml index 9524870667..349d14fb51 100644 --- a/aliyun-java-sdk-core/pom.xml +++ b/aliyun-java-sdk-core/pom.xml @@ -92,7 +92,7 @@ org.glassfish.jaxb jaxb-runtime - 4.0.5 + 2.3.2 org.bouncycastle @@ -136,12 +136,6 @@ runtime test - - org.ini4j - ini4j - 0.5.4 - test - org.slf4j slf4j-api diff --git a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/DefaultAcsClient.java b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/DefaultAcsClient.java index 49fb154ac0..b6392c8c31 100644 --- a/aliyun-java-sdk-core/src/main/java/com/aliyuncs/DefaultAcsClient.java +++ b/aliyun-java-sdk-core/src/main/java/com/aliyuncs/DefaultAcsClient.java @@ -385,9 +385,7 @@ private HttpResponse doRealAction(AcsRequest 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() @@ -435,7 +433,7 @@ private HttpResponse doRealAction(AcsRequest 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) { diff --git a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/utils/ProfileUtilsTest.java b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/utils/ProfileUtilsTest.java index be1cdd44ee..01b90c1848 100644 --- a/aliyun-java-sdk-core/src/test/java/com/aliyuncs/utils/ProfileUtilsTest.java +++ b/aliyun-java-sdk-core/src/test/java/com/aliyuncs/utils/ProfileUtilsTest.java @@ -1,6 +1,5 @@ package com.aliyuncs.utils; -import org.ini4j.Wini; import org.junit.Assert; import org.junit.Test; @@ -29,14 +28,11 @@ public void testProfile() throws IOException { + "\t3\n" + "str = #comment\n" + "\ttest\n"; - Wini ini = new Wini(new StringReader(context)); Map> 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")); @@ -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(); @@ -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(); @@ -159,4 +115,93 @@ public void testProfile() throws IOException { } } + @Test + public void testParseFileFromPath() throws IOException { + String resourcePath = "src/test/resources/configTest.ini"; + + Map> 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> 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> 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> 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> 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> iniMap = ProfileUtils.parseFile(new StringReader(context)); + Assert.assertEquals(1, iniMap.size()); + Assert.assertEquals("value\ncontinuation", iniMap.get("?").get("key")); + } + } diff --git a/java-sdk-function-test/src/test/java/com/aliyuncs/CredentialsTest.java b/java-sdk-function-test/src/test/java/com/aliyuncs/CredentialsTest.java index ef0d194090..a2d23a3c77 100644 --- a/java-sdk-function-test/src/test/java/com/aliyuncs/CredentialsTest.java +++ b/java-sdk-function-test/src/test/java/com/aliyuncs/CredentialsTest.java @@ -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")); } }