RFR: 8159055: ImageIcon.setImage can't handle null parameter [v14]
Phil Race
prr at openjdk.org
Tue Jul 22 18:23:03 UTC 2025
On Tue, 8 Jul 2025 12:56:07 GMT, Alexey Ivanov <aivanov at openjdk.org> wrote:
>> Prasanta Sadhukhan has updated the pull request incrementally with one additional commit since the last revision:
>>
>> constructor test removal
>
> test/jdk/javax/swing/ImageIcon/ImageIconNullImageTest.java line 34:
>
>> 32: import javax.swing.ImageIcon;
>> 33:
>> 34: public class ImageIconNullImageTest {
>
> I guess we're going to update the test later with follow-up fixes…
>
> However, I suggest dropping `ImageIcon-` from the test class name — the test is in `ImageIcon` folder, therefore it tests `ImageIcon`.
I don't have a problem with the test name .. either way .. but if you drop it from the test name you should also drop it from all the method names.
This test is really long. I thought I sent you a more succinct way of doing it.
Here's what I wrote - tests only the null cases but its a start.
import java.awt.Image;
import java.io.File;
import java.net.URL;
import javax.swing.ImageIcon;
public class ImageIconTest {
static enum ArgType { FILE, URL, BYTEARRAY, IMAGE };
public static void main(String[] args) throws Exception {
String s = null;
byte[] b = null;
Image i = null;
URL u = null;
for (ArgType a : ArgType.values()) {
boolean expected = true;
boolean passed = false;
try {
switch (a) {
case FILE :
expected = false;
new ImageIcon((String)null);
passed = true; // no exception expected for this case
break;
case URL :
new ImageIcon((URL)null);
break;
case BYTEARRAY :
byte[] bytes = null;
new ImageIcon(bytes);
break;
case IMAGE :
new ImageIcon((Image)null);
break;
}
} catch (NullPointerException e) {
if (expected) passed = true;
}
if (expected && !passed) {
System.err.println("Did not receive expected exception for : " + a);
throw new RuntimeException("Test failed");
}
if (!expected && !passed) {
System.err.println("Received unexpected exception for : " + a);
throw new RuntimeException("Test failed");
}
}
// test setter
try {
ImageIcon ii = new ImageIcon();
ii.setImage((Image)null);
throw new RuntimeException("No NPE");
} catch (NullPointerException e) {
// expected
}
}
}
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/25767#discussion_r2223383983
More information about the client-libs-dev
mailing list