Skip to content

Package: MultiTryTestRule$1

MultiTryTestRule$1

nameinstructionbranchcomplexitylinemethod
evaluate()
M: 41 C: 13
24%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 7 C: 4
36%
M: 0 C: 1
100%
{...}
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2019 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.test.common;
15:
16: import java.text.MessageFormat;
17:
18: import org.junit.rules.TestRule;
19: import org.junit.runner.Description;
20: import org.junit.runners.model.Statement;
21:
22: /**
23: * This JUnit rule allows to try unit tests a given number of times. If at least one of the tries succeeds, the test
24: * counts as passed. Every try executes all {@link org.junit.Before Before} and {@link org.junit.After After} methods
25: * of the test. However, it does not re-execute {@link org.junit.BeforeClass BeforeClass} and
26: * {@link org.junit.AfterClass AfterClass} methods.
27: *
28: * @author Lucas Koehler
29: *
30: */
31: public class MultiTryTestRule implements TestRule {
32:
33:         private final int maxTries;
34:         private boolean executeAll = true;
35:
36:         /**
37:          * Create a new {@link MultiTryTestRule} which allows the given number of tries for all test cases of a test class.
38:          * The number of tries is never less than 1 even if the given number of tries is lower.
39:          *
40:          * @param maxTries The maximum number of tries
41:          */
42:         public MultiTryTestRule(int maxTries) {
43:                 this.maxTries = Integer.max(1, maxTries);
44:         }
45:
46:         /**
47:          * Create a new {@link MultiTryTestRule} which allows the given number of tries for test cases of a test class.
48:          * The number of tries is never less than 1 even if the given number of tries is lower.
49:          *
50:          * @param maxTries The maximum number of tries
51:          * @param executeAll <code>true</code> if all test cases of the test class should use multiple tries.
52:          * <code>false</code> if only test cases annotated with {@link MultiTry} should use multiple tries.
53:          */
54:         public MultiTryTestRule(int maxTries, boolean executeAll) {
55:                 this.maxTries = Integer.max(1, maxTries);
56:                 this.executeAll = executeAll;
57:         }
58:
59:         @Override
60:         public Statement apply(Statement base, Description description) {
61:                 final MultiTry multiTry = description.getAnnotation(MultiTry.class);
62:                 if (!executeAll && multiTry == null) {
63:                         // no annotation and not all tests are multi tried => no multi try for this test
64:                         return base;
65:                 }
66:
67:                 final int tries = Integer.max(maxTries, multiTry == null ? 1 : multiTry.maxTries());
68:                 return new Statement() {
69:
70:                         // CHECKSTYLE.OFF: IllegalCatch
71:                         @Override
72:                         public void evaluate() throws Throwable {
73:                                 Throwable lastCaughtThrowable = null;
74:•                                for (int i = 1; i <= tries; i++) {
75:                                         try {
76:                                                 base.evaluate();
77:                                                 return;
78:                                         } catch (final Throwable t) {
79:                                                 System.err
80:                                                         .println(MessageFormat.format("{0}: Try {1} failed.", description.getDisplayName(), i)); //$NON-NLS-1$
81:                                                 lastCaughtThrowable = t;
82:                                         }
83:                                 }
84:                                 System.err.println(MessageFormat.format("{0} finally failed after {1} tries.", //$NON-NLS-1$
85:                                         description.getDisplayName(), tries));
86:                                 throw lastCaughtThrowable;
87:                         }
88:                         // CHECKSTYLE.ON: IllegalCatch
89:                 };
90:         }
91: }