HADOOP-10220: add tests and fix a bug.