-
Notifications
You must be signed in to change notification settings - Fork 542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SUREFIRE-1939] Fix NPE in SystemUtils.toJdkHomeFromJvmExec if java home has 2 components #387
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,15 @@ public void incorrectJdkPath() | |
assertThat( SystemUtils.isJava9AtLeast( incorrect.getAbsolutePath() ) ).isFalse(); | ||
} | ||
|
||
@Test | ||
public void incorrectJdkPathShouldNotNPE() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is Any inputs on these points? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, the reason I submitted this is because unit tests fail, due to this: on my machine JAVA_HOME is
This leads to Also, the test is about incorrect JDK paths, and such a path is an incorrect JDK path. However, one incorrect case crashes with a non-informative NPE. I would say a "cannot find jvm executable" error on the return-null path would be more helpful for users that misconfigured their systems. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @CMoH There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've backtracked the production scope callers of the SystemUtils method and the parameter can come from three possible places:
The only one that could in theory lead to the NPE is the Conclusion: This is not an actual problem in production. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the confusion here stems from the fact that the original test
To explain what the original test case does: The test assumes that Not entirely sure why this particular scenario is tested. Sort of a negative test case to check there are no false positive results with slight deviations from correct input probably. Since (2) could as well be deliberate I would suggest only fixing (1) by adding an assumption, for example like this: @Test
public void incorrectJdkPath()
{
File jre = new File( System.getProperty( "java.home" ) );
assumeTrue( "jre".equals( jre.getName() )
&& (new File(jre.getParentFile(), "bin" + separator + "javac").exists()
|| new File(jre.getParentFile(), "bin" + separator + "javac.exe").exists()) );
File jdk = jre.getParentFile();
File incorrect = jdk.getParentFile();
assertThat( SystemUtils.isJava9AtLeast( incorrect.getAbsolutePath() ) ).isFalse();
} The If one is convinced that (2) is a coding error rather than a deliberate choice, one could replace the second-to-last line with something like this: File incorrect = new File( jdk.getParentFile(), "not_java.exe" ); |
||
{ | ||
File jre = new File( "/opt/jdk" ); | ||
File jdk = jre.getParentFile(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The The whole setup ( I would write the whole test as a one-liner and name it something like assertThat( SystemUtils.isJava9AtLeast( new File( "/" ) ) ).isFalse(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, as far as the new test goes, see #387 (comment). Feel free to remove the extra test. The reason for submitting this is because I had another issue, but after the initial clone of this plugin I got a NPE at the first build. I hoped fixing this would help others. From my perspective, this null-check is both conservative and more than insignificant, in short not worthy of this elaborate debate about it. |
||
File incorrect = jdk.getParentFile(); | ||
assertThat( SystemUtils.isJava9AtLeast( incorrect.getAbsolutePath() ) ).isFalse(); | ||
} | ||
|
||
@Test | ||
public void shouldHaveJavaPath() | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have unit test for it ... ?
If not please create - it should be easy
Unit test should coverage all cases described in java doc for this method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I didn't feel the need to add a new unit test since the existing unit test fails with NPE under the described conditions (i.e JAVA_HOME set to a directory 2 levels up from root,
/opt/openjdk-bin-11.0.11_p9
in my case).However, I have added a system-independent unit test to showcase the situation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel the fix is more complete now. Thanks for the comment