001/**
002 * Copyright 2005-2015 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.uif.util;
017
018import java.util.concurrent.Callable;
019
020import org.junit.Rule;
021import org.junit.rules.MethodRule;
022import org.junit.runners.model.FrameworkMethod;
023import org.junit.runners.model.Statement;
024
025/**
026 * Base class for wrapping all JUnit tests in call to
027 * {@link ProcessLogger#follow(String, String, Callable)}.
028 * 
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031public class ProcessLoggingUnitTest {
032
033    /**
034     * Thread local, contains the current repetition index while running.
035     */
036    private static final ThreadLocal<Integer> REPETITION = new ThreadLocal<Integer>();
037
038    protected static int getRepetition() {
039        return REPETITION.get() == null ? 0 : REPETITION.get();
040    }
041
042    /**
043     * Provides the number of times the test should be run.  Subclasses may provide an alternate value.
044     * @return
045     */
046    protected int getRepetitions() {
047        return 1;
048    }
049
050    @Rule
051    public MethodRule processLogRule = new MethodRule() {
052        @Override
053        public Statement apply(final Statement base,
054                final FrameworkMethod method, Object target) {
055            return new Statement() {
056                @Override
057                public void evaluate() throws Throwable {
058                    int repetitions = getRepetitions();
059                    for (int i=0;i < repetitions;i++) {
060                        REPETITION.set(i);
061                        try {
062                            ProcessLogger.follow("test", "Test Run " + method.getName(), new Callable<Void>() {
063
064                                @Override
065                                public Void call() throws Exception {
066                                    try {
067                                        base.evaluate();
068                                    } catch (Throwable e) {
069                                        if (e instanceof Error)
070                                            throw (Error) e;
071                                        else
072                                            throw (Exception) e;
073                                    }
074                                    return null;
075                                }
076
077                                @Override
078                                public String toString() {
079                                    return "Test Run " + method.getName();
080                                }
081                            });
082                        } catch (Throwable t) {
083                            System.err.println("Error running test "
084                                    + method.getName());
085                            t.printStackTrace();
086                            throw t;
087                        } finally {
088                            REPETITION.remove();
089                        }
090                    }
091                }
092            };
093        }
094    };
095
096}