From 19ce8a7dd7ccd30f83d36bad09794e9d251c6326 Mon Sep 17 00:00:00 2001 From: felmer <felmer> Date: Wed, 12 Nov 2008 09:41:42 +0000 Subject: [PATCH] LMS-641 Move method Util.assertInterfaceWithOnlyVoidMethods from project 'server-common' to ClassUtils. Also tests added. SVN: 8956 --- .../cisd/common/utilities/ClassUtils.java | 22 +++++++++ .../cisd/common/utilities/ClassUtilsTest.java | 45 +++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java b/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java index b19a4eec995..faa706cfdb9 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java @@ -84,6 +84,27 @@ public final class ClassUtils return org.apache.commons.lang.ClassUtils.getAllSuperclasses(object.getClass()); } + /** + * Asserts that the specified class is an interface which has only methods with no return value. + * + * @throws AssertionError if it isn't an interface or at least one method has a return value. + */ + public static void assertInterfaceWithOnlyVoidMethods(Class<?> clazz) + { + assert clazz != null : "Unspecified class."; + assert clazz.isInterface() : "Is not an interface: " + clazz.getName(); + Method[] methods = clazz.getMethods(); + for (Method method : methods) + { + Class<?> returnType = method.getReturnType(); + if (Void.TYPE.equals(returnType) == false) + { + throw new AssertionError("Method " + clazz.getName() + "." + method.getName() + + " has non-void return type: " + returnType.getName()); + } + } + } + /** * Creates a new instance of a class specified by its fully-qualified name. * @@ -479,4 +500,5 @@ public final class ClassUtils throw new CheckedExceptionTunnel(ex); } } + } \ No newline at end of file diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java index 050e28c8765..fcb59e70e15 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java @@ -123,7 +123,52 @@ public final class ClassUtilsTest ClassUtils.create(Appendable.class, MyClass.class.getName(), list); assertSame(list, ((MyClass) appendable).iterable); } + + private static interface AnInterfaceWithOnlyVoidMethods + { + public void exec(); + public void print(String message); + } + + private static interface AnInterfaceWithNotOnlyVoidMethods + { + public int exec(); + public void print(String message); + } + + @Test + public void testAssertInterfaceWithOnlyVoidMethodsWithAnInterfaceWithOnlyVoidMethods() + { + ClassUtils.assertInterfaceWithOnlyVoidMethods(AnInterfaceWithOnlyVoidMethods.class); + } + @Test + public void testAssertInterfaceWithOnlyVoidMethodsWithAnInterfaceWithNotOnlyVoidMethods() + { + try + { + ClassUtils.assertInterfaceWithOnlyVoidMethods(AnInterfaceWithNotOnlyVoidMethods.class); + fail("AssertionError expected"); + } catch (AssertionError e) + { + assertEquals("Method " + AnInterfaceWithNotOnlyVoidMethods.class.getName() + + ".exec has non-void return type: int", e.getMessage()); + } + } + + @Test + public void testAssertInterfaceWithOnlyVoidMethodsWithAClass() + { + try + { + ClassUtils.assertInterfaceWithOnlyVoidMethods(String.class); + fail("AssertionError expected"); + } catch (AssertionError e) + { + assertEquals("Is not an interface: java.lang.String", e.getMessage()); + } + } + @Test public final void testSetFieldValueWithExpectedThrowable() { -- GitLab