You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RemoveListenersDeprecatedTest.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package com.vaadin.server;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertNotNull;
  4. import static org.junit.Assert.assertTrue;
  5. import java.lang.reflect.Method;
  6. import java.lang.reflect.Modifier;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.function.Predicate;
  10. import java.util.regex.Pattern;
  11. import org.junit.Test;
  12. import com.vaadin.data.provider.AbstractDataProvider;
  13. import com.vaadin.event.EventRouter;
  14. import com.vaadin.event.MethodEventSource;
  15. import com.vaadin.shared.Registration;
  16. import com.vaadin.tests.VaadinClasses;
  17. public class RemoveListenersDeprecatedTest {
  18. private static final List<Predicate<Method>> ALLOW_REMOVE_LISTENER = new ArrayList<>();
  19. static {
  20. ALLOW_REMOVE_LISTENER.add(
  21. RemoveListenersDeprecatedTest::acceptAbstarctClientConnectorRemoveMethods);
  22. ALLOW_REMOVE_LISTENER
  23. .add(RemoveListenersDeprecatedTest::acceptAbstractDataProvider);
  24. ALLOW_REMOVE_LISTENER
  25. .add(RemoveListenersDeprecatedTest::acceptMethodEventSource);
  26. }
  27. @Test
  28. public void allRemoveListenerMethodsMarkedAsDeprecated() {
  29. Pattern removePattern = Pattern.compile("remove.*Listener");
  30. Pattern addPattern = Pattern.compile("add.*Listener");
  31. int count = 0;
  32. for (Class<? extends Object> serverClass : VaadinClasses
  33. .getAllServerSideClasses()) {
  34. count++;
  35. if (serverClass.equals(EventRouter.class)) {
  36. continue;
  37. }
  38. for (Method method : serverClass.getDeclaredMethods()) {
  39. if (Modifier.isPrivate(method.getModifiers())) {
  40. continue;
  41. }
  42. if (addPattern.matcher(method.getName()).matches()
  43. && method.getAnnotation(Deprecated.class) == null) {
  44. Class<?> returnType = method.getReturnType();
  45. assertEquals("Method " + method.getName()
  46. + " is not deprectated in class "
  47. + serverClass.getName()
  48. + " and doesn't return a Registration object",
  49. Registration.class, returnType);
  50. }
  51. if (ALLOW_REMOVE_LISTENER.stream()
  52. .anyMatch(predicate -> predicate.test(method))) {
  53. continue;
  54. }
  55. if (removePattern.matcher(method.getName()).matches()) {
  56. assertNotNull(
  57. "Method " + method.getName() + " in class "
  58. + serverClass.getName()
  59. + " has not been marked as deprecated.",
  60. method.getAnnotation(Deprecated.class));
  61. }
  62. }
  63. }
  64. assertTrue(count > 0);
  65. }
  66. private static boolean acceptMethodEventSource(Method method) {
  67. return method.getDeclaringClass().equals(MethodEventSource.class)
  68. && method.getParameterCount() == 2;
  69. }
  70. private static boolean acceptAbstarctClientConnectorRemoveMethods(
  71. Method method) {
  72. if (method.getDeclaringClass().equals(AbstractClientConnector.class)) {
  73. if (method.getParameterCount() == 2) {
  74. return true;
  75. } else if (method.getParameterCount() == 0) {
  76. return false;
  77. } else {
  78. return method.getParameterTypes()[0].equals(String.class);
  79. }
  80. }
  81. return false;
  82. }
  83. private static boolean acceptAbstractDataProvider(Method method) {
  84. return method.getDeclaringClass().equals(AbstractDataProvider.class)
  85. && method.getParameterCount() == 2;
  86. }
  87. }