diff options
author | Aleksi Hietanen <aleksi@vaadin.com> | 2016-09-23 13:14:52 +0300 |
---|---|---|
committer | Denis Anisimov <denis@vaadin.com> | 2016-09-29 13:14:35 +0000 |
commit | 2bee988272657df4d886ea79b04be9fbc54d3928 (patch) | |
tree | 6825ec60243faac7fd4114928090e115e2e11d01 /compatibility-server/src/test | |
parent | 599b61bc8598db35fa111880dd4db57f9da2adda (diff) | |
download | vaadin-framework-2bee988272657df4d886ea79b04be9fbc54d3928.tar.gz vaadin-framework-2bee988272657df4d886ea79b04be9fbc54d3928.zip |
Unify listeners on the server side
This patch updates server side add*Listener methods to return
a registration object and deprecates their corresponding
remove*Listener methods.
Additionally this patch removes add/removeListener methods which were
deprecated in 7.0.
Change-Id: I26ac5f11882512288fbbf601c7cb2aaff653ec76
Diffstat (limited to 'compatibility-server/src/test')
-rw-r--r-- | compatibility-server/src/test/java/com/vaadin/tests/server/component/AbstractListenerMethodsTestBase.java | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/AbstractListenerMethodsTestBase.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/AbstractListenerMethodsTestBase.java new file mode 100644 index 0000000000..0d6166b2d9 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/AbstractListenerMethodsTestBase.java @@ -0,0 +1,181 @@ +package com.vaadin.tests.server.component; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +import org.easymock.EasyMock; +import org.junit.Assert; + +import com.vaadin.tests.VaadinClasses; +import com.vaadin.ui.Component; + +public abstract class AbstractListenerMethodsTestBase { + + public static void main(String[] args) { + findAllListenerMethods(); + } + + private static void findAllListenerMethods() { + Set<Class<?>> classes = new HashSet<>(); + for (Class<?> c : VaadinClasses.getAllServerSideClasses()) { + while (c != null && c.getName().startsWith("com.vaadin.")) { + classes.add(c); + c = c.getSuperclass(); + } + } + + for (Class<?> c : classes) { + boolean found = false; + for (Method m : c.getDeclaredMethods()) { + String methodName = m.getName(); + if (methodName.startsWith("add") + && methodName.endsWith("Listener") + && !"addListener".equals(methodName)) { + if (m.getParameterTypes().length != 1) { + continue; + } + String packageName = "com.vaadin.tests.server"; + if (Component.class.isAssignableFrom(c)) { + packageName += ".component." + + c.getSimpleName().toLowerCase(); + continue; + } + + if (!found) { + found = true; + System.out.println("package " + packageName + ";"); + + System.out.println("import " + + AbstractListenerMethodsTestBase.class + .getName() + + ";"); + System.out.println("import " + c.getName() + ";"); + System.out + .println( + "public class " + c.getSimpleName() + + "Listeners extends " + + AbstractListenerMethodsTestBase.class + .getSimpleName() + + " {"); + } + + String listenerClassName = m.getParameterTypes()[0] + .getSimpleName(); + String eventClassName = listenerClassName + .replaceFirst("Listener$", "Event"); + System.out.println("public void test" + listenerClassName + + "() throws Exception {"); + System.out.println(" testListener(" + c.getSimpleName() + + ".class, " + eventClassName + ".class, " + + listenerClassName + ".class);"); + System.out.println("}"); + } + } + if (found) { + System.out.println("}"); + System.out.println(); + } + } + } + + protected void testListenerAddGetRemove(Class<?> testClass, + Class<?> eventClass, Class<?> listenerClass) throws Exception { + // Create a component for testing + Object c = testClass.newInstance(); + testListenerAddGetRemove(testClass, eventClass, listenerClass, c); + + } + + protected void testListenerAddGetRemove(Class<?> cls, Class<?> eventClass, + Class<?> listenerClass, Object c) throws Exception { + + Object mockListener1 = EasyMock.createMock(listenerClass); + Object mockListener2 = EasyMock.createMock(listenerClass); + + // Verify we start from no listeners + verifyListeners(c, eventClass); + + // Add one listener and verify + addListener(c, mockListener1, listenerClass); + verifyListeners(c, eventClass, mockListener1); + + // Add another listener and verify + addListener(c, mockListener2, listenerClass); + verifyListeners(c, eventClass, mockListener1, mockListener2); + + // Ensure we can fetch using parent class also + if (eventClass.getSuperclass() != null) { + verifyListeners(c, eventClass.getSuperclass(), mockListener1, + mockListener2); + } + + // Remove the first and verify + removeListener(c, mockListener1, listenerClass); + verifyListeners(c, eventClass, mockListener2); + + // Remove the remaining and verify + removeListener(c, mockListener2, listenerClass); + verifyListeners(c, eventClass); + + } + + private void removeListener(Object c, Object listener, + Class<?> listenerClass) throws IllegalArgumentException, + IllegalAccessException, InvocationTargetException, + SecurityException, NoSuchMethodException { + Method method = getRemoveListenerMethod(c.getClass(), listenerClass); + method.invoke(c, listener); + + } + + private void addListener(Object c, Object listener1, Class<?> listenerClass) + throws IllegalArgumentException, IllegalAccessException, + InvocationTargetException, SecurityException, + NoSuchMethodException { + Method method = getAddListenerMethod(c.getClass(), listenerClass); + method.invoke(c, listener1); + } + + private Collection<?> getListeners(Object c, Class<?> eventType) + throws IllegalArgumentException, IllegalAccessException, + InvocationTargetException, SecurityException, + NoSuchMethodException { + Method method = getGetListenersMethod(c.getClass()); + return (Collection<?>) method.invoke(c, eventType); + } + + private Method getGetListenersMethod(Class<? extends Object> cls) + throws SecurityException, NoSuchMethodException { + return cls.getMethod("getListeners", Class.class); + } + + private Method getAddListenerMethod(Class<?> cls, Class<?> listenerClass) + throws SecurityException, NoSuchMethodException { + return cls.getMethod("add" + listenerClass.getSimpleName(), + listenerClass); + + } + + private Method getRemoveListenerMethod(Class<?> cls, Class<?> listenerClass) + throws SecurityException, NoSuchMethodException { + return cls.getMethod("remove" + listenerClass.getSimpleName(), + listenerClass); + + } + + private void verifyListeners(Object c, Class<?> eventClass, + Object... expectedListeners) throws IllegalArgumentException, + SecurityException, IllegalAccessException, + InvocationTargetException, NoSuchMethodException { + Collection<?> registeredListeners = getListeners(c, eventClass); + Assert.assertEquals("Number of listeners", expectedListeners.length, + registeredListeners.size()); + + Assert.assertArrayEquals(expectedListeners, + registeredListeners.toArray()); + + } +}
\ No newline at end of file |