-
Notifications
You must be signed in to change notification settings - Fork 26.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Dubbo-1684] add unit test for dubbo spring config (#1809)
* add test for config spring module * add more test for serviceBean and refBean
- Loading branch information
Showing
14 changed files
with
606 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...ig/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/ServiceBeanTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* 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; | ||
|
||
import com.alibaba.dubbo.config.annotation.Service; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.not; | ||
import static org.hamcrest.CoreMatchers.nullValue; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class ServiceBeanTest { | ||
@Test | ||
public void testGetService() { | ||
TestService service = mock(TestService.class); | ||
ServiceBean serviceBean = new ServiceBean(service); | ||
|
||
Service beanService = serviceBean.getService(); | ||
Assert.assertThat(beanService, not(nullValue())); | ||
} | ||
|
||
abstract class TestService implements Service { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
...pring/src/test/java/com/alibaba/dubbo/config/spring/schema/DubboNamespaceHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/* | ||
* 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.schema; | ||
|
||
import com.alibaba.dubbo.config.ApplicationConfig; | ||
import com.alibaba.dubbo.config.ModuleConfig; | ||
import com.alibaba.dubbo.config.MonitorConfig; | ||
import com.alibaba.dubbo.config.ProtocolConfig; | ||
import com.alibaba.dubbo.config.ProviderConfig; | ||
import com.alibaba.dubbo.config.spring.ConfigTest; | ||
import com.alibaba.dubbo.config.spring.ServiceBean; | ||
import com.alibaba.dubbo.config.spring.api.DemoService; | ||
import com.alibaba.dubbo.config.spring.impl.DemoServiceImpl; | ||
import org.junit.Test; | ||
import org.springframework.beans.factory.BeanCreationException; | ||
import org.springframework.context.support.ClassPathXmlApplicationContext; | ||
|
||
import java.util.Map; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.not; | ||
import static org.hamcrest.CoreMatchers.nullValue; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class DubboNamespaceHandlerTest { | ||
@Test | ||
public void testProviderXml() { | ||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/demo-provider.xml"); | ||
ctx.start(); | ||
|
||
ProtocolConfig protocolConfig = ctx.getBean(ProtocolConfig.class); | ||
assertThat(protocolConfig, not(nullValue())); | ||
assertThat(protocolConfig.getName(), is("dubbo")); | ||
assertThat(protocolConfig.getPort(), is(20813)); | ||
|
||
ApplicationConfig applicationConfig = ctx.getBean(ApplicationConfig.class); | ||
assertThat(applicationConfig, not(nullValue())); | ||
assertThat(applicationConfig.getName(), is("demo-provider")); | ||
|
||
DemoService service = ctx.getBean(DemoService.class); | ||
assertThat(service, not(nullValue())); | ||
} | ||
|
||
@Test | ||
public void testMultiProtocol() { | ||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-protocol.xml"); | ||
ctx.start(); | ||
|
||
Map<String, ProtocolConfig> protocolConfigMap = ctx.getBeansOfType(ProtocolConfig.class); | ||
assertThat(protocolConfigMap.size(), is(2)); | ||
|
||
ProtocolConfig rmiProtocolConfig = protocolConfigMap.get("rmi"); | ||
assertThat(rmiProtocolConfig.getPort(), is(10991)); | ||
|
||
ProtocolConfig dubboProtocolConfig = protocolConfigMap.get("dubbo"); | ||
assertThat(dubboProtocolConfig.getPort(), is(20881)); | ||
} | ||
|
||
@Test | ||
public void testDefaultProtocol() { | ||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/override-protocol.xml"); | ||
ctx.start(); | ||
|
||
ProtocolConfig protocolConfig = ctx.getBean(ProtocolConfig.class); | ||
assertThat(protocolConfig.getName(), is("dubbo")); | ||
} | ||
|
||
@Test | ||
public void testCustomParameter() { | ||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/customize-parameter.xml"); | ||
ctx.start(); | ||
|
||
ProtocolConfig protocolConfig = ctx.getBean(ProtocolConfig.class); | ||
assertThat(protocolConfig.getParameters().size(), is(1)); | ||
assertThat(protocolConfig.getParameters().get("protocol-paramA"), is("protocol-paramA")); | ||
|
||
ServiceBean serviceBean = ctx.getBean(ServiceBean.class); | ||
assertThat(serviceBean.getParameters().size(), is(1)); | ||
assertThat(serviceBean.getParameters().get("service-paramA"), is("service-paramA")); | ||
} | ||
|
||
|
||
@Test | ||
public void testDelayFixedTime() { | ||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/delay-fixed-time.xml"); | ||
ctx.start(); | ||
|
||
assertThat(ctx.getBean(ServiceBean.class).getDelay(), is(300)); | ||
} | ||
|
||
@Test | ||
public void testTimeoutConfig() { | ||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/provider-nested-service.xml"); | ||
ctx.start(); | ||
|
||
Map<String, ProviderConfig> providerConfigMap = ctx.getBeansOfType(ProviderConfig.class); | ||
|
||
assertThat(providerConfigMap.get("com.alibaba.dubbo.config.ProviderConfig").getTimeout(), is(2000)); | ||
} | ||
|
||
@Test | ||
public void testMonitor() { | ||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/provider-with-monitor.xml"); | ||
ctx.start(); | ||
|
||
assertThat(ctx.getBean(MonitorConfig.class), not(nullValue())); | ||
} | ||
|
||
@Test(expected = BeanCreationException.class) | ||
public void testMultiMonitor() { | ||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/multi-monitor.xml"); | ||
ctx.start(); | ||
} | ||
|
||
@Test(expected = BeanCreationException.class) | ||
public void testMultiProviderConfig() { | ||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/provider-multi.xml"); | ||
ctx.start(); | ||
} | ||
|
||
@Test | ||
public void testModuleInfo() { | ||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/provider-with-module.xml"); | ||
ctx.start(); | ||
|
||
ModuleConfig moduleConfig = ctx.getBean(ModuleConfig.class); | ||
assertThat(moduleConfig.getName(), is("test-module")); | ||
} | ||
|
||
@Test(expected = BeanCreationException.class) | ||
public void testNotificationWithWrongBean() { | ||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/consumer-notification.xml"); | ||
ctx.start(); | ||
} | ||
|
||
@Test | ||
public void testProperty() { | ||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/service-class.xml"); | ||
ctx.start(); | ||
|
||
ServiceBean serviceBean = ctx.getBean(ServiceBean.class); | ||
|
||
String prefix = ((DemoServiceImpl) serviceBean.getRef()).getPrefix(); | ||
assertThat(prefix, is("welcome:")); | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
...ing/src/test/java/com/alibaba/dubbo/config/spring/status/DataSourceStatusCheckerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* 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.status; | ||
|
||
import com.alibaba.dubbo.common.status.Status; | ||
import com.alibaba.dubbo.config.spring.ServiceBean; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Answers; | ||
import org.mockito.Mock; | ||
import org.springframework.context.ApplicationContext; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.ArgumentMatchers.anyBoolean; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.MockitoAnnotations.initMocks; | ||
|
||
public class DataSourceStatusCheckerTest { | ||
private DataSourceStatusChecker dataSourceStatusChecker; | ||
|
||
@Mock | ||
private ApplicationContext applicationContext; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
initMocks(this); | ||
this.dataSourceStatusChecker = new DataSourceStatusChecker(); | ||
new ServiceBean<Object>().setApplicationContext(applicationContext); | ||
} | ||
|
||
@Test | ||
public void testWithoutApplicationContext() { | ||
Status status = dataSourceStatusChecker.check(); | ||
|
||
assertThat(status.getLevel(), is(Status.Level.UNKNOWN)); | ||
} | ||
|
||
@Test | ||
public void testWithoutDatasource() { | ||
Map<String, DataSource> map = new HashMap<String, DataSource>(); | ||
given(applicationContext.getBeansOfType(eq(DataSource.class), anyBoolean(), anyBoolean())).willReturn(map); | ||
|
||
Status status = dataSourceStatusChecker.check(); | ||
|
||
assertThat(status.getLevel(), is(Status.Level.UNKNOWN)); | ||
} | ||
|
||
@Test | ||
public void testWithDatasourceHasNextResult() throws SQLException { | ||
Map<String, DataSource> map = new HashMap<String, DataSource>(); | ||
DataSource dataSource = mock(DataSource.class); | ||
Connection connection = mock(Connection.class, Answers.RETURNS_DEEP_STUBS); | ||
given(dataSource.getConnection()).willReturn(connection); | ||
given(connection.getMetaData().getTypeInfo().next()).willReturn(true); | ||
|
||
map.put("mockDatabase", dataSource); | ||
given(applicationContext.getBeansOfType(eq(DataSource.class), anyBoolean(), anyBoolean())).willReturn(map); | ||
Status status = dataSourceStatusChecker.check(); | ||
|
||
assertThat(status.getLevel(), is(Status.Level.OK)); | ||
} | ||
|
||
@Test | ||
public void testWithDatasourceNotHasNextResult() throws SQLException { | ||
Map<String, DataSource> map = new HashMap<String, DataSource>(); | ||
DataSource dataSource = mock(DataSource.class); | ||
Connection connection = mock(Connection.class, Answers.RETURNS_DEEP_STUBS); | ||
given(dataSource.getConnection()).willReturn(connection); | ||
given(connection.getMetaData().getTypeInfo().next()).willReturn(false); | ||
|
||
map.put("mockDatabase", dataSource); | ||
given(applicationContext.getBeansOfType(eq(DataSource.class), anyBoolean(), anyBoolean())).willReturn(map); | ||
Status status = dataSourceStatusChecker.check(); | ||
|
||
assertThat(status.getLevel(), is(Status.Level.ERROR)); | ||
} | ||
} |
Oops, something went wrong.