On Wed, 6 Aug 2025 02:58:01 GMT, Prasanta Sadhukhan <psadhuk...@openjdk.org> 
wrote:

>> When trying to call 'icon.setImage(null);' where 'icon' is an instance of 
>> ImageIcon, a null pointer exception is thrown at runtime.
>> The code tried to get the `id` for that image and instantiates 
>> `MediaTracker` to associate the null image to that `id` and checks the 
>> status of loading this null image, removes the null image from the tracker 
>> and then tries to get the image width where it throws NPE as image is null.
>> 
>> It's better to not go through all MediaTracker usage and bail out initially 
>> itself for null image..
>
> Prasanta Sadhukhan has updated the pull request incrementally with one 
> additional commit since the last revision:
> 
>   Test update

Don't you want to use JUnit for 
`test/jdk/javax/swing/ImageIcon/ImageIconTest.java`?

I think using separate small methods to test `ImageIcon` constructors is much 
clearer and readable than switching over a set of enums. Moreover, a JUnit test 
will run all the test methods, which means you'll get the full picture instead 
of failing the entire test when the a first assertion fails.

[I posted `ImageIconConstructorsTest.java` that uses 
JUnit](https://github.com/openjdk/jdk/pull/25767#issuecomment-3049620519). To 
run this test with jtreg add the following tags:


/*
 * @test
 * @summary Verifies behavior of ImageIcon constructors with null parameters
 * @run junit ImageIconConstructorsTest
 */
public final class ImageIconConstructorsTest {
}


You'll have to add test methods for invalid files and invalid data, though.

The jtreg report looks like this:

----------System.out:(0/0)----------
----------System.err:(31/2237)----------
STARTED    ImageIconConstructorsTest::stringNull 'stringNull()'
SUCCESSFUL ImageIconConstructorsTest::stringNull 'stringNull()' [15ms]
STARTED    ImageIconConstructorsTest::noArgs 'noArgs()'
SUCCESSFUL ImageIconConstructorsTest::noArgs 'noArgs()' [0ms]
STARTED    ImageIconConstructorsTest::imageNull 'imageNull()'
SUCCESSFUL ImageIconConstructorsTest::imageNull 'imageNull()' [0ms]
...

-------------

PR Comment: https://git.openjdk.org/jdk/pull/25767#issuecomment-3198087156

Reply via email to