1717 */
1818package org .apache .hadoop .mapred ;
1919
20- import static org .junit .Assert .assertEquals ;
21- import static org .junit .Assert .assertFalse ;
22- import static org .junit .Assert .assertTrue ;
23-
2420import java .io .IOException ;
2521import java .text .ParseException ;
2622import java .util .HashSet ;
2723import java .util .Iterator ;
2824import java .util .Random ;
2925
30- import org .junit .Assert ;
31-
3226import org .apache .hadoop .mapred .Counters .Counter ;
3327import org .apache .hadoop .mapred .Counters .CountersExceededException ;
3428import org .apache .hadoop .mapred .Counters .Group ;
3832import org .apache .hadoop .mapreduce .TaskCounter ;
3933import org .apache .hadoop .mapreduce .counters .FrameworkCounterGroup ;
4034import org .apache .hadoop .mapreduce .counters .CounterGroupFactory .FrameworkGroupFactory ;
41- import org .junit .Test ;
35+ import org .junit .jupiter . api . Test ;
4236import org .slf4j .Logger ;
4337import org .slf4j .LoggerFactory ;
4438
39+ import static org .junit .jupiter .api .Assertions .assertEquals ;
40+ import static org .junit .jupiter .api .Assertions .assertFalse ;
41+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
42+ import static org .junit .jupiter .api .Assertions .assertNull ;
43+ import static org .junit .jupiter .api .Assertions .assertTrue ;
44+ import static org .junit .jupiter .api .Assertions .fail ;
45+
4546/**
4647 * TestCounters checks the sanity and recoverability of {@code Counters}
4748 */
4849public class TestCounters {
49- enum myCounters {TEST1 , TEST2 };
50+ enum myCounters {TEST1 , TEST2 }
5051 private static final long MAX_VALUE = 10 ;
5152 private static final Logger LOG = LoggerFactory .getLogger (TestCounters .class );
5253
@@ -85,14 +86,14 @@ private Counters getEnumCounters(String[] gNames, String[] cNames) {
8586 */
8687 private void testCounter (Counters counter ) throws ParseException {
8788 String compactEscapedString = counter .makeEscapedCompactString ();
88- assertFalse (" compactEscapedString should not contain null" ,
89- compactEscapedString . contains ( " null") );
89+ assertFalse (compactEscapedString . contains ( " null") ,
90+ "compactEscapedString should not contain null" );
9091
9192 Counters recoveredCounter =
9293 Counters .fromEscapedCompactString (compactEscapedString );
9394 // Check for recovery from string
94- assertEquals ("Recovered counter does not match on content" ,
95- counter , recoveredCounter );
95+ assertEquals (counter , recoveredCounter ,
96+ "Recovered counter does not match on content" );
9697 }
9798
9899 @ Test
@@ -109,7 +110,7 @@ public void testCounters() throws IOException {
109110 // I. Check enum counters that have resource bundler
110111 testCounter (getEnumCounters (keysWithResource ));
111112
112- // II. Check enum counters that dont have resource bundler
113+ // II. Check enum counters that don't have resource bundler
113114 testCounter (getEnumCounters (keysWithoutResource ));
114115
115116 // III. Check string counters
@@ -134,19 +135,19 @@ public void testCounterValue() {
134135 long expectedValue = initValue ;
135136 Counter counter = counters .findCounter ("foo" , "bar" );
136137 counter .setValue (initValue );
137- assertEquals ("Counter value is not initialized correctly" ,
138- expectedValue , counter . getValue () );
138+ assertEquals (expectedValue , counter . getValue () ,
139+ "Counter value is not initialized correctly" );
139140 for (int j = 0 ; j < NUMBER_INC ; j ++) {
140141 int incValue = rand .nextInt ();
141142 counter .increment (incValue );
142143 expectedValue += incValue ;
143- assertEquals ("Counter value is not incremented correctly" ,
144- expectedValue , counter . getValue () );
144+ assertEquals (expectedValue , counter . getValue () ,
145+ "Counter value is not incremented correctly" );
145146 }
146147 expectedValue = rand .nextInt ();
147148 counter .setValue (expectedValue );
148- assertEquals ("Counter value is not set correctly" ,
149- expectedValue , counter . getValue () );
149+ assertEquals (expectedValue , counter . getValue () ,
150+ "Counter value is not set correctly" );
150151 }
151152 }
152153
@@ -174,29 +175,27 @@ public void testWriteWithLegacyNames() {
174175
175176 @ SuppressWarnings ("deprecation" )
176177 private void checkLegacyNames (Counters counters ) {
177- assertEquals ("New name" , 1 , counters .findCounter (
178- TaskCounter .class .getName (), "MAP_INPUT_RECORDS" ).getValue ());
179- assertEquals ("Legacy name" , 1 , counters .findCounter (
178+ assertEquals (1 , counters .findCounter (
179+ TaskCounter .class .getName (), "MAP_INPUT_RECORDS" ).getValue (), "New name" );
180+ assertEquals (1 , counters .findCounter (
180181 "org.apache.hadoop.mapred.Task$Counter" ,
181- "MAP_INPUT_RECORDS" ).getValue ());
182- assertEquals ("Legacy enum" , 1 ,
183- counters .findCounter (Task .Counter .MAP_INPUT_RECORDS ).getValue ());
182+ "MAP_INPUT_RECORDS" ).getValue (), "Legacy name" );
183+ assertEquals (1 , counters .findCounter (Task .Counter .MAP_INPUT_RECORDS ).getValue (), "Legacy enum" );
184184
185- assertEquals ("New name" , 1 , counters .findCounter (
186- JobCounter .class .getName (), "DATA_LOCAL_MAPS" ).getValue ());
187- assertEquals ("Legacy name" , 1 , counters .findCounter (
185+ assertEquals (1 , counters .findCounter (
186+ JobCounter .class .getName (), "DATA_LOCAL_MAPS" ).getValue (), "New name" );
187+ assertEquals (1 , counters .findCounter (
188188 "org.apache.hadoop.mapred.JobInProgress$Counter" ,
189- "DATA_LOCAL_MAPS" ).getValue ());
190- assertEquals ("Legacy enum" , 1 ,
191- counters .findCounter (JobInProgress .Counter .DATA_LOCAL_MAPS ).getValue ());
189+ "DATA_LOCAL_MAPS" ).getValue (), "Legacy name" );
190+ assertEquals (1 , counters .findCounter (JobInProgress .Counter .DATA_LOCAL_MAPS ).getValue (), "Legacy enum" );
192191
193- assertEquals ("New name" , 1 , counters .findCounter (
194- FileSystemCounter .class .getName (), "FILE_BYTES_READ" ).getValue ());
195- assertEquals ("New name and method" , 1 , counters .findCounter ("file" ,
196- FileSystemCounter .BYTES_READ ).getValue ());
197- assertEquals ("Legacy name" , 1 , counters .findCounter (
192+ assertEquals (1 , counters .findCounter (
193+ FileSystemCounter .class .getName (), "FILE_BYTES_READ" ).getValue (), "New name" );
194+ assertEquals (1 , counters .findCounter ("file" ,
195+ FileSystemCounter .BYTES_READ ).getValue (), "New name and method" );
196+ assertEquals (1 , counters .findCounter (
198197 "FileSystemCounters" ,
199- "FILE_BYTES_READ" ).getValue ());
198+ "FILE_BYTES_READ" ).getValue (), "Legacy name" );
200199 }
201200
202201 @ SuppressWarnings ("deprecation" )
@@ -248,8 +247,8 @@ public void testLegacyGetGroupNames() {
248247 counters .findCounter ("fs2" , FileSystemCounter .BYTES_READ ).increment (1 );
249248 counters .incrCounter ("group1" , "counter1" , 1 );
250249
251- HashSet <String > groups = new HashSet <String >(counters .getGroupNames ());
252- HashSet <String > expectedGroups = new HashSet <String >();
250+ HashSet <String > groups = new HashSet <>(counters .getGroupNames ());
251+ HashSet <String > expectedGroups = new HashSet <>();
253252 expectedGroups .add ("group1" );
254253 expectedGroups .add ("FileSystemCounters" ); //Legacy Name
255254 expectedGroups .add ("org.apache.hadoop.mapreduce.FileSystemCounter" );
@@ -266,8 +265,8 @@ public void testMakeCompactString() {
266265 assertEquals ("group1.counter1:1" , counters .makeCompactString ());
267266 counters .incrCounter ("group2" , "counter2" , 3 );
268267 String cs = counters .makeCompactString ();
269- assertTrue ("Bad compact string" ,
270- cs .equals (GC1 + ',' + GC2 ) || cs .equals (GC2 + ',' + GC1 ));
268+ assertTrue (
269+ cs .equals (GC1 + ',' + GC2 ) || cs .equals (GC2 + ',' + GC1 ), "Bad compact string" );
271270 }
272271
273272 @ Test
@@ -281,11 +280,7 @@ private void testMaxCountersLimits(final Counters counters) {
281280 counters .findCounter ("test" , "test" + i );
282281 }
283282 setExpected (counters );
284- shouldThrow (CountersExceededException .class , new Runnable () {
285- public void run () {
286- counters .findCounter ("test" , "bad" );
287- }
288- });
283+ shouldThrow (CountersExceededException .class , () -> counters .findCounter ("test" , "bad" ));
289284 checkExpected (counters );
290285 }
291286
@@ -295,11 +290,7 @@ private void testMaxGroupsLimits(final Counters counters) {
295290 counters .findCounter ("test" + i , "test" );
296291 }
297292 setExpected (counters );
298- shouldThrow (CountersExceededException .class , new Runnable () {
299- public void run () {
300- counters .findCounter ("bad" , "test" );
301- }
302- });
293+ shouldThrow (CountersExceededException .class , () -> counters .findCounter ("bad" , "test" ));
303294 checkExpected (counters );
304295 }
305296
@@ -321,7 +312,7 @@ private void shouldThrow(Class<? extends Exception> ecls, Runnable runnable) {
321312 } catch (CountersExceededException e ) {
322313 return ;
323314 }
324- Assert . fail ("Should've thrown " + ecls .getSimpleName ());
315+ fail ("Should've thrown " + ecls .getSimpleName ());
325316 }
326317
327318 public static void main (String [] args ) throws IOException {
@@ -341,12 +332,12 @@ public void testFrameworkCounter() {
341332
342333 org .apache .hadoop .mapreduce .Counter count1 =
343334 counterGroup .findCounter (JobCounter .NUM_FAILED_MAPS .toString ());
344- Assert . assertNotNull (count1 );
335+ assertNotNull (count1 );
345336
346337 // Verify no exception get thrown when finding an unknown counter
347338 org .apache .hadoop .mapreduce .Counter count2 =
348339 counterGroup .findCounter ("Unknown" );
349- Assert . assertNull (count2 );
340+ assertNull (count2 );
350341 }
351342
352343 @ SuppressWarnings ("rawtypes" )
@@ -363,19 +354,19 @@ public void testTaskCounter() {
363354 org .apache .hadoop .mapreduce .Counter count1 =
364355 counterGroup .findCounter (
365356 TaskCounter .PHYSICAL_MEMORY_BYTES .toString ());
366- Assert . assertNotNull (count1 );
357+ assertNotNull (count1 );
367358 count1 .increment (10 );
368359 count1 .increment (10 );
369- Assert . assertEquals (20 , count1 .getValue ());
360+ assertEquals (20 , count1 .getValue ());
370361
371362 // Verify no exception get thrown when finding an unknown counter
372363 org .apache .hadoop .mapreduce .Counter count2 =
373364 counterGroup .findCounter (
374365 TaskCounter .MAP_PHYSICAL_MEMORY_BYTES_MAX .toString ());
375- Assert . assertNotNull (count2 );
366+ assertNotNull (count2 );
376367 count2 .increment (5 );
377368 count2 .increment (10 );
378- Assert . assertEquals (10 , count2 .getValue ());
369+ assertEquals (10 , count2 .getValue ());
379370 }
380371
381372 @ Test
@@ -385,12 +376,12 @@ public void testFilesystemCounter() {
385376
386377 org .apache .hadoop .mapreduce .Counter count1 =
387378 fsGroup .findCounter ("ANY_BYTES_READ" );
388- Assert . assertNotNull (count1 );
379+ assertNotNull (count1 );
389380
390381 // Verify no exception get thrown when finding an unknown counter
391382 org .apache .hadoop .mapreduce .Counter count2 =
392383 fsGroup .findCounter ("Unknown" );
393- Assert . assertNull (count2 );
384+ assertNull (count2 );
394385 }
395386
396387}
0 commit comments