Skip to content

Commit

Permalink
Release resource after use in ConfigParserTest (apache#2905)
Browse files Browse the repository at this point in the history
  • Loading branch information
biyuhao authored and beiwei30 committed Dec 10, 2018
1 parent d4d827e commit 368a59e
Showing 1 changed file with 96 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -47,121 +48,130 @@ private String streamToString(InputStream stream) {
}

@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), "serviceKey");
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), "serviceKey");
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), "testgroup/servicekey:1.0.0");
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), "testgroup/servicekey:1.0.0");
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), "serviceKey");
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), "serviceKey");
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), "serviceKey");
Assert.fail();
public void parseConfiguratorsServiceNoRuleTest() throws IOException {
try (InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoRule.yml")) {
ConfigParser.parseConfigurators(streamToString(yamlStream), "serviceKey");
Assert.fail();
}
}

@Test
public void parseConfiguratorsAppMultiServicesTest() {
InputStream yamlStream = this.getClass().getResourceAsStream("/AppMultiServices.yml");
String yamlFile = streamToString(yamlStream);
List<URL> urls = ConfigParser.parseConfigurators(yamlFile, "service1");
Assert.assertNotNull(urls);
Assert.assertEquals(2, 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");

List<URL> urls2 = ConfigParser.parseConfigurators(yamlFile, "service-not-exist");
Assert.assertNotNull(urls2);
Assert.assertEquals(0, urls2.size());
public void parseConfiguratorsAppMultiServicesTest() throws IOException {
try (InputStream yamlStream = this.getClass().getResourceAsStream("/AppMultiServices.yml")) {
String yamlFile = streamToString(yamlStream);
List<URL> urls = ConfigParser.parseConfigurators(yamlFile, "service1");
Assert.assertNotNull(urls);
Assert.assertEquals(2, 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");

List<URL> urls2 = ConfigParser.parseConfigurators(yamlFile, "service-not-exist");
Assert.assertNotNull(urls2);
Assert.assertEquals(0, urls2.size());
}
}


@Test
public void parseConfiguratorsAppAnyServicesTest() {
InputStream yamlStream = this.getClass().getResourceAsStream("/AppAnyServices.yml");
List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream), "service1");
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), "service1");
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), "service1");
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), "service1");
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), "service1");
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), "service1");
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");
}
}

}

0 comments on commit 368a59e

Please sign in to comment.