Skip to content

Commit 67a40db

Browse files
committed
HBASE-23621 Reduced the number of Checkstyle violations in tests of hbase-common
1 parent 248d80b commit 67a40db

File tree

11 files changed

+156
-268
lines changed

11 files changed

+156
-268
lines changed

hbase-common/src/test/java/org/apache/hadoop/hbase/ClassFinder.java

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
18-
1918
package org.apache.hadoop.hbase;
2019

2120
import java.io.File;
@@ -53,24 +52,32 @@ public class ClassFinder {
5352

5453
public interface ResourcePathFilter {
5554
boolean isCandidatePath(String resourcePath, boolean isJar);
56-
};
55+
}
5756

5857
public interface FileNameFilter {
5958
boolean isCandidateFile(String fileName, String absFilePath);
60-
};
59+
}
6160

6261
public interface ClassFilter {
6362
boolean isCandidateClass(Class<?> c);
64-
};
63+
}
6564

6665
public static class Not implements ResourcePathFilter, FileNameFilter, ClassFilter {
6766
private ResourcePathFilter resourcePathFilter;
6867
private FileNameFilter fileNameFilter;
6968
private ClassFilter classFilter;
7069

71-
public Not(ResourcePathFilter resourcePathFilter){this.resourcePathFilter = resourcePathFilter;}
72-
public Not(FileNameFilter fileNameFilter){this.fileNameFilter = fileNameFilter;}
73-
public Not(ClassFilter classFilter){this.classFilter = classFilter;}
70+
public Not(ResourcePathFilter resourcePathFilter) {
71+
this.resourcePathFilter = resourcePathFilter;
72+
}
73+
74+
public Not(FileNameFilter fileNameFilter) {
75+
this.fileNameFilter = fileNameFilter;
76+
}
77+
78+
public Not(ClassFilter classFilter) {
79+
this.classFilter = classFilter;
80+
}
7481

7582
@Override
7683
public boolean isCandidatePath(String resourcePath, boolean isJar) {
@@ -90,7 +97,10 @@ public static class And implements ClassFilter, ResourcePathFilter {
9097
ClassFilter[] classFilters;
9198
ResourcePathFilter[] resourcePathFilters;
9299

93-
public And(ClassFilter...classFilters) { this.classFilters = classFilters; }
100+
public And(ClassFilter...classFilters) {
101+
this.classFilters = classFilters;
102+
}
103+
94104
public And(ResourcePathFilter... resourcePathFilters) {
95105
this.resourcePathFilters = resourcePathFilters;
96106
}
@@ -120,10 +130,6 @@ public ClassFinder(ClassLoader classLoader) {
120130
this(null, null, null, classLoader);
121131
}
122132

123-
public ClassFinder() {
124-
this(ClassLoader.getSystemClassLoader());
125-
}
126-
127133
public ClassFinder(ResourcePathFilter resourcePathFilter, FileNameFilter fileNameFilter,
128134
ClassFilter classFilter) {
129135
this(resourcePathFilter, fileNameFilter, classFilter, ClassLoader.getSystemClassLoader());
@@ -180,7 +186,7 @@ public Set<Class<?>> findClasses(String packageName, boolean proceedOnExceptions
180186
}
181187
}
182188

183-
Set<Class<?>> classes = new HashSet<Class<?>>();
189+
Set<Class<?>> classes = new HashSet<>();
184190
for (File directory : dirs) {
185191
classes.addAll(findClassesFromFiles(directory, packageName, proceedOnExceptions));
186192
}
@@ -193,16 +199,16 @@ public Set<Class<?>> findClasses(String packageName, boolean proceedOnExceptions
193199
private Set<Class<?>> findClassesFromJar(String jarFileName,
194200
String packageName, boolean proceedOnExceptions)
195201
throws IOException, ClassNotFoundException, LinkageError {
196-
JarInputStream jarFile = null;
202+
JarInputStream jarFile;
197203
try {
198204
jarFile = new JarInputStream(new FileInputStream(jarFileName));
199205
} catch (IOException ioEx) {
200206
LOG.warn("Failed to look for classes in " + jarFileName + ": " + ioEx);
201207
throw ioEx;
202208
}
203209

204-
Set<Class<?>> classes = new HashSet<Class<?>>();
205-
JarEntry entry = null;
210+
Set<Class<?>> classes = new HashSet<>();
211+
JarEntry entry;
206212
try {
207213
while (true) {
208214
try {
@@ -248,7 +254,7 @@ private Set<Class<?>> findClassesFromJar(String jarFileName,
248254

249255
private Set<Class<?>> findClassesFromFiles(File baseDirectory, String packageName,
250256
boolean proceedOnExceptions) throws ClassNotFoundException, LinkageError {
251-
Set<Class<?>> classes = new HashSet<Class<?>>();
257+
Set<Class<?>> classes = new HashSet<>();
252258
if (!baseDirectory.exists()) {
253259
LOG.warn("Failed to find " + baseDirectory.getAbsolutePath());
254260
return classes;
@@ -285,16 +291,11 @@ private Class<?> makeClass(String className, boolean proceedOnExceptions)
285291
Class<?> c = Class.forName(className, false, classLoader);
286292
boolean isCandidateClass = null == classFilter || classFilter.isCandidateClass(c);
287293
return isCandidateClass ? c : null;
288-
} catch (ClassNotFoundException classNotFoundEx) {
289-
if (!proceedOnExceptions) {
290-
throw classNotFoundEx;
291-
}
292-
LOG.debug("Failed to instantiate or check " + className + ": " + classNotFoundEx);
293-
} catch (LinkageError linkageEx) {
294+
} catch (ClassNotFoundException | LinkageError exception) {
294295
if (!proceedOnExceptions) {
295-
throw linkageEx;
296+
throw exception;
296297
}
297-
LOG.debug("Failed to instantiate or check " + className + ": " + linkageEx);
298+
LOG.debug("Failed to instantiate or check " + className + ": " + exception);
298299
}
299300
return null;
300301
}
@@ -313,5 +314,5 @@ public boolean accept(File file) {
313314
&& (null == nameFilter
314315
|| nameFilter.isCandidateFile(file.getName(), file.getAbsolutePath())));
315316
}
316-
};
317-
};
317+
}
318+
}

hbase-common/src/test/java/org/apache/hadoop/hbase/HBaseCommonTestingUtility.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ public Configuration getConfiguration() {
7676
private File dataTestDir = null;
7777

7878
/**
79-
* @return Where to write test data on local filesystem, specific to
80-
* the test. Useful for tests that do not use a cluster.
81-
* Creates it if it does not exist already.
79+
* @return Where to write test data on local filesystem, specific to the test. Useful for tests
80+
* that do not use a cluster. Creates it if it does not exist already.
8281
*/
8382
public Path getDataTestDir() {
8483
if (this.dataTestDir == null) {
@@ -88,10 +87,9 @@ public Path getDataTestDir() {
8887
}
8988

9089
/**
91-
* @param subdirName
92-
* @return Path to a subdirectory named <code>subdirName</code> under
93-
* {@link #getDataTestDir()}.
94-
* Does *NOT* create it if it does not exist.
90+
* @param subdirName the name of the subdirectory in the test data directory
91+
* @return Path to a subdirectory named {code subdirName} under
92+
* {@link #getDataTestDir()}. Does *NOT* create it if it does not exist.
9593
*/
9694
public Path getDataTestDir(final String subdirName) {
9795
return new Path(getDataTestDir(), subdirName);
@@ -115,7 +113,10 @@ protected Path setupDataTestDir() {
115113
this.dataTestDir = new File(testPath.toString()).getAbsoluteFile();
116114
// Set this property so if mapreduce jobs run, they will use this as their home dir.
117115
System.setProperty("test.build.dir", this.dataTestDir.toString());
118-
if (deleteOnExit()) this.dataTestDir.deleteOnExit();
116+
117+
if (deleteOnExit()) {
118+
this.dataTestDir.deleteOnExit();
119+
}
119120

120121
createSubDir("hbase.local.dir", testPath, "hbase-local-dir");
121122

@@ -125,7 +126,11 @@ protected Path setupDataTestDir() {
125126
protected void createSubDir(String propertyName, Path parent, String subDirName) {
126127
Path newPath = new Path(parent, subDirName);
127128
File newDir = new File(newPath.toString()).getAbsoluteFile();
128-
if (deleteOnExit()) newDir.deleteOnExit();
129+
130+
if (deleteOnExit()) {
131+
newDir.deleteOnExit();
132+
}
133+
129134
conf.set(propertyName, newDir.getAbsolutePath());
130135
}
131136

@@ -140,9 +145,8 @@ boolean deleteOnExit() {
140145

141146
/**
142147
* @return True if we removed the test dirs
143-
* @throws IOException
144148
*/
145-
public boolean cleanupTestDir() throws IOException {
149+
public boolean cleanupTestDir() {
146150
if (deleteDir(this.dataTestDir)) {
147151
this.dataTestDir = null;
148152
return true;
@@ -153,9 +157,8 @@ public boolean cleanupTestDir() throws IOException {
153157
/**
154158
* @param subdir Test subdir name.
155159
* @return True if we removed the test dir
156-
* @throws IOException
157160
*/
158-
boolean cleanupTestDir(final String subdir) throws IOException {
161+
boolean cleanupTestDir(final String subdir) {
159162
if (this.dataTestDir == null) {
160163
return false;
161164
}
@@ -164,9 +167,9 @@ boolean cleanupTestDir(final String subdir) throws IOException {
164167

165168
/**
166169
* @return Where to write test data on local filesystem; usually
167-
* {@link #DEFAULT_BASE_TEST_DIRECTORY}
168-
* Should not be used by the unit tests, hence its's private.
169-
* Unit test will use a subdirectory of this directory.
170+
* {@link #DEFAULT_BASE_TEST_DIRECTORY}
171+
* Should not be used by the unit tests, hence its's private.
172+
* Unit test will use a subdirectory of this directory.
170173
* @see #setupDataTestDir()
171174
*/
172175
private Path getBaseTestDir() {
@@ -185,17 +188,19 @@ public Path getRandomDir() {
185188
/**
186189
* @param dir Directory to delete
187190
* @return True if we deleted it.
188-
* @throws IOException
189191
*/
190-
boolean deleteDir(final File dir) throws IOException {
192+
boolean deleteDir(final File dir) {
191193
if (dir == null || !dir.exists()) {
192194
return true;
193195
}
194196
int ntries = 0;
195197
do {
196198
ntries += 1;
197199
try {
198-
if (deleteOnExit()) FileUtils.deleteDirectory(dir);
200+
if (deleteOnExit()) {
201+
FileUtils.deleteDirectory(dir);
202+
}
203+
199204
return true;
200205
} catch (IOException ex) {
201206
LOG.warn("Failed to delete " + dir.getAbsolutePath());

hbase-common/src/test/java/org/apache/hadoop/hbase/ResourceChecker.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
2019
package org.apache.hadoop.hbase;
2120

2221
import org.apache.commons.logging.Log;
@@ -46,7 +45,6 @@ public ResourceChecker(final String tagLine) {
4645
this.tagLine = tagLine;
4746
}
4847

49-
5048
/**
5149
* Class to implement for each type of resource.
5250
*/
@@ -83,21 +81,22 @@ public String getName() {
8381

8482
/**
8583
* The value for the resource.
86-
* @param phase
84+
* @param phase the {@link Phase} to get the value for
8785
*/
8886
abstract public int getVal(Phase phase);
8987

9088
/*
9189
* Retrieves List of Strings which would be logged in logEndings()
9290
*/
93-
public List<String> getStringsToLog() { return null; }
91+
public List<String> getStringsToLog() {
92+
return null;
93+
}
9494
}
9595

9696
private List<ResourceAnalyzer> ras = new ArrayList<ResourceAnalyzer>();
9797
private int[] initialValues;
9898
private int[] endingValues;
9999

100-
101100
private void fillInit() {
102101
initialValues = new int[ras.size()];
103102
fill(Phase.INITIAL, initialValues);
@@ -141,7 +140,11 @@ private void logInit() {
141140
StringBuilder sb = new StringBuilder();
142141
for (ResourceAnalyzer ra : ras) {
143142
int cur = initialValues[i++];
144-
if (sb.length() > 0) sb.append(", ");
143+
144+
if (sb.length() > 0) {
145+
sb.append(", ");
146+
}
147+
145148
sb.append(ra.getName()).append("=").append(cur);
146149
}
147150
LOG.info("before: " + tagLine + " " + sb);
@@ -156,7 +159,11 @@ private void logEndings() {
156159
for (ResourceAnalyzer ra : ras) {
157160
int curP = initialValues[i];
158161
int curN = endingValues[i++];
159-
if (sb.length() > 0) sb.append(", ");
162+
163+
if (sb.length() > 0) {
164+
sb.append(", ");
165+
}
166+
160167
sb.append(ra.getName()).append("=").append(curN).append(" (was ").append(curP).append(")");
161168
if (curN > curP) {
162169
List<String> strings = ra.getStringsToLog();
@@ -171,7 +178,6 @@ private void logEndings() {
171178
LOG.info("after: " + tagLine + " " + sb);
172179
}
173180

174-
175181
/**
176182
* To be called as the beginning of a test method:
177183
* - measure the resources

0 commit comments

Comments
 (0)