1

I recently discovered that one of my units tests fails due to a bug in the JDK i.e. JDK-8080774

What is the best way of writing a conditional unit test – which checks the output one way for JDK versions where the bug fix is present and a different way when the bug fix is absent?

In case of JDK bug 8080774 – it is not fixed at all in JDK 6 or older, it is fixed in JDK version 7u91 or higher as well as in JDK version 8u60 or higher

I already considered parsing the output of System.getProperty("java.version") but I am looking for a better/more elegant solution, if possible.

For example Apache Commons has SystemUtils.isJavaVersionAtLeast() method but it does not have enough granularity.

Adrian
  • 6,013
  • 10
  • 47
  • 68
  • 3
    Why are you still using old, insecure, buggy versions of the JDK? Is there any way to get everything current and avoid a whole bunch of issues? – Brian Knoblauch Dec 08 '15 at 14:23
  • 1
    @Brian The JDK upgrade process is managed by the production support team - outside my control – Adrian Dec 08 '15 at 14:29
  • 4
    If this is for production, your code needs to work around the bug, so you should test that your code produces the correct output on all versions. It would be very odd to have your tests verify that, yes, it does produce the incorrect output in the versions of Java that have this bug. – khelwood Dec 08 '15 at 14:35
  • 1
    I don't know what's not elegant about parsing the actual Java version when the test should act different based on the Java version. – Gimby Dec 08 '15 at 14:42
  • @Gimby Parsing a string and dealing with all sorts of corner cases seems to me like reinventing the wheel – Adrian Dec 08 '15 at 14:54
  • Note: it is apparently possible to change the default date formatting, see: http://www.coderanch.com/t/482904/java/java/setting-custom-Date-format-globally – ebyrob Dec 08 '15 at 15:13

1 Answers1

1

EDIT - Posted before the question was clearly mentioning this

As Brian pointed out, you need to get the latest stable version of JDK as much as possible.

However, you can get the VM version by fetching java.version property at runtime.

Please see this post for more information

Community
  • 1
  • 1
aksappy
  • 3,400
  • 3
  • 23
  • 49
  • I already considered parsing the output of System.getProperty("java.version") but I am looking for a better solution. – Adrian Dec 08 '15 at 14:30
  • That was not in the question at all :) – aksappy Dec 08 '15 at 14:31
  • No worries.. You sure you checked the post? There are other ways too! – aksappy Dec 08 '15 at 14:35
  • 1
    I checked this post and any remotely relevant posts on SO already e.g.https://stackoverflow.com/questions/198431/how-do-you-compare-two-version-strings-in-java ; most people would go with writing (an imperfect) parsing routine. – Adrian Dec 08 '15 at 14:58
  • I've done this in the past and had perfectly acceptable (for an imperfect situation) results. – Brian Knoblauch Dec 08 '15 at 17:48