Add final junit testcase and fix bugs