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

Phil Race prr at openjdk.org
Thu Jul 24 19:21:00 UTC 2025


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

>> Prasanta Sadhukhan has updated the pull request incrementally with one additional commit since the last revision:
>> 
>>   Remove unneeded vars
>
> 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((Image)null);
                           } else if (v == ArgVal.INVALIDDATA) {
                               expected = false;
                               new ImageIcon((Image)Toolkit.getDefaultToolkit().createImage(fileName), "gif");
                               passed = true; // no exception expected for this case
                           }
                           break;
                    }
                } catch (NullPointerException e) {
                    if (expected) {
                        passed = true;
                    }
/*
// I don't know why you have this in your version
                } catch (Exception ex) {
                    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_r2229369799


More information about the client-libs-dev mailing list