private DataStore verifyThrottled(final DataStore dataStore) { return (DataStore) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { DataStore.class }, new AbstractInvocationHandler() { @Override protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable { try { method.invoke(dataStore, args); fail(String.format("Throttled request did not generate a 503 error: %s(%s)", method.getName(), Joiner.on(",").join(args))); } catch (InvocationTargetException e) { assertTrue(e.getCause() instanceof EmoClientException); EmoClientException ce = (EmoClientException) e.getCause(); assertEquals(ce.getResponse().getStatus(), HttpStatus.SERVICE_UNAVAILABLE_503); } // This should be unreachable; the caller doesn't care what the result is if (method.getReturnType().isPrimitive()) { return Primitives.defaultValueForPrimitiveOrWrapper(method.getReturnType()); } return null; } }); }
protected void invokeMethod(Class<?> usedClass, String methodName, Object... params) throws Exception { Class<?> subjectClass = loadClass(usedClass); if (params == null) { params = new Object[] {}; } Method targetedMethod = null; for (Method method : subjectClass.getMethods()) { if (method.getName().equals(methodName) && (method.getParameterTypes().length == params.length)) { boolean paramCanMatch = true; for (int i = 0; i < method.getParameterTypes().length && paramCanMatch; i++) { Class<?> paramClass = method.getParameterTypes()[i]; if (params[i] != null && !paramClass.isAssignableFrom(params[i].getClass()) && !(paramClass.isPrimitive() && paramClass.equals(Primitives.primitiveTypeOf(params[i].getClass())))) { paramCanMatch = false; } } if (!paramCanMatch) { continue; } targetedMethod = method; break; } } if (targetedMethod == null) { throw new Exception("Aucune méthode " + methodName + " ne matche pour les paramètres données"); } // On n'utile pas directement subjectClass.getMethod(methodName, paramClass) car la m�thode test�e peut avoir des types primitifs en param�tre. targetedMethod.invoke(createNewInstanceOfTestedClass(subjectClass), params); }
public boolean isValidReturnType(Class clazz) { if (method.getReturnType().isPrimitive()) { return Primitives.primitiveTypeOf(clazz) == method.getReturnType(); } else { return method.getReturnType().isAssignableFrom(clazz); } }
Object returnValueFor(Class<?> type) { if (type.isPrimitive()) { return primitiveOf(type); } else if (Primitives.isPrimitiveWrapper(type)) { return Primitives.primitiveWrapperOf(type); //new instances are used instead of Collections.emptyList(), etc. //to avoid UnsupportedOperationException if code under test modifies returned collection } else if (type == Collection.class) { return new LinkedList<Object>(); } else if (type == Set.class) { return new HashSet<Object>(); } else if (type == HashSet.class) { return new HashSet<Object>(); } else if (type == SortedSet.class) { return new TreeSet<Object>(); } else if (type == TreeSet.class) { return new TreeSet<Object>(); } else if (type == LinkedHashSet.class) { return new LinkedHashSet<Object>(); } else if (type == List.class) { return new LinkedList<Object>(); } else if (type == LinkedList.class) { return new LinkedList<Object>(); } else if (type == ArrayList.class) { return new ArrayList<Object>(); } else if (type == Map.class) { return new HashMap<Object, Object>(); } else if (type == HashMap.class) { return new HashMap<Object, Object>(); } else if (type == SortedMap.class) { return new TreeMap<Object, Object>(); } else if (type == TreeMap.class) { return new TreeMap<Object, Object>(); } else if (type == LinkedHashMap.class) { return new LinkedHashMap<Object, Object>(); } //Let's not care about the rest of collections. return null; }
Object returnValueFor(Class<?> type) { if (Primitives.isPrimitiveOrWrapper(type)) { return Primitives.defaultValueForPrimitiveOrWrapper(type); //new instances are used instead of Collections.emptyList(), etc. //to avoid UnsupportedOperationException if code under test modifies returned collection } else if (type == Collection.class) { return new LinkedList<Object>(); } else if (type == Set.class) { return new HashSet<Object>(); } else if (type == HashSet.class) { return new HashSet<Object>(); } else if (type == SortedSet.class) { return new TreeSet<Object>(); } else if (type == TreeSet.class) { return new TreeSet<Object>(); } else if (type == LinkedHashSet.class) { return new LinkedHashSet<Object>(); } else if (type == List.class) { return new LinkedList<Object>(); } else if (type == LinkedList.class) { return new LinkedList<Object>(); } else if (type == ArrayList.class) { return new ArrayList<Object>(); } else if (type == Map.class) { return new HashMap<Object, Object>(); } else if (type == HashMap.class) { return new HashMap<Object, Object>(); } else if (type == SortedMap.class) { return new TreeMap<Object, Object>(); } else if (type == TreeMap.class) { return new TreeMap<Object, Object>(); } else if (type == LinkedHashMap.class) { return new LinkedHashMap<Object, Object>(); } // TODO return empty Iterable ; see issue 175 //Let's not care about the rest of collections. return null; }
public boolean isValidReturnType(Class clazz) { if (method.getReturnType().isPrimitive() || clazz.isPrimitive()) { return Primitives.primitiveTypeOf(clazz) == Primitives.primitiveTypeOf(method.getReturnType()); } else { return method.getReturnType().isAssignableFrom(clazz); } }
@SuppressWarnings("unchecked") <T> T returnForConsumerLambda(Consumer<T> consumer) { Class<?>[] typeArgs = TypeResolver.resolveRawArguments(Consumer.class, consumer.getClass()); return (T) Primitives.defaultValue(typeArgs[0]); }