RFR: 8159055: ImageIcon.setImage can't handle null parameter [v18]

Prasanta Sadhukhan psadhukhan at openjdk.org
Fri Jul 25 03:36:45 UTC 2025


On Thu, 24 Jul 2025 19:17:43 GMT, Phil Race <prr at openjdk.org> wrote:

>> test/jdk/javax/swing/ImageIcon/ImageIconTest.java line 54:
>> 
>>> 52: 
>>> 53:         for (ArgType a : ArgType.values()) {
>>> 54:             for (final boolean invalid : new boolean[]{false, true}) {
>> 
>> This "invalid==false" means use null and "invalid=true" means use non-null but invalid image data 
>> seems odd.
>> 
>> Can we rename invalid to "invalidImageData" ? Or may be better an other enum ?
>
> Like this ?
> 
> 
> import java.io.File;
> import java.io.FileOutputStream;
> import java.net.URL;
> import java.util.Random;
> import java.awt.Image;
> import java.awt.Toolkit;
> import javax.swing.ImageIcon;
> 
> public class ImageIconTest {
> 
>     static enum ArgType { FILE, URL, BYTEARRAY, IMAGE };
>     static enum ArgVal { NULL, INVALIDDATA };
> 
>     public static void main(String[] args) throws Exception {
> 
>         String imgName = "invalid.gif";
>         byte[] invalidData = new byte[100];
>         new Random().nextBytes(invalidData);
>         try (FileOutputStream fos = new FileOutputStream(imgName)) {
>             fos.write(invalidData);
>         }
>         String fileName = (new File(System.getProperty("test.src", "."), imgName)).getName();
> 
>         for (ArgType a : ArgType.values()) {
>             for (final ArgVal v : ArgVal.values()) {
>                 System.out.println("Testing for ArgType " + a + " for case " + v);
>                 boolean expected = true;
>                 boolean passed = false;
>                 try {
>                     switch (a) {
>                        case FILE :
>                            expected = false;
>                            String s = (v == ArgVal.NULL) ? null : fileName;
>                            new ImageIcon(s);
>                            passed = true; // no exception expected for this case
>                            break;
>                        case URL :
>                            if (v == ArgVal.NULL) {
>                                new ImageIcon((URL)null);
>                            } else if (v == ArgVal.INVALIDDATA) {
>                                expected = false;
>                                new ImageIcon("file://" + imgName, "gif");
>                                passed = true; // no exception expected for this case
>                            }
>                            break;
>                        case BYTEARRAY :
>                            if (v == ArgVal.NULL) {
>                                byte[] bytes = null;
>                                new ImageIcon(bytes);
>                            } else if (v == ArgVal.INVALIDDATA) {
>                                expected = false;
>                                new ImageIcon(new byte[0], "gif");
>                                passed = true; // no exception expected for this case
>                            }
>                            break;
>                        case IMAGE :
>                            if (v == ArgVal.NULL) {
>                                new ImageIcon((Ima...

Updated

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

PR Review Comment: https://git.openjdk.org/jdk/pull/25767#discussion_r2230063306


More information about the client-libs-dev mailing list