import java.io.FileNotFoundException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
+import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
* @param args
* array of argument classes
*/
- static void assertContainsConstructor(Class<?> clazz, Class<?>... args) {
+ static Constructor<?> assertContainsConstructor(Class<?> clazz, Class<?>... args) {
try {
- clazz.getDeclaredConstructor(args);
+ return clazz.getDeclaredConstructor(args);
} catch (NoSuchMethodException e) {
throw new AssertionError("Constructor with args " + Arrays.toString(args) + " does not exists in class "
+ clazz.getSimpleName());
* method name
* @param args
* array of parameter type classes
+ * @return method with given name, return type and parameter types
*/
- static void assertContainsMethod(Class<?> clazz, Class<?> returnType, String name, Class<?>... args) {
+ static Method assertContainsMethod(Class<?> clazz, Class<?> returnType, String name, Class<?>... args) {
try {
Method m = clazz.getDeclaredMethod(name, args);
assertEquals(returnType, m.getReturnType());
+ return m;
} catch (NoSuchMethodException e) {
throw new AssertionError("Method " + name + " with args " + Arrays.toString(args)
+ " does not exists in class " + clazz.getSimpleName());
}
/**
- * Asserts that class containes hashCode, equals and toString methods.
+ * Asserts that class contains hashCode, equals and toString methods.
*
* @param clazz
* class to test
assertContainsMethod(clazz, String.class, "toString");
}
+ /**
+ * Asserts that constructor contains check for illegal argument.
+ *
+ * @param constructor
+ * constructor to invoke
+ * @param errorMsg
+ * expected error message
+ * @param args
+ * constructor arguments
+ * @throws Exception
+ */
+ static void assertContainsRestrictionCheck(Constructor<?> constructor, String errorMsg, Object... args)
+ throws Exception {
+ try {
+ constructor.newInstance(args);
+ fail("constructor invocation should fail");
+ } catch (InvocationTargetException e) {
+ Throwable cause = e.getCause();
+ assertTrue(cause instanceof IllegalArgumentException);
+ assertEquals(errorMsg, cause.getMessage());
+ }
+ }
+
+ /**
+ * Asserts that method contains check for illegal argument.
+ *
+ * @param obj
+ * object to test (can be null, if method is static)
+ * @param method
+ * method to invoke
+ * @param errorMsg
+ * expected error message
+ * @param args
+ * constructor arguments
+ * @throws Exception
+ */
+ static void assertContainsRestrictionCheck(Object obj, Method method, String errorMsg, Object... args)
+ throws Exception {
+ try {
+ method.invoke(obj, args);
+ fail("method invocation should fail");
+ } catch (InvocationTargetException e) {
+ Throwable cause = e.getCause();
+ assertTrue(cause instanceof IllegalArgumentException);
+ assertEquals(errorMsg, cause.getMessage());
+ }
+ }
+
/**
* Asserts that class contains 'public static
* java.util.List<com.google.common.collect.Range<java.lang.Integer>>
* @param clazz
* class to test
*/
- static void assertContainsGetLength(Class<?> clazz) {
+ static void assertContainsGetLengthOrRange(Class<?> clazz, boolean isLength) {
try {
- Method m = clazz.getDeclaredMethod("getLength");
+ Method m = clazz.getDeclaredMethod(isLength ? "length" : "range");
java.lang.reflect.Type returnType = m.getGenericReturnType();
assertTrue("Return type of getLength method must be ParameterizedType",
returnType instanceof ParameterizedType);
args = pRange.getActualTypeArguments();
assertEquals(1, args.length);
- java.lang.reflect.Type integer = args[0];
- assertEquals("class java.lang.Integer", integer.toString());
} catch (NoSuchMethodException e) {
- throw new AssertionError("Method getLength does not exists in class " + clazz.getSimpleName());
+ throw new AssertionError("Method length()/range() does not exists in class " + clazz.getSimpleName());
}
}
return result;
}
- static List<File> getSourceFiles(String path) throws FileNotFoundException {
- final String resPath = BaseCompilationTest.class.getResource(path).getPath();
+ static List<File> getSourceFiles(String path) throws Exception {
+ final URI resPath = BaseCompilationTest.class.getResource(path).toURI();
final File sourcesDir = new File(resPath);
if (sourcesDir.exists()) {
final List<File> sourceFiles = new ArrayList<>();