1   /*
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase;
22  
23  
24  /**
25   * Class that implements a JUnit rule to be called before and after each
26   *  test method to check the resources used:
27   *   - file descriptors
28   *   - threads
29   *  @see ResourceChecker
30   */
31  public class ResourceCheckerJUnitRule extends org.junit.rules.TestWatcher {
32    private ResourceChecker cu;
33    private boolean endDone;
34  
35    /**
36     * To be called before the test methods
37     * @param testName
38     */
39    private void start(String testName) {
40      cu = new ResourceChecker("before "+testName);
41      endDone = false;
42    }
43  
44    /**
45     * To be called after the test methods
46     * @param testName
47     */
48    private void end(String testName) {
49      if (!endDone) {
50        endDone = true;
51        cu.logInfo(ResourceChecker.Phase.END, "after " + testName);
52        cu.check("after "+testName);
53      }
54    }
55  
56    /**
57     * Get the test name from the JUnit Description
58     * @param description
59     * @return the string for the short test name
60     */
61    private String descriptionToShortTestName(
62      org.junit.runner.Description description) {
63      final int toRemove = "org.apache.hadoop.hbase.".length();
64      return description.getTestClass().getName().substring(toRemove) +
65        "#" + description.getMethodName();
66    }
67  
68    @Override
69    protected void succeeded(org.junit.runner.Description description) {
70      end(descriptionToShortTestName(description));
71    }
72  
73    @Override
74    protected void failed(java.lang.Throwable e, org.junit.runner.Description description) {
75      end(descriptionToShortTestName(description));
76    }
77  
78    @Override
79    protected void starting(org.junit.runner.Description description) {
80      start(descriptionToShortTestName(description));
81    }
82  
83    @Override
84    protected void finished(org.junit.runner.Description description) {
85      end(descriptionToShortTestName(description));
86    }
87  }
88