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.

DeclarativeTestBase.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.design;
  17. import java.beans.BeanInfo;
  18. import java.beans.Introspector;
  19. import java.beans.PropertyDescriptor;
  20. import java.lang.reflect.Method;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import org.junit.Assert;
  24. import com.vaadin.shared.Connector;
  25. import com.vaadin.ui.Component;
  26. import com.vaadin.ui.Flash;
  27. public abstract class DeclarativeTestBase<T extends Component>
  28. extends DeclarativeTestBaseBase<T> {
  29. private static boolean debug = false;
  30. private final Map<Class<?>, EqualsAsserter<?>> comparators = new HashMap<Class<?>, EqualsAsserter<?>>();
  31. private static EqualsAsserter standardEqualsComparator = new EqualsAsserter<Object>() {
  32. @Override
  33. public void assertObjectEquals(Object o1, Object o2) {
  34. Assert.assertEquals(o1, o2);
  35. }
  36. };
  37. public class IntrospectorEqualsAsserter<T> implements EqualsAsserter<T> {
  38. private Class<T> c;
  39. public IntrospectorEqualsAsserter(Class<T> c) {
  40. this.c = c;
  41. }
  42. @Override
  43. public void assertObjectEquals(T o1, T o2) {
  44. try {
  45. BeanInfo bi = Introspector.getBeanInfo(c);
  46. for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
  47. Method readMethod = pd.getReadMethod();
  48. Method writeMethod = pd.getWriteMethod();
  49. if (readMethod == null || writeMethod == null) {
  50. continue;
  51. }
  52. // Needed to access public properties inherited from a
  53. // nonpublic superclass, see #17425
  54. readMethod.setAccessible(true);
  55. writeMethod.setAccessible(true);
  56. if (Connector.class.isAssignableFrom(c)
  57. && readMethod.getName().equals("getParent")) {
  58. // Hack to break cycles in the connector hierarchy
  59. continue;
  60. }
  61. try {
  62. c.getDeclaredMethod(readMethod.getName());
  63. } catch (Exception e) {
  64. // Not declared in this class, will be tested by parent
  65. // class tester
  66. if (debug) {
  67. System.out.println("Skipped " + c.getSimpleName()
  68. + "." + readMethod.getName());
  69. }
  70. continue;
  71. }
  72. if (debug) {
  73. System.out.println("Testing " + c.getSimpleName() + "."
  74. + readMethod.getName());
  75. }
  76. Object v1 = readMethod.invoke(o1);
  77. Object v2 = readMethod.invoke(o2);
  78. assertEquals(pd.getDisplayName(), v1, v2);
  79. }
  80. } catch (Exception e) {
  81. throw new RuntimeException(e);
  82. }
  83. }
  84. }
  85. {
  86. comparators.put(Flash.class,
  87. new IntrospectorEqualsAsserter<Flash>(Flash.class) {
  88. @Override
  89. public void assertObjectEquals(Flash o1, Flash o2) {
  90. super.assertObjectEquals(o1, o2);
  91. assertEquals("parameterNames", o1.getParameterNames(),
  92. o2.getParameterNames());
  93. for (String name : o1.getParameterNames()) {
  94. assertEquals("Parameter " + name,
  95. o1.getParameter(name),
  96. o2.getParameter(name));
  97. }
  98. }
  99. });
  100. }
  101. @Override
  102. protected EqualsAsserter getComparator(Class c) {
  103. com.vaadin.tests.design.DeclarativeTestBaseBase.EqualsAsserter<?> comp = comparators
  104. .get(c);
  105. if (comp == null) {
  106. if (c.isEnum()) {
  107. return standardEqualsComparator;
  108. }
  109. if (debug) {
  110. System.out.println("No comparator found for " + c.getName()
  111. + ". Using introspector.");
  112. }
  113. return new IntrospectorEqualsAsserter<T>(c);
  114. }
  115. return comp;
  116. }
  117. }