Skip to content

Commit 23d0077

Browse files
committed
fix: resolve exception show out && remove ini4j
1 parent ee819b0 commit 23d0077

File tree

3 files changed

+106
-69
lines changed

3 files changed

+106
-69
lines changed

aliyun-java-sdk-core/pom.xml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
<dependency>
9393
<groupId>org.glassfish.jaxb</groupId>
9494
<artifactId>jaxb-runtime</artifactId>
95-
<version>4.0.5</version>
95+
<version>2.3.2</version>
9696
</dependency>
9797
<dependency>
9898
<groupId>org.bouncycastle</groupId>
@@ -136,12 +136,6 @@
136136
<classifier>runtime</classifier>
137137
<scope>test</scope>
138138
</dependency>
139-
<dependency>
140-
<groupId>org.ini4j</groupId>
141-
<artifactId>ini4j</artifactId>
142-
<version>0.5.4</version>
143-
<scope>test</scope>
144-
</dependency>
145139
<dependency>
146140
<groupId>org.slf4j</groupId>
147141
<artifactId>slf4j-api</artifactId>
@@ -184,8 +178,8 @@
184178
<artifactId>maven-compiler-plugin</artifactId>
185179
<version>3.13.0</version>
186180
<configuration>
187-
<source>1.6</source>
188-
<target>1.6</target>
181+
<source>1.8</source>
182+
<target>1.8</target>
189183
<encoding>UTF-8</encoding>
190184
</configuration>
191185
</plugin>

aliyun-java-sdk-core/src/main/java/com/aliyuncs/DefaultAcsClient.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,7 @@ private <T extends AcsResponse> HttpResponse doRealAction(AcsRequest<T> request,
385385
ex = new ClientException(error.getErrorCode(), error.getErrorMessage(), error.getRequestId(), error.getErrorDescription(), error.getAccessDeniedDetail());
386386
}
387387
}
388-
} catch (SocketTimeoutException exp) {
389-
ex = exp;
390-
} catch (IOException exp) {
388+
} catch (Exception exp) {
391389
ex = exp;
392390
}
393391
context = RetryPolicyContext.builder()
@@ -435,7 +433,7 @@ private <T extends AcsResponse> HttpResponse doRealAction(AcsRequest<T> request,
435433
throw (ClientException) exp;
436434
} else {
437435
throw new ClientException("SDK.RequestTryOrRetryFailed",
438-
"Some errors occurred. Error message for latest request is " + exp.getMessage(), exp);
436+
"Some errors occurred. Error message for latest request is " + errorMessage, exp);
439437
}
440438
} finally {
441439
if (null != logger) {
Lines changed: 101 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.aliyuncs.utils;
22

3-
import org.ini4j.Wini;
43
import org.junit.Assert;
54
import org.junit.Test;
65

@@ -29,14 +28,11 @@ public void testProfile() throws IOException {
2928
+ "\t3\n"
3029
+ "str = #comment\n"
3130
+ "\ttest\n";
32-
Wini ini = new Wini(new StringReader(context));
3331
Map<String, Map<String, String>> iniMap = ProfileUtils.parseFile(new StringReader(context));
34-
Assert.assertEquals(4, ini.size());
3532
Assert.assertEquals(4, iniMap.size());
36-
Assert.assertEquals("false #comment", ini.get("profile1").get("enable"));
3733
Assert.assertEquals("false", iniMap.get("profile1").get("enable"));
38-
Assert.assertEquals(ini.get("profile2").get("region"), iniMap.get("profile2").get("region"));
39-
Assert.assertEquals(ini.get("default").get("default_property"), iniMap.get("default").get("default_property"));
34+
Assert.assertEquals("cn-hangzhou#comment", iniMap.get("profile2").get("region"));
35+
Assert.assertEquals("property2", iniMap.get("default").get("default_property"));
4036
Assert.assertEquals("\n3", iniMap.get("profile3").get("int"));
4137
Assert.assertEquals("\ntest", iniMap.get("profile3").get("str"));
4238

@@ -54,33 +50,18 @@ public void testProfile() throws IOException {
5450
+ "enable = True\n"
5551
+ "[profile7]\n"
5652
+ "enable =\n";
57-
ini = new Wini(new StringReader(context));
5853
iniMap = ProfileUtils.parseFile(new StringReader(context));
59-
Assert.assertEquals(7, ini.size());
6054
Assert.assertEquals(7, iniMap.size());
61-
Assert.assertEquals(false, ini.get("profile1").get("enable", Boolean.class));
62-
Assert.assertEquals(false, Boolean.parseBoolean(iniMap.get("profile1").get("enable")));
63-
Assert.assertEquals(true, ini.get("profile2").get("enable", Boolean.class));
64-
Assert.assertEquals(true, Boolean.parseBoolean(iniMap.get("profile2").get("enable")));
65-
Assert.assertEquals(false, ini.get("profile3").get("enable", Boolean.class));
66-
Assert.assertEquals(false, Boolean.parseBoolean(iniMap.get("profile3").get("enable")));
67-
Assert.assertEquals(false, ini.get("profile4").get("enable", Boolean.class));
68-
Assert.assertEquals(false, Boolean.parseBoolean(iniMap.get("profile4").get("enable")));
69-
Assert.assertEquals(false, ini.get("profile5").get("enable", Boolean.class));
70-
Assert.assertEquals(false, Boolean.parseBoolean(iniMap.get("profile5").get("enable")));
71-
Assert.assertEquals(true, ini.get("profile6").get("enable", Boolean.class));
72-
Assert.assertEquals(true, Boolean.parseBoolean(iniMap.get("profile6").get("enable")));
73-
Assert.assertEquals(false, ini.get("profile7").get("enable", Boolean.class));
74-
Assert.assertEquals(false, Boolean.parseBoolean(iniMap.get("profile7").get("enable")));
55+
Assert.assertEquals("false", iniMap.get("profile1").get("enable"));
56+
Assert.assertEquals("true", iniMap.get("profile2").get("enable"));
57+
Assert.assertEquals("null", iniMap.get("profile3").get("enable"));
58+
Assert.assertEquals("1", iniMap.get("profile4").get("enable"));
59+
Assert.assertEquals("False", iniMap.get("profile5").get("enable"));
60+
Assert.assertEquals("True", iniMap.get("profile6").get("enable"));
61+
Assert.assertEquals("", iniMap.get("profile7").get("enable"));
7562

7663
context = "[invalid\n"
7764
+ "enable = false\n";
78-
try {
79-
new Wini(new StringReader(context));
80-
Assert.fail();
81-
} catch (Exception e) {
82-
Assert.assertTrue(e.getMessage().contains("parse error"));
83-
}
8465
try {
8566
ProfileUtils.parseFile(new StringReader(context));
8667
Assert.fail();
@@ -92,65 +73,40 @@ public void testProfile() throws IOException {
9273
+ "enable = false\n"
9374
+ "str = \n"
9475
+ "\ttest\n";
95-
;
96-
try {
97-
new Wini(new StringReader(context));
98-
Assert.fail();
99-
} catch (Exception e) {
100-
Assert.assertTrue(e.getMessage().contains("parse error"));
101-
}
10276
iniMap = ProfileUtils.parseFile(new StringReader(context));
10377
Assert.assertEquals(0, iniMap.size());
10478

10579
context = "[profile1]\n"
10680
+ "[profile2]\n";
107-
ini = new Wini(new StringReader(context));
10881
iniMap = ProfileUtils.parseFile(new StringReader(context));
109-
Assert.assertEquals(2, ini.size());
11082
Assert.assertEquals(2, iniMap.size());
111-
Assert.assertEquals(ini.get("profile1").size(), iniMap.get("profile1").size());
112-
Assert.assertEquals(ini.get("profile2").size(), iniMap.get("profile2").size());
83+
Assert.assertEquals(0, iniMap.get("profile1").size());
84+
Assert.assertEquals(0, iniMap.get("profile2").size());
11385

11486
context = "enable=true\n"
11587
+ "key=value\n"
11688
+ "str = \n"
11789
+ "\ttest\n";
118-
ini = new Wini(new StringReader(context));
11990
iniMap = ProfileUtils.parseFile(new StringReader(context));
120-
Assert.assertEquals(1, ini.size());
12191
Assert.assertEquals(1, iniMap.size());
122-
Assert.assertEquals(4, ini.get("?").size());
12392
Assert.assertEquals(3, iniMap.get("?").size());
124-
Assert.assertEquals("true", ini.get("?").get("enable"));
12593
Assert.assertEquals("true", iniMap.get("?").get("enable"));
126-
Assert.assertEquals("value", ini.get("?").get("key"));
12794
Assert.assertEquals("value", iniMap.get("?").get("key"));
95+
Assert.assertEquals("\ntest", iniMap.get("?").get("str"));
12896

12997
context = "\ttest\n";
130-
ini = new Wini(new StringReader(context));
13198
iniMap = ProfileUtils.parseFile(new StringReader(context));
132-
Assert.assertEquals(1, ini.size());
13399
Assert.assertEquals(1, iniMap.size());
134-
Assert.assertEquals(1, ini.get("?").size());
135100
Assert.assertEquals(1, iniMap.get("?").size());
136101

137102
context = "[profile1]\n"
138103
+ "key \n";
139-
ini = new Wini(new StringReader(context));
140104
iniMap = ProfileUtils.parseFile(new StringReader(context));
141-
Assert.assertEquals(1, ini.size());
142105
Assert.assertEquals(1, iniMap.size());
143-
Assert.assertNull(ini.get("profile1").get("key"));
144106
Assert.assertNull(iniMap.get("profile1").get("key"));
145107

146108
context = "[profile1]\n"
147109
+ " = value\n";
148-
try {
149-
new Wini(new StringReader(context));
150-
Assert.fail();
151-
} catch (Exception e) {
152-
Assert.assertTrue(e.getMessage().contains("parse error"));
153-
}
154110
try {
155111
ProfileUtils.parseFile(new StringReader(context));
156112
Assert.fail();
@@ -159,4 +115,93 @@ public void testProfile() throws IOException {
159115
}
160116
}
161117

118+
@Test
119+
public void testParseFileFromPath() throws IOException {
120+
String resourcePath = "src/test/resources/configTest.ini";
121+
122+
Map<String, Map<String, String>> iniMap = ProfileUtils.parseFile(resourcePath);
123+
124+
Assert.assertTrue("Should contain client profile", iniMap.containsKey("client"));
125+
Assert.assertTrue("Should contain default profile", iniMap.containsKey("default"));
126+
Assert.assertTrue("Should contain client1 profile", iniMap.containsKey("client1"));
127+
Assert.assertTrue("Should contain client2 profile", iniMap.containsKey("client2"));
128+
Assert.assertTrue("Should contain client3 profile", iniMap.containsKey("client3"));
129+
Assert.assertTrue("Should contain client4 profile", iniMap.containsKey("client4"));
130+
131+
Assert.assertEquals("access_key", iniMap.get("default").get("type"));
132+
Assert.assertEquals("foo", iniMap.get("default").get("access_key_id"));
133+
Assert.assertEquals("bar", iniMap.get("default").get("access_key_secret"));
134+
Assert.assertEquals("cn-hangzhou", iniMap.get("default").get("region_id"));
135+
136+
Assert.assertEquals("ecs_ram_role", iniMap.get("client1").get("type"));
137+
Assert.assertEquals("EcsRamRoleTest", iniMap.get("client1").get("role_name"));
138+
139+
Assert.assertEquals("bearer_token", iniMap.get("client3").get("type"));
140+
Assert.assertEquals("bearer_token", iniMap.get("client3").get("bearer_token"));
141+
}
142+
143+
@Test
144+
public void testReaderCloseException() throws IOException {
145+
String context = "[profile1]\n"
146+
+ "key = value\n";
147+
148+
// 使用一个会在关闭时抛出异常的 Reader
149+
StringReader reader = new StringReader(context) {
150+
@Override
151+
public void close() {
152+
super.close();
153+
// 这里不抛出异常,因为 StringReader.close() 不会抛出 IOException
154+
// 但这个测试确保了 finally 块中的 close() 调用被执行
155+
}
156+
};
157+
158+
Map<String, Map<String, String>> iniMap = ProfileUtils.parseFile(reader);
159+
Assert.assertEquals(1, iniMap.size());
160+
Assert.assertEquals("value", iniMap.get("profile1").get("key"));
161+
}
162+
163+
@Test
164+
public void testPropertyContinuationWithoutCurrentProperty() throws IOException {
165+
String context = "\ttest_continuation\n";
166+
167+
Map<String, Map<String, String>> iniMap = ProfileUtils.parseFile(new StringReader(context));
168+
Assert.assertEquals(1, iniMap.size());
169+
Assert.assertEquals(1, iniMap.get("?").size());
170+
// 当没有当前属性时,currentPropertyBeingRead 为 null
171+
// currentPropertyValue 也为 null,所以结果是 "null\ntest_continuation"
172+
Assert.assertEquals("null\ntest_continuation", iniMap.get("?").get(null));
173+
}
174+
175+
@Test
176+
public void testRemoveTrailingCommentsWithNoMatch() throws IOException {
177+
String context = "[profile1]\n"
178+
+ "key = value_without_comment\n";
179+
180+
Map<String, Map<String, String>> iniMap = ProfileUtils.parseFile(new StringReader(context));
181+
Assert.assertEquals(1, iniMap.size());
182+
Assert.assertEquals("value_without_comment", iniMap.get("profile1").get("key"));
183+
}
184+
185+
@Test
186+
public void testPropertyDefinitionWithoutCurrentProfile() throws IOException {
187+
String context = "global_key = global_value\n"
188+
+ "[profile1]\n"
189+
+ "key = value\n";
190+
191+
Map<String, Map<String, String>> iniMap = ProfileUtils.parseFile(new StringReader(context));
192+
Assert.assertEquals(2, iniMap.size());
193+
Assert.assertEquals("global_value", iniMap.get("?").get("global_key"));
194+
Assert.assertEquals("value", iniMap.get("profile1").get("key"));
195+
}
196+
197+
@Test
198+
public void testPropertyContinuationWithoutCurrentProfile() throws IOException {
199+
String context = "key = value\n"
200+
+ "\tcontinuation\n";
201+
202+
Map<String, Map<String, String>> iniMap = ProfileUtils.parseFile(new StringReader(context));
203+
Assert.assertEquals(1, iniMap.size());
204+
Assert.assertEquals("value\ncontinuation", iniMap.get("?").get("key"));
205+
}
206+
162207
}

0 commit comments

Comments
 (0)