001    /**
002     * Copyright (c) 2000-2013 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 java.lang.reflect.Method;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import org.junit.internal.AssumptionViolatedException;
023    import org.junit.runners.model.MultipleFailureException;
024    import org.junit.runners.model.Statement;
025    
026    /**
027     * @author Miguel Pastor
028     */
029    public class RunAfterTestMethodCallback extends AbstractStatementCallback {
030    
031            public RunAfterTestMethodCallback(
032                    Object instance, Method method, Statement statement,
033                    TestContextHandler testContextHandler) {
034    
035                    super(statement, testContextHandler);
036    
037                    _instance = instance;
038                    _method = method;
039            }
040    
041            @Override
042            public void evaluate() throws Throwable {
043                    List<Throwable> throwables = new ArrayList<Throwable>();
044    
045                    Statement statement = getStatement();
046    
047                    if (statement != null) {
048                            try {
049                                    statement.evaluate();
050                            }
051                            catch (Throwable t) {
052                                    if (!(t instanceof AssumptionViolatedException)) {
053                                            throwables.add(t);
054                                    }
055                            }
056                    }
057    
058                    try {
059                            TestContextHandler testContextHandler = getTestContextHandler();
060    
061                            testContextHandler.runAfterTestMethod(_instance, _method);
062                    }
063                    catch (Exception e) {
064                            throwables.add(e);
065                    }
066    
067                    if (!throwables.isEmpty()) {
068                            throw new MultipleFailureException(throwables);
069                    }
070            }
071    
072            private Object _instance;
073            private Method _method;
074    
075    }