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.

ParameterizedTB3Runner.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright 2000-2014 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.tb3;
  17. import java.lang.reflect.Method;
  18. import java.lang.reflect.Modifier;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.LinkedHashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.junit.runners.Parameterized.Parameters;
  25. import org.junit.runners.model.FrameworkMethod;
  26. import org.junit.runners.model.InitializationError;
  27. /**
  28. * TestBench test runner which supports static @Parameters annotated methods
  29. * providing parameters for the corresponding setter.
  30. * <p>
  31. * {@code @Parameters public static Collection<String> getThemes() } creates one
  32. * permutation for each value returned by {@code getThemes()}. The value is
  33. * automatically assigned to the test instance using {@code setTheme(String)}
  34. * before invoking the test method
  35. *
  36. * @author Vaadin Ltd
  37. */
  38. public class ParameterizedTB3Runner extends TB3Runner {
  39. public ParameterizedTB3Runner(Class<?> klass) throws InitializationError {
  40. super(klass);
  41. }
  42. @Override
  43. protected List<FrameworkMethod> computeTestMethods() {
  44. List<FrameworkMethod> methods = super.computeTestMethods();
  45. Map<Method, Collection<String>> parameters = new LinkedHashMap<Method, Collection<String>>();
  46. // Find all @Parameters methods and invoke them to find out permutations
  47. for (Method m : getTestClass().getJavaClass().getMethods()) {
  48. Parameters p = m.getAnnotation(Parameters.class);
  49. if (p == null) {
  50. continue;
  51. }
  52. if (!m.getName().startsWith("get") || !m.getName().endsWith("s")) {
  53. throw new IllegalStateException(
  54. "Method "
  55. + m.getName()
  56. + " is annotated with @Parameter but is not named getSomeThings() as it should");
  57. }
  58. if (m.getParameterTypes().length != 0) {
  59. throw new IllegalStateException(
  60. "Method "
  61. + m.getName()
  62. + " annotated with @Parameter should not have any arguments");
  63. }
  64. if (!Modifier.isStatic(m.getModifiers())) {
  65. throw new IllegalStateException("Method " + m.getName()
  66. + " annotated with @Parameter must be static");
  67. }
  68. // getThemes -> setTheme
  69. String setter = "set" + m.getName().substring("get".length());
  70. setter = setter.substring(0, setter.length() - 1);
  71. // property = property.substring(0, 1).toLowerCase()
  72. // + property.substring(1);
  73. Method setterMethod;
  74. try {
  75. setterMethod = getTestClass().getJavaClass().getMethod(setter,
  76. String.class);
  77. } catch (Exception e) {
  78. throw new IllegalStateException("No setter " + setter
  79. + " found in "
  80. + getTestClass().getJavaClass().getName(), e);
  81. }
  82. Collection<String> values;
  83. try {
  84. values = (Collection<String>) m.invoke(null);
  85. if (!values.isEmpty()) {
  86. // Ignore any empty collections to allow e.g. integration
  87. // tests to use "/demo" path by default without adding that
  88. // to the screenshot name
  89. parameters.put(setterMethod, values);
  90. }
  91. } catch (Exception e) {
  92. throw new IllegalStateException("The setter " + m.getName()
  93. + " could not be invoked", e);
  94. }
  95. }
  96. // Add method permutations for all @Parameters
  97. for (Method setter : parameters.keySet()) {
  98. List<FrameworkMethod> newMethods = new ArrayList<FrameworkMethod>();
  99. for (FrameworkMethod m : methods) {
  100. if (!(m instanceof TBMethod)) {
  101. System.err.println("Unknown method type: "
  102. + m.getClass().getName());
  103. newMethods.add(m);
  104. continue;
  105. }
  106. // testFoo
  107. // testBar
  108. // ->
  109. // testFoo[valo]
  110. // testFoo[runo]
  111. // testBar[valo]
  112. // testBar[runo]
  113. for (final String value : parameters.get(setter)) {
  114. newMethods.add(new TBMethodWithBefore((TBMethod) m, setter,
  115. value));
  116. }
  117. }
  118. // Update methods so next parameters will use all expanded methods
  119. methods = newMethods;
  120. }
  121. return methods;
  122. }
  123. public static class TBMethodWithBefore extends TBMethod {
  124. private Method setter;
  125. private String value;
  126. private TBMethod parent;
  127. public TBMethodWithBefore(TBMethod m, Method setter, String value) {
  128. super(m.getMethod(), m.getCapabilities());
  129. parent = m;
  130. this.setter = setter;
  131. this.value = value;
  132. }
  133. @Override
  134. public Object invokeExplosively(Object target, Object... params)
  135. throws Throwable {
  136. setter.invoke(target, value);
  137. return parent.invokeExplosively(target, params);
  138. }
  139. @Override
  140. public String getName() {
  141. return parent.getName() + "[" + value + "]";
  142. };
  143. }
  144. }