001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.test;
016    
017    import org.junit.rules.TestRule;
018    import org.junit.runner.Description;
019    import org.junit.runners.model.Statement;
020    
021    /**
022     * @author Shuyang Zhou
023     */
024    public class AggregateTestRule implements TestRule {
025    
026            public AggregateTestRule(TestRule... testRules) {
027                    if (testRules == null) {
028                            throw new NullPointerException("Test rules is null");
029                    }
030    
031                    if (testRules.length < 2) {
032                            throw new IllegalArgumentException(
033                                    "Rule number " + testRules.length + " is less than 2");
034                    }
035    
036                    _testRules = testRules;
037            }
038    
039            @Override
040            public Statement apply(Statement statement, Description description) {
041                    for (int i = _testRules.length - 1; i >= 0; i--) {
042                            statement = _testRules[i].apply(statement, description);
043                    }
044    
045                    return statement;
046            }
047    
048            private final TestRule[] _testRules;
049    
050    }