View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.uif.util;
17  
18  import java.util.concurrent.Callable;
19  
20  import org.junit.Rule;
21  import org.junit.rules.MethodRule;
22  import org.junit.runners.model.FrameworkMethod;
23  import org.junit.runners.model.Statement;
24  
25  /**
26   * Base class for wrapping all JUnit tests in call to
27   * {@link ProcessLogger#follow(String, String, Callable)}.
28   * 
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  public class ProcessLoggingUnitTest {
32  
33      /**
34       * Thread local, contains the current repetition index while running.
35       */
36      private static final ThreadLocal<Integer> REPETITION = new ThreadLocal<Integer>();
37  
38      protected static int getRepetition() {
39          return REPETITION.get() == null ? 0 : REPETITION.get();
40      }
41  
42      /**
43       * Provides the number of times the test should be run.  Subclasses may provide an alternate value.
44       * @return
45       */
46      protected int getRepetitions() {
47          return 1;
48      }
49  
50      @Rule
51      public MethodRule processLogRule = new MethodRule() {
52          @Override
53          public Statement apply(final Statement base,
54                  final FrameworkMethod method, Object target) {
55              return new Statement() {
56                  @Override
57                  public void evaluate() throws Throwable {
58                      int repetitions = getRepetitions();
59                      for (int i=0;i < repetitions;i++) {
60                          REPETITION.set(i);
61                          try {
62                              ProcessLogger.follow("test", "Test Run " + method.getName(), new Callable<Void>() {
63  
64                                  @Override
65                                  public Void call() throws Exception {
66                                      try {
67                                          base.evaluate();
68                                      } catch (Throwable e) {
69                                          if (e instanceof Error)
70                                              throw (Error) e;
71                                          else
72                                              throw (Exception) e;
73                                      }
74                                      return null;
75                                  }
76  
77                                  @Override
78                                  public String toString() {
79                                      return "Test Run " + method.getName();
80                                  }
81                              });
82                          } catch (Throwable t) {
83                              System.err.println("Error running test "
84                                      + method.getName());
85                              t.printStackTrace();
86                              throw t;
87                          } finally {
88                              REPETITION.remove();
89                          }
90                      }
91                  }
92              };
93          }
94      };
95  
96  }