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