You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by ca...@apache.org on 2019/01/03 09:42:17 UTC

[incubator-dubbo] branch master updated: Release resource after use in ConfigParserTest (#3127)

This is an automated email from the ASF dual-hosted git repository.

carryxyh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 2147d90  Release resource after use in ConfigParserTest (#3127)
2147d90 is described below

commit 2147d90679bdaf56f515bc8bae08fcc301771022
Author: Yuhao Bi <by...@gmail.com>
AuthorDate: Thu Jan 3 17:42:11 2019 +0800

    Release resource after use in ConfigParserTest (#3127)
    
    Release resource after use in ConfigParserTest
---
 .../configurator/parser/ConfigParserTest.java      | 187 +++++++++++----------
 1 file changed, 96 insertions(+), 91 deletions(-)

diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/configurator/parser/ConfigParserTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/configurator/parser/ConfigParserTest.java
index 0c365c0..25af544 100644
--- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/configurator/parser/ConfigParserTest.java
+++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/configurator/parser/ConfigParserTest.java
@@ -27,6 +27,7 @@ import org.yaml.snakeyaml.TypeDescription;
 import org.yaml.snakeyaml.Yaml;
 import org.yaml.snakeyaml.constructor.Constructor;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.List;
 
@@ -35,129 +36,133 @@ import java.util.List;
  */
 public class ConfigParserTest {
 
-    private String streamToString(InputStream stream) {
-        try {
-            byte[] bytes = new byte[stream.available()];
-            stream.read(bytes);
-            return new String(bytes);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        return null;
+    private String streamToString(InputStream stream) throws IOException {
+        byte[] bytes = new byte[stream.available()];
+        stream.read(bytes);
+        return new String(bytes);
     }
 
     @Test
-    public void snakeYamlBasicTest() {
-        InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoApp.yml");
+    public void snakeYamlBasicTest() throws IOException {
+        try(InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoApp.yml")) {
 
-        Constructor constructor = new Constructor(ConfiguratorConfig.class);
-        TypeDescription carDescription = new TypeDescription(ConfiguratorConfig.class);
-        carDescription.addPropertyParameters("items", ConfigItem.class);
-        constructor.addTypeDescription(carDescription);
+            Constructor constructor = new Constructor(ConfiguratorConfig.class);
+            TypeDescription carDescription = new TypeDescription(ConfiguratorConfig.class);
+            carDescription.addPropertyParameters("items", ConfigItem.class);
+            constructor.addTypeDescription(carDescription);
 
-        Yaml yaml = new Yaml(constructor);
-        ConfiguratorConfig config = yaml.load(yamlStream);
-        System.out.println(config);
+            Yaml yaml = new Yaml(constructor);
+            ConfiguratorConfig config = yaml.load(yamlStream);
+            System.out.println(config);
+        }
     }
 
     @Test
     public void parseConfiguratorsServiceNoAppTest() throws Exception {
-        InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoApp.yml");
-        List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
-        Assert.assertNotNull(urls);
-        Assert.assertEquals(2, urls.size());
-        URL url = urls.get(0);
-        Assert.assertEquals(url.getAddress(), "127.0.0.1:20880");
-        Assert.assertEquals(url.getParameter(Constants.WEIGHT_KEY, 0), 222);
+        try(InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoApp.yml")) {
+            List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
+            Assert.assertNotNull(urls);
+            Assert.assertEquals(2, urls.size());
+            URL url = urls.get(0);
+            Assert.assertEquals(url.getAddress(), "127.0.0.1:20880");
+            Assert.assertEquals(url.getParameter(Constants.WEIGHT_KEY, 0), 222);
+        }
     }
 
     @Test
     public void parseConfiguratorsServiceGroupVersionTest() throws Exception {
-        InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceGroupVersion.yml");
-        List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
-        Assert.assertNotNull(urls);
-        Assert.assertEquals(1, urls.size());
-        URL url = urls.get(0);
-        Assert.assertEquals("testgroup", url.getParameter(Constants.GROUP_KEY));
-        Assert.assertEquals("1.0.0", url.getParameter(Constants.VERSION_KEY));
+        try(InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceGroupVersion.yml")) {
+            List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
+            Assert.assertNotNull(urls);
+            Assert.assertEquals(1, urls.size());
+            URL url = urls.get(0);
+            Assert.assertEquals("testgroup", url.getParameter(Constants.GROUP_KEY));
+            Assert.assertEquals("1.0.0", url.getParameter(Constants.VERSION_KEY));
+        }
     }
 
     @Test
-    public void parseConfiguratorsServiceMultiAppsTest() {
-        InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceMultiApps.yml");
-        List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
-        Assert.assertNotNull(urls);
-        Assert.assertEquals(4, urls.size());
-        URL url = urls.get(0);
-        Assert.assertEquals("127.0.0.1", url.getAddress());
-        Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0));
-        Assert.assertNotNull(url.getParameter(Constants.APPLICATION_KEY));
+    public void parseConfiguratorsServiceMultiAppsTest() throws IOException {
+        try(InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceMultiApps.yml")) {
+            List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
+            Assert.assertNotNull(urls);
+            Assert.assertEquals(4, urls.size());
+            URL url = urls.get(0);
+            Assert.assertEquals("127.0.0.1", url.getAddress());
+            Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0));
+            Assert.assertNotNull(url.getParameter(Constants.APPLICATION_KEY));
+        }
     }
 
     @Test(expected = IllegalStateException.class)
-    public void parseConfiguratorsServiceNoRuleTest() {
-        InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoRule.yml");
-        ConfigParser.parseConfigurators(streamToString(yamlStream));
-        Assert.fail();
+    public void parseConfiguratorsServiceNoRuleTest() throws IOException {
+        try(InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoRule.yml")) {
+            ConfigParser.parseConfigurators(streamToString(yamlStream));
+            Assert.fail();
+        }
     }
 
     @Test
-    public void parseConfiguratorsAppMultiServicesTest() {
-        InputStream yamlStream = this.getClass().getResourceAsStream("/AppMultiServices.yml");
-        String yamlFile = streamToString(yamlStream);
-        List<URL> urls = ConfigParser.parseConfigurators(yamlFile);
-        Assert.assertNotNull(urls);
-        Assert.assertEquals(4, urls.size());
-        URL url = urls.get(0);
-        Assert.assertEquals("127.0.0.1", url.getAddress());
-        Assert.assertEquals("service1", url.getServiceInterface());
-        Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0));
-        Assert.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY));
-        Assert.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer");
+    public void parseConfiguratorsAppMultiServicesTest() throws IOException {
+        try(InputStream yamlStream = this.getClass().getResourceAsStream("/AppMultiServices.yml")) {
+            String yamlFile = streamToString(yamlStream);
+            List<URL> urls = ConfigParser.parseConfigurators(yamlFile);
+            Assert.assertNotNull(urls);
+            Assert.assertEquals(4, urls.size());
+            URL url = urls.get(0);
+            Assert.assertEquals("127.0.0.1", url.getAddress());
+            Assert.assertEquals("service1", url.getServiceInterface());
+            Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0));
+            Assert.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY));
+            Assert.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer");
+        }
     }
 
 
     @Test
-    public void parseConfiguratorsAppAnyServicesTest() {
-        InputStream yamlStream = this.getClass().getResourceAsStream("/AppAnyServices.yml");
-        List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
-        Assert.assertNotNull(urls);
-        Assert.assertEquals(2, urls.size());
-        URL url = urls.get(0);
-        Assert.assertEquals("127.0.0.1", url.getAddress());
-        Assert.assertEquals("*", url.getServiceInterface());
-        Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0));
-        Assert.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY));
-        Assert.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer");
+    public void parseConfiguratorsAppAnyServicesTest() throws IOException {
+        try(InputStream yamlStream = this.getClass().getResourceAsStream("/AppAnyServices.yml")) {
+            List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
+            Assert.assertNotNull(urls);
+            Assert.assertEquals(2, urls.size());
+            URL url = urls.get(0);
+            Assert.assertEquals("127.0.0.1", url.getAddress());
+            Assert.assertEquals("*", url.getServiceInterface());
+            Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0));
+            Assert.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY));
+            Assert.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer");
+        }
     }
 
     @Test
-    public void parseConfiguratorsAppNoServiceTest() {
-        InputStream yamlStream = this.getClass().getResourceAsStream("/AppNoService.yml");
-        List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
-        Assert.assertNotNull(urls);
-        Assert.assertEquals(1, urls.size());
-        URL url = urls.get(0);
-        Assert.assertEquals("127.0.0.1", url.getAddress());
-        Assert.assertEquals("*", url.getServiceInterface());
-        Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0));
-        Assert.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY));
-        Assert.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer");
+    public void parseConfiguratorsAppNoServiceTest() throws IOException {
+        try(InputStream yamlStream = this.getClass().getResourceAsStream("/AppNoService.yml")) {
+            List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
+            Assert.assertNotNull(urls);
+            Assert.assertEquals(1, urls.size());
+            URL url = urls.get(0);
+            Assert.assertEquals("127.0.0.1", url.getAddress());
+            Assert.assertEquals("*", url.getServiceInterface());
+            Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0));
+            Assert.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY));
+            Assert.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer");
+        }
     }
 
     @Test
-    public void parseConsumerSpecificProvidersTest() {
-        InputStream yamlStream = this.getClass().getResourceAsStream("/ConsumerSpecificProviders.yml");
-        List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
-        Assert.assertNotNull(urls);
-        Assert.assertEquals(1, urls.size());
-        URL url = urls.get(0);
-        Assert.assertEquals("127.0.0.1", url.getAddress());
-        Assert.assertEquals("*", url.getServiceInterface());
-        Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0));
-        Assert.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY));
-        Assert.assertEquals("127.0.0.1:20880", url.getParameter(Constants.OVERRIDE_PROVIDERS_KEY));
-        Assert.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer");
+    public void parseConsumerSpecificProvidersTest() throws IOException {
+        try(InputStream yamlStream = this.getClass().getResourceAsStream("/ConsumerSpecificProviders.yml")) {
+            List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
+            Assert.assertNotNull(urls);
+            Assert.assertEquals(1, urls.size());
+            URL url = urls.get(0);
+            Assert.assertEquals("127.0.0.1", url.getAddress());
+            Assert.assertEquals("*", url.getServiceInterface());
+            Assert.assertEquals(6666, url.getParameter(Constants.TIMEOUT_KEY, 0));
+            Assert.assertEquals("random", url.getParameter(Constants.LOADBALANCE_KEY));
+            Assert.assertEquals("127.0.0.1:20880", url.getParameter(Constants.OVERRIDE_PROVIDERS_KEY));
+            Assert.assertEquals(url.getParameter(Constants.APPLICATION_KEY), "demo-consumer");
+        }
     }
 
 }