Ensure HABTM relationships produce valid class names (Fixes #17119)

See https://github.com/rails/rails/issues/17119 for the back story.
It seems to be [available, but turned off by default](https://kenai.com/projects/jruby/pages/PerformanceTuning). 

Since this is only in a test, do you think it is unreasonable to require JRuby users to turn `ObjectSpace` on for the purposes of running the tests? (i.e., it will not affect anyone using Rails, only those running the tests)

Unfortunately, `const_get` and `const_defined?` behave differently between 1.9.3, 2.0, and 2.1, so we cannot use those (if they are even available by default in JRuby -- which I'm not sure of, since they might rely on `ObjectSpace` for all I know).