Skip to content

Commit b7dde72

Browse files
htynknlovepoem
authored andcommitted
[Dubbo-1684] add unit test for dubbo spring config (#1809)
* add test for config spring module * add more test for serviceBean and refBean
1 parent 301bc34 commit b7dde72

File tree

14 files changed

+606
-6
lines changed

14 files changed

+606
-6
lines changed

.codecov.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ coverage:
66
threshold: 0.1%
77
ignore:
88
- "dubbo-demo/.*"
9-
- "dubbo-common/src/main/java/com/alibaba/dubbo/common/json/*.java" # internal JSON impl is deprecate, ignore test coverage for them
9+
- "dubbo-common/src/main/java/com/alibaba/dubbo/common/json/*.java" # internal JSON impl is deprecate, ignore test coverage for them
10+
- "dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/AnnotationBean.java" # Deprecated

dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/ConfigTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import com.alibaba.dubbo.rpc.RpcException;
4747
import com.alibaba.dubbo.rpc.service.GenericException;
4848
import com.alibaba.dubbo.rpc.service.GenericService;
49-
import junit.framework.Assert;
49+
import org.junit.Assert;
5050
import org.junit.Ignore;
5151
import org.junit.Test;
5252
import org.springframework.beans.factory.BeanCreationException;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.config.spring;
18+
19+
import com.alibaba.dubbo.config.annotation.Service;
20+
import org.junit.Assert;
21+
import org.junit.Test;
22+
23+
import static org.hamcrest.CoreMatchers.not;
24+
import static org.hamcrest.CoreMatchers.nullValue;
25+
import static org.mockito.Mockito.mock;
26+
27+
public class ServiceBeanTest {
28+
@Test
29+
public void testGetService() {
30+
TestService service = mock(TestService.class);
31+
ServiceBean serviceBean = new ServiceBean(service);
32+
33+
Service beanService = serviceBean.getService();
34+
Assert.assertThat(beanService, not(nullValue()));
35+
}
36+
37+
abstract class TestService implements Service {
38+
39+
}
40+
}

dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessorTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
import java.util.Map;
3737

3838
import static com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.BEAN_NAME;
39+
import static org.hamcrest.CoreMatchers.is;
40+
import static org.hamcrest.CoreMatchers.not;
41+
import static org.hamcrest.CoreMatchers.nullValue;
42+
import static org.hamcrest.MatcherAssert.assertThat;
3943

4044
/**
4145
* {@link ReferenceAnnotationBeanPostProcessor} Test
@@ -174,6 +178,25 @@ public void testGetInjectedMethodReferenceBeanMap() {
174178

175179
}
176180

181+
@Test
182+
public void testModuleInfo() {
183+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestBean.class);
184+
185+
ReferenceAnnotationBeanPostProcessor beanPostProcessor = context.getBean(BEAN_NAME,
186+
ReferenceAnnotationBeanPostProcessor.class);
187+
188+
189+
Map<InjectionMetadata.InjectedElement, ReferenceBean<?>> referenceBeanMap =
190+
beanPostProcessor.getInjectedMethodReferenceBeanMap();
191+
192+
for (Map.Entry<InjectionMetadata.InjectedElement, ReferenceBean<?>> entry : referenceBeanMap.entrySet()) {
193+
ReferenceBean<?> referenceBean = entry.getValue();
194+
195+
assertThat(referenceBean.getModule().getName(),is("defaultModule"));
196+
assertThat(referenceBean.getMonitor(), not(nullValue()));
197+
}
198+
}
199+
177200
private static class AncestorBean {
178201

179202

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.config.spring.schema;
18+
19+
import com.alibaba.dubbo.config.ApplicationConfig;
20+
import com.alibaba.dubbo.config.ModuleConfig;
21+
import com.alibaba.dubbo.config.MonitorConfig;
22+
import com.alibaba.dubbo.config.ProtocolConfig;
23+
import com.alibaba.dubbo.config.ProviderConfig;
24+
import com.alibaba.dubbo.config.spring.ConfigTest;
25+
import com.alibaba.dubbo.config.spring.ServiceBean;
26+
import com.alibaba.dubbo.config.spring.api.DemoService;
27+
import com.alibaba.dubbo.config.spring.impl.DemoServiceImpl;
28+
import org.junit.Test;
29+
import org.springframework.beans.factory.BeanCreationException;
30+
import org.springframework.context.support.ClassPathXmlApplicationContext;
31+
32+
import java.util.Map;
33+
34+
import static org.hamcrest.CoreMatchers.is;
35+
import static org.hamcrest.CoreMatchers.not;
36+
import static org.hamcrest.CoreMatchers.nullValue;
37+
import static org.junit.Assert.assertThat;
38+
39+
public class DubboNamespaceHandlerTest {
40+
@Test
41+
public void testProviderXml() {
42+
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/demo-provider.xml");
43+
ctx.start();
44+
45+
ProtocolConfig protocolConfig = ctx.getBean(ProtocolConfig.class);
46+
assertThat(protocolConfig, not(nullValue()));
47+
assertThat(protocolConfig.getName(), is("dubbo"));
48+
assertThat(protocolConfig.getPort(), is(20813));
49+
50+
ApplicationConfig applicationConfig = ctx.getBean(ApplicationConfig.class);
51+
assertThat(applicationConfig, not(nullValue()));
52+
assertThat(applicationConfig.getName(), is("demo-provider"));
53+
54+
DemoService service = ctx.getBean(DemoService.class);
55+
assertThat(service, not(nullValue()));
56+
}
57+
58+
@Test
59+
public void testMultiProtocol() {
60+
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-protocol.xml");
61+
ctx.start();
62+
63+
Map<String, ProtocolConfig> protocolConfigMap = ctx.getBeansOfType(ProtocolConfig.class);
64+
assertThat(protocolConfigMap.size(), is(2));
65+
66+
ProtocolConfig rmiProtocolConfig = protocolConfigMap.get("rmi");
67+
assertThat(rmiProtocolConfig.getPort(), is(10991));
68+
69+
ProtocolConfig dubboProtocolConfig = protocolConfigMap.get("dubbo");
70+
assertThat(dubboProtocolConfig.getPort(), is(20881));
71+
}
72+
73+
@Test
74+
public void testDefaultProtocol() {
75+
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/override-protocol.xml");
76+
ctx.start();
77+
78+
ProtocolConfig protocolConfig = ctx.getBean(ProtocolConfig.class);
79+
assertThat(protocolConfig.getName(), is("dubbo"));
80+
}
81+
82+
@Test
83+
public void testCustomParameter() {
84+
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/customize-parameter.xml");
85+
ctx.start();
86+
87+
ProtocolConfig protocolConfig = ctx.getBean(ProtocolConfig.class);
88+
assertThat(protocolConfig.getParameters().size(), is(1));
89+
assertThat(protocolConfig.getParameters().get("protocol-paramA"), is("protocol-paramA"));
90+
91+
ServiceBean serviceBean = ctx.getBean(ServiceBean.class);
92+
assertThat(serviceBean.getParameters().size(), is(1));
93+
assertThat(serviceBean.getParameters().get("service-paramA"), is("service-paramA"));
94+
}
95+
96+
97+
@Test
98+
public void testDelayFixedTime() {
99+
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/delay-fixed-time.xml");
100+
ctx.start();
101+
102+
assertThat(ctx.getBean(ServiceBean.class).getDelay(), is(300));
103+
}
104+
105+
@Test
106+
public void testTimeoutConfig() {
107+
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/provider-nested-service.xml");
108+
ctx.start();
109+
110+
Map<String, ProviderConfig> providerConfigMap = ctx.getBeansOfType(ProviderConfig.class);
111+
112+
assertThat(providerConfigMap.get("com.alibaba.dubbo.config.ProviderConfig").getTimeout(), is(2000));
113+
}
114+
115+
@Test
116+
public void testMonitor() {
117+
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/provider-with-monitor.xml");
118+
ctx.start();
119+
120+
assertThat(ctx.getBean(MonitorConfig.class), not(nullValue()));
121+
}
122+
123+
@Test(expected = BeanCreationException.class)
124+
public void testMultiMonitor() {
125+
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-monitor.xml");
126+
ctx.start();
127+
}
128+
129+
@Test(expected = BeanCreationException.class)
130+
public void testMultiProviderConfig() {
131+
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/provider-multi.xml");
132+
ctx.start();
133+
}
134+
135+
@Test
136+
public void testModuleInfo() {
137+
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/provider-with-module.xml");
138+
ctx.start();
139+
140+
ModuleConfig moduleConfig = ctx.getBean(ModuleConfig.class);
141+
assertThat(moduleConfig.getName(), is("test-module"));
142+
}
143+
144+
@Test(expected = BeanCreationException.class)
145+
public void testNotificationWithWrongBean() {
146+
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/consumer-notification.xml");
147+
ctx.start();
148+
}
149+
150+
@Test
151+
public void testProperty() {
152+
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/service-class.xml");
153+
ctx.start();
154+
155+
ServiceBean serviceBean = ctx.getBean(ServiceBean.class);
156+
157+
String prefix = ((DemoServiceImpl) serviceBean.getRef()).getPrefix();
158+
assertThat(prefix, is("welcome:"));
159+
}
160+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.config.spring.status;
18+
19+
import com.alibaba.dubbo.common.status.Status;
20+
import com.alibaba.dubbo.config.spring.ServiceBean;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.mockito.Answers;
24+
import org.mockito.Mock;
25+
import org.springframework.context.ApplicationContext;
26+
27+
import javax.sql.DataSource;
28+
import java.sql.Connection;
29+
import java.sql.SQLException;
30+
import java.util.HashMap;
31+
import java.util.Map;
32+
33+
import static org.hamcrest.CoreMatchers.is;
34+
import static org.junit.Assert.assertThat;
35+
import static org.mockito.ArgumentMatchers.anyBoolean;
36+
import static org.mockito.ArgumentMatchers.eq;
37+
import static org.mockito.BDDMockito.given;
38+
import static org.mockito.Mockito.mock;
39+
import static org.mockito.MockitoAnnotations.initMocks;
40+
41+
public class DataSourceStatusCheckerTest {
42+
private DataSourceStatusChecker dataSourceStatusChecker;
43+
44+
@Mock
45+
private ApplicationContext applicationContext;
46+
47+
@Before
48+
public void setUp() throws Exception {
49+
initMocks(this);
50+
this.dataSourceStatusChecker = new DataSourceStatusChecker();
51+
new ServiceBean<Object>().setApplicationContext(applicationContext);
52+
}
53+
54+
@Test
55+
public void testWithoutApplicationContext() {
56+
Status status = dataSourceStatusChecker.check();
57+
58+
assertThat(status.getLevel(), is(Status.Level.UNKNOWN));
59+
}
60+
61+
@Test
62+
public void testWithoutDatasource() {
63+
Map<String, DataSource> map = new HashMap<String, DataSource>();
64+
given(applicationContext.getBeansOfType(eq(DataSource.class), anyBoolean(), anyBoolean())).willReturn(map);
65+
66+
Status status = dataSourceStatusChecker.check();
67+
68+
assertThat(status.getLevel(), is(Status.Level.UNKNOWN));
69+
}
70+
71+
@Test
72+
public void testWithDatasourceHasNextResult() throws SQLException {
73+
Map<String, DataSource> map = new HashMap<String, DataSource>();
74+
DataSource dataSource = mock(DataSource.class);
75+
Connection connection = mock(Connection.class, Answers.RETURNS_DEEP_STUBS);
76+
given(dataSource.getConnection()).willReturn(connection);
77+
given(connection.getMetaData().getTypeInfo().next()).willReturn(true);
78+
79+
map.put("mockDatabase", dataSource);
80+
given(applicationContext.getBeansOfType(eq(DataSource.class), anyBoolean(), anyBoolean())).willReturn(map);
81+
Status status = dataSourceStatusChecker.check();
82+
83+
assertThat(status.getLevel(), is(Status.Level.OK));
84+
}
85+
86+
@Test
87+
public void testWithDatasourceNotHasNextResult() throws SQLException {
88+
Map<String, DataSource> map = new HashMap<String, DataSource>();
89+
DataSource dataSource = mock(DataSource.class);
90+
Connection connection = mock(Connection.class, Answers.RETURNS_DEEP_STUBS);
91+
given(dataSource.getConnection()).willReturn(connection);
92+
given(connection.getMetaData().getTypeInfo().next()).willReturn(false);
93+
94+
map.put("mockDatabase", dataSource);
95+
given(applicationContext.getBeansOfType(eq(DataSource.class), anyBoolean(), anyBoolean())).willReturn(map);
96+
Status status = dataSourceStatusChecker.check();
97+
98+
assertThat(status.getLevel(), is(Status.Level.ERROR));
99+
}
100+
}

0 commit comments

Comments
 (0)