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