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.

StateGetDoesNotMarkDirtyTest.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package com.vaadin.tests.server.component;
  2. import static org.junit.Assert.assertTrue;
  3. import java.lang.reflect.Constructor;
  4. import java.lang.reflect.InvocationTargetException;
  5. import java.lang.reflect.Method;
  6. import java.lang.reflect.Modifier;
  7. import java.util.Arrays;
  8. import java.util.HashSet;
  9. import java.util.Locale;
  10. import java.util.Set;
  11. import org.junit.Before;
  12. import org.junit.Test;
  13. import org.mockito.Mockito;
  14. import com.vaadin.server.VaadinSession;
  15. import com.vaadin.tests.VaadinClasses;
  16. import com.vaadin.ui.Component;
  17. import com.vaadin.ui.ComponentRootSetter;
  18. import com.vaadin.ui.Composite;
  19. import com.vaadin.ui.ConnectorTracker;
  20. import com.vaadin.ui.Label;
  21. import com.vaadin.ui.UI;
  22. public class StateGetDoesNotMarkDirtyTest {
  23. private final Set<String> excludedMethods = new HashSet<>();
  24. @Before
  25. public void setUp() {
  26. excludedMethods.add(Label.class.getName() + "getDataProviderValue");
  27. excludedMethods.add("getConnectorId");
  28. excludedMethods.add("getContent");
  29. excludedMethods.add("com.vaadin.ui.Grid:getSelectAllCheckBoxVisible");
  30. excludedMethods.add("com.vaadin.ui.TreeGrid:getDataProvider");
  31. }
  32. @Test
  33. public void testGetDoesntMarkStateDirty() throws Exception {
  34. int count = 0;
  35. for (Class<? extends Component> clazz : VaadinClasses.getComponents()) {
  36. if (clazz.isInterface()
  37. || Modifier.isAbstract(clazz.getModifiers())) {
  38. continue;
  39. }
  40. Component newInstance = construct(clazz);
  41. if (newInstance == null) {
  42. continue;
  43. }
  44. count++;
  45. prepareMockUI(newInstance);
  46. Set<Method> methods = new HashSet<>();
  47. methods.addAll(Arrays.asList(clazz.getMethods()));
  48. methods.addAll(Arrays.asList(clazz.getDeclaredMethods()));
  49. for (Method method : methods) {
  50. try {
  51. if (method.getName().startsWith("is")
  52. || method.getName().startsWith("get")) {
  53. if (method.getName().startsWith("getState")) {
  54. continue;
  55. }
  56. if (method.getParameterTypes().length > 0) {
  57. // usually getters do not have params, if they have
  58. // we still wouldnt know what to put into
  59. continue;
  60. }
  61. if (excludedMethods.contains(
  62. clazz.getName() + ":" + method.getName())) {
  63. // blacklisted method for specific classes
  64. continue;
  65. }
  66. if (excludedMethods.contains(method.getName())) {
  67. // blacklisted method for all classes
  68. continue;
  69. }
  70. // just to make sure we can invoke it
  71. method.setAccessible(true);
  72. try {
  73. method.invoke(newInstance);
  74. } catch (InvocationTargetException e) {
  75. if (e.getCause() instanceof UnsupportedOperationException) {
  76. // Overridden getter which is not supposed to be
  77. // called
  78. } else {
  79. throw e;
  80. }
  81. }
  82. }
  83. } catch (Exception e) {
  84. System.err.println("problem with method " + clazz.getName()
  85. + "# " + method.getName());
  86. e.printStackTrace();
  87. throw e;
  88. }
  89. }
  90. }
  91. assertTrue(count > 0);
  92. }
  93. private void prepareMockUI(Component newInstance) {
  94. UI ui = mockUI();
  95. ConnectorTracker connectorTracker = ui.getConnectorTracker();
  96. Mockito.doThrow(new RuntimeException("getState(true) called in getter"))
  97. .when(connectorTracker).markDirty(newInstance);
  98. newInstance.setParent(null);
  99. newInstance.setParent(ui);
  100. }
  101. private UI mockUI() {
  102. UI ui = Mockito.mock(UI.class);
  103. Mockito.when(ui.getLocale()).thenReturn(Locale.ENGLISH);
  104. ConnectorTracker connectorTracker = Mockito
  105. .mock(ConnectorTracker.class);
  106. Mockito.when(ui.getConnectorTracker()).thenReturn(connectorTracker);
  107. return ui;
  108. }
  109. private Component construct(Class<? extends Component> clazz) {
  110. try {
  111. Constructor<? extends Component> declaredConstructor = clazz
  112. .getDeclaredConstructor();
  113. declaredConstructor.setAccessible(true);
  114. Component component = declaredConstructor.newInstance();
  115. if (component instanceof UI) {
  116. return component;
  117. }
  118. if (component.getClass().equals(Composite.class)) {
  119. // Plain Composite needs a root.
  120. ComponentRootSetter.setRoot(component, new Label());
  121. }
  122. emulateAttach(component);
  123. return component;
  124. } catch (NoSuchMethodException e) {
  125. // no default CTOR, skip
  126. return null;
  127. } catch (InstantiationException | IllegalAccessException
  128. | IllegalArgumentException | InvocationTargetException e) {
  129. throw new RuntimeException(e);
  130. }
  131. }
  132. private void emulateAttach(Component component) {
  133. UI ui = mockUI();
  134. VaadinSession session = Mockito.mock(VaadinSession.class);
  135. Mockito.when(session.hasLock()).thenReturn(true);
  136. Mockito.when(ui.getSession()).thenReturn(session);
  137. component.setParent(ui);
  138. component.attach();
  139. }
  140. }