Skip to content

Commit 8ab63bf

Browse files
HorizonNetJenkins
authored andcommitted
HBASE-23621 Reduced the number of Checkstyle violations in tests of hbase-common
Signed-off-by: stack <[email protected]> Signed-off-by: Viraj Jasani <[email protected]> (cherry picked from commit 4562e14) Change-Id: I5cc6252210dce6a111b25def4a09a991045141f1
1 parent db972d4 commit 8ab63bf

11 files changed

Lines changed: 141 additions & 279 deletions

File tree

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

Lines changed: 20 additions & 19 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;
@@ -68,9 +67,17 @@ public static class Not implements ResourcePathFilter, FileNameFilter, ClassFilt
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());
@@ -193,7 +199,7 @@ 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) {
@@ -202,7 +208,7 @@ private Set<Class<?>> findClassesFromJar(String jarFileName,
202208
}
203209

204210
Set<Class<?>> classes = new HashSet<>();
205-
JarEntry entry = null;
211+
JarEntry entry;
206212
try {
207213
while (true) {
208214
try {
@@ -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 (NoClassDefFoundError|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
}

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

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,34 @@
3737
/**
3838
* Common helpers for testing HBase that do not depend on specific server/etc. things.
3939
* @see org.apache.hadoop.hbase.HBaseCommonTestingUtility
40-
*
4140
*/
4241
@InterfaceAudience.Public
4342
public class HBaseCommonTestingUtility {
4443
protected static final Logger LOG = LoggerFactory.getLogger(HBaseCommonTestingUtility.class);
4544

46-
/** Compression algorithms to use in parameterized JUnit 4 tests */
45+
/**
46+
* Compression algorithms to use in parameterized JUnit 4 tests
47+
*/
4748
public static final List<Object[]> COMPRESSION_ALGORITHMS_PARAMETERIZED =
4849
Arrays.asList(new Object[][] {
4950
{ Compression.Algorithm.NONE },
5051
{ Compression.Algorithm.GZ }
5152
});
5253

53-
/** This is for unit tests parameterized with a two booleans. */
54+
/**
55+
* This is for unit tests parameterized with a two booleans.
56+
*/
5457
public static final List<Object[]> BOOLEAN_PARAMETERIZED =
5558
Arrays.asList(new Object[][] {
5659
{false},
5760
{true}
5861
});
5962

60-
/** Compression algorithms to use in testing */
63+
/**
64+
* Compression algorithms to use in testing
65+
*/
6166
public static final Compression.Algorithm[] COMPRESSION_ALGORITHMS = {
62-
Compression.Algorithm.NONE, Compression.Algorithm.GZ
67+
Compression.Algorithm.NONE, Compression.Algorithm.GZ
6368
};
6469

6570
protected Configuration conf;
@@ -98,9 +103,8 @@ public Configuration getConfiguration() {
98103
private File dataTestDir = null;
99104

100105
/**
101-
* @return Where to write test data on local filesystem, specific to
102-
* the test. Useful for tests that do not use a cluster.
103-
* Creates it if it does not exist already.
106+
* @return Where to write test data on local filesystem, specific to the test. Useful for tests
107+
* that do not use a cluster. Creates it if it does not exist already.
104108
*/
105109
public Path getDataTestDir() {
106110
if (this.dataTestDir == null) {
@@ -110,10 +114,9 @@ public Path getDataTestDir() {
110114
}
111115

112116
/**
113-
* @param subdirName
114-
* @return Path to a subdirectory named <code>subdirName</code> under
115-
* {@link #getDataTestDir()}.
116-
* Does *NOT* create it if it does not exist.
117+
* @param subdirName the name of the subdirectory in the test data directory
118+
* @return Path to a subdirectory named {code subdirName} under
119+
* {@link #getDataTestDir()}. Does *NOT* create it if it does not exist.
117120
*/
118121
public Path getDataTestDir(final String subdirName) {
119122
return new Path(getDataTestDir(), subdirName);
@@ -134,7 +137,10 @@ protected Path setupDataTestDir() {
134137
this.dataTestDir = new File(testPath.toString()).getAbsoluteFile();
135138
// Set this property so if mapreduce jobs run, they will use this as their home dir.
136139
System.setProperty("test.build.dir", this.dataTestDir.toString());
137-
if (deleteOnExit()) this.dataTestDir.deleteOnExit();
140+
141+
if (deleteOnExit()) {
142+
this.dataTestDir.deleteOnExit();
143+
}
138144

139145
createSubDir("hbase.local.dir", testPath, "hbase-local-dir");
140146

@@ -154,11 +160,14 @@ public UUID getRandomUUID() {
154160
ThreadLocalRandom.current().nextLong());
155161
}
156162

157-
158163
protected void createSubDir(String propertyName, Path parent, String subDirName) {
159164
Path newPath = new Path(parent, subDirName);
160165
File newDir = new File(newPath.toString()).getAbsoluteFile();
161-
if (deleteOnExit()) newDir.deleteOnExit();
166+
167+
if (deleteOnExit()) {
168+
newDir.deleteOnExit();
169+
}
170+
162171
conf.set(propertyName, newDir.getAbsolutePath());
163172
}
164173

@@ -173,9 +182,8 @@ boolean deleteOnExit() {
173182

174183
/**
175184
* @return True if we removed the test dirs
176-
* @throws IOException
177185
*/
178-
public boolean cleanupTestDir() throws IOException {
186+
public boolean cleanupTestDir() {
179187
if (deleteDir(this.dataTestDir)) {
180188
this.dataTestDir = null;
181189
return true;
@@ -186,9 +194,8 @@ public boolean cleanupTestDir() throws IOException {
186194
/**
187195
* @param subdir Test subdir name.
188196
* @return True if we removed the test dir
189-
* @throws IOException
190197
*/
191-
boolean cleanupTestDir(final String subdir) throws IOException {
198+
boolean cleanupTestDir(final String subdir) {
192199
if (this.dataTestDir == null) {
193200
return false;
194201
}
@@ -197,9 +204,9 @@ boolean cleanupTestDir(final String subdir) throws IOException {
197204

198205
/**
199206
* @return Where to write test data on local filesystem; usually
200-
* {@link #DEFAULT_BASE_TEST_DIRECTORY}
201-
* Should not be used by the unit tests, hence its's private.
202-
* Unit test will use a subdirectory of this directory.
207+
* {@link #DEFAULT_BASE_TEST_DIRECTORY}
208+
* Should not be used by the unit tests, hence its's private.
209+
* Unit test will use a subdirectory of this directory.
203210
* @see #setupDataTestDir()
204211
*/
205212
private Path getBaseTestDir() {
@@ -212,17 +219,19 @@ private Path getBaseTestDir() {
212219
/**
213220
* @param dir Directory to delete
214221
* @return True if we deleted it.
215-
* @throws IOException
216222
*/
217-
boolean deleteDir(final File dir) throws IOException {
223+
boolean deleteDir(final File dir) {
218224
if (dir == null || !dir.exists()) {
219225
return true;
220226
}
221227
int ntries = 0;
222228
do {
223229
ntries += 1;
224230
try {
225-
if (deleteOnExit()) FileUtils.deleteDirectory(dir);
231+
if (deleteOnExit()) {
232+
FileUtils.deleteDirectory(dir);
233+
}
234+
226235
return true;
227236
} catch (IOException ex) {
228237
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 java.util.ArrayList;
@@ -47,7 +46,6 @@ public ResourceChecker(final String tagLine) {
4746
this.tagLine = tagLine;
4847
}
4948

50-
5149
/**
5250
* Class to implement for each type of resource.
5351
*/
@@ -84,21 +82,22 @@ public String getName() {
8482

8583
/**
8684
* The value for the resource.
87-
* @param phase
85+
* @param phase the {@link Phase} to get the value for
8886
*/
8987
abstract public int getVal(Phase phase);
9088

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

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

101-
102101
private void fillInit() {
103102
initialValues = new int[ras.size()];
104103
fill(Phase.INITIAL, initialValues);
@@ -142,7 +141,11 @@ private void logInit() {
142141
StringBuilder sb = new StringBuilder();
143142
for (ResourceAnalyzer ra : ras) {
144143
int cur = initialValues[i++];
145-
if (sb.length() > 0) sb.append(", ");
144+
145+
if (sb.length() > 0) {
146+
sb.append(", ");
147+
}
148+
146149
sb.append(ra.getName()).append("=").append(cur);
147150
}
148151
LOG.info("before: " + tagLine + " " + sb);
@@ -157,7 +160,11 @@ private void logEndings() {
157160
for (ResourceAnalyzer ra : ras) {
158161
int curP = initialValues[i];
159162
int curN = endingValues[i++];
160-
if (sb.length() > 0) sb.append(", ");
163+
164+
if (sb.length() > 0) {
165+
sb.append(", ");
166+
}
167+
161168
sb.append(ra.getName()).append("=").append(curN).append(" (was ").append(curP).append(")");
162169
if (curN > curP) {
163170
List<String> strings = ra.getStringsToLog();
@@ -172,7 +179,6 @@ private void logEndings() {
172179
LOG.info("after: " + tagLine + " " + sb);
173180
}
174181

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

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141

4242
@Category({MiscTests.class, SmallTests.class})
4343
public class TestCellUtil {
44-
4544
@ClassRule
4645
public static final HBaseClassTestRule CLASS_RULE =
4746
HBaseClassTestRule.forClass(TestCellUtil.class);
@@ -78,7 +77,7 @@ public Cell current() {
7877
}
7978

8079
@Override
81-
public boolean advance() throws IOException {
80+
public boolean advance() {
8281
if (this.count < cellsCount) {
8382
this.current = new TestCell(this.count);
8483
this.count++;
@@ -219,28 +218,28 @@ public long heapSize() {
219218
*/
220219
@Test
221220
public void testCreateCellScannerOverflow() throws IOException {
222-
consume(doCreateCellScanner(1, 1), 1 * 1);
223-
consume(doCreateCellScanner(3, 0), 3 * 0);
221+
consume(doCreateCellScanner(1, 1), 1);
222+
consume(doCreateCellScanner(3, 0), 0);
224223
consume(doCreateCellScanner(3, 3), 3 * 3);
225-
consume(doCreateCellScanner(0, 1), 0 * 1);
224+
consume(doCreateCellScanner(0, 1), 0);
226225
// Do big number. See HBASE-11813 for why.
227226
final int hundredK = 100000;
228-
consume(doCreateCellScanner(hundredK, 0), hundredK * 0);
227+
consume(doCreateCellScanner(hundredK, 0), 0);
229228
consume(doCreateCellArray(1), 1);
230229
consume(doCreateCellArray(0), 0);
231230
consume(doCreateCellArray(3), 3);
232231
List<CellScannable> cells = new ArrayList<>(hundredK);
233232
for (int i = 0; i < hundredK; i++) {
234233
cells.add(new TestCellScannable(1));
235234
}
236-
consume(CellUtil.createCellScanner(cells), hundredK * 1);
235+
consume(CellUtil.createCellScanner(cells), hundredK);
237236
NavigableMap<byte [], List<Cell>> m = new TreeMap<>(Bytes.BYTES_COMPARATOR);
238237
List<Cell> cellArray = new ArrayList<>(hundredK);
239238
for (int i = 0; i < hundredK; i++) {
240239
cellArray.add(new TestCell(i));
241240
}
242241
m.put(new byte [] {'f'}, cellArray);
243-
consume(CellUtil.createCellScanner(m), hundredK * 1);
242+
consume(CellUtil.createCellScanner(m), hundredK);
244243
}
245244

246245
private CellScanner doCreateCellArray(final int itemsPerList) {
@@ -251,8 +250,7 @@ private CellScanner doCreateCellArray(final int itemsPerList) {
251250
return CellUtil.createCellScanner(cells);
252251
}
253252

254-
private CellScanner doCreateCellScanner(final int listsCount, final int itemsPerList)
255-
throws IOException {
253+
private CellScanner doCreateCellScanner(final int listsCount, final int itemsPerList) {
256254
List<CellScannable> cells = new ArrayList<>(listsCount);
257255
for (int i = 0; i < listsCount; i++) {
258256
CellScannable cs = new CellScannable() {
@@ -536,11 +534,10 @@ public void testWriteCell() throws IOException {
536534

537535
// Workaround for jdk 11 - reflective access to interface default methods for testGetType
538536
private abstract class CellForMockito implements Cell {
539-
540537
}
541538

542539
@Test
543-
public void testGetType() throws IOException {
540+
public void testGetType() {
544541
CellForMockito c = Mockito.mock(CellForMockito.class);
545542
Mockito.when(c.getType()).thenCallRealMethod();
546543
for (CellForMockito.Type type : CellForMockito.Type.values()) {

0 commit comments

Comments
 (0)