提交 6a1c14ba 编写于 作者: W william.liangf

DUBBO-94 增加AOP和Autowire场景的单元测试

git-svn-id: http://code.alibabatech.com/svn/dubbo/trunk@567 1a56cb94-b969-4eaa-88fa-be21384802f2
上级 6728fc12
......@@ -26,6 +26,8 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.utils.NetUtils;
import com.alibaba.dubbo.config.api.DemoService;
import com.alibaba.dubbo.config.consumer.DemoActionByAnnotation;
import com.alibaba.dubbo.config.consumer.DemoActionBySetter;
import com.alibaba.dubbo.config.provider.impl.DemoServiceImpl;
import com.alibaba.dubbo.registry.RegistryService;
import com.alibaba.dubbo.registry.support.SimpleRegistryExporter;
......@@ -175,6 +177,43 @@ public class ConfigTest {
consumer.setTimeout(2000);
Assert.assertEquals("1000", System.getProperty("sun.rmi.transport.tcp.responseTimeout"));
}
@Test
public void testAutowireAndAOP() throws Exception {
ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/demo-provider.xml");
providerContext.start();
try {
ClassPathXmlApplicationContext byNameContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/aop-autowire-byname.xml");
byNameContext.start();
try {
DemoActionBySetter demoActionBySetter = (DemoActionBySetter) byNameContext.getBean("demoActionBySetter");
Assert.assertNotNull(demoActionBySetter.getDemoService());
Assert.assertEquals("aop:say:hello", demoActionBySetter.getDemoService().sayName("hello"));
DemoActionByAnnotation demoActionByAnnotation = (DemoActionByAnnotation) byNameContext.getBean("demoActionByAnnotation");
Assert.assertNotNull(demoActionByAnnotation.getDemoService());
Assert.assertEquals("aop:say:hello", demoActionByAnnotation.getDemoService().sayName("hello"));
} finally {
byNameContext.stop();
byNameContext.close();
}
ClassPathXmlApplicationContext byTypeContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/aop-autowire-bytype.xml");
byTypeContext.start();
try {
DemoActionBySetter demoActionBySetter = (DemoActionBySetter) byTypeContext.getBean("demoActionBySetter");
Assert.assertNotNull(demoActionBySetter.getDemoService());
Assert.assertEquals("aop:say:hello", demoActionBySetter.getDemoService().sayName("hello"));
DemoActionByAnnotation demoActionByAnnotation = (DemoActionByAnnotation) byTypeContext.getBean("demoActionByAnnotation");
Assert.assertNotNull(demoActionByAnnotation.getDemoService());
Assert.assertEquals("aop:say:hello", demoActionByAnnotation.getDemoService().sayName("hello"));
} finally {
byTypeContext.stop();
byTypeContext.close();
}
} finally {
providerContext.stop();
providerContext.close();
}
}
@Test
public void testAppendFilter() throws Exception {
......
/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed 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.consumer;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.api.DemoService;
/**
* DemoAction
*
* @author william.liangf
*/
public class DemoActionByAnnotation {
@Autowired
private DemoService demoService;
public DemoService getDemoService() {
return demoService;
}
}
/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed 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.consumer;
import com.alibaba.dubbo.config.api.DemoService;
/**
* DemoAction
*
* @author william.liangf
*/
public class DemoActionBySetter {
private DemoService demoService;
public DemoService getDemoService() {
return demoService;
}
public void setDemoService(DemoService demoService) {
this.demoService = demoService;
}
}
/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed 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.consumer;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
* DemoInterceptor
*
* @author william.liangf
*/
public class DemoInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable {
return "aop:" + invocation.proceed();
}
}
<!--
- Copyright 1999-2011 Alibaba Group.
-
- Licensed 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.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd" default-autowire="byName">
<context:annotation-config />
<bean id="demoInterceptor" class="com.alibaba.dubbo.config.consumer.DemoInterceptor" />
<bean id="demoAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="demoInterceptor" />
<property name="patterns">
<list>
<value>.*</value>
</list>
</property>
</bean>
<bean id="demoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>demoService</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>demoAdvisor</value>
</list>
</property>
</bean>
<!-- 当前应用信息配置 -->
<dubbo:application name="consumer" />
<!-- 连接注册中心配置 -->
<dubbo:registry address="N/A" />
<!-- 引用服务配置 -->
<dubbo:reference id="demoService" interface="com.alibaba.dubbo.config.api.DemoService" url="dubbo://127.0.0.1:20813" />
<bean id="demoActionBySetter" class="com.alibaba.dubbo.config.consumer.DemoActionBySetter" />
<bean id="demoActionByAnnotation" class="com.alibaba.dubbo.config.consumer.DemoActionByAnnotation" />
</beans>
\ No newline at end of file
<!--
- Copyright 1999-2011 Alibaba Group.
-
- Licensed 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.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd" default-autowire="byName">
<context:annotation-config />
<bean id="demoInterceptor" class="com.alibaba.dubbo.config.consumer.DemoInterceptor" />
<bean id="demoAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="demoInterceptor" />
<property name="patterns">
<list>
<value>.*</value>
</list>
</property>
</bean>
<bean id="demoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>demoService</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>demoAdvisor</value>
</list>
</property>
</bean>
<!-- 当前应用信息配置 -->
<dubbo:application name="consumer" />
<!-- 连接注册中心配置 -->
<dubbo:registry address="N/A" />
<!-- 引用服务配置 -->
<dubbo:reference id="demoService" interface="com.alibaba.dubbo.config.api.DemoService" url="dubbo://127.0.0.1:20813" />
<bean id="demoActionBySetter" class="com.alibaba.dubbo.config.consumer.DemoActionBySetter" />
<bean id="demoActionByAnnotation" class="com.alibaba.dubbo.config.consumer.DemoActionByAnnotation" />
</beans>
\ No newline at end of file
<!--
- Copyright 1999-2011 Alibaba Group.
-
- Licensed 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.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<!-- 当前应用信息配置 -->
<dubbo:application name="demo-provider" />
<!-- 连接注册中心配置 -->
<dubbo:registry address="N/A" />
<!-- 暴露服务协议配置 -->
<dubbo:protocol name="dubbo" port="20813" />
<!-- 暴露服务配置 -->
<dubbo:service interface="com.alibaba.dubbo.config.api.DemoService" ref="demoService" />
<bean id="demoService" class="com.alibaba.dubbo.config.provider.impl.DemoServiceImpl" />
</beans>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册