View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.UUID;
24  
25  import org.apache.commons.io.FileUtils;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.classification.InterfaceAudience;
29  import org.apache.hadoop.classification.InterfaceStability;
30  import org.apache.hadoop.fs.FileSystem;
31  import org.apache.hadoop.fs.Path;
32  
33  /**
34   * Common helpers for testing HBase that do not depend on specific server/etc. things.
35   * @see {@link HBaseTestingUtility}
36   *
37   */
38  @InterfaceAudience.Public
39  @InterfaceStability.Unstable
40  public class HBaseCommonTestingUtility {
41    protected static final Log LOG = LogFactory.getLog(HBaseCommonTestingUtility.class);
42  
43    /**
44     * System property key to get base test directory value
45     */
46    public static final String BASE_TEST_DIRECTORY_KEY =
47      "test.build.data.basedirectory";
48  
49    /**
50     * Default base directory for test output.
51     */
52    public static final String DEFAULT_BASE_TEST_DIRECTORY = "target/test-data";
53  
54    /** Directory where we put the data for this instance of HBaseTestingUtility*/
55    private File dataTestDir = null;
56  
57    /**
58     * @return Where to write test data on local filesystem, specific to
59     *  the test.  Useful for tests that do not use a cluster.
60     * Creates it if it does not exist already.
61     * @see #getTestFileSystem()
62     */
63    public Path getDataTestDir() {
64      if (this.dataTestDir == null){
65        setupDataTestDir();
66      }
67      return new Path(this.dataTestDir.getAbsolutePath());
68    }
69  
70    /**
71     * @param subdirName
72     * @return Path to a subdirectory named <code>subdirName</code> under
73     * {@link #getDataTestDir()}.
74     * Does *NOT* create it if it does not exist.
75     */
76    public Path getDataTestDir(final String subdirName) {
77      return new Path(getDataTestDir(), subdirName);
78    }
79  
80    /**
81     * Sets up a directory for a test to use.
82     *
83     * @return New directory path, if created.
84     */
85    protected Path setupDataTestDir() {
86      if (this.dataTestDir != null) {
87        LOG.warn("Data test dir already setup in " +
88          dataTestDir.getAbsolutePath());
89        return null;
90      }
91  
92      String randomStr = UUID.randomUUID().toString();
93      Path testPath= new Path(getBaseTestDir(), randomStr);
94  
95      this.dataTestDir = new File(testPath.toString()).getAbsoluteFile();
96      this.dataTestDir.deleteOnExit();
97      return testPath;
98    }
99  
100   /**
101    * @return True if we removed the test dirs
102    * @throws IOException
103    */
104   public boolean cleanupTestDir() throws IOException {
105     if (deleteDir(this.dataTestDir)) {
106       this.dataTestDir = null;
107       return true;
108     }
109     return false;
110   }
111 
112   /**
113    * @param subdir Test subdir name.
114    * @return True if we removed the test dir
115    * @throws IOException
116    */
117   boolean cleanupTestDir(final String subdir) throws IOException {
118     if (this.dataTestDir == null){
119       return false;
120     }
121     return deleteDir(new File(this.dataTestDir, subdir));
122   }
123 
124   /**
125    * @return Where to write test data on local filesystem; usually
126    * {@link #DEFAULT_BASE_TEST_DIRECTORY}
127    * Should not be used by the unit tests, hence its's private.
128    * Unit test will use a subdirectory of this directory.
129    * @see #setupDataTestDir()
130    * @see #getTestFileSystem()
131    */
132   private Path getBaseTestDir() {
133     String PathName = System.getProperty(
134       BASE_TEST_DIRECTORY_KEY, DEFAULT_BASE_TEST_DIRECTORY);
135 
136     return new Path(PathName);
137   }
138 
139   /**
140    * @param dir Directory to delete
141    * @return True if we deleted it.
142    * @throws IOException
143    */
144   boolean deleteDir(final File dir) throws IOException {
145     if (dir == null || !dir.exists()) {
146       return true;
147     }
148     try {
149       FileUtils.deleteDirectory(dir);
150       return true;
151     } catch (IOException ex) {
152       LOG.warn("Failed to delete " + dir.getAbsolutePath());
153       return false;
154     }
155   }
156 }