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 4.9KB

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