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 0c365c02ad8..25af5447d3f 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.Yaml; import org.yaml.snakeyaml.constructor.Constructor; +import java.io.IOException; import java.io.InputStream; import java.util.List; @@ -35,129 +36,133 @@ */ 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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"); + } } }