Skip to content

Commit e0f0e63

Browse files
committed
Revert "Release resource after use in ConfigParserTest (apache#3127)"
This reverts commit 2147d90.
1 parent 5fb6039 commit e0f0e63

File tree

1 file changed

+91
-96
lines changed

1 file changed

+91
-96
lines changed

dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/configurator/parser/ConfigParserTest.java

Lines changed: 91 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.yaml.snakeyaml.Yaml;
2828
import org.yaml.snakeyaml.constructor.Constructor;
2929

30-
import java.io.IOException;
3130
import java.io.InputStream;
3231
import java.util.List;
3332

@@ -36,133 +35,129 @@
3635
*/
3736
public class ConfigParserTest {
3837

39-
private String streamToString(InputStream stream) throws IOException {
40-
byte[] bytes = new byte[stream.available()];
41-
stream.read(bytes);
42-
return new String(bytes);
38+
private String streamToString(InputStream stream) {
39+
try {
40+
byte[] bytes = new byte[stream.available()];
41+
stream.read(bytes);
42+
return new String(bytes);
43+
} catch (Exception e) {
44+
e.printStackTrace();
45+
}
46+
return null;
4347
}
4448

4549
@Test
46-
public void snakeYamlBasicTest() throws IOException {
47-
try(InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoApp.yml")) {
50+
public void snakeYamlBasicTest() {
51+
InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoApp.yml");
4852

49-
Constructor constructor = new Constructor(ConfiguratorConfig.class);
50-
TypeDescription carDescription = new TypeDescription(ConfiguratorConfig.class);
51-
carDescription.addPropertyParameters("items", ConfigItem.class);
52-
constructor.addTypeDescription(carDescription);
53+
Constructor constructor = new Constructor(ConfiguratorConfig.class);
54+
TypeDescription carDescription = new TypeDescription(ConfiguratorConfig.class);
55+
carDescription.addPropertyParameters("items", ConfigItem.class);
56+
constructor.addTypeDescription(carDescription);
5357

54-
Yaml yaml = new Yaml(constructor);
55-
ConfiguratorConfig config = yaml.load(yamlStream);
56-
System.out.println(config);
57-
}
58+
Yaml yaml = new Yaml(constructor);
59+
ConfiguratorConfig config = yaml.load(yamlStream);
60+
System.out.println(config);
5861
}
5962

6063
@Test
6164
public void parseConfiguratorsServiceNoAppTest() throws Exception {
62-
try(InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoApp.yml")) {
63-
List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
64-
Assert.assertNotNull(urls);
65-
Assert.assertEquals(2, urls.size());
66-
URL url = urls.get(0);
67-
Assert.assertEquals(url.getAddress(), "127.0.0.1:20880");
68-
Assert.assertEquals(url.getParameter(Constants.WEIGHT_KEY, 0), 222);
69-
}
65+
InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoApp.yml");
66+
List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
67+
Assert.assertNotNull(urls);
68+
Assert.assertEquals(2, urls.size());
69+
URL url = urls.get(0);
70+
Assert.assertEquals(url.getAddress(), "127.0.0.1:20880");
71+
Assert.assertEquals(url.getParameter(Constants.WEIGHT_KEY, 0), 222);
7072
}
7173

7274
@Test
7375
public void parseConfiguratorsServiceGroupVersionTest() throws Exception {
74-
try(InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceGroupVersion.yml")) {
75-
List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
76-
Assert.assertNotNull(urls);
77-
Assert.assertEquals(1, urls.size());
78-
URL url = urls.get(0);
79-
Assert.assertEquals("testgroup", url.getParameter(Constants.GROUP_KEY));
80-
Assert.assertEquals("1.0.0", url.getParameter(Constants.VERSION_KEY));
81-
}
76+
InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceGroupVersion.yml");
77+
List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
78+
Assert.assertNotNull(urls);
79+
Assert.assertEquals(1, urls.size());
80+
URL url = urls.get(0);
81+
Assert.assertEquals("testgroup", url.getParameter(Constants.GROUP_KEY));
82+
Assert.assertEquals("1.0.0", url.getParameter(Constants.VERSION_KEY));
8283
}
8384

8485
@Test
85-
public void parseConfiguratorsServiceMultiAppsTest() throws IOException {
86-
try(InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceMultiApps.yml")) {
87-
List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
88-
Assert.assertNotNull(urls);
89-
Assert.assertEquals(4, urls.size());
90-
URL url = urls.get(0);
91-
Assert.assertEquals("127.0.0.1", url.getAddress());
92-
Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0));
93-
Assert.assertNotNull(url.getParameter(Constants.APPLICATION_KEY));
94-
}
86+
public void parseConfiguratorsServiceMultiAppsTest() {
87+
InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceMultiApps.yml");
88+
List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
89+
Assert.assertNotNull(urls);
90+
Assert.assertEquals(4, urls.size());
91+
URL url = urls.get(0);
92+
Assert.assertEquals("127.0.0.1", url.getAddress());
93+
Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0));
94+
Assert.assertNotNull(url.getParameter(Constants.APPLICATION_KEY));
9595
}
9696

9797
@Test(expected = IllegalStateException.class)
98-
public void parseConfiguratorsServiceNoRuleTest() throws IOException {
99-
try(InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoRule.yml")) {
100-
ConfigParser.parseConfigurators(streamToString(yamlStream));
101-
Assert.fail();
102-
}
98+
public void parseConfiguratorsServiceNoRuleTest() {
99+
InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoRule.yml");
100+
ConfigParser.parseConfigurators(streamToString(yamlStream));
101+
Assert.fail();
103102
}
104103

105104
@Test
106-
public void parseConfiguratorsAppMultiServicesTest() throws IOException {
107-
try(InputStream yamlStream = this.getClass().getResourceAsStream("/AppMultiServices.yml")) {
108-
String yamlFile = streamToString(yamlStream);
109-
List<URL> urls = ConfigParser.parseConfigurators(yamlFile);
110-
Assert.assertNotNull(urls);
111-
Assert.assertEquals(4, urls.size());
112-
URL url = urls.get(0);
113-
Assert.assertEquals("127.0.0.1", url.getAddress());
114-
Assert.assertEquals("service1", url.getServiceInterface());
115-
Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0));
116-
Assert.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY));
117-
Assert.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer");
118-
}
105+
public void parseConfiguratorsAppMultiServicesTest() {
106+
InputStream yamlStream = this.getClass().getResourceAsStream("/AppMultiServices.yml");
107+
String yamlFile = streamToString(yamlStream);
108+
List<URL> urls = ConfigParser.parseConfigurators(yamlFile);
109+
Assert.assertNotNull(urls);
110+
Assert.assertEquals(4, urls.size());
111+
URL url = urls.get(0);
112+
Assert.assertEquals("127.0.0.1", url.getAddress());
113+
Assert.assertEquals("service1", url.getServiceInterface());
114+
Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0));
115+
Assert.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY));
116+
Assert.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer");
119117
}
120118

121119

122120
@Test
123-
public void parseConfiguratorsAppAnyServicesTest() throws IOException {
124-
try(InputStream yamlStream = this.getClass().getResourceAsStream("/AppAnyServices.yml")) {
125-
List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
126-
Assert.assertNotNull(urls);
127-
Assert.assertEquals(2, urls.size());
128-
URL url = urls.get(0);
129-
Assert.assertEquals("127.0.0.1", url.getAddress());
130-
Assert.assertEquals("*", url.getServiceInterface());
131-
Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0));
132-
Assert.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY));
133-
Assert.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer");
134-
}
121+
public void parseConfiguratorsAppAnyServicesTest() {
122+
InputStream yamlStream = this.getClass().getResourceAsStream("/AppAnyServices.yml");
123+
List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
124+
Assert.assertNotNull(urls);
125+
Assert.assertEquals(2, urls.size());
126+
URL url = urls.get(0);
127+
Assert.assertEquals("127.0.0.1", url.getAddress());
128+
Assert.assertEquals("*", url.getServiceInterface());
129+
Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0));
130+
Assert.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY));
131+
Assert.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer");
135132
}
136133

137134
@Test
138-
public void parseConfiguratorsAppNoServiceTest() throws IOException {
139-
try(InputStream yamlStream = this.getClass().getResourceAsStream("/AppNoService.yml")) {
140-
List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
141-
Assert.assertNotNull(urls);
142-
Assert.assertEquals(1, urls.size());
143-
URL url = urls.get(0);
144-
Assert.assertEquals("127.0.0.1", url.getAddress());
145-
Assert.assertEquals("*", url.getServiceInterface());
146-
Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0));
147-
Assert.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY));
148-
Assert.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer");
149-
}
135+
public void parseConfiguratorsAppNoServiceTest() {
136+
InputStream yamlStream = this.getClass().getResourceAsStream("/AppNoService.yml");
137+
List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
138+
Assert.assertNotNull(urls);
139+
Assert.assertEquals(1, urls.size());
140+
URL url = urls.get(0);
141+
Assert.assertEquals("127.0.0.1", url.getAddress());
142+
Assert.assertEquals("*", url.getServiceInterface());
143+
Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0));
144+
Assert.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY));
145+
Assert.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer");
150146
}
151147

152148
@Test
153-
public void parseConsumerSpecificProvidersTest() throws IOException {
154-
try(InputStream yamlStream = this.getClass().getResourceAsStream("/ConsumerSpecificProviders.yml")) {
155-
List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
156-
Assert.assertNotNull(urls);
157-
Assert.assertEquals(1, urls.size());
158-
URL url = urls.get(0);
159-
Assert.assertEquals("127.0.0.1", url.getAddress());
160-
Assert.assertEquals("*", url.getServiceInterface());
161-
Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0));
162-
Assert.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY));
163-
Assert.assertEquals("127.0.0.1:20880", url.getParameter(Constants.OVERRIDE_PROVIDERS_KEY));
164-
Assert.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer");
165-
}
149+
public void parseConsumerSpecificProvidersTest() {
150+
InputStream yamlStream = this.getClass().getResourceAsStream("/ConsumerSpecificProviders.yml");
151+
List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
152+
Assert.assertNotNull(urls);
153+
Assert.assertEquals(1, urls.size());
154+
URL url = urls.get(0);
155+
Assert.assertEquals("127.0.0.1", url.getAddress());
156+
Assert.assertEquals("*", url.getServiceInterface());
157+
Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0));
158+
Assert.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY));
159+
Assert.assertEquals("127.0.0.1:20880", url.getParameter(Constants.OVERRIDE_PROVIDERS_KEY));
160+
Assert.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer");
166161
}
167162

168163
}

0 commit comments

Comments
 (0)