Add test cases to Java implementation and fix unwanted runtime errors
("behavior_loop sampled before it was looped") in various loop cases.