Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dubbo-1684] add unit test for dubbo spring config #1809

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ coverage:
threshold: 0.1%
ignore:
- "dubbo-demo/.*"
- "dubbo-common/src/main/java/com/alibaba/dubbo/common/json/*.java" # internal JSON impl is deprecate, ignore test coverage for them
- "dubbo-common/src/main/java/com/alibaba/dubbo/common/json/*.java" # internal JSON impl is deprecate, ignore test coverage for them
- "dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/AnnotationBean.java" # Deprecated
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import com.alibaba.dubbo.rpc.RpcException;
import com.alibaba.dubbo.rpc.service.GenericException;
import com.alibaba.dubbo.rpc.service.GenericService;
import junit.framework.Assert;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
Expand Down
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 {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
import java.util.Map;

import static com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.BEAN_NAME;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;

/**
* {@link ReferenceAnnotationBeanPostProcessor} Test
Expand Down Expand Up @@ -174,6 +178,25 @@ public void testGetInjectedMethodReferenceBeanMap() {

}

@Test
public void testModuleInfo() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestBean.class);

ReferenceAnnotationBeanPostProcessor beanPostProcessor = context.getBean(BEAN_NAME,
ReferenceAnnotationBeanPostProcessor.class);


Map<InjectionMetadata.InjectedElement, ReferenceBean<?>> referenceBeanMap =
beanPostProcessor.getInjectedMethodReferenceBeanMap();

for (Map.Entry<InjectionMetadata.InjectedElement, ReferenceBean<?>> entry : referenceBeanMap.entrySet()) {
ReferenceBean<?> referenceBean = entry.getValue();

assertThat(referenceBean.getModule().getName(),is("defaultModule"));
assertThat(referenceBean.getMonitor(), not(nullValue()));
}
}

private static class AncestorBean {


Expand Down
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:"));
}
}
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));
}
}
Loading