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.MainServletTestRule",
092 "com.liferay.portal.test.rule.HypersonicServerTestRule",
093 "com.liferay.portal.test.rule.PersistenceTestRule",
094 TransactionalTestRule.class.getName(),
095 SynchronousDestinationTestRule.class.getName(),
096 "com.liferay.portal.test.rule.SynchronousMailTestRule",
097 "com.liferay.document.library.webdav.test." +
098 "WebDAVEnvironmentConfigTestRule",
099 "com.liferay.portal.test.rule.SyntheticBundleRule"
100 };
101
102 private static final Comparator<TestRule> _testRuleComparator =
103 new Comparator<TestRule>() {
104
105 @Override
106 public int compare(TestRule testRule1, TestRule testRule2) {
107 return getIndex(testRule1.getClass()) -
108 getIndex(testRule2.getClass());
109 }
110
111 private int getIndex(Class<?> testRuleClass) {
112 Set<String> testRuleClassNames = new HashSet<>();
113
114 while (TestRule.class.isAssignableFrom(testRuleClass)) {
115 testRuleClassNames.add(testRuleClass.getName());
116
117 testRuleClass = testRuleClass.getSuperclass();
118 }
119
120 for (int i = 0; i < _ORDERED_RULE_CLASS_NAMES.length; i++) {
121 if (testRuleClassNames.contains(
122 _ORDERED_RULE_CLASS_NAMES[i])) {
123
124 return i;
125 }
126 }
127
128 throw new IllegalArgumentException(
129 "Unknown test rule class : " + testRuleClass);
130 }
131
132 };
133
134 private final TestRule[] _testRules;
135
136 }