Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

TB3Runner.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 2000-2013 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.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.LinkedList;
  21. import java.util.List;
  22. import java.util.concurrent.ExecutorService;
  23. import java.util.concurrent.Executors;
  24. import org.junit.Test;
  25. import org.junit.runners.BlockJUnit4ClassRunner;
  26. import org.junit.runners.Parameterized;
  27. import org.junit.runners.model.FrameworkMethod;
  28. import org.junit.runners.model.InitializationError;
  29. import org.junit.runners.model.Statement;
  30. import org.openqa.selenium.remote.DesiredCapabilities;
  31. import com.vaadin.tests.tb3.AbstractTB3Test.BrowserUtil;
  32. /**
  33. * This runner is loosely based on FactoryTestRunner by Ted Young
  34. * (http://tedyoung.me/2011/01/23/junit-runtime-tests-custom-runners/). The
  35. * generated test names give information about the parameters used (unlike
  36. * {@link Parameterized}).
  37. *
  38. * @since 7.1
  39. */
  40. public class TB3Runner extends BlockJUnit4ClassRunner {
  41. /**
  42. * This is the total limit of actual JUnit test instances run in parallel
  43. */
  44. private static final int MAX_CONCURRENT_TESTS = 50;
  45. /**
  46. * This is static so it is shared by all tests running concurrently on the
  47. * same machine and thus can limit the number of threads in use.
  48. */
  49. private static final ExecutorService service = Executors
  50. .newFixedThreadPool(MAX_CONCURRENT_TESTS);
  51. public TB3Runner(Class<?> klass) throws InitializationError {
  52. super(klass);
  53. setScheduler(new ParallelScheduler(service));
  54. }
  55. @Override
  56. protected List<FrameworkMethod> computeTestMethods() {
  57. List<FrameworkMethod> tests = new LinkedList<FrameworkMethod>();
  58. if (!AbstractTB3Test.class.isAssignableFrom(getTestClass()
  59. .getJavaClass())) {
  60. throw new RuntimeException(getClass().getName() + " only supports "
  61. + AbstractTB3Test.class.getName());
  62. }
  63. try {
  64. AbstractTB3Test testClassInstance = (AbstractTB3Test) getTestClass()
  65. .getOnlyConstructor().newInstance();
  66. Collection<DesiredCapabilities> desiredCapabilites = testClassInstance
  67. .getBrowsersToTest();
  68. if (testClassInstance.runLocally()) {
  69. desiredCapabilites = new ArrayList<DesiredCapabilities>();
  70. desiredCapabilites.add(BrowserUtil
  71. .firefox(MultiBrowserTest.TESTED_FIREFOX_VERSION));
  72. }
  73. for (DesiredCapabilities capabilities : desiredCapabilites) {
  74. // Find any methods marked with @Test.
  75. for (FrameworkMethod m : getTestClass().getAnnotatedMethods(
  76. Test.class)) {
  77. tests.add(new TB3Method(m.getMethod(), capabilities));
  78. }
  79. }
  80. } catch (Exception e) {
  81. throw new RuntimeException("Error retrieving browsers to run on", e);
  82. }
  83. return tests;
  84. }
  85. /*
  86. * (non-Javadoc)
  87. *
  88. * @see
  89. * org.junit.runners.BlockJUnit4ClassRunner#withBefores(org.junit.runners
  90. * .model.FrameworkMethod, java.lang.Object,
  91. * org.junit.runners.model.Statement)
  92. */
  93. @Override
  94. protected Statement withBefores(final FrameworkMethod method,
  95. final Object target, Statement statement) {
  96. if (!(method instanceof TB3Method)) {
  97. throw new RuntimeException("Unexpected method type "
  98. + method.getClass().getName() + ", expected TB3Method");
  99. }
  100. final TB3Method tb3method = (TB3Method) method;
  101. // setDesiredCapabilities before running the real @Befores (which use
  102. // capabilities)
  103. final Statement realBefores = super.withBefores(method, target,
  104. statement);
  105. return new Statement() {
  106. @Override
  107. public void evaluate() throws Throwable {
  108. ((AbstractTB3Test) target)
  109. .setDesiredCapabilities(tb3method.capabilities);
  110. try {
  111. realBefores.evaluate();
  112. } catch (Throwable t) {
  113. // Give the test a chance to e.g. produce an error
  114. // screenshot before failing the test by re-throwing the
  115. // exception
  116. ((AbstractTB3Test) target).onUncaughtException(t);
  117. throw t;
  118. }
  119. }
  120. };
  121. }
  122. private static class TB3Method extends FrameworkMethod {
  123. private DesiredCapabilities capabilities;
  124. public TB3Method(Method method, DesiredCapabilities capabilities) {
  125. super(method);
  126. this.capabilities = capabilities;
  127. }
  128. @Override
  129. public Object invokeExplosively(final Object target, Object... params)
  130. throws Throwable {
  131. // Executes the test method with the supplied parameters
  132. return super.invokeExplosively(target);
  133. }
  134. @Override
  135. public String getName() {
  136. return String.format("%s[%s]", getMethod().getName(),
  137. BrowserUtil.getUniqueIdentifier(capabilities));
  138. }
  139. }
  140. }