This source file includes following definitions.
- hashCode
- equals
- toString
- getGetterInfo
- getSetterInfo
- invokeMethod
- Feature
- testBuilderHasCorrectSetters
package org.chromium.content.browser;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.DownloadInfo.Builder;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Random;
public class DownloadInfoTest extends InstrumentationTestCase {
static class AccessorSignature {
final String mMethodNameWithoutPrefix;
final Class<?> mReturnTypeOrParam;
AccessorSignature(String methodNameWithoutPrefix, Class<?> returnTypeOrParam) {
mMethodNameWithoutPrefix = methodNameWithoutPrefix;
mReturnTypeOrParam = returnTypeOrParam;
}
@Override
public int hashCode() {
return mMethodNameWithoutPrefix.hashCode() * 31
+ mReturnTypeOrParam.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof AccessorSignature) {
AccessorSignature other = (AccessorSignature) obj;
return other.mReturnTypeOrParam == mReturnTypeOrParam
&& other.mMethodNameWithoutPrefix.equals(mMethodNameWithoutPrefix);
}
return false;
}
@Override
public String toString() {
return "{ " + mMethodNameWithoutPrefix + ", " + mReturnTypeOrParam.getName() + "}";
}
}
AccessorSignature getGetterInfo(Method method) {
if (method.getParameterTypes().length == 0) {
Class<?> returnType = method.getReturnType();
if (returnType.isPrimitive() || returnType == String.class) {
String methodName = method.getName();
if (returnType.equals(Boolean.TYPE)) {
if (methodName.matches("(is|has).*")) {
return new AccessorSignature(methodName.replaceFirst("is|has", ""),
returnType);
}
} else {
if (methodName.startsWith("get")) {
return new AccessorSignature(methodName.substring(3), returnType);
}
}
}
}
return null;
}
AccessorSignature getSetterInfo(Method method) {
if (method.getParameterTypes().length == 1) {
Class<?> parameter = method.getParameterTypes()[0];
String methodName = method.getName();
if (methodName.startsWith("set")) {
if (parameter.equals(Boolean.TYPE)) {
return new AccessorSignature(
methodName.replaceFirst("set(Is|Has)", ""),
parameter);
} else {
return new AccessorSignature(methodName.substring(3), parameter);
}
}
}
return null;
}
Object invokeMethod(Method method, Object instance, Object... args) throws Exception {
try {
return method.invoke(instance, args);
} catch (IllegalArgumentException e) {
throw e;
} catch (IllegalAccessException e) {
throw e;
} catch (InvocationTargetException e) {
throw e;
}
}
@SmallTest
@Feature({"Downloads"})
public void testBuilderHasCorrectSetters() {
HashMap<AccessorSignature, Method> downloadInfoGetters =
new HashMap<AccessorSignature, Method>();
HashMap<AccessorSignature, Method> builderSetters =
new HashMap<AccessorSignature, Method>();
for (Method m : DownloadInfo.class.getMethods()) {
AccessorSignature info = getGetterInfo(m);
if (info != null) {
downloadInfoGetters.put(info, m);
}
}
assertTrue("There should be at least one getter.",
downloadInfoGetters.size() > 0);
for (Method m : Builder.class.getMethods()) {
AccessorSignature info = getSetterInfo(m);
if (info != null) {
builderSetters.put(info, m);
}
}
assertEquals("Mismatch between getters and setters.",
downloadInfoGetters.keySet(), builderSetters.keySet());
Random random = new Random();
HashMap<AccessorSignature, Object> valuesForBuilder =
new HashMap<DownloadInfoTest.AccessorSignature, Object>();
for (AccessorSignature signature : builderSetters.keySet()) {
if (signature.mReturnTypeOrParam.equals(String.class)) {
String value = signature.mMethodNameWithoutPrefix
+ Integer.toString(random.nextInt());
valuesForBuilder.put(signature, value);
} else if (signature.mReturnTypeOrParam.equals(Boolean.TYPE)) {
valuesForBuilder.put(signature, Boolean.TRUE);
} else {
valuesForBuilder.put(signature, Integer.valueOf(random.nextInt(100)));
}
}
Builder builder = new Builder();
for (AccessorSignature signature : builderSetters.keySet()) {
Method setter = builderSetters.get(signature);
try {
invokeMethod(setter, builder, valuesForBuilder.get(signature));
} catch (Exception e) {
fail("Exception while setting value in the setter. Signature: " + signature
+ " value:" + valuesForBuilder.get(signature) + ":" + e);
}
}
DownloadInfo downloadInfo = builder.build();
for (AccessorSignature signature : downloadInfoGetters.keySet()) {
Method getter = downloadInfoGetters.get(signature);
try {
Object returnValue = invokeMethod(getter, downloadInfo);
assertEquals(signature.toString(),
valuesForBuilder.get(signature).toString(), returnValue.toString());
} catch (Exception e) {
fail("Exception while getting value from getter. Signature: " + signature
+ " value:" + valuesForBuilder.get(signature));
}
}
DownloadInfo newDownloadInfo = Builder.fromDownloadInfo(downloadInfo).build();
for (AccessorSignature signature : downloadInfoGetters.keySet()) {
Method getter = downloadInfoGetters.get(signature);
try {
Object returnValue1 = invokeMethod(getter, downloadInfo);
Object returnValue2 = invokeMethod(getter, newDownloadInfo);
assertEquals(signature.toString(), returnValue1, returnValue2);
} catch (Exception e) {
fail("Exception while getting value from getter. Signature: " + signature
+ " value:" + valuesForBuilder.get(signature) + ":" + e);
}
}
}
}