Skip to content

Commit

Permalink
[Dubbo - unit test coverage] dubbo-common Unit test coverage (apache#…
Browse files Browse the repository at this point in the history
…2094)

* add new unit tests

* add ClassUtilsTest.

* add ExtensionLoaderTest.

* add bytecode unit tests.
  • Loading branch information
zonghaishang authored and lovepoem committed Jul 18, 2018
1 parent da6556f commit 040be91
Show file tree
Hide file tree
Showing 115 changed files with 1,438 additions and 563 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ private static Object instantiate(Class<?> cl) throws Exception {
return cl.newInstance();
}

private static Object getConstructorArg(Class<?> cl) {
public static Object getConstructorArg(Class<?> cl) {
if (boolean.class.equals(cl) || Boolean.class.equals(cl)) {
return Boolean.FALSE;
} else if (byte.class.equals(cl) || Byte.class.equals(cl)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
*/
package org.apache.dubbo.common.bytecode;

import org.apache.dubbo.common.utils.ClassHelper;
import org.apache.dubbo.common.utils.ReflectUtils;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
Expand All @@ -29,6 +26,8 @@
import javassist.CtNewMethod;
import javassist.LoaderClassPath;
import javassist.NotFoundException;
import org.apache.dubbo.common.utils.ClassHelper;
import org.apache.dubbo.common.utils.ReflectUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -92,10 +91,15 @@ public static ClassPool getClassPool(ClassLoader loader) {
}

private static String modifier(int mod) {
if (Modifier.isPublic(mod)) return "public";
if (Modifier.isProtected(mod)) return "protected";
if (Modifier.isPrivate(mod)) return "private";
return "";
StringBuilder modifier = new StringBuilder();
if (Modifier.isPublic(mod)) modifier.append("public");
if (Modifier.isProtected(mod)) modifier.append("protected");
if (Modifier.isPrivate(mod)) modifier.append("private");

if (Modifier.isStatic(mod)) modifier.append(" static");
if (Modifier.isVolatile(mod)) modifier.append(" volatile");

return modifier.toString();
}

public String getClassName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;
Expand Down Expand Up @@ -262,11 +264,21 @@ public static Class<?> getGenericClass(Class<?> cls, int i) {
if (genericClass instanceof ParameterizedType) {
return (Class<?>) ((ParameterizedType) genericClass).getRawType();
} else if (genericClass instanceof GenericArrayType) {
return (Class<?>) ((GenericArrayType) genericClass).getGenericComponentType();
Type type = ((GenericArrayType) genericClass).getGenericComponentType();
if (type instanceof TypeVariable) {
return type.getClass();
}
return (((GenericArrayType) genericClass).getGenericComponentType() instanceof Class<?>)
? (Class<?>) ((GenericArrayType) genericClass).getGenericComponentType()
: ((GenericArrayType) genericClass).getGenericComponentType().getClass();
} else if (genericClass != null) {
if (genericClass instanceof TypeVariable) {
return genericClass.getClass();
}
return (Class<?>) genericClass;
}
} catch (Throwable e) {

}
if (cls.getSuperclass() != null) {
return getGenericClass(cls.getSuperclass(), i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public String getExtensionName(T extensionInstance) {
}

public String getExtensionName(Class<?> extensionClass) {
getExtensionClasses();// load class
return cachedNames.get(extensionClass);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.common.beanutil;

import org.junit.Assert;
import org.junit.Test;

public class JavaBeanAccessorTest {

@Test
public void testIsAccessByMethod(){
Assert.assertTrue(JavaBeanAccessor.isAccessByMethod(JavaBeanAccessor.METHOD));
Assert.assertTrue(JavaBeanAccessor.isAccessByMethod(JavaBeanAccessor.ALL));
Assert.assertFalse(JavaBeanAccessor.isAccessByMethod(JavaBeanAccessor.FIELD));
}

@Test
public void testIsAccessByField(){
Assert.assertTrue(JavaBeanAccessor.isAccessByField(JavaBeanAccessor.FIELD));
Assert.assertTrue(JavaBeanAccessor.isAccessByField(JavaBeanAccessor.ALL));
Assert.assertFalse(JavaBeanAccessor.isAccessByField(JavaBeanAccessor.METHOD));
}
}
Loading

0 comments on commit 040be91

Please sign in to comment.