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.rule;
016    
017    import com.liferay.portal.kernel.test.ReflectionTestUtil;
018    import com.liferay.portal.kernel.test.rule.callback.BaseTestCallback;
019    
020    import org.junit.internal.runners.statements.ExpectException;
021    import org.junit.internal.runners.statements.FailOnTimeout;
022    import org.junit.internal.runners.statements.InvokeMethod;
023    import org.junit.internal.runners.statements.RunAfters;
024    import org.junit.internal.runners.statements.RunBefores;
025    import org.junit.rules.TestRule;
026    import org.junit.runner.Description;
027    import org.junit.runners.model.Statement;
028    
029    /**
030     * @author Shuyang Zhou
031     */
032    public class BaseTestRule<C, M> implements TestRule {
033    
034            public BaseTestRule(BaseTestCallback<C, M> baseTestCallback) {
035                    _baseTestCallback = baseTestCallback;
036            }
037    
038            @Override
039            public final Statement apply(
040                    Statement statement, final Description description) {
041    
042                    return new StatementWrapper(statement) {
043    
044                            @Override
045                            public void evaluate() throws Throwable {
046                                    String methodName = description.getMethodName();
047    
048                                    C c = null;
049                                    M m = null;
050                                    Object target = null;
051    
052                                    if (methodName == null) {
053                                            c = _baseTestCallback.doBeforeClass(description);
054                                    }
055                                    else {
056                                            target = inspectTarget(statement);
057    
058                                            m = _baseTestCallback.doBeforeMethod(description, target);
059                                    }
060    
061                                    try {
062                                            statement.evaluate();
063                                    }
064                                    finally {
065                                            if (methodName == null) {
066                                                    _baseTestCallback.doAfterClass(description, c);
067                                            }
068                                            else {
069                                                    _baseTestCallback.doAfterMethod(description, m, target);
070                                            }
071                                    }
072                            }
073    
074                    };
075            }
076    
077            public static abstract class StatementWrapper extends Statement {
078    
079                    public StatementWrapper(Statement statement) {
080                            this.statement = statement;
081                    }
082    
083                    public Statement getStatement() {
084                            return statement;
085                    }
086    
087                    protected final Statement statement;
088    
089            }
090    
091            protected Object inspectTarget(Statement statement) {
092                    while (statement instanceof StatementWrapper) {
093                            StatementWrapper statementWrapper = (StatementWrapper)statement;
094    
095                            statement = statementWrapper.getStatement();
096                    }
097    
098                    if ((statement instanceof InvokeMethod) ||
099                            (statement instanceof RunAfters) ||
100                            (statement instanceof RunBefores)) {
101    
102                            return ReflectionTestUtil.getFieldValue(statement, "target");
103                    }
104                    else if (statement instanceof ExpectException) {
105                            return inspectTarget(
106                                    ReflectionTestUtil.<Statement>getFieldValue(statement, "next"));
107                    }
108                    else if (statement instanceof FailOnTimeout) {
109                            return inspectTarget(
110                                    ReflectionTestUtil.<Statement>getFieldValue(
111                                            statement, "originalStatement"));
112                    }
113    
114                    throw new IllegalStateException("Unknow statement " + statement);
115            }
116    
117            private final BaseTestCallback<C, M> _baseTestCallback;
118    
119    }