Skip to content

Commit

Permalink
Add Test cases for YAML properties
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed Oct 18, 2018
1 parent fa967c6 commit cb7da3e
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 4 deletions.
16 changes: 13 additions & 3 deletions dependencies-bom/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand Down Expand Up @@ -29,8 +30,8 @@
<url>https://github.com/apache/incubator-dubbo</url>
<connection>scm:git:https://github.com/apache/incubator-dubbo.git</connection>
<developerConnection>scm:git:https://github.com/apache/incubator-dubbo.git</developerConnection>
<tag>HEAD</tag>
</scm>
<tag>HEAD</tag>
</scm>
<mailingLists>
<mailingList>
<name>Development List</name>
Expand Down Expand Up @@ -113,6 +114,7 @@
<activation_version>1.2.0</activation_version>
<hessian_lite_version>3.2.4</hessian_lite_version>
<alibaba_spring_context_support_version>1.0.1</alibaba_spring_context_support_version>
<yaml_version>1.17</yaml_version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -369,6 +371,13 @@
<version>${alibaba_spring_context_support_version}</version>
</dependency>

<!-- YAML -->
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>${yaml_version}</version>
</dependency>

<!-- Test lib -->
<dependency>
<groupId>org.apache.curator</groupId>
Expand All @@ -382,6 +391,7 @@
<version>${embedded_redis_version}</version>
<scope>test</scope>
</dependency>

</dependencies>
</dependencyManagement>

Expand Down
5 changes: 5 additions & 0 deletions dubbo-config/dubbo-config-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@
<artifactId>tomcat-embed-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.config.spring.beans.factory.config;

import org.springframework.beans.factory.config.YamlProcessor;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.representer.Representer;
import org.yaml.snakeyaml.resolver.Resolver;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;

/**
* YAML {@link PropertySourceFactory} implementation, some source code is copied Spring Boot
* org.springframework.boot.env.YamlPropertySourceLoader , see {@link #createYaml()} and {@link #process()}
*
* @since 2.6.5
*/
public class YamlPropertySourceFactory extends YamlProcessor implements PropertySourceFactory {

@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
setResources(resource.getResource());
return new MapPropertySource(name, process());
}

@Override
protected Yaml createYaml() {
return new Yaml(new StrictMapAppenderConstructor(), new Representer(),
new DumperOptions(), new Resolver() {
@Override
public void addImplicitResolver(Tag tag, Pattern regexp,
String first) {
if (tag == Tag.TIMESTAMP) {
return;
}
super.addImplicitResolver(tag, regexp, first);
}
});
}

public Map<String, Object> process() {
final Map<String, Object> result = new LinkedHashMap<String, Object>();
process(new MatchCallback() {
@Override
public void process(Properties properties, Map<String, Object> map) {
result.putAll(getFlattenedMap(map));
}
});
return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.config.spring.beans.factory.config;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

/**
* {@link YamlPropertySourceFactory} Test
*
* @since 2.6.5
*/
@RunWith(SpringRunner.class)
@PropertySource(name = "yaml-source", value = {"classpath:/META-INF/dubbo.yml"}, factory = YamlPropertySourceFactory.class)
@Configuration
@ContextConfiguration(classes = YamlPropertySourceFactoryTest.class)
public class YamlPropertySourceFactoryTest {

@Autowired
private Environment environment;

@Value("${dubbo.consumer.default}")
private Boolean isDefault;

@Value("${dubbo.consumer.client}")
private String client;

@Value("${dubbo.consumer.threadpool}")
private String threadPool;

@Value("${dubbo.consumer.corethreads}")
private Integer coreThreads;

@Value("${dubbo.consumer.threads}")
private Integer threads;

@Value("${dubbo.consumer.queues}")
private Integer queues;

@Test
public void testProperty() {
Assert.assertEquals(isDefault, environment.getProperty("dubbo.consumer.default", Boolean.class));
Assert.assertEquals(client, environment.getProperty("dubbo.consumer.client", String.class));
Assert.assertEquals(threadPool, environment.getProperty("dubbo.consumer.threadpool", String.class));
Assert.assertEquals(coreThreads, environment.getProperty("dubbo.consumer.corethreads", Integer.class));
Assert.assertEquals(threads, environment.getProperty("dubbo.consumer.threads", Integer.class));
Assert.assertEquals(queues, environment.getProperty("dubbo.consumer.queues", Integer.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,50 @@


import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.beans.factory.config.YamlPropertySourceFactory;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@TestPropertySource(locations = "classpath:/dubbo.properties")
@ContextConfiguration(classes = DefaultDubboConfigBinder.class)
@PropertySource(name = "yaml-source", value = {"classpath:/META-INF/dubbo.yml"}, factory = YamlPropertySourceFactory.class)
@Configuration
@ContextConfiguration(classes = {DefaultDubboConfigBinder.class, DefaultDubboConfigBinderTest.class})
public class DefaultDubboConfigBinderTest {

@Autowired
private DubboConfigBinder dubboConfigBinder;

@Value("${dubbo.consumer.default}")
private Boolean isDefault;

@Value("${dubbo.consumer.client}")
private String client;

@Value("${dubbo.consumer.threadpool}")
private String threadPool;

@Value("${dubbo.consumer.corethreads}")
private Integer coreThreads;

@Value("${dubbo.consumer.threads}")
private Integer threads;

@Value("${dubbo.consumer.queues}")
private Integer queues;

@Test
public void testBinder() {

Expand All @@ -52,5 +78,15 @@ public void testBinder() {
dubboConfigBinder.bind("dubbo.protocol", protocolConfig);
Assert.assertEquals(Integer.valueOf(20881), protocolConfig.getPort());

ConsumerConfig consumerConfig = new ConsumerConfig();
dubboConfigBinder.bind("dubbo.consumer", consumerConfig);

Assert.assertEquals(isDefault, consumerConfig.isDefault());
Assert.assertEquals(client, consumerConfig.getClient());
Assert.assertEquals(threadPool, consumerConfig.getThreadpool());
Assert.assertEquals(coreThreads, consumerConfig.getCorethreads());
Assert.assertEquals(threads, consumerConfig.getThreads());
Assert.assertEquals(queues, consumerConfig.getQueues());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
dubbo:
consumer:
default: false
client: netty
threadpool: cached
corethreads: 1
threads: 10
queues: 99

0 comments on commit cb7da3e

Please sign in to comment.