Skip to content

Commit

Permalink
Refactor testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenlj committed Nov 3, 2017
1 parent 983bb57 commit 30985fa
Show file tree
Hide file tree
Showing 46 changed files with 373 additions and 640 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
* Created by ken.lj on 2017/9/24.
*/
public interface AsyncService {

String asyncMethod();
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

31 changes: 29 additions & 2 deletions consumer-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -46,6 +50,31 @@
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-simple</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
</dependency>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
</dependency>


<dependency>
<groupId>org.apache.tomcat</groupId>
Expand All @@ -57,8 +86,6 @@
</dependency>
</dependencies>



<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.alibaba.dubbo.test;

import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

Expand All @@ -8,10 +10,12 @@
* @date 2017/09/10
*/
@SpringBootApplication
@DubboComponentScan(basePackages = "com.alibaba.dubbo.test.service")
public class ConsumerTestApp {

public static void main(String[] args) throws InterruptedException {
SpringApplication.run(ConsumerTestApp.class, args);
Thread.sleep(100000000);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.alibaba.dubbo.test;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.rpc.service.GenericService;
import com.alibaba.dubbo.test.service.AnnotateService;
import com.alibaba.dubbo.test.service.AsyncService;
import com.alibaba.dubbo.test.service.DemoService;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* 自动执行一遍所有服务调用
*
* @author ken.lj
* @date 2017/11/3
*/
@Service
public class DubboServiceTestRunner implements ApplicationContextAware {
Logger logger = LoggerFactory.getLogger(DubboServiceTestRunner.class);
private ApplicationContext context;

@Autowired
private DemoService demoService;

@Autowired
private AsyncService asyncService;

@Reference
private AnnotateService annotateService;

@PostConstruct
public void start() {
logger.info("****** start testcase! ******");
try {
runSimple();
runAync();
runAnnotate();
runGeneric();
} catch (Throwable t) {
logger.error("有测试用例报错,请检查!", t);
}
}

public void runSimple() {
demoService.testString("string param");
}

public void runAync() {
asyncService.asyncMethod();
}

public void runAnnotate() {
annotateService.sayHello();
}

public void runGeneric() {
GenericService genericService = (GenericService) context.getBean("genericTestService");
Map<String, Set<String>> map = new HashMap<String, Set<String>>();
Set<String> set = new HashSet<String>();
set.add("v1");
set.add("v2");
map.put("key", set);
Object result = genericService.$invoke("testGenericWithJsonSerialization", new String[]{"java.util.Map"}, new Object[]{map});
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.alibaba.dubbo.test.conf;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.RegistryConfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

Expand All @@ -8,7 +13,28 @@
* @date 2017/09/10
*/
@Configuration
@ImportResource(locations = {"classpath:dubbo-common.xml", "classpath:dubbo-${config.file}.xml"})
@ImportResource(locations = {"classpath:*.xml"})
public class DubboConfiguration {

@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("consumer-test");
return applicationConfig;
}

@Bean
public ConsumerConfig consumerConfig() {
ConsumerConfig consumerConfig = new ConsumerConfig();
consumerConfig.setTimeout(3000);
return consumerConfig;
}

@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://127.0.0.1:2181");
registryConfig.setClient("curator");
return registryConfig;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.alibaba.dubbo.test.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.test.service.AnnotateService;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author ken.lj
* @date 2017/09/10
*/
@RestController("annotation")
public class AnnotationParentReferenceController extends BaseController<AnnotateService> {

@Reference
public AnnotateService annotateServiceSub;

@RequestMapping("/subclass")
String subHello() {
return annotateServiceSub.sayHello();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.alibaba.dubbo.test.web;

import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.test.service.AsyncService;

import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;

/**
* @author ken.lj
* @date 2017/11/3
*/
@RestController
@RequestMapping("/async")
public class AsyncController {
@Autowired(required = false)
private AsyncService asyncService;

@RequestMapping("/hello")
public String testSimple() throws Exception {
String result = asyncService.asyncMethod();
Assert.assertNull(result);
Future<String> future = RpcContext.getContext().getFuture();

return future.get();
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package com.alibaba.dubbo.test.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.test.service.AnnotateService;

import org.springframework.web.bind.annotation.RequestMapping;

/**
* 测试Reference支持父类属性注入
* @author ken.lj
* @date 2017/09/10
*/
public class BaseController<T extends AnnotateService> {

@Reference
public T annotateService;

@RequestMapping("/hello")
@RequestMapping("/parentclass")
String hello() {
return annotateService.sayHello();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.alibaba.dubbo.test.web;

import com.alibaba.dubbo.rpc.service.GenericService;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* @author ken.lj
* @date 2017/11/3
*/
@RestController
@RequestMapping("/simple")
public class GenericReferenceController implements ApplicationContextAware {

private ApplicationContext context;

@RequestMapping("/reference/generic")
public String testReferenceGeneric() {
GenericService genericService = (GenericService) context.getBean("genericTestService");
Map<String, Set<String>> map = new HashMap<String, Set<String>>();
Set<String> set = new HashSet<String>();
set.add("v1");
set.add("v2");
map.put("key", set);
Object result = genericService.$invoke("testGenericWithJsonSerialization", new String[]{"java.util.Map"}, new Object[]{map});
return "generic";
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
}

This file was deleted.

Loading

0 comments on commit 30985fa

Please sign in to comment.