|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8159055 |
| 27 | + * @summary Verifies null parameter and invalid data handling of |
| 28 | + * ImageIcon constructors and setImage method |
| 29 | + * @run main ImageIconTest |
| 30 | + */ |
| 31 | + |
| 32 | +import java.io.File; |
| 33 | +import java.io.FileOutputStream; |
| 34 | +import java.net.URI; |
| 35 | +import java.net.URL; |
| 36 | +import java.util.Random; |
| 37 | +import java.awt.Image; |
| 38 | +import java.awt.Toolkit; |
| 39 | +import javax.swing.ImageIcon; |
| 40 | + |
| 41 | +public class ImageIconTest { |
| 42 | + |
| 43 | + static enum ArgType { FILE, URL, BYTE_ARRAY, IMAGE, SET_IMAGE }; |
| 44 | + static enum ArgVal { NULL, INVALID_DATA }; |
| 45 | + |
| 46 | + public static void main(String[] args) throws Exception { |
| 47 | + |
| 48 | + String imgName = "invalid.gif"; |
| 49 | + byte[] invalidData = new byte[100]; |
| 50 | + new Random().nextBytes(invalidData); |
| 51 | + try (FileOutputStream fos = new FileOutputStream(imgName)) { |
| 52 | + fos.write(invalidData); |
| 53 | + } |
| 54 | + File file = new File(imgName); |
| 55 | + file.deleteOnExit(); |
| 56 | + |
| 57 | + for (ArgType a : ArgType.values()) { |
| 58 | + for (final ArgVal v : ArgVal.values()) { |
| 59 | + System.out.println("Testing for ArgType " + a + " for case " + v); |
| 60 | + boolean expected = true; |
| 61 | + boolean passed = false; |
| 62 | + try { |
| 63 | + switch (a) { |
| 64 | + |
| 65 | + case FILE : |
| 66 | + |
| 67 | + expected = false; |
| 68 | + String s = (v == ArgVal.NULL) ? null : imgName; |
| 69 | + new ImageIcon(s); |
| 70 | + passed = true; // no exception expected for this case |
| 71 | + break; |
| 72 | + |
| 73 | + case URL : |
| 74 | + |
| 75 | + if (v == ArgVal.NULL) { |
| 76 | + |
| 77 | + new ImageIcon((URL)null); |
| 78 | + |
| 79 | + } else if (v == ArgVal.INVALID_DATA) { |
| 80 | + expected = false; |
| 81 | + new ImageIcon(new URI("file://" + imgName).toURL()); |
| 82 | + passed = true; // no exception expected for this case |
| 83 | + |
| 84 | + } |
| 85 | + break; |
| 86 | + |
| 87 | + case BYTE_ARRAY : |
| 88 | + |
| 89 | + if (v == ArgVal.NULL) { |
| 90 | + |
| 91 | + byte[] bytes = null; |
| 92 | + new ImageIcon(bytes); |
| 93 | + |
| 94 | + } else if (v == ArgVal.INVALID_DATA) { |
| 95 | + expected = false; |
| 96 | + |
| 97 | + new ImageIcon(invalidData); |
| 98 | + |
| 99 | + passed = true; // no exception expected for this case |
| 100 | + } |
| 101 | + break; |
| 102 | + |
| 103 | + case IMAGE : |
| 104 | + |
| 105 | + if (v == ArgVal.NULL) { |
| 106 | + |
| 107 | + new ImageIcon((Image)null); |
| 108 | + |
| 109 | + } else if (v == ArgVal.INVALID_DATA) { |
| 110 | + expected = false; |
| 111 | + |
| 112 | + new ImageIcon((Image)Toolkit.getDefaultToolkit().createImage(imgName)); |
| 113 | + |
| 114 | + passed = true; // no exception expected for this case |
| 115 | + } |
| 116 | + break; |
| 117 | + |
| 118 | + case SET_IMAGE : |
| 119 | + |
| 120 | + ImageIcon ii = new ImageIcon(); |
| 121 | + |
| 122 | + if (v == ArgVal.NULL) { |
| 123 | + |
| 124 | + ii.setImage((Image) null); |
| 125 | + |
| 126 | + } else if (v == ArgVal.INVALID_DATA) { |
| 127 | + expected = false; |
| 128 | + |
| 129 | + ii.setImage((Image)Toolkit.getDefaultToolkit().createImage(imgName)); |
| 130 | + |
| 131 | + passed = true; // no exception expected for this case |
| 132 | + } |
| 133 | + break; |
| 134 | + } |
| 135 | + } catch (NullPointerException e) { |
| 136 | + if (expected) { |
| 137 | + passed = true; |
| 138 | + } |
| 139 | + } |
| 140 | + if (expected && !passed) { |
| 141 | + throw new RuntimeException("Did not receive expected exception for : " + a); |
| 142 | + } |
| 143 | + if (!expected && !passed) { |
| 144 | + throw new RuntimeException("Received unexpected exception for : " + a); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + } |
| 150 | +} |
0 commit comments