001
014
015 package com.liferay.portal.kernel.test.rule;
016
017 import java.util.Arrays;
018 import java.util.Comparator;
019 import java.util.HashSet;
020 import java.util.Set;
021
022 import org.junit.rules.TestRule;
023 import org.junit.runner.Description;
024 import org.junit.runners.model.Statement;
025
026
029 public class AggregateTestRule implements ArquillianClassRuleHandler, TestRule {
030
031 public AggregateTestRule(boolean sort, TestRule... testRules) {
032 if (testRules == null) {
033 throw new NullPointerException("Test rules is null");
034 }
035
036 if (testRules.length < 2) {
037 throw new IllegalArgumentException(
038 "Rule number " + testRules.length + " is less than 2");
039 }
040
041 _testRules = testRules;
042
043 if (sort) {
044 Arrays.sort(_testRules, _testRuleComparator);
045 }
046 }
047
048 public AggregateTestRule(TestRule... testRules) {
049 this(true, testRules);
050 }
051
052 @Override
053 public Statement apply(Statement statement, Description description) {
054 for (int i = _testRules.length - 1; i >= 0; i--) {
055 statement = _testRules[i].apply(statement, description);
056 }
057
058 return statement;
059 }
060
061 @Override
062 public void handleAfterClass(boolean enable) {
063 for (TestRule testRule : _testRules) {
064 if (testRule instanceof ArquillianClassRuleHandler) {
065 ArquillianClassRuleHandler arquillianTestRuleHandler =
066 (ArquillianClassRuleHandler)testRule;
067
068 arquillianTestRuleHandler.handleAfterClass(enable);
069 }
070 }
071 }
072
073 @Override
074 public void handleBeforeClass(boolean enable) {
075 for (TestRule testRule : _testRules) {
076 if (testRule instanceof ArquillianClassRuleHandler) {
077 ArquillianClassRuleHandler arquillianTestRuleHandler =
078 (ArquillianClassRuleHandler)testRule;
079
080 arquillianTestRuleHandler.handleBeforeClass(enable);
081 }
082 }
083 }
084
085 private static final String[] _ORDERED_RULE_CLASS_NAMES = new String[] {
086 HeapDumpTestRule.class.getName(), CodeCoverageAssertor.class.getName(),
087 NewEnvTestRule.class.getName(),
088 "com.liferay.portal.test.rule.PortalExecutorManagerTestRule",
089 AssumeTestRule.class.getName(),
090 "com.liferay.portal.test.rule.LiferayIntegrationTestRule",
091 "com.liferay.portal.test.rule.HypersonicServerTestRule",
092 "com.liferay.portal.test.rule.PersistenceTestRule",
093 TransactionalTestRule.class.getName(),
094 SynchronousDestinationTestRule.class.getName(),
095 "com.liferay.portal.test.rule.SynchronousMailTestRule",
096 "com.liferay.document.library.webdav.test." +
097 "WebDAVEnvironmentConfigTestRule",
098 "com.liferay.portal.test.rule.SyntheticBundleRule"
099 };
100
101 private static final Comparator<TestRule> _testRuleComparator =
102 new Comparator<TestRule>() {
103
104 @Override
105 public int compare(TestRule testRule1, TestRule testRule2) {
106 return getIndex(testRule1.getClass()) -
107 getIndex(testRule2.getClass());
108 }
109
110 private int getIndex(Class<?> testRuleClass) {
111 Set<String> testRuleClassNames = new HashSet<>();
112
113 while (TestRule.class.isAssignableFrom(testRuleClass)) {
114 testRuleClassNames.add(testRuleClass.getName());
115
116 testRuleClass = testRuleClass.getSuperclass();
117 }
118
119 for (int i = 0; i < _ORDERED_RULE_CLASS_NAMES.length; i++) {
120 if (testRuleClassNames.contains(
121 _ORDERED_RULE_CLASS_NAMES[i])) {
122
123 return i;
124 }
125 }
126
127 throw new IllegalArgumentException(
128 "Unknown test rule class : " + testRuleClass);
129 }
130
131 };
132
133 private final TestRule[] _testRules;
134
135 }