use of org.apache.dubbo.metadata.definition.model.ServiceDefinition in project dubbo by alibaba.
the class MetadataTest method testInnerClassType.
/**
*/
@Test
public void testInnerClassType() {
TypeDefinitionBuilder builder = new TypeDefinitionBuilder();
TypeDefinition td = builder.build(OuterClass.InnerClass.class, OuterClass.InnerClass.class);
System.out.println(">> testInnerClassType: " + new Gson().toJson(td));
Assertions.assertEquals("org.apache.dubbo.metadata.definition.common.OuterClass$InnerClass", td.getType());
Assertions.assertEquals(1, td.getProperties().size());
Assertions.assertNotNull(td.getProperties().get("name"));
Assertions.assertEquals(DefaultTypeBuilder.class.getName(), td.getTypeBuilderName());
ServiceDefinition sd = MetadataUtils.generateMetadata(TestService.class);
System.out.println(">> testInnerClassType: " + new Gson().toJson(sd));
Assertions.assertEquals(TestService.class.getName(), sd.getCanonicalName());
Assertions.assertEquals(TestService.class.getMethods().length, sd.getMethods().size());
boolean containsType = false;
for (TypeDefinition type : sd.getTypes()) {
if (type.getType().equals("org.apache.dubbo.metadata.definition.common.OuterClass$InnerClass")) {
containsType = true;
break;
}
}
Assertions.assertTrue(containsType);
}
use of org.apache.dubbo.metadata.definition.model.ServiceDefinition in project dubbo by alibaba.
the class MetadataUtils method generateMetadata.
/**
* com.taobao.hsf.metadata.store.MetadataInfoStoreServiceRedis.publishClassInfo(ServiceMetadata) 生成元数据的代码
*/
public static ServiceDefinition generateMetadata(Class<?> interfaceClass) {
ServiceDefinition sd = new ServiceDefinition();
sd.setCanonicalName(interfaceClass.getCanonicalName());
sd.setCodeSource(ClassUtils.getCodeSource(interfaceClass));
TypeDefinitionBuilder builder = new TypeDefinitionBuilder();
List<Method> methods = ClassUtils.getPublicNonStaticMethods(interfaceClass);
for (Method method : methods) {
MethodDefinition md = new MethodDefinition();
md.setName(method.getName());
Class<?>[] paramTypes = method.getParameterTypes();
Type[] genericParamTypes = method.getGenericParameterTypes();
String[] parameterTypes = new String[paramTypes.length];
for (int i = 0; i < paramTypes.length; i++) {
try {
TypeDefinition td = builder.build(genericParamTypes[i], paramTypes[i]);
parameterTypes[i] = td.getType();
} catch (Exception e) {
parameterTypes[i] = paramTypes[i].getName();
}
}
md.setParameterTypes(parameterTypes);
try {
TypeDefinition td = builder.build(method.getGenericReturnType(), method.getReturnType());
md.setReturnType(td.getType());
} catch (Exception e) {
md.setReturnType(method.getReturnType().getName());
}
sd.getMethods().add(md);
}
sd.setTypes(builder.getTypeDefinitions());
return sd;
}
use of org.apache.dubbo.metadata.definition.model.ServiceDefinition in project dubbo by alibaba.
the class FailoverMetadataReportTest method testReadWriteAllMetadataReport.
@Test
public void testReadWriteAllMetadataReport() {
URL url = mockURL.addParameter("strategy", "all");
FailoverMetadataReport report = getFailoverReport(url);
Assertions.assertNotNull(report.getProxyReports(), "metadata reports should not be null.");
Assertions.assertEquals(2, report.getProxyReports().size(), "expect 2 metadata report, actual " + report.getProxyReports().size());
MetadataIdentifier identifier = new MetadataIdentifier("helloService", null, null, null, "test");
ServiceDefinition definition = new ServiceDefinition();
definition.setCanonicalName("helloService");
report.storeProviderMetadata(identifier, definition);
Assertions.assertNotNull(report.getServiceDefinition(identifier));
// assert all metadata report write already.
for (FailoverMetadataReport.MetadataReportHolder holder : report.getProxyReports()) {
Assertions.assertNotNull(holder.report.getServiceDefinition(identifier));
}
HashMap parameterMap = new HashMap();
report.storeConsumerMetadata(identifier, parameterMap);
// assert all metadata report write already.
for (FailoverMetadataReport.MetadataReportHolder holder : report.getProxyReports()) {
Assertions.assertEquals(parameterMap, ((MockMetadataReport) holder.report).consumerMetadata.get(identifier));
}
SubscriberMetadataIdentifier subscribeIdentifier = new SubscriberMetadataIdentifier("test", "1.0");
MetadataInfo metadataInfo = new MetadataInfo(subscribeIdentifier.getApplication(), subscribeIdentifier.getRevision(), null);
report.publishAppMetadata(subscribeIdentifier, metadataInfo);
Assertions.assertEquals(metadataInfo, report.getAppMetadata(subscribeIdentifier, null));
// assert all metadata report write already.
for (FailoverMetadataReport.MetadataReportHolder holder : report.getProxyReports()) {
Assertions.assertEquals(metadataInfo, holder.report.getAppMetadata(subscribeIdentifier, null));
}
report.registerServiceAppMapping("helloService", "test", null);
Set<String> appNames = report.getServiceAppMapping("helloService", null, null);
Assertions.assertEquals(appNames, report.getServiceAppMapping("helloService", null, null));
// assert all metadata report write already.
for (FailoverMetadataReport.MetadataReportHolder holder : report.getProxyReports()) {
Assertions.assertEquals(appNames, holder.report.getServiceAppMapping("helloService", null, null));
}
ServiceMetadataIdentifier serviceIdentifier = new ServiceMetadataIdentifier("helloService", null, null, null, "1.0", "dubbo");
report.saveServiceMetadata(serviceIdentifier, url);
Assertions.assertNotNull(report.getExportedURLs(serviceIdentifier));
// assert all metadata report write already.
for (FailoverMetadataReport.MetadataReportHolder holder : report.getProxyReports()) {
Assertions.assertNotNull(holder.report.getExportedURLs(serviceIdentifier));
}
report.saveSubscribedData(subscribeIdentifier, new HashSet<>());
Assertions.assertNotNull(report.getSubscribedURLs(subscribeIdentifier));
// assert all metadata report write already.
for (FailoverMetadataReport.MetadataReportHolder holder : report.getProxyReports()) {
Assertions.assertNotNull(holder.report.getSubscribedURLs(subscribeIdentifier));
}
}
use of org.apache.dubbo.metadata.definition.model.ServiceDefinition in project dubbo by alibaba.
the class ServiceDefinitionBuilderTest method testBuild.
@Test
public void testBuild() {
ServiceDefinition serviceDefinition = build(processingEnv, getType(TestServiceImpl.class));
assertEquals(TestServiceImpl.class.getTypeName(), serviceDefinition.getCanonicalName());
assertEquals("org/apache/dubbo/metadata/tools/TestServiceImpl.class", serviceDefinition.getCodeSource());
// types
int i = 0;
assertEquals("org.apache.dubbo.metadata.tools.TestServiceImpl", serviceDefinition.getTypes().get(i++).getType());
assertEquals("org.apache.dubbo.metadata.tools.GenericTestService", serviceDefinition.getTypes().get(i++).getType());
assertEquals("org.apache.dubbo.metadata.tools.DefaultTestService", serviceDefinition.getTypes().get(i++).getType());
assertEquals("org.apache.dubbo.metadata.tools.TestService", serviceDefinition.getTypes().get(i++).getType());
assertEquals("java.lang.AutoCloseable", serviceDefinition.getTypes().get(i++).getType());
assertEquals("java.io.Serializable", serviceDefinition.getTypes().get(i++).getType());
assertEquals("java.util.EventListener", serviceDefinition.getTypes().get(i++).getType());
// methods
assertEquals(14, serviceDefinition.getMethods().size());
}
Aggregations