Skip to content
Snippets Groups Projects
Commit 19ce8a7d authored by felmer's avatar felmer
Browse files

LMS-641 Move method Util.assertInterfaceWithOnlyVoidMethods from project...

LMS-641 Move method Util.assertInterfaceWithOnlyVoidMethods from project 'server-common' to ClassUtils. Also tests added.

SVN: 8956
parent 209a4a58
No related branches found
No related tags found
No related merge requests found
...@@ -84,6 +84,27 @@ public final class ClassUtils ...@@ -84,6 +84,27 @@ public final class ClassUtils
return org.apache.commons.lang.ClassUtils.getAllSuperclasses(object.getClass()); 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. * Creates a new instance of a class specified by its fully-qualified name.
* *
...@@ -479,4 +500,5 @@ public final class ClassUtils ...@@ -479,4 +500,5 @@ public final class ClassUtils
throw new CheckedExceptionTunnel(ex); throw new CheckedExceptionTunnel(ex);
} }
} }
} }
\ No newline at end of file
...@@ -123,7 +123,52 @@ public final class ClassUtilsTest ...@@ -123,7 +123,52 @@ public final class ClassUtilsTest
ClassUtils.create(Appendable.class, MyClass.class.getName(), list); ClassUtils.create(Appendable.class, MyClass.class.getName(), list);
assertSame(list, ((MyClass) appendable).iterable); 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 @Test
public final void testSetFieldValueWithExpectedThrowable() public final void testSetFieldValueWithExpectedThrowable()
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment