Add [annotation] test case for ServiceDefinitionBuilderTest & remove unused parameter (#8788)
* Add [annotation] test case for ServiceDefinitionBuilderTest & remove unused parameter * Fix ut * Fix ut
This commit is contained in:
parent
feb53c3f6b
commit
f2c5eb9372
|
|
@ -46,7 +46,7 @@ public class TypeDefinitionBuilder {
|
|||
}
|
||||
|
||||
public static TypeDefinition build(Type type, Class<?> clazz, Map<String, TypeDefinition> typeCache) {
|
||||
TypeBuilder builder = getGenericTypeBuilder(type, clazz);
|
||||
TypeBuilder builder = getGenericTypeBuilder(clazz);
|
||||
TypeDefinition td;
|
||||
|
||||
if (clazz.isPrimitive() || ClassUtils.isSimpleType(clazz)) { // changed since 2.7.6
|
||||
|
|
@ -60,10 +60,10 @@ public class TypeDefinitionBuilder {
|
|||
return td;
|
||||
}
|
||||
|
||||
private static TypeBuilder getGenericTypeBuilder(Type type, Class<?> clazz) {
|
||||
private static TypeBuilder getGenericTypeBuilder(Class<?> clazz) {
|
||||
for (TypeBuilder builder : BUILDERS) {
|
||||
try {
|
||||
if (builder.accept(type, clazz)) {
|
||||
if (builder.accept(clazz)) {
|
||||
return builder;
|
||||
}
|
||||
} catch (NoClassDefFoundError cnfe) {
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ import java.util.Map;
|
|||
public class ArrayTypeBuilder implements TypeBuilder {
|
||||
|
||||
@Override
|
||||
public boolean accept(Type type, Class<?> clazz) {
|
||||
public boolean accept(Class<?> clazz) {
|
||||
if (clazz == null) {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -44,7 +44,7 @@ public class ArrayTypeBuilder implements TypeBuilder {
|
|||
}
|
||||
td = new TypeDefinition(canonicalName);
|
||||
typeCache.put(canonicalName, td);
|
||||
// Process the component type of an array.
|
||||
// Process the component type of array.
|
||||
Class<?> componentType = clazz.getComponentType();
|
||||
TypeDefinition itemTd = TypeDefinitionBuilder.build(componentType, componentType, typeCache);
|
||||
if (itemTd != null) {
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ import java.util.Map;
|
|||
public class CollectionTypeBuilder implements TypeBuilder {
|
||||
|
||||
@Override
|
||||
public boolean accept(Type type, Class<?> clazz) {
|
||||
public boolean accept(Class<?> clazz) {
|
||||
if (clazz == null) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ public class EnumTypeBuilder implements TypeBuilder {
|
|||
private static final Logger logger = LoggerFactory.getLogger(TypeDefinitionBuilder.class);
|
||||
|
||||
@Override
|
||||
public boolean accept(Type type, Class<?> clazz) {
|
||||
public boolean accept(Class<?> clazz) {
|
||||
if (clazz == null) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ import static org.apache.dubbo.common.utils.TypeUtils.isParameterizedType;
|
|||
public class MapTypeBuilder implements TypeBuilder {
|
||||
|
||||
@Override
|
||||
public boolean accept(Type type, Class<?> clazz) {
|
||||
public boolean accept(Class<?> clazz) {
|
||||
if (clazz == null) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,9 +30,9 @@ import java.util.Map;
|
|||
public interface TypeBuilder extends Prioritized {
|
||||
|
||||
/**
|
||||
* Whether the build accept the type or class passed in.
|
||||
* Whether the build accept the class passed in.
|
||||
*/
|
||||
boolean accept(Type type, Class<?> clazz);
|
||||
boolean accept(Class<?> clazz);
|
||||
|
||||
/**
|
||||
* Build type definition with the type or class.
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ public final class ClassUtils {
|
|||
return null;
|
||||
}
|
||||
|
||||
String path = codeSource.getLocation().toExternalForm();
|
||||
String path = location.toExternalForm();
|
||||
|
||||
if (path.endsWith(".jar") && path.contains("/")) {
|
||||
return path.substring(path.lastIndexOf('/') + 1);
|
||||
|
|
|
|||
|
|
@ -33,30 +33,42 @@ import java.util.List;
|
|||
public class ServiceDefinitionBuilderTest {
|
||||
|
||||
@Test
|
||||
public void testBuilderComplextObject() {
|
||||
public void testBuilderComplexObject() {
|
||||
FullServiceDefinition fullServiceDefinition = ServiceDefinitionBuilder.buildFullDefinition(DemoService.class);
|
||||
checkComplextObjectAsParam(fullServiceDefinition);
|
||||
checkComplexObjectAsParam(fullServiceDefinition);
|
||||
}
|
||||
|
||||
|
||||
void checkComplextObjectAsParam(FullServiceDefinition fullServiceDefinition) {
|
||||
void checkComplexObjectAsParam(FullServiceDefinition fullServiceDefinition) {
|
||||
Assertions.assertEquals(fullServiceDefinition.getAnnotations(),
|
||||
Arrays.asList("@org.apache.dubbo.metadata.definition.service.annotation.MockTypeAnnotation(value=666)"));
|
||||
|
||||
List<MethodDefinition> methodDefinitions = fullServiceDefinition.getMethods();
|
||||
MethodDefinition complexCompute = null;
|
||||
MethodDefinition findComplexObject = null;
|
||||
MethodDefinition testAnnotation = null;
|
||||
for (MethodDefinition methodDefinition : methodDefinitions) {
|
||||
if ("complexCompute".equals(methodDefinition.getName())) {
|
||||
complexCompute = methodDefinition;
|
||||
} else if ("findComplexObject".equals(methodDefinition.getName())) {
|
||||
findComplexObject = methodDefinition;
|
||||
} else if ("testAnnotation".equals(methodDefinition.getName())) {
|
||||
testAnnotation = methodDefinition;
|
||||
}
|
||||
}
|
||||
Assertions.assertTrue(Arrays.equals(complexCompute.getParameterTypes(), new String[]{String.class.getName(), ComplexObject.class.getName()}));
|
||||
Assertions.assertEquals(complexCompute.getReturnType(), String.class.getName());
|
||||
|
||||
Assertions.assertTrue(Arrays.equals(findComplexObject.getParameterTypes(), new String[]{String.class.getName(), "int", "long",
|
||||
String[].class.getCanonicalName(), "java.util.List<java.lang.Integer>", ComplexObject.TestEnum.class.getCanonicalName()}));
|
||||
String[].class.getCanonicalName(), "java.util.List<java.lang.Integer>", ComplexObject.TestEnum.class.getCanonicalName()}));
|
||||
Assertions.assertEquals(findComplexObject.getReturnType(), ComplexObject.class.getCanonicalName());
|
||||
|
||||
Assertions.assertEquals(testAnnotation.getAnnotations(), Arrays.asList(
|
||||
"@org.apache.dubbo.metadata.definition.service.annotation.MockMethodAnnotation(value=777)",
|
||||
"@org.apache.dubbo.metadata.definition.service.annotation.MockMethodAnnotation2(value=888)"));
|
||||
Assertions.assertEquals(testAnnotation.getReturnType(), "void");
|
||||
|
||||
|
||||
List<TypeDefinition> typeDefinitions = fullServiceDefinition.getTypes();
|
||||
|
||||
TypeDefinition topTypeDefinition = null;
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ public class Test3TypeBuilder implements TypeBuilder {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean accept(Type type, Class<?> clazz) {
|
||||
public boolean accept (Class<?> clazz) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ public class TestTypeBuilder implements TypeBuilder {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean accept(Type type, Class<?> clazz) {
|
||||
public boolean accept(Class<?> clazz) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,15 +16,23 @@
|
|||
*/
|
||||
package org.apache.dubbo.metadata.definition.service;
|
||||
|
||||
import org.apache.dubbo.metadata.definition.service.annotation.MockMethodAnnotation;
|
||||
import org.apache.dubbo.metadata.definition.service.annotation.MockMethodAnnotation2;
|
||||
import org.apache.dubbo.metadata.definition.service.annotation.MockTypeAnnotation;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* for test
|
||||
*/
|
||||
@MockTypeAnnotation(666)
|
||||
public interface DemoService {
|
||||
|
||||
String complexCompute(String input, ComplexObject co);
|
||||
|
||||
ComplexObject findComplexObject(String var1, int var2, long l, String[] var3, List<Integer> var4, ComplexObject.TestEnum testEnum);
|
||||
|
||||
@MockMethodAnnotation(777)
|
||||
@MockMethodAnnotation2(888)
|
||||
void testAnnotation(boolean flag);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* 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 org.apache.dubbo.metadata.definition.service.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MockMethodAnnotation {
|
||||
int value();
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* 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 org.apache.dubbo.metadata.definition.service.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MockMethodAnnotation2 {
|
||||
int value();
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* 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 org.apache.dubbo.metadata.definition.service.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MockTypeAnnotation {
|
||||
int value();
|
||||
}
|
||||
|
|
@ -61,7 +61,7 @@ public class ProtobufTypeBuilder implements TypeBuilder, Prioritized {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean accept(Type type, Class<?> clazz) {
|
||||
public boolean accept(Class<?> clazz) {
|
||||
if (clazz == null) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue