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/branches/branch-2@1577397 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Christopher Douglas 2014-03-14 00:47:04 +00:00
parent 9695bc7af6
commit 8f915e55fd
12 changed files with 162 additions and 124 deletions

View File

@ -67,6 +67,9 @@ Release 2.4.0 - UNRELEASED
HADOOP-10386. Log proxy hostname in various exceptions being thrown in a HA HADOOP-10386. Log proxy hostname in various exceptions being thrown in a HA
setup. (wheat9) setup. (wheat9)
HADOOP-3679. Fixup assert ordering in unit tests to yield meaningful error
messages. (Jay Vyas via cdouglas)
OPTIMIZATIONS OPTIMIZATIONS
BUG FIXES BUG FIXES

View File

@ -278,11 +278,11 @@ public void testChildParentResolution() throws URISyntaxException, IOException {
Path child = new Path("foo2://bar2/baz2"); Path child = new Path("foo2://bar2/baz2");
assertEquals(child, new Path(parent, child)); assertEquals(child, new Path(parent, child));
} }
@Test (timeout = 30000) @Test (timeout = 30000)
public void testScheme() throws java.io.IOException { public void testScheme() throws java.io.IOException {
assertEquals("foo:/bar", new Path("foo:/","/bar").toString()); assertEquals("foo:/bar", new Path("foo:/","/bar").toString());
assertEquals("foo://bar/baz", new Path("foo://bar/","/baz").toString()); assertEquals("foo://bar/baz", new Path("foo://bar/","/baz").toString());
} }
@Test (timeout = 30000) @Test (timeout = 30000)
@ -311,12 +311,19 @@ public void testURI() throws URISyntaxException, IOException {
@Test (timeout = 30000) @Test (timeout = 30000)
public void testPathToUriConversion() throws URISyntaxException, IOException { public void testPathToUriConversion() throws URISyntaxException, IOException {
// Path differs from URI in that it ignores the query part.. // Path differs from URI in that it ignores the query part..
assertEquals(new URI(null, null, "/foo?bar", null, null), new Path("/foo?bar").toUri()); assertEquals("? mark char in to URI",
assertEquals(new URI(null, null, "/foo\"bar", null, null), new Path("/foo\"bar").toUri()); new URI(null, null, "/foo?bar", null, null),
assertEquals(new URI(null, null, "/foo bar", null, null), new Path("/foo bar").toUri()); new Path("/foo?bar").toUri());
// therefore "foo?bar" is a valid Path, so a URI created from a Path has path "foo?bar" assertEquals("escape slashes chars in to URI",
// where in a straight URI the path part is just "foo" new URI(null, null, "/foo\"bar", null, null),
assertEquals("/foo?bar", new Path("http://localhost/foo?bar").toUri().getPath()); new Path("/foo\"bar").toUri());
assertEquals("spaces in chars to URI",
new URI(null, null, "/foo bar", null, null),
new Path("/foo bar").toUri());
// therefore "foo?bar" is a valid Path, so a URI created from a Path
// has path "foo?bar" where in a straight URI the path part is just "foo"
assertEquals("/foo?bar",
new Path("http://localhost/foo?bar").toUri().getPath());
assertEquals("/foo", new URI("http://localhost/foo?bar").getPath()); assertEquals("/foo", new URI("http://localhost/foo?bar").getPath());
// The path part handling in Path is equivalent to URI // The path part handling in Path is equivalent to URI
@ -332,11 +339,14 @@ public void testPathToUriConversion() throws URISyntaxException, IOException {
@Test (timeout = 30000) @Test (timeout = 30000)
public void testReservedCharacters() throws URISyntaxException, IOException { public void testReservedCharacters() throws URISyntaxException, IOException {
// URI encodes the path // URI encodes the path
assertEquals("/foo%20bar", new URI(null, null, "/foo bar", null, null).getRawPath()); assertEquals("/foo%20bar",
new URI(null, null, "/foo bar", null, null).getRawPath());
// URI#getPath decodes the path // URI#getPath decodes the path
assertEquals("/foo bar", new URI(null, null, "/foo bar", null, null).getPath()); assertEquals("/foo bar",
new URI(null, null, "/foo bar", null, null).getPath());
// URI#toString returns an encoded path // URI#toString returns an encoded path
assertEquals("/foo%20bar", new URI(null, null, "/foo bar", null, null).toString()); assertEquals("/foo%20bar",
new URI(null, null, "/foo bar", null, null).toString());
assertEquals("/foo%20bar", new Path("/foo bar").toUri().toString()); assertEquals("/foo%20bar", new Path("/foo bar").toUri().toString());
// Reserved chars are not encoded // Reserved chars are not encoded
assertEquals("/foo;bar", new URI("/foo;bar").getPath()); assertEquals("/foo;bar", new URI("/foo;bar").getPath());
@ -345,12 +355,17 @@ public void testReservedCharacters() throws URISyntaxException, IOException {
assertEquals("/foo+bar", new URI("/foo+bar").getRawPath()); assertEquals("/foo+bar", new URI("/foo+bar").getRawPath());
// URI#getPath decodes the path part (and URL#getPath does not decode) // URI#getPath decodes the path part (and URL#getPath does not decode)
assertEquals("/foo bar", new Path("http://localhost/foo bar").toUri().getPath()); assertEquals("/foo bar",
assertEquals("/foo%20bar", new Path("http://localhost/foo bar").toUri().toURL().getPath()); new Path("http://localhost/foo bar").toUri().getPath());
assertEquals("/foo?bar", new URI("http", "localhost", "/foo?bar", null, null).getPath()); assertEquals("/foo%20bar",
assertEquals("/foo%3Fbar", new URI("http", "localhost", "/foo?bar", null, null).toURL().getPath()); new Path("http://localhost/foo bar").toUri().toURL().getPath());
assertEquals("/foo?bar",
new URI("http", "localhost", "/foo?bar", null, null).getPath());
assertEquals("/foo%3Fbar",
new URI("http", "localhost", "/foo?bar", null, null).
toURL().getPath());
} }
@Test (timeout = 30000) @Test (timeout = 30000)
public void testMakeQualified() throws URISyntaxException { public void testMakeQualified() throws URISyntaxException {
URI defaultUri = new URI("hdfs://host1/dir1"); URI defaultUri = new URI("hdfs://host1/dir1");

View File

@ -125,10 +125,10 @@ public void testStatFileNotFound() throws Exception {
// expected // expected
} }
} }
@Test(timeout=10000) @Test(timeout=10000)
public void testStatEnvironment() throws Exception { public void testStatEnvironment() throws Exception {
assertEquals(stat.getEnvironment("LANG"), "C"); assertEquals("C", stat.getEnvironment("LANG"));
} }
@Test(timeout=10000) @Test(timeout=10000)

View File

@ -139,24 +139,25 @@ public void testDataOutputByteBufferCompatibility() throws IOException {
writeJunk(dob, r, seed, 1000); writeJunk(dob, r, seed, 1000);
writeJunk(dobb, r, seed, 1000); writeJunk(dobb, r, seed, 1000);
byte[] check = toBytes(dobb.getData(), dobb.getLength()); byte[] check = toBytes(dobb.getData(), dobb.getLength());
assertEquals(dob.getLength(), check.length); assertEquals(check.length, dob.getLength());
assertArrayEquals(Arrays.copyOf(dob.getData(), dob.getLength()), check); assertArrayEquals(check, Arrays.copyOf(dob.getData(), dob.getLength()));
dob.reset(); dob.reset();
dobb.reset(); dobb.reset();
writeJunk(dob, r, seed, 3000); writeJunk(dob, r, seed, 3000);
writeJunk(dobb, r, seed, 3000); writeJunk(dobb, r, seed, 3000);
check = toBytes(dobb.getData(), dobb.getLength()); check = toBytes(dobb.getData(), dobb.getLength());
assertEquals(dob.getLength(), check.length); assertEquals(check.length, dob.getLength());
assertArrayEquals(Arrays.copyOf(dob.getData(), dob.getLength()), check); assertArrayEquals(check, Arrays.copyOf(dob.getData(), dob.getLength()));
dob.reset(); dob.reset();
dobb.reset(); dobb.reset();
writeJunk(dob, r, seed, 1000); writeJunk(dob, r, seed, 1000);
writeJunk(dobb, r, seed, 1000); writeJunk(dobb, r, seed, 1000);
check = toBytes(dobb.getData(), dobb.getLength()); check = toBytes(dobb.getData(), dobb.getLength());
assertEquals(dob.getLength(), check.length); assertEquals("Failed Checking length = " + check.length,
assertArrayEquals(Arrays.copyOf(dob.getData(), dob.getLength()), check); check.length, dob.getLength());
assertArrayEquals(check, Arrays.copyOf(dob.getData(), dob.getLength()));
} }
@Test @Test

View File

@ -190,16 +190,16 @@ public void testSkipFully() throws IOException {
IOUtils.skipFully(in, 2); IOUtils.skipFully(in, 2);
fail("expected to get a PrematureEOFException"); fail("expected to get a PrematureEOFException");
} catch (EOFException e) { } catch (EOFException e) {
assertEquals(e.getMessage(), "Premature EOF from inputStream " + assertEquals("Premature EOF from inputStream " +
"after skipping 1 byte(s)."); "after skipping 1 byte(s).",e.getMessage());
} }
in.reset(); in.reset();
try { try {
IOUtils.skipFully(in, 20); IOUtils.skipFully(in, 20);
fail("expected to get a PrematureEOFException"); fail("expected to get a PrematureEOFException");
} catch (EOFException e) { } catch (EOFException e) {
assertEquals(e.getMessage(), "Premature EOF from inputStream " + assertEquals("Premature EOF from inputStream " +
"after skipping 5 byte(s)."); "after skipping 5 byte(s).",e.getMessage());
} }
in.reset(); in.reset();
IOUtils.skipFully(in, 5); IOUtils.skipFully(in, 5);
@ -207,8 +207,8 @@ public void testSkipFully() throws IOException {
IOUtils.skipFully(in, 10); IOUtils.skipFully(in, 10);
fail("expected to get a PrematureEOFException"); fail("expected to get a PrematureEOFException");
} catch (EOFException e) { } catch (EOFException e) {
assertEquals(e.getMessage(), "Premature EOF from inputStream " + assertEquals("Premature EOF from inputStream " +
"after skipping 0 byte(s)."); "after skipping 0 byte(s).",e.getMessage());
} }
} finally { } finally {
in.close(); in.close();

View File

@ -212,10 +212,13 @@ public void testCompare() throws Exception {
assertEquals(ret1, ret2); assertEquals(ret1, ret2);
// test equal assertEquals("Equivalence of different txt objects, same content" ,
assertEquals(txt1.compareTo(txt3), 0); 0,
assertEquals(comparator.compare(out1.getData(), 0, out3.getLength(), txt1.compareTo(txt3));
out3.getData(), 0, out3.getLength()), 0); assertEquals("Equvalence of data output buffers",
0,
comparator.compare(out1.getData(), 0, out3.getLength(),
out3.getData(), 0, out3.getLength()));
} }
} }
@ -287,7 +290,7 @@ public ConcurrentEncodeDecodeThread(String name) {
@Override @Override
public void run() { public void run() {
String name = this.getName(); final String name = this.getName();
DataOutputBuffer out = new DataOutputBuffer(); DataOutputBuffer out = new DataOutputBuffer();
DataInputBuffer in = new DataInputBuffer(); DataInputBuffer in = new DataInputBuffer();
for (int i=0; i < 1000; ++i) { for (int i=0; i < 1000; ++i) {
@ -298,7 +301,7 @@ public void run() {
in.reset(out.getData(), out.getLength()); in.reset(out.getData(), out.getLength());
String s = WritableUtils.readString(in); String s = WritableUtils.readString(in);
assertEquals(name, s); assertEquals("input buffer reset contents = " + name, name, s);
} catch (Exception ioe) { } catch (Exception ioe) {
throw new RuntimeException(ioe); throw new RuntimeException(ioe);
} }
@ -388,13 +391,19 @@ public void testbytesToCodePointWithInvalidUTF() {
} }
} }
public void testUtf8Length() { public void testUtf8Length() {
assertEquals("testUtf8Length1 error !!!", 1, Text.utf8Length(new String(new char[]{(char)1}))); assertEquals("testUtf8Length1 error !!!",
assertEquals("testUtf8Length127 error !!!", 1, Text.utf8Length(new String(new char[]{(char)127}))); 1, Text.utf8Length(new String(new char[]{(char)1})));
assertEquals("testUtf8Length128 error !!!", 2, Text.utf8Length(new String(new char[]{(char)128}))); assertEquals("testUtf8Length127 error !!!",
assertEquals("testUtf8Length193 error !!!", 2, Text.utf8Length(new String(new char[]{(char)193}))); 1, Text.utf8Length(new String(new char[]{(char)127})));
assertEquals("testUtf8Length225 error !!!", 2, Text.utf8Length(new String(new char[]{(char)225}))); assertEquals("testUtf8Length128 error !!!",
assertEquals("testUtf8Length254 error !!!", 2, Text.utf8Length(new String(new char[]{(char)254}))); 2, Text.utf8Length(new String(new char[]{(char)128})));
assertEquals("testUtf8Length193 error !!!",
2, Text.utf8Length(new String(new char[]{(char)193})));
assertEquals("testUtf8Length225 error !!!",
2, Text.utf8Length(new String(new char[]{(char)225})));
assertEquals("testUtf8Length254 error !!!",
2, Text.utf8Length(new String(new char[]{(char)254})));
} }
public static void main(String[] args) throws Exception public static void main(String[] args) throws Exception

View File

@ -228,7 +228,7 @@ public void run() {
assertTrue("Exception from RPC exchange() " + e, false); assertTrue("Exception from RPC exchange() " + e, false);
} }
assertEquals(indata.length, outdata.length); assertEquals(indata.length, outdata.length);
assertEquals(val, 3); assertEquals(3, val);
for (int i = 0; i < outdata.length; i++) { for (int i = 0; i < outdata.length; i++) {
assertEquals(outdata[i], i); assertEquals(outdata[i], i);
} }
@ -468,17 +468,17 @@ private void testCallsInternal(Configuration conf) throws IOException {
assertTrue(Arrays.equals(stringResults, null)); assertTrue(Arrays.equals(stringResults, null));
UTF8 utf8Result = (UTF8)proxy.echo(new UTF8("hello world")); UTF8 utf8Result = (UTF8)proxy.echo(new UTF8("hello world"));
assertEquals(utf8Result, new UTF8("hello world")); assertEquals(new UTF8("hello world"), utf8Result );
utf8Result = (UTF8)proxy.echo((UTF8)null); utf8Result = (UTF8)proxy.echo((UTF8)null);
assertEquals(utf8Result, null); assertEquals(null, utf8Result);
int intResult = proxy.add(1, 2); int intResult = proxy.add(1, 2);
assertEquals(intResult, 3); assertEquals(intResult, 3);
intResult = proxy.add(new int[] {1, 2}); intResult = proxy.add(new int[] {1, 2});
assertEquals(intResult, 3); assertEquals(intResult, 3);
// Test protobufs // Test protobufs
EnumDescriptorProto sendProto = EnumDescriptorProto sendProto =
EnumDescriptorProto.newBuilder().setName("test").build(); EnumDescriptorProto.newBuilder().setName("test").build();
@ -603,28 +603,28 @@ public void testServerAddress() throws IOException {
} finally { } finally {
server.stop(); server.stop();
} }
assertEquals(bindAddr.getAddress(), InetAddress.getLocalHost()); assertEquals(InetAddress.getLocalHost(), bindAddr.getAddress());
} }
@Test @Test
public void testAuthorization() throws IOException { public void testAuthorization() throws IOException {
Configuration conf = new Configuration(); Configuration conf = new Configuration();
conf.setBoolean(CommonConfigurationKeys.HADOOP_SECURITY_AUTHORIZATION, conf.setBoolean(CommonConfigurationKeys.HADOOP_SECURITY_AUTHORIZATION,
true); true);
// Expect to succeed // Expect to succeed
conf.set(ACL_CONFIG, "*"); conf.set(ACL_CONFIG, "*");
doRPCs(conf, false); doRPCs(conf, false);
// Reset authorization to expect failure // Reset authorization to expect failure
conf.set(ACL_CONFIG, "invalid invalid"); conf.set(ACL_CONFIG, "invalid invalid");
doRPCs(conf, true); doRPCs(conf, true);
conf.setInt(CommonConfigurationKeys.IPC_SERVER_RPC_READ_THREADS_KEY, 2); conf.setInt(CommonConfigurationKeys.IPC_SERVER_RPC_READ_THREADS_KEY, 2);
// Expect to succeed // Expect to succeed
conf.set(ACL_CONFIG, "*"); conf.set(ACL_CONFIG, "*");
doRPCs(conf, false); doRPCs(conf, false);
// Reset authorization to expect failure // Reset authorization to expect failure
conf.set(ACL_CONFIG, "invalid invalid"); conf.set(ACL_CONFIG, "invalid invalid");
doRPCs(conf, true); doRPCs(conf, true);
@ -659,42 +659,42 @@ public void testStopNonRegisteredProxy() throws IOException {
*/ */
@Test @Test
public void testStopMockObject() throws IOException { public void testStopMockObject() throws IOException {
RPC.stopProxy(MockitoUtil.mockProtocol(TestProtocol.class)); RPC.stopProxy(MockitoUtil.mockProtocol(TestProtocol.class));
} }
@Test @Test
public void testStopProxy() throws IOException { public void testStopProxy() throws IOException {
StoppedProtocol proxy = RPC.getProxy(StoppedProtocol.class, StoppedProtocol proxy = RPC.getProxy(StoppedProtocol.class,
StoppedProtocol.versionID, null, conf); StoppedProtocol.versionID, null, conf);
StoppedInvocationHandler invocationHandler = (StoppedInvocationHandler) StoppedInvocationHandler invocationHandler = (StoppedInvocationHandler)
Proxy.getInvocationHandler(proxy); Proxy.getInvocationHandler(proxy);
assertEquals(invocationHandler.getCloseCalled(), 0); assertEquals(0, invocationHandler.getCloseCalled());
RPC.stopProxy(proxy); RPC.stopProxy(proxy);
assertEquals(invocationHandler.getCloseCalled(), 1); assertEquals(1, invocationHandler.getCloseCalled());
} }
@Test @Test
public void testWrappedStopProxy() throws IOException { public void testWrappedStopProxy() throws IOException {
StoppedProtocol wrappedProxy = RPC.getProxy(StoppedProtocol.class, StoppedProtocol wrappedProxy = RPC.getProxy(StoppedProtocol.class,
StoppedProtocol.versionID, null, conf); StoppedProtocol.versionID, null, conf);
StoppedInvocationHandler invocationHandler = (StoppedInvocationHandler) StoppedInvocationHandler invocationHandler = (StoppedInvocationHandler)
Proxy.getInvocationHandler(wrappedProxy); Proxy.getInvocationHandler(wrappedProxy);
StoppedProtocol proxy = (StoppedProtocol) RetryProxy.create(StoppedProtocol.class, StoppedProtocol proxy = (StoppedProtocol) RetryProxy.create(StoppedProtocol.class,
wrappedProxy, RetryPolicies.RETRY_FOREVER); wrappedProxy, RetryPolicies.RETRY_FOREVER);
assertEquals(invocationHandler.getCloseCalled(), 0); assertEquals(0, invocationHandler.getCloseCalled());
RPC.stopProxy(proxy); RPC.stopProxy(proxy);
assertEquals(invocationHandler.getCloseCalled(), 1); assertEquals(1, invocationHandler.getCloseCalled());
} }
@Test @Test
public void testErrorMsgForInsecureClient() throws IOException { public void testErrorMsgForInsecureClient() throws IOException {
Configuration serverConf = new Configuration(conf); Configuration serverConf = new Configuration(conf);
SecurityUtil.setAuthenticationMethod(AuthenticationMethod.KERBEROS, SecurityUtil.setAuthenticationMethod(AuthenticationMethod.KERBEROS,
serverConf); serverConf);
UserGroupInformation.setConfiguration(serverConf); UserGroupInformation.setConfiguration(serverConf);
final Server server = new RPC.Builder(serverConf).setProtocol(TestProtocol.class) final Server server = new RPC.Builder(serverConf).setProtocol(TestProtocol.class)
.setInstance(new TestImpl()).setBindAddress(ADDRESS).setPort(0) .setInstance(new TestImpl()).setBindAddress(ADDRESS).setPort(0)
.setNumHandlers(5).setVerbose(true).build(); .setNumHandlers(5).setVerbose(true).build();

View File

@ -54,17 +54,17 @@ public class TestNetworkTopologyWithNodeGroup {
@Test @Test
public void testNumOfChildren() throws Exception { public void testNumOfChildren() throws Exception {
assertEquals(cluster.getNumOfLeaves(), dataNodes.length); assertEquals(dataNodes.length, cluster.getNumOfLeaves());
} }
@Test @Test
public void testNumOfRacks() throws Exception { public void testNumOfRacks() throws Exception {
assertEquals(cluster.getNumOfRacks(), 3); assertEquals(3, cluster.getNumOfRacks());
} }
@Test @Test
public void testRacks() throws Exception { public void testRacks() throws Exception {
assertEquals(cluster.getNumOfRacks(), 3); assertEquals(3, cluster.getNumOfRacks());
assertTrue(cluster.isOnSameRack(dataNodes[0], dataNodes[1])); assertTrue(cluster.isOnSameRack(dataNodes[0], dataNodes[1]));
assertTrue(cluster.isOnSameRack(dataNodes[1], dataNodes[2])); assertTrue(cluster.isOnSameRack(dataNodes[1], dataNodes[2]));
assertFalse(cluster.isOnSameRack(dataNodes[2], dataNodes[3])); assertFalse(cluster.isOnSameRack(dataNodes[2], dataNodes[3]));
@ -76,7 +76,7 @@ public void testRacks() throws Exception {
@Test @Test
public void testNodeGroups() throws Exception { public void testNodeGroups() throws Exception {
assertEquals(cluster.getNumOfRacks(), 3); assertEquals(3, cluster.getNumOfRacks());
assertTrue(cluster.isOnSameNodeGroup(dataNodes[0], dataNodes[1])); assertTrue(cluster.isOnSameNodeGroup(dataNodes[0], dataNodes[1]));
assertFalse(cluster.isOnSameNodeGroup(dataNodes[1], dataNodes[2])); assertFalse(cluster.isOnSameNodeGroup(dataNodes[1], dataNodes[2]));
assertFalse(cluster.isOnSameNodeGroup(dataNodes[2], dataNodes[3])); assertFalse(cluster.isOnSameNodeGroup(dataNodes[2], dataNodes[3]));
@ -88,11 +88,11 @@ public void testNodeGroups() throws Exception {
@Test @Test
public void testGetDistance() throws Exception { public void testGetDistance() throws Exception {
assertEquals(cluster.getDistance(dataNodes[0], dataNodes[0]), 0); assertEquals(0, cluster.getDistance(dataNodes[0], dataNodes[0]));
assertEquals(cluster.getDistance(dataNodes[0], dataNodes[1]), 2); assertEquals(2, cluster.getDistance(dataNodes[0], dataNodes[1]));
assertEquals(cluster.getDistance(dataNodes[0], dataNodes[2]), 4); assertEquals(4, cluster.getDistance(dataNodes[0], dataNodes[2]));
assertEquals(cluster.getDistance(dataNodes[0], dataNodes[3]), 6); assertEquals(6, cluster.getDistance(dataNodes[0], dataNodes[3]));
assertEquals(cluster.getDistance(dataNodes[0], dataNodes[6]), 8); assertEquals(8, cluster.getDistance(dataNodes[0], dataNodes[6]));
} }
@Test @Test

View File

@ -28,100 +28,107 @@
public class TestGenericsUtil extends TestCase { public class TestGenericsUtil extends TestCase {
public void testToArray() { public void testToArray() {
//test a list of size 10 //test a list of size 10
List<Integer> list = new ArrayList<Integer>(); List<Integer> list = new ArrayList<Integer>();
for(int i=0; i<10; i++) { for(int i=0; i<10; i++) {
list.add(i); list.add(i);
} }
Integer[] arr = GenericsUtil.toArray(list); Integer[] arr = GenericsUtil.toArray(list);
for (int i = 0; i < arr.length; i++) { for (int i = 0; i < arr.length; i++) {
assertEquals(list.get(i), arr[i]); assertEquals(
"Array has identical elements as input list",
list.get(i), arr[i]);
} }
} }
public void testWithEmptyList() { public void testWithEmptyList() {
try { try {
List<String> list = new ArrayList<String>(); List<String> list = new ArrayList<String>();
String[] arr = GenericsUtil.toArray(list); String[] arr = GenericsUtil.toArray(list);
fail("Empty array should throw exception"); fail("Empty array should throw exception");
System.out.println(arr); //use arr so that compiler will not complain System.out.println(arr); //use arr so that compiler will not complain
}catch (IndexOutOfBoundsException ex) { }catch (IndexOutOfBoundsException ex) {
//test case is successful //test case is successful
} }
} }
public void testWithEmptyList2() { public void testWithEmptyList2() {
List<String> list = new ArrayList<String>(); List<String> list = new ArrayList<String>();
//this method should not throw IndexOutOfBoundsException //this method should not throw IndexOutOfBoundsException
String[] arr = GenericsUtil.<String>toArray(String.class, list); String[] arr = GenericsUtil.<String>toArray(String.class, list);
assertEquals(0, arr.length); assertEquals("Assert list creation w/ no elements results in length 0",
0, arr.length);
} }
/** This class uses generics */ /** This class uses generics */
private class GenericClass<T> { private class GenericClass<T> {
T dummy; T dummy;
List<T> list = new ArrayList<T>(); List<T> list = new ArrayList<T>();
void add(T item) { void add(T item) {
list.add(item); list.add(item);
} }
T[] funcThatUsesToArray() { T[] funcThatUsesToArray() {
T[] arr = GenericsUtil.toArray(list); T[] arr = GenericsUtil.toArray(list);
return arr; return arr;
} }
} }
public void testWithGenericClass() { public void testWithGenericClass() {
GenericClass<String> testSubject = new GenericClass<String>(); GenericClass<String> testSubject = new GenericClass<String>();
testSubject.add("test1"); testSubject.add("test1");
testSubject.add("test2"); testSubject.add("test2");
try { try {
//this cast would fail, if we had not used GenericsUtil.toArray, since the //this cast would fail, if we had not used GenericsUtil.toArray, since the
//rmethod would return Object[] rather than String[] //rmethod would return Object[] rather than String[]
String[] arr = testSubject.funcThatUsesToArray(); String[] arr = testSubject.funcThatUsesToArray();
assertEquals("test1", arr[0]); assertEquals("test1", arr[0]);
assertEquals("test2", arr[1]); assertEquals("test2", arr[1]);
}catch (ClassCastException ex) { }catch (ClassCastException ex) {
fail("GenericsUtil#toArray() is not working for generic classes"); fail("GenericsUtil#toArray() is not working for generic classes");
} }
} }
public void testGenericOptionsParser() throws Exception { public void testGenericOptionsParser() throws Exception {
GenericOptionsParser parser = new GenericOptionsParser( GenericOptionsParser parser = new GenericOptionsParser(
new Configuration(), new String[] {"-jt"}); new Configuration(), new String[] {"-jt"});
assertEquals(parser.getRemainingArgs().length, 0); assertEquals(0, parser.getRemainingArgs().length);
// test if -D accepts -Dx=y=z // test if -D accepts -Dx=y=z
parser = parser =
new GenericOptionsParser(new Configuration(), new GenericOptionsParser(new Configuration(),
new String[] {"-Dx=y=z"}); new String[] {"-Dx=y=z"});
assertEquals(parser.getConfiguration().get("x"), "y=z"); assertEquals(
"Options parser gets entire ='s expresion",
"y=z", parser.getConfiguration().get("x"));
} }
public void testGetClass() { public void testGetClass() {
//test with Integer //test with Integer
Integer x = new Integer(42); Integer x = new Integer(42);
Class<Integer> c = GenericsUtil.getClass(x); Class<Integer> c = GenericsUtil.getClass(x);
assertEquals(Integer.class, c); assertEquals("Correct generic type is acquired from object",
Integer.class, c);
//test with GenericClass<Integer> //test with GenericClass<Integer>
GenericClass<Integer> testSubject = new GenericClass<Integer>(); GenericClass<Integer> testSubject = new GenericClass<Integer>();
Class<GenericClass<Integer>> c2 = GenericsUtil.getClass(testSubject); Class<GenericClass<Integer>> c2 = GenericsUtil.getClass(testSubject);
assertEquals(GenericClass.class, c2); assertEquals("Inner generics are acquired from object.",
GenericClass.class, c2);
} }
} }

View File

@ -34,6 +34,7 @@ public void testConstructor() {
// Deserialize it back // Deserialize it back
FileHandle handle2 = new FileHandle(); FileHandle handle2 = new FileHandle();
handle2.deserialize(xdr.asReadOnlyWrap()); handle2.deserialize(xdr.asReadOnlyWrap());
Assert.assertEquals(handle.getFileId(), 1024); Assert.assertEquals("Failed: Assert 1024 is id ", 1024,
handle.getFileId());
} }
} }

View File

@ -53,19 +53,19 @@ public void testDuplicates() throws IOException {
IdUserGroup.updateMapInternal(uMap, "user", GET_ALL_USERS_CMD, ":"); IdUserGroup.updateMapInternal(uMap, "user", GET_ALL_USERS_CMD, ":");
assertTrue(uMap.size() == 5); assertTrue(uMap.size() == 5);
assertEquals(uMap.get(0), "root"); assertEquals("root", uMap.get(0));
assertEquals(uMap.get(11501), "hdfs"); assertEquals("hdfs", uMap.get(11501));
assertEquals(uMap.get(11502), "hdfs2"); assertEquals("hdfs2",uMap.get(11502));
assertEquals(uMap.get(2), "bin"); assertEquals("bin", uMap.get(2));
assertEquals(uMap.get(1), "daemon"); assertEquals("daemon", uMap.get(1));
IdUserGroup.updateMapInternal(gMap, "group", GET_ALL_GROUPS_CMD, ":"); IdUserGroup.updateMapInternal(gMap, "group", GET_ALL_GROUPS_CMD, ":");
assertTrue(gMap.size() == 3); assertTrue(gMap.size() == 3);
assertEquals(gMap.get(11501), "hdfs"); assertEquals("hdfs",gMap.get(11501));
assertEquals(gMap.get(497), "mapred"); assertEquals("mapred", gMap.get(497));
assertEquals(gMap.get(498), "mapred3"); assertEquals("mapred3", gMap.get(498));
} }
@Test @Test
public void testUserUpdateSetting() throws IOException { public void testUserUpdateSetting() throws IOException {
IdUserGroup iug = new IdUserGroup(); IdUserGroup iug = new IdUserGroup();

View File

@ -21,24 +21,26 @@
import org.junit.Test; import org.junit.Test;
public class TestXDR { public class TestXDR {
static final int WRITE_VALUE=23;
private void serializeInt(int times) { private void serializeInt(int times) {
XDR w = new XDR(); XDR w = new XDR();
for (int i = 0; i < times; ++i) for (int i = 0; i < times; ++i)
w.writeInt(23); w.writeInt(WRITE_VALUE);
XDR r = w.asReadOnlyWrap(); XDR r = w.asReadOnlyWrap();
for (int i = 0; i < times; ++i) for (int i = 0; i < times; ++i)
Assert.assertEquals(r.readInt(), 23); Assert.assertEquals(
WRITE_VALUE,r.readInt());
} }
private void serializeLong(int times) { private void serializeLong(int times) {
XDR w = new XDR(); XDR w = new XDR();
for (int i = 0; i < times; ++i) for (int i = 0; i < times; ++i)
w.writeLongAsHyper(23); w.writeLongAsHyper(WRITE_VALUE);
XDR r = w.asReadOnlyWrap(); XDR r = w.asReadOnlyWrap();
for (int i = 0; i < times; ++i) for (int i = 0; i < times; ++i)
Assert.assertEquals(r.readHyper(), 23); Assert.assertEquals(WRITE_VALUE, r.readHyper());
} }
@Test @Test