Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
156 changed files
with
1,084 additions
and
981 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
...rver/src/test/java/com/vaadin/tests/server/component/AbstractListenerMethodsTestBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.