org.mockito.internal.util.Primitives Java Examples

The following examples show how to use org.mockito.internal.util.Primitives. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AdHocThrottleTest.java    From emodb with Apache License 2.0 6 votes vote down vote up
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;
                }
            });
}
 
Example #2
Source File: TestReflections.java    From sarl with Apache License 2.0 5 votes vote down vote up
private static boolean isValidArgs(boolean varargs, Object[] args, Class<?>[] params) {
	for (int i = 0; i < args.length; ++i) {
		if (i >= params.length) {
			return false;
		}
		if (args[i] == null) {
			if (params[i].isPrimitive()) {
				return false;
			}
		} else if ((!(params[i].isInstance(args[i]))) && varargs && i == params.length - 1) {
			Class<?> componentType = params[i].getComponentType();

			Class<?>[] newParams = new Class[args.length - params.length + 1];
			for (int j = 0; j < newParams.length; ++j) {
				newParams[j] = componentType;
			}

			Object[] newArgs = new Object[newParams.length];
			for (int j = 0; j < newArgs.length; ++j, ++i) {
				newArgs[j] = args[i];
			}

			return isValidArgs(false, newArgs, newParams);
		} else if (!(params[i].isInstance(args[i]))) {
			if (Primitives.isPrimitiveOrWrapper(params[i])) {
				if (!Objects.equal(
						Primitives.primitiveTypeOf(params[i]),
						Primitives.primitiveTypeOf(args[i].getClass()))) {
					return false;
				}
			} else {
				return false;
			}
		}
	}
	return true;
}
 
Example #3
Source File: Invocation.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public boolean isValidReturnType(Class clazz) {
    if (method.getReturnType().isPrimitive()) {
        return Primitives.primitiveTypeOf(clazz) == method.getReturnType();
    } else {
        return method.getReturnType().isAssignableFrom(clazz);
    }
}
 
Example #4
Source File: ReturnsEmptyValues.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #5
Source File: ReturnsEmptyValues.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #6
Source File: MethodInfo.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example #7
Source File: QpidByteBufferTest.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
private void testPutGet(final Class<?> primitiveTargetClass, final boolean unsigned, final Object value) throws Exception
{
    int size = sizeof(primitiveTargetClass);

    _parent.position(1);
    _parent.limit(size + 1);

    _slicedBuffer = _parent.slice();
    _parent.limit(_parent.capacity());

    assertEquals("Unexpected position ", (long) 0, (long) _slicedBuffer.position());
    assertEquals("Unexpected limit ", (long) size, (long) _slicedBuffer.limit());
    assertEquals("Unexpected capacity ", (long) size, (long) _slicedBuffer.capacity());

    String methodSuffix = getMethodSuffix(primitiveTargetClass, unsigned);
    Method put = _slicedBuffer.getClass().getMethod("put" + methodSuffix, Primitives.primitiveTypeOf(value.getClass()));
    Method get = _slicedBuffer.getClass().getMethod("get" + methodSuffix);


    _slicedBuffer.mark();
    QpidByteBuffer rv = (QpidByteBuffer) put.invoke(_slicedBuffer, value);
    assertEquals("Unexpected builder return value for type " + methodSuffix, _slicedBuffer, rv);

    assertEquals("Unexpected position for type " + methodSuffix,
                        (long) size,
                        (long) _slicedBuffer.position());

    try
    {
        invokeMethod(put, value);
        fail("BufferOverflowException should be thrown for put with insufficient room for " + methodSuffix);
    }
    catch (BufferOverflowException e)
    {
        // pass
    }

    _slicedBuffer.reset();

    assertEquals("Unexpected position after reset", (long) 0, (long) _slicedBuffer.position());

    Object retrievedValue = get.invoke(_slicedBuffer);
    assertEquals("Unexpected value retrieved from get method for " + methodSuffix, value, retrievedValue);
    try
    {
        invokeMethod(get);
        fail("BufferUnderflowException not thrown for get with insufficient room for " + methodSuffix);
    }
    catch (BufferUnderflowException ite)
    {
        // pass
    }
    _slicedBuffer.dispose();
    _slicedBuffer = null;
}
 
Example #8
Source File: LambdaAwareHandyReturnValues.java    From mockito-java8 with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private <T> T internalReturnForLambda(Object consumer, Class consumerType) {
    Class<?>[] typeArgs = TypeResolver.resolveRawArguments(consumerType, consumer.getClass());
    return (T) Primitives.defaultValue(typeArgs[0]);
}