use of org.apache.dubbo.config.bootstrap.DubboBootstrap in project dubbo by alibaba.
the class GenericServiceTest method testGenericImplementationWithBeanSerialization.
@Test
public void testGenericImplementationWithBeanSerialization() throws Exception {
final AtomicReference reference = new AtomicReference();
ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
service.setInterface(DemoService.class.getName());
service.setRef(new GenericService() {
public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
if ("getUsers".equals(method)) {
GenericParameter arg = new GenericParameter();
arg.method = method;
arg.parameterTypes = parameterTypes;
arg.arguments = args;
reference.set(arg);
return args[0];
}
if ("sayName".equals(method)) {
return null;
}
return args;
}
});
ReferenceConfig<DemoService> ref = null;
ref = new ReferenceConfig<DemoService>();
ref.setInterface(DemoService.class);
ref.setUrl("dubbo://127.0.0.1:29581?scope=remote&generic=bean&timeout=3000");
DubboBootstrap bootstrap = DubboBootstrap.getInstance().application(new ApplicationConfig("generic-test")).registry(new RegistryConfig("N/A")).protocol(new ProtocolConfig("dubbo", 29581)).service(service).reference(ref);
bootstrap.start();
try {
DemoService demoService = bootstrap.getCache().get(ref);
User user = new User();
user.setName("zhangsan");
List<User> users = new ArrayList<User>();
users.add(user);
List<User> result = demoService.getUsers(users);
Assertions.assertEquals(users.size(), result.size());
Assertions.assertEquals(user.getName(), result.get(0).getName());
GenericParameter gp = (GenericParameter) reference.get();
Assertions.assertEquals("getUsers", gp.method);
Assertions.assertEquals(1, gp.parameterTypes.length);
Assertions.assertEquals(ReflectUtils.getName(List.class), gp.parameterTypes[0]);
Assertions.assertEquals(1, gp.arguments.length);
Assertions.assertTrue(gp.arguments[0] instanceof JavaBeanDescriptor);
JavaBeanDescriptor descriptor = (JavaBeanDescriptor) gp.arguments[0];
Assertions.assertTrue(descriptor.isCollectionType());
Assertions.assertEquals(ArrayList.class.getName(), descriptor.getClassName());
Assertions.assertEquals(1, descriptor.propertySize());
descriptor = (JavaBeanDescriptor) descriptor.getProperty(0);
Assertions.assertTrue(descriptor.isBeanType());
Assertions.assertEquals(User.class.getName(), descriptor.getClassName());
Assertions.assertEquals(user.getName(), ((JavaBeanDescriptor) descriptor.getProperty("name")).getPrimitiveProperty());
Assertions.assertNull(demoService.sayName("zhangsan"));
} finally {
bootstrap.stop();
}
}
use of org.apache.dubbo.config.bootstrap.DubboBootstrap in project dubbo by alibaba.
the class GenericServiceTest method testGenericReferenceException.
@SuppressWarnings("unchecked")
@Test
public void testGenericReferenceException() {
ServiceConfig<DemoService> service = new ServiceConfig<DemoService>();
service.setInterface(DemoService.class.getName());
service.setRef(new DemoServiceImpl());
ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
reference.setInterface(DemoService.class);
reference.setUrl("dubbo://127.0.0.1:29581?scope=remote&timeout=3000");
reference.setGeneric(true);
DubboBootstrap bootstrap = DubboBootstrap.getInstance().application(new ApplicationConfig("generic-test")).registry(new RegistryConfig("N/A")).protocol(new ProtocolConfig("dubbo", 29581)).service(service).reference(reference);
bootstrap.start();
try {
GenericService genericService = ReferenceConfigCache.getCache().get(reference);
List<Map<String, Object>> users = new ArrayList<Map<String, Object>>();
Map<String, Object> user = new HashMap<String, Object>();
user.put("class", "org.apache.dubbo.config.api.User");
user.put("name", "actual.provider");
users.add(user);
users = (List<Map<String, Object>>) genericService.$invoke("getUsers", new String[] { List.class.getName() }, new Object[] { users });
Assertions.assertEquals(1, users.size());
Assertions.assertEquals("actual.provider", users.get(0).get("name"));
} finally {
bootstrap.stop();
}
}
Aggregations