1 /*
   2  * Copyright (c) 2003, 2017, 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 4481957
  27  * @key headful
  28  * @summary Tests that applet-supplied ImageReader, ImageWriter, and
  29  *          IIOMetadataFormat implementations do not throw unexpected exceptions
  30  *          when indirectly attempting to access ResourceBundles
  31  * @run main AppletResourceTest
  32  * @run applet AppletResourceTest.html
  33  */
  34 
  35 import java.applet.Applet;
  36 import java.awt.Rectangle;
  37 import java.awt.image.BufferedImage;
  38 import java.io.IOException;
  39 import java.util.Iterator;
  40 import java.util.ListResourceBundle;
  41 import java.util.Locale;
  42 import java.util.MissingResourceException;
  43 import java.util.Vector;
  44 
  45 import javax.imageio.IIOException;
  46 import javax.imageio.ImageReadParam;
  47 import javax.imageio.ImageReader;
  48 import javax.imageio.ImageTypeSpecifier;
  49 import javax.imageio.event.IIOReadWarningListener;
  50 import javax.imageio.metadata.IIOInvalidTreeException;
  51 import javax.imageio.metadata.IIOMetadata;
  52 import javax.imageio.spi.ImageReaderSpi;
  53 
  54 import org.w3c.dom.Node;
  55 
  56 public class AppletResourceTest extends Applet {
  57 
  58     public static void main(String[] argv) {
  59         new AppletResourceTest().init();
  60     }
  61 
  62     public void init() {
  63         DummyImageReaderImpl reader;
  64         MyReadWarningListener listener = new MyReadWarningListener();
  65         Locale[] locales = {new Locale("ru"),
  66                             new Locale("fr"),
  67                             new Locale("uk")};
  68 
  69         reader = new DummyImageReaderImpl(new DummyImageReaderSpiImpl());
  70         reader.setAvailableLocales(locales);
  71         reader.setLocale(new Locale("fr"));
  72         reader.addIIOReadWarningListener(listener);
  73 
  74         String baseName = "AppletResourceTest$BugStats";
  75         try {
  76             reader.processWarningOccurred("WarningMessage");
  77             reader.processWarningOccurred(baseName, "water");
  78         } catch (MissingResourceException mre) {
  79             throw new RuntimeException("Test failed: couldn't load resource");
  80         }
  81 
  82 
  83     }
  84 
  85     private class MyReadWarningListener implements IIOReadWarningListener {
  86         public void warningOccurred(ImageReader source,
  87                                     String warning)
  88             {
  89                 System.out.println("warning occurred: " + warning);
  90             }
  91     }
  92 
  93     public static class BugStats extends ListResourceBundle {
  94 
  95         public Object[][] getContents(){
  96             return contents;
  97         }
  98 
  99         private Object[][] contents = {
 100             {"coffee", new String("coffee from Stats class")},
 101             {"tea", new String("tea from Stats class")},
 102             {"water", new String("water from Stats class")}
 103         };
 104     }
 105 
 106 
 107     public static class DummyImageReaderImpl extends ImageReader {
 108 
 109         public DummyImageReaderImpl(ImageReaderSpi originatingProvider) {
 110             super(originatingProvider);
 111         }
 112 
 113         public int getNumImages(boolean allowSearch) throws IOException {
 114             return 5;
 115         }
 116 
 117         public int getWidth(int imageIndex) throws IOException {
 118             if (input == null)
 119                 throw new IllegalStateException();
 120             if (imageIndex >= 5 || imageIndex < 0)
 121                 throw new IndexOutOfBoundsException();
 122 
 123             return 10;
 124         }
 125 
 126         public int getHeight(int imageIndex) throws IOException {
 127             if (input == null)
 128                 throw new IllegalStateException();
 129             if (imageIndex >= 5 || imageIndex < 0)
 130                 throw new IndexOutOfBoundsException();
 131 
 132             return 15;
 133         }
 134 
 135         public Iterator getImageTypes(int imageIndex) throws IOException {
 136             if (input == null)
 137                 throw new IllegalStateException();
 138             if (imageIndex >= 5 || imageIndex < 0)
 139                 throw new IndexOutOfBoundsException();
 140 
 141             Vector imageTypes = new Vector();
 142             imageTypes.add(ImageTypeSpecifier.createFromBufferedImageType
 143                            (BufferedImage.TYPE_BYTE_GRAY ));
 144             return imageTypes.iterator();
 145         }
 146 
 147         public IIOMetadata getStreamMetadata() throws IOException {
 148             return new DummyIIOMetadataImpl(true, null, null, null, null);
 149         }
 150 
 151         public IIOMetadata getImageMetadata(int imageIndex)
 152           throws IOException {
 153 
 154             if (input == null)
 155                 throw new IllegalStateException();
 156             if (imageIndex >= 5 || imageIndex < 0)
 157                 throw new IndexOutOfBoundsException();
 158             if (seekForwardOnly) {
 159                 if (imageIndex < minIndex)
 160                     throw new IndexOutOfBoundsException();
 161                 minIndex = imageIndex;
 162             }
 163             return new DummyIIOMetadataImpl(true, null, null, null, null);
 164         }
 165 
 166 
 167         public BufferedImage read(int imageIndex, ImageReadParam param)
 168           throws IOException {
 169             if (input == null)
 170                 throw new IllegalStateException();
 171             if (imageIndex >= 5 || imageIndex < 0)
 172                 throw new IndexOutOfBoundsException();
 173             if (seekForwardOnly) {
 174                 if (imageIndex < minIndex)
 175                     throw new IndexOutOfBoundsException();
 176                 minIndex = imageIndex;
 177             }
 178 
 179             return getDestination(param, getImageTypes(imageIndex), 10, 15);
 180         }
 181 
 182 // protected  methods - now public
 183 
 184         public  boolean abortRequested() {
 185             return super.abortRequested();
 186         }
 187 
 188         public  void clearAbortRequest() {
 189             super.clearAbortRequest();
 190         }
 191 
 192         public  void processImageComplete() {
 193             super.processImageComplete();
 194         }
 195 
 196         public  void processImageProgress(float percentageDone) {
 197             super.processImageProgress(percentageDone);
 198         }
 199 
 200         public  void processImageStarted(int imageIndex) {
 201             super.processImageStarted(imageIndex);
 202         }
 203 
 204         public  void processImageUpdate(BufferedImage theImage,
 205                                         int minX,
 206                                         int minY,
 207                                         int width,
 208                                         int height,
 209                                         int periodX,
 210                                         int periodY,
 211                                         int[] bands) {
 212             super.processImageUpdate(theImage,
 213                                      minX,
 214                                      minY,
 215                                      width,
 216                                      height,
 217                                      periodX,
 218                                      periodY,
 219                                      bands);
 220         }
 221 
 222         public  void processPassComplete(BufferedImage theImage) {
 223             super. processPassComplete(theImage);
 224         }
 225 
 226         public  void processPassStarted(BufferedImage theImage,
 227                                         int pass, int minPass,
 228                                         int maxPass,
 229                                         int minX,
 230                                         int minY,
 231                                         int periodX,
 232                                         int periodY,
 233                                         int[] bands) {
 234             super.processPassStarted(theImage,
 235                                      pass,
 236                                      minPass,
 237                                      maxPass,
 238                                      minX,
 239                                      minY,
 240                                      periodX,
 241                                      periodY,
 242                                      bands);
 243         }
 244 
 245         public  void processReadAborted() {
 246             super.processReadAborted();
 247         }
 248 
 249         public  void processSequenceComplete() {
 250             super.processSequenceComplete();
 251         }
 252 
 253         public  void processSequenceStarted(int minIndex) {
 254             super.processSequenceStarted(minIndex);
 255         }
 256 
 257         public  void processThumbnailComplete() {
 258             super.processThumbnailComplete();
 259         }
 260 
 261         public  void processThumbnailPassComplete(BufferedImage theThumbnail) {
 262             super.processThumbnailPassComplete(theThumbnail);
 263         }
 264 
 265         public  void processThumbnailPassStarted(BufferedImage theThumbnail,
 266                                                  int pass,
 267                                                  int minPass,
 268                                                  int maxPass,
 269                                                  int minX,
 270                                                  int minY,
 271                                                  int periodX,
 272                                                  int periodY,
 273                                                  int[] bands) {
 274             super.processThumbnailPassStarted(theThumbnail,
 275                                               pass,
 276                                               minPass,
 277                                               maxPass,
 278                                               minX,
 279                                               minY,
 280                                               periodX,
 281                                               periodY,
 282                                               bands);
 283         }
 284 
 285         public  void processThumbnailProgress(float percentageDone) {
 286             super.processThumbnailProgress(percentageDone);
 287         }
 288 
 289         public  void processThumbnailStarted(int imageIndex, int thumbnailIndex) {
 290             super.processThumbnailStarted(imageIndex, thumbnailIndex);
 291         }
 292 
 293         public  void processThumbnailUpdate(BufferedImage theThumbnail,
 294                                             int minX,
 295                                             int minY,
 296                                             int width,
 297                                             int height,
 298                                             int periodX,
 299                                             int periodY,
 300                                             int[] bands) {
 301             super.processThumbnailUpdate(theThumbnail,
 302                                          minX,
 303                                          minY,
 304                                          width,
 305                                          height,
 306                                          periodX,
 307                                          periodY,
 308                                          bands);
 309         }
 310 
 311         public  void processWarningOccurred(String warning) {
 312             super.processWarningOccurred(warning);
 313         }
 314 
 315 
 316 
 317         public static Rectangle getSourceRegion(ImageReadParam param,
 318                                                 int srcWidth,
 319                                                 int srcHeight) {
 320             return ImageReader.getSourceRegion(param, srcWidth, srcHeight);
 321         }
 322 
 323         public static void computeRegions(ImageReadParam param,
 324                                           int srcWidth,
 325                                           int srcHeight,
 326                                           BufferedImage image,
 327                                           Rectangle srcRegion,
 328                                           Rectangle destRegion) {
 329             ImageReader.computeRegions(param,
 330                                        srcWidth,
 331                                        srcHeight,
 332                                        image,
 333                                        srcRegion,
 334                                        destRegion);
 335         }
 336 
 337         public static void checkReadParamBandSettings(ImageReadParam param,
 338                                                       int numSrcBands,
 339                                                       int numDstBands) {
 340             ImageReader.checkReadParamBandSettings( param,
 341                                                     numSrcBands,
 342                                                     numDstBands);
 343         }
 344 
 345         public static BufferedImage getDestination(ImageReadParam param,
 346                                                    Iterator imageTypes,
 347                                                    int width,
 348                                                    int height) throws IIOException {
 349             return ImageReader.getDestination(param,
 350                                               imageTypes,
 351                                               width,
 352                                               height);
 353         }
 354 
 355         public  void setAvailableLocales(Locale[] locales) {
 356             if (locales == null || locales.length == 0)
 357                 availableLocales = null;
 358             else
 359                 availableLocales = (Locale[])locales.clone();
 360         }
 361 
 362         public  void processWarningOccurred(String baseName, String keyword) {
 363             super.processWarningOccurred(baseName, keyword);
 364         }
 365     }
 366 
 367     public static class DummyIIOMetadataImpl extends IIOMetadata {
 368 
 369         public DummyIIOMetadataImpl() {
 370             super();
 371         }
 372 
 373         public DummyIIOMetadataImpl(boolean standardMetadataFormatSupported,
 374                                     String nativeMetadataFormatName,
 375                                     String nativeMetadataFormatClassName,
 376                                     String[] extraMetadataFormatNames,
 377                                     String[] extraMetadataFormatClassNames) {
 378             super(standardMetadataFormatSupported,
 379                   nativeMetadataFormatName,
 380                   nativeMetadataFormatClassName,
 381                   extraMetadataFormatNames,
 382                   extraMetadataFormatClassNames);
 383         }
 384 
 385         public boolean isReadOnly() {
 386             return true;
 387         }
 388 
 389         public Node getAsTree(String formatName) {
 390             return null;
 391         }
 392 
 393         public void mergeTree(String formatName, Node root)
 394           throws IIOInvalidTreeException {
 395             throw new IllegalStateException();
 396         }
 397 
 398         public void reset() {
 399             throw new IllegalStateException();
 400         }
 401     }
 402 
 403     public static class DummyImageReaderSpiImpl extends ImageReaderSpi {
 404 
 405         static final String[] names ={ "myformat" };
 406 
 407         public DummyImageReaderSpiImpl() {
 408             super("vendorName",
 409                   "version",
 410                   names,
 411                   null,
 412                   null,
 413                   "DummyImageReaderImpl",
 414                   STANDARD_INPUT_TYPE,
 415                   null,
 416                   true,
 417                   null,
 418                   null,
 419                   null,
 420                   null,
 421                   true,
 422                   null,
 423                   null,
 424                   null,
 425                   null);
 426         }
 427         public boolean canDecodeInput(Object source)
 428           throws IOException {
 429             return true;
 430         }
 431         public ImageReader createReaderInstance(Object extension)
 432           throws IOException {
 433             return new DummyImageReaderImpl(this);
 434         }
 435         public String getDescription(Locale locale) {
 436             return "DummyImageReaderSpiImpl";
 437         }
 438     }
 439 }