Skip to content

Commit

Permalink
Release resource after use in ConfigParserTest (apache#3127)
Browse files Browse the repository at this point in the history
Release resource after use in ConfigParserTest
  • Loading branch information
biyuhao authored and carryxyh committed Jan 3, 2019
1 parent 3bf77ab commit 2147d90
Showing 1 changed file with 96 additions and 91 deletions.
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 @@ -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<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");
}
}

}

0 comments on commit 2147d90

Please sign in to comment.