Adding tests for cast exception and removing a redundant stream close