Skip to content

Commit 5d6cb4a

Browse files
committed
HADOOP-3679. Fixup assert ordering in unit tests to yield meaningful error
messages. Contributed by Jay Vyas git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1577396 13f79535-47bb-0310-9956-ffa450edef68
1 parent ad6b0c1 commit 5d6cb4a

File tree

12 files changed

+162
-124
lines changed

12 files changed

+162
-124
lines changed

hadoop-common-project/hadoop-common/CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,9 @@ Release 2.4.0 - UNRELEASED
370370
HADOOP-10386. Log proxy hostname in various exceptions being thrown in a HA
371371
setup. (wheat9)
372372

373+
HADOOP-3679. Fixup assert ordering in unit tests to yield meaningful error
374+
messages. (Jay Vyas via cdouglas)
375+
373376
OPTIMIZATIONS
374377

375378
BUG FIXES

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestPath.java

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,11 @@ public void testChildParentResolution() throws URISyntaxException, IOException {
278278
Path child = new Path("foo2://bar2/baz2");
279279
assertEquals(child, new Path(parent, child));
280280
}
281-
281+
282282
@Test (timeout = 30000)
283283
public void testScheme() throws java.io.IOException {
284-
assertEquals("foo:/bar", new Path("foo:/","/bar").toString());
285-
assertEquals("foo://bar/baz", new Path("foo://bar/","/baz").toString());
284+
assertEquals("foo:/bar", new Path("foo:/","/bar").toString());
285+
assertEquals("foo://bar/baz", new Path("foo://bar/","/baz").toString());
286286
}
287287

288288
@Test (timeout = 30000)
@@ -311,12 +311,19 @@ public void testURI() throws URISyntaxException, IOException {
311311
@Test (timeout = 30000)
312312
public void testPathToUriConversion() throws URISyntaxException, IOException {
313313
// Path differs from URI in that it ignores the query part..
314-
assertEquals(new URI(null, null, "/foo?bar", null, null), new Path("/foo?bar").toUri());
315-
assertEquals(new URI(null, null, "/foo\"bar", null, null), new Path("/foo\"bar").toUri());
316-
assertEquals(new URI(null, null, "/foo bar", null, null), new Path("/foo bar").toUri());
317-
// therefore "foo?bar" is a valid Path, so a URI created from a Path has path "foo?bar"
318-
// where in a straight URI the path part is just "foo"
319-
assertEquals("/foo?bar", new Path("http://localhost/foo?bar").toUri().getPath());
314+
assertEquals("? mark char in to URI",
315+
new URI(null, null, "/foo?bar", null, null),
316+
new Path("/foo?bar").toUri());
317+
assertEquals("escape slashes chars in to URI",
318+
new URI(null, null, "/foo\"bar", null, null),
319+
new Path("/foo\"bar").toUri());
320+
assertEquals("spaces in chars to URI",
321+
new URI(null, null, "/foo bar", null, null),
322+
new Path("/foo bar").toUri());
323+
// therefore "foo?bar" is a valid Path, so a URI created from a Path
324+
// has path "foo?bar" where in a straight URI the path part is just "foo"
325+
assertEquals("/foo?bar",
326+
new Path("http://localhost/foo?bar").toUri().getPath());
320327
assertEquals("/foo", new URI("http://localhost/foo?bar").getPath());
321328

322329
// The path part handling in Path is equivalent to URI
@@ -332,11 +339,14 @@ public void testPathToUriConversion() throws URISyntaxException, IOException {
332339
@Test (timeout = 30000)
333340
public void testReservedCharacters() throws URISyntaxException, IOException {
334341
// URI encodes the path
335-
assertEquals("/foo%20bar", new URI(null, null, "/foo bar", null, null).getRawPath());
342+
assertEquals("/foo%20bar",
343+
new URI(null, null, "/foo bar", null, null).getRawPath());
336344
// URI#getPath decodes the path
337-
assertEquals("/foo bar", new URI(null, null, "/foo bar", null, null).getPath());
345+
assertEquals("/foo bar",
346+
new URI(null, null, "/foo bar", null, null).getPath());
338347
// URI#toString returns an encoded path
339-
assertEquals("/foo%20bar", new URI(null, null, "/foo bar", null, null).toString());
348+
assertEquals("/foo%20bar",
349+
new URI(null, null, "/foo bar", null, null).toString());
340350
assertEquals("/foo%20bar", new Path("/foo bar").toUri().toString());
341351
// Reserved chars are not encoded
342352
assertEquals("/foo;bar", new URI("/foo;bar").getPath());
@@ -345,12 +355,17 @@ public void testReservedCharacters() throws URISyntaxException, IOException {
345355
assertEquals("/foo+bar", new URI("/foo+bar").getRawPath());
346356

347357
// URI#getPath decodes the path part (and URL#getPath does not decode)
348-
assertEquals("/foo bar", new Path("http://localhost/foo bar").toUri().getPath());
349-
assertEquals("/foo%20bar", new Path("http://localhost/foo bar").toUri().toURL().getPath());
350-
assertEquals("/foo?bar", new URI("http", "localhost", "/foo?bar", null, null).getPath());
351-
assertEquals("/foo%3Fbar", new URI("http", "localhost", "/foo?bar", null, null).toURL().getPath());
358+
assertEquals("/foo bar",
359+
new Path("http://localhost/foo bar").toUri().getPath());
360+
assertEquals("/foo%20bar",
361+
new Path("http://localhost/foo bar").toUri().toURL().getPath());
362+
assertEquals("/foo?bar",
363+
new URI("http", "localhost", "/foo?bar", null, null).getPath());
364+
assertEquals("/foo%3Fbar",
365+
new URI("http", "localhost", "/foo?bar", null, null).
366+
toURL().getPath());
352367
}
353-
368+
354369
@Test (timeout = 30000)
355370
public void testMakeQualified() throws URISyntaxException {
356371
URI defaultUri = new URI("hdfs://host1/dir1");

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestStat.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ public void testStatFileNotFound() throws Exception {
123123
// expected
124124
}
125125
}
126-
126+
127127
@Test(timeout=10000)
128128
public void testStatEnvironment() throws Exception {
129-
assertEquals(stat.getEnvironment("LANG"), "C");
129+
assertEquals("C", stat.getEnvironment("LANG"));
130130
}
131131

132132
@Test(timeout=10000)

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestDataByteBuffers.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,24 +139,25 @@ public void testDataOutputByteBufferCompatibility() throws IOException {
139139
writeJunk(dob, r, seed, 1000);
140140
writeJunk(dobb, r, seed, 1000);
141141
byte[] check = toBytes(dobb.getData(), dobb.getLength());
142-
assertEquals(dob.getLength(), check.length);
143-
assertArrayEquals(Arrays.copyOf(dob.getData(), dob.getLength()), check);
142+
assertEquals(check.length, dob.getLength());
143+
assertArrayEquals(check, Arrays.copyOf(dob.getData(), dob.getLength()));
144144

145145
dob.reset();
146146
dobb.reset();
147147
writeJunk(dob, r, seed, 3000);
148148
writeJunk(dobb, r, seed, 3000);
149149
check = toBytes(dobb.getData(), dobb.getLength());
150-
assertEquals(dob.getLength(), check.length);
151-
assertArrayEquals(Arrays.copyOf(dob.getData(), dob.getLength()), check);
150+
assertEquals(check.length, dob.getLength());
151+
assertArrayEquals(check, Arrays.copyOf(dob.getData(), dob.getLength()));
152152

153153
dob.reset();
154154
dobb.reset();
155155
writeJunk(dob, r, seed, 1000);
156156
writeJunk(dobb, r, seed, 1000);
157157
check = toBytes(dobb.getData(), dobb.getLength());
158-
assertEquals(dob.getLength(), check.length);
159-
assertArrayEquals(Arrays.copyOf(dob.getData(), dob.getLength()), check);
158+
assertEquals("Failed Checking length = " + check.length,
159+
check.length, dob.getLength());
160+
assertArrayEquals(check, Arrays.copyOf(dob.getData(), dob.getLength()));
160161
}
161162

162163
@Test

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestIOUtils.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,25 +221,25 @@ public void testSkipFully() throws IOException {
221221
IOUtils.skipFully(in, 2);
222222
fail("expected to get a PrematureEOFException");
223223
} catch (EOFException e) {
224-
assertEquals(e.getMessage(), "Premature EOF from inputStream " +
225-
"after skipping 1 byte(s).");
224+
assertEquals("Premature EOF from inputStream " +
225+
"after skipping 1 byte(s).",e.getMessage());
226226
}
227227
in.reset();
228228
try {
229229
IOUtils.skipFully(in, 20);
230230
fail("expected to get a PrematureEOFException");
231231
} catch (EOFException e) {
232-
assertEquals(e.getMessage(), "Premature EOF from inputStream " +
233-
"after skipping 5 byte(s).");
232+
assertEquals("Premature EOF from inputStream " +
233+
"after skipping 5 byte(s).",e.getMessage());
234234
}
235235
in.reset();
236236
IOUtils.skipFully(in, 5);
237237
try {
238238
IOUtils.skipFully(in, 10);
239239
fail("expected to get a PrematureEOFException");
240240
} catch (EOFException e) {
241-
assertEquals(e.getMessage(), "Premature EOF from inputStream " +
242-
"after skipping 0 byte(s).");
241+
assertEquals("Premature EOF from inputStream " +
242+
"after skipping 0 byte(s).",e.getMessage());
243243
}
244244
} finally {
245245
in.close();

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestText.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,13 @@ public void testCompare() throws Exception {
212212

213213
assertEquals(ret1, ret2);
214214

215-
// test equal
216-
assertEquals(txt1.compareTo(txt3), 0);
217-
assertEquals(comparator.compare(out1.getData(), 0, out3.getLength(),
218-
out3.getData(), 0, out3.getLength()), 0);
215+
assertEquals("Equivalence of different txt objects, same content" ,
216+
0,
217+
txt1.compareTo(txt3));
218+
assertEquals("Equvalence of data output buffers",
219+
0,
220+
comparator.compare(out1.getData(), 0, out3.getLength(),
221+
out3.getData(), 0, out3.getLength()));
219222
}
220223
}
221224

@@ -287,7 +290,7 @@ public ConcurrentEncodeDecodeThread(String name) {
287290

288291
@Override
289292
public void run() {
290-
String name = this.getName();
293+
final String name = this.getName();
291294
DataOutputBuffer out = new DataOutputBuffer();
292295
DataInputBuffer in = new DataInputBuffer();
293296
for (int i=0; i < 1000; ++i) {
@@ -298,7 +301,7 @@ public void run() {
298301
in.reset(out.getData(), out.getLength());
299302
String s = WritableUtils.readString(in);
300303

301-
assertEquals(name, s);
304+
assertEquals("input buffer reset contents = " + name, name, s);
302305
} catch (Exception ioe) {
303306
throw new RuntimeException(ioe);
304307
}
@@ -388,13 +391,19 @@ public void testbytesToCodePointWithInvalidUTF() {
388391
}
389392
}
390393

391-
public void testUtf8Length() {
392-
assertEquals("testUtf8Length1 error !!!", 1, Text.utf8Length(new String(new char[]{(char)1})));
393-
assertEquals("testUtf8Length127 error !!!", 1, Text.utf8Length(new String(new char[]{(char)127})));
394-
assertEquals("testUtf8Length128 error !!!", 2, Text.utf8Length(new String(new char[]{(char)128})));
395-
assertEquals("testUtf8Length193 error !!!", 2, Text.utf8Length(new String(new char[]{(char)193})));
396-
assertEquals("testUtf8Length225 error !!!", 2, Text.utf8Length(new String(new char[]{(char)225})));
397-
assertEquals("testUtf8Length254 error !!!", 2, Text.utf8Length(new String(new char[]{(char)254})));
394+
public void testUtf8Length() {
395+
assertEquals("testUtf8Length1 error !!!",
396+
1, Text.utf8Length(new String(new char[]{(char)1})));
397+
assertEquals("testUtf8Length127 error !!!",
398+
1, Text.utf8Length(new String(new char[]{(char)127})));
399+
assertEquals("testUtf8Length128 error !!!",
400+
2, Text.utf8Length(new String(new char[]{(char)128})));
401+
assertEquals("testUtf8Length193 error !!!",
402+
2, Text.utf8Length(new String(new char[]{(char)193})));
403+
assertEquals("testUtf8Length225 error !!!",
404+
2, Text.utf8Length(new String(new char[]{(char)225})));
405+
assertEquals("testUtf8Length254 error !!!",
406+
2, Text.utf8Length(new String(new char[]{(char)254})));
398407
}
399408

400409
public static void main(String[] args) throws Exception

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestRPC.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public void run() {
228228
assertTrue("Exception from RPC exchange() " + e, false);
229229
}
230230
assertEquals(indata.length, outdata.length);
231-
assertEquals(val, 3);
231+
assertEquals(3, val);
232232
for (int i = 0; i < outdata.length; i++) {
233233
assertEquals(outdata[i], i);
234234
}
@@ -468,17 +468,17 @@ private void testCallsInternal(Configuration conf) throws IOException {
468468
assertTrue(Arrays.equals(stringResults, null));
469469

470470
UTF8 utf8Result = (UTF8)proxy.echo(new UTF8("hello world"));
471-
assertEquals(utf8Result, new UTF8("hello world"));
471+
assertEquals(new UTF8("hello world"), utf8Result );
472472

473473
utf8Result = (UTF8)proxy.echo((UTF8)null);
474-
assertEquals(utf8Result, null);
474+
assertEquals(null, utf8Result);
475475

476476
int intResult = proxy.add(1, 2);
477477
assertEquals(intResult, 3);
478478

479479
intResult = proxy.add(new int[] {1, 2});
480480
assertEquals(intResult, 3);
481-
481+
482482
// Test protobufs
483483
EnumDescriptorProto sendProto =
484484
EnumDescriptorProto.newBuilder().setName("test").build();
@@ -603,28 +603,28 @@ public void testServerAddress() throws IOException {
603603
} finally {
604604
server.stop();
605605
}
606-
assertEquals(bindAddr.getAddress(), InetAddress.getLocalHost());
606+
assertEquals(InetAddress.getLocalHost(), bindAddr.getAddress());
607607
}
608-
608+
609609
@Test
610610
public void testAuthorization() throws IOException {
611611
Configuration conf = new Configuration();
612612
conf.setBoolean(CommonConfigurationKeys.HADOOP_SECURITY_AUTHORIZATION,
613613
true);
614-
614+
615615
// Expect to succeed
616616
conf.set(ACL_CONFIG, "*");
617617
doRPCs(conf, false);
618-
618+
619619
// Reset authorization to expect failure
620620
conf.set(ACL_CONFIG, "invalid invalid");
621621
doRPCs(conf, true);
622-
622+
623623
conf.setInt(CommonConfigurationKeys.IPC_SERVER_RPC_READ_THREADS_KEY, 2);
624624
// Expect to succeed
625625
conf.set(ACL_CONFIG, "*");
626626
doRPCs(conf, false);
627-
627+
628628
// Reset authorization to expect failure
629629
conf.set(ACL_CONFIG, "invalid invalid");
630630
doRPCs(conf, true);
@@ -659,42 +659,42 @@ public void testStopNonRegisteredProxy() throws IOException {
659659
*/
660660
@Test
661661
public void testStopMockObject() throws IOException {
662-
RPC.stopProxy(MockitoUtil.mockProtocol(TestProtocol.class));
662+
RPC.stopProxy(MockitoUtil.mockProtocol(TestProtocol.class));
663663
}
664-
664+
665665
@Test
666666
public void testStopProxy() throws IOException {
667667
StoppedProtocol proxy = RPC.getProxy(StoppedProtocol.class,
668668
StoppedProtocol.versionID, null, conf);
669669
StoppedInvocationHandler invocationHandler = (StoppedInvocationHandler)
670670
Proxy.getInvocationHandler(proxy);
671-
assertEquals(invocationHandler.getCloseCalled(), 0);
671+
assertEquals(0, invocationHandler.getCloseCalled());
672672
RPC.stopProxy(proxy);
673-
assertEquals(invocationHandler.getCloseCalled(), 1);
673+
assertEquals(1, invocationHandler.getCloseCalled());
674674
}
675-
675+
676676
@Test
677677
public void testWrappedStopProxy() throws IOException {
678678
StoppedProtocol wrappedProxy = RPC.getProxy(StoppedProtocol.class,
679679
StoppedProtocol.versionID, null, conf);
680680
StoppedInvocationHandler invocationHandler = (StoppedInvocationHandler)
681681
Proxy.getInvocationHandler(wrappedProxy);
682-
682+
683683
StoppedProtocol proxy = (StoppedProtocol) RetryProxy.create(StoppedProtocol.class,
684684
wrappedProxy, RetryPolicies.RETRY_FOREVER);
685-
686-
assertEquals(invocationHandler.getCloseCalled(), 0);
685+
686+
assertEquals(0, invocationHandler.getCloseCalled());
687687
RPC.stopProxy(proxy);
688-
assertEquals(invocationHandler.getCloseCalled(), 1);
688+
assertEquals(1, invocationHandler.getCloseCalled());
689689
}
690-
690+
691691
@Test
692692
public void testErrorMsgForInsecureClient() throws IOException {
693693
Configuration serverConf = new Configuration(conf);
694694
SecurityUtil.setAuthenticationMethod(AuthenticationMethod.KERBEROS,
695695
serverConf);
696696
UserGroupInformation.setConfiguration(serverConf);
697-
697+
698698
final Server server = new RPC.Builder(serverConf).setProtocol(TestProtocol.class)
699699
.setInstance(new TestImpl()).setBindAddress(ADDRESS).setPort(0)
700700
.setNumHandlers(5).setVerbose(true).build();

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetworkTopologyWithNodeGroup.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ public class TestNetworkTopologyWithNodeGroup {
5454

5555
@Test
5656
public void testNumOfChildren() throws Exception {
57-
assertEquals(cluster.getNumOfLeaves(), dataNodes.length);
57+
assertEquals(dataNodes.length, cluster.getNumOfLeaves());
5858
}
5959

6060
@Test
6161
public void testNumOfRacks() throws Exception {
62-
assertEquals(cluster.getNumOfRacks(), 3);
62+
assertEquals(3, cluster.getNumOfRacks());
6363
}
6464

6565
@Test
6666
public void testRacks() throws Exception {
67-
assertEquals(cluster.getNumOfRacks(), 3);
67+
assertEquals(3, cluster.getNumOfRacks());
6868
assertTrue(cluster.isOnSameRack(dataNodes[0], dataNodes[1]));
6969
assertTrue(cluster.isOnSameRack(dataNodes[1], dataNodes[2]));
7070
assertFalse(cluster.isOnSameRack(dataNodes[2], dataNodes[3]));
@@ -76,7 +76,7 @@ public void testRacks() throws Exception {
7676

7777
@Test
7878
public void testNodeGroups() throws Exception {
79-
assertEquals(cluster.getNumOfRacks(), 3);
79+
assertEquals(3, cluster.getNumOfRacks());
8080
assertTrue(cluster.isOnSameNodeGroup(dataNodes[0], dataNodes[1]));
8181
assertFalse(cluster.isOnSameNodeGroup(dataNodes[1], dataNodes[2]));
8282
assertFalse(cluster.isOnSameNodeGroup(dataNodes[2], dataNodes[3]));
@@ -88,11 +88,11 @@ public void testNodeGroups() throws Exception {
8888

8989
@Test
9090
public void testGetDistance() throws Exception {
91-
assertEquals(cluster.getDistance(dataNodes[0], dataNodes[0]), 0);
92-
assertEquals(cluster.getDistance(dataNodes[0], dataNodes[1]), 2);
93-
assertEquals(cluster.getDistance(dataNodes[0], dataNodes[2]), 4);
94-
assertEquals(cluster.getDistance(dataNodes[0], dataNodes[3]), 6);
95-
assertEquals(cluster.getDistance(dataNodes[0], dataNodes[6]), 8);
91+
assertEquals(0, cluster.getDistance(dataNodes[0], dataNodes[0]));
92+
assertEquals(2, cluster.getDistance(dataNodes[0], dataNodes[1]));
93+
assertEquals(4, cluster.getDistance(dataNodes[0], dataNodes[2]));
94+
assertEquals(6, cluster.getDistance(dataNodes[0], dataNodes[3]));
95+
assertEquals(8, cluster.getDistance(dataNodes[0], dataNodes[6]));
9696
}
9797

9898
@Test

0 commit comments

Comments
 (0)