1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
35
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
45
46 public static final String BASE_TEST_DIRECTORY_KEY =
47 "test.build.data.basedirectory";
48
49
50
51
52 public static final String DEFAULT_BASE_TEST_DIRECTORY = "target/test-data";
53
54
55 private File dataTestDir = null;
56
57
58
59
60
61
62
63 public Path getDataTestDir() {
64 if (this.dataTestDir == null){
65 setupDataTestDir();
66 }
67 return new Path(this.dataTestDir.getAbsolutePath());
68 }
69
70
71
72
73
74
75
76 public Path getDataTestDir(final String subdirName) {
77 return new Path(getDataTestDir(), subdirName);
78 }
79
80
81
82
83
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
102
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
114
115
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
126
127
128
129
130
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
141
142
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 }