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.

DedicatedStateTest.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package com.vaadin.server;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertNotEquals;
  4. import static org.junit.Assert.fail;
  5. import java.lang.reflect.Method;
  6. import java.util.HashSet;
  7. import java.util.Set;
  8. import org.junit.Test;
  9. import com.vaadin.navigator.Navigator;
  10. import com.vaadin.tests.VaadinClasses;
  11. import com.vaadin.ui.Composite;
  12. import com.vaadin.ui.LegacyWindow;
  13. import com.vaadin.ui.components.colorpicker.ColorPickerHistory;
  14. import com.vaadin.ui.components.colorpicker.ColorPickerPopup;
  15. import com.vaadin.ui.components.colorpicker.ColorPickerPreview;
  16. import com.vaadin.ui.components.colorpicker.ColorPickerSelect;
  17. import com.vaadin.ui.components.grid.NoSelectionModel;
  18. /**
  19. * @author Vaadin Ltd
  20. *
  21. */
  22. public class DedicatedStateTest {
  23. private static final Set<String> WHITE_LIST = createWhiteList();
  24. @Test
  25. public void checkDedicatedStates() {
  26. VaadinClasses.getAllServerSideClasses().stream().filter(
  27. clazz -> AbstractClientConnector.class.isAssignableFrom(clazz))
  28. .forEach(this::checkState);
  29. }
  30. private void checkState(Class<?> clazz) {
  31. if (WHITE_LIST.contains(clazz.getCanonicalName())
  32. || Composite.class.isAssignableFrom(clazz)) {
  33. return;
  34. }
  35. Method getStateNoArg = getStateNoArg(clazz);
  36. Class<?> stateType = getStateNoArg.getReturnType();
  37. // check that stateType differs from the super class's state type
  38. Class<?> superclass = clazz.getSuperclass();
  39. if (!clazz.equals(AbstractClientConnector.class)
  40. && !superclass.equals(AbstractExtension.class)) {
  41. assertNotEquals(
  42. "Class " + clazz
  43. + " has the same state type as its super class "
  44. + clazz.getSuperclass(),
  45. stateType, getStateNoArg(superclass).getReturnType());
  46. }
  47. try {
  48. Method getStateOneArg = clazz.getDeclaredMethod("getState",
  49. boolean.class);
  50. assertEquals(stateType, getStateOneArg.getReturnType());
  51. } catch (NoSuchMethodException e) {
  52. fail("Class " + clazz
  53. + " doesn't have its own getState(boolean) method");
  54. } catch (SecurityException e) {
  55. throw new RuntimeException(e);
  56. }
  57. }
  58. private Method getStateNoArg(Class<?> clazz) {
  59. try {
  60. return clazz.getDeclaredMethod("getState");
  61. } catch (NoSuchMethodException e) {
  62. fail("Class " + clazz + " doesn't have its own getState() method");
  63. return null;
  64. } catch (SecurityException e) {
  65. throw new RuntimeException(e);
  66. }
  67. }
  68. private static Set<String> createWhiteList() {
  69. Set<String> list = new HashSet<>();
  70. list.add(AbstractExtension.class.getCanonicalName());
  71. list.add(Navigator.EmptyView.class.getCanonicalName());
  72. list.add(ColorPickerHistory.class.getCanonicalName());
  73. list.add(ColorPickerPopup.class.getCanonicalName());
  74. list.add(ColorPickerPreview.class.getCanonicalName());
  75. list.add(ColorPickerSelect.class.getCanonicalName());
  76. list.add(NoSelectionModel.class.getCanonicalName());
  77. list.add(LegacyWindow.class.getCanonicalName());
  78. return list;
  79. }
  80. }