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.

RepeatRule.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (C) 2016, Matthias Sohn <matthias.sohn@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.junit;
  11. import java.text.MessageFormat;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import org.junit.rules.TestRule;
  15. import org.junit.runner.Description;
  16. import org.junit.runners.model.Statement;
  17. /**
  18. * {@link org.junit.rules.TestRule} which enables to run the same JUnit test
  19. * repeatedly. Add this rule to the test class
  20. *
  21. * <pre>
  22. * public class MyTest {
  23. * &#64;Rule
  24. * public RepeatRule repeatRule = new RepeatRule();
  25. * ...
  26. * }
  27. * </pre>
  28. *
  29. * and annotate the test to be repeated with the
  30. * {@code @Repeat(n=<repetitions>)} annotation
  31. *
  32. * <pre>
  33. * &#64;Test
  34. * &#64;Repeat(n = 100)
  35. * public void test() {
  36. * ...
  37. * }
  38. * </pre>
  39. *
  40. * then this test will be repeated 100 times. If any test execution fails test
  41. * repetition will be stopped.
  42. */
  43. public class RepeatRule implements TestRule {
  44. private static final Logger LOG = Logger
  45. .getLogger(RepeatRule.class.getName());
  46. /**
  47. * Exception thrown if repeated execution of a test annotated with
  48. * {@code @Repeat} failed.
  49. */
  50. public static class RepeatedTestException extends RuntimeException {
  51. private static final long serialVersionUID = 1L;
  52. /**
  53. * Constructor
  54. *
  55. * @param message
  56. * the error message
  57. */
  58. public RepeatedTestException(String message) {
  59. super(message);
  60. }
  61. /**
  62. * Constructor
  63. *
  64. * @param message
  65. * the error message
  66. * @param cause
  67. * exception causing this exception
  68. */
  69. public RepeatedTestException(String message, Throwable cause) {
  70. super(message, cause);
  71. }
  72. }
  73. private static class RepeatStatement extends Statement {
  74. private final int repetitions;
  75. private boolean abortOnFailure;
  76. private final Statement statement;
  77. private RepeatStatement(int repetitions, boolean abortOnFailure,
  78. Statement statement) {
  79. this.repetitions = repetitions;
  80. this.abortOnFailure = abortOnFailure;
  81. this.statement = statement;
  82. }
  83. @Override
  84. public void evaluate() throws Throwable {
  85. int failures = 0;
  86. for (int i = 0; i < repetitions; i++) {
  87. try {
  88. statement.evaluate();
  89. } catch (Throwable e) {
  90. failures += 1;
  91. RepeatedTestException ex = new RepeatedTestException(
  92. MessageFormat.format(
  93. "Repeated test failed when run for the {0}. time",
  94. Integer.valueOf(i + 1)),
  95. e);
  96. LOG.log(Level.SEVERE, ex.getMessage(), ex);
  97. if (abortOnFailure) {
  98. throw ex;
  99. }
  100. }
  101. }
  102. if (failures > 0) {
  103. RepeatedTestException e = new RepeatedTestException(
  104. MessageFormat.format(
  105. "Test failed {0} times out of {1} repeated executions",
  106. Integer.valueOf(failures),
  107. Integer.valueOf(repetitions)));
  108. LOG.log(Level.SEVERE, e.getMessage(), e);
  109. throw e;
  110. }
  111. }
  112. }
  113. /** {@inheritDoc} */
  114. @Override
  115. public Statement apply(Statement statement, Description description) {
  116. Statement result = statement;
  117. Repeat repeat = description.getAnnotation(Repeat.class);
  118. if (repeat != null) {
  119. int n = repeat.n();
  120. boolean abortOnFailure = repeat.abortOnFailure();
  121. result = new RepeatStatement(n, abortOnFailure, statement);
  122. }
  123. return result;
  124. }
  125. }