Path: blob/master/test/jdk/javax/imageio/AppletResourceTest.java
41145 views
/*1* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 448195726* @summary Tests that applet-supplied ImageReader, ImageWriter, and27* IIOMetadataFormat implementations do not throw unexpected exceptions28* when indirectly attempting to access ResourceBundles29* @run main AppletResourceTest30*/3132import java.awt.Rectangle;33import java.awt.image.BufferedImage;34import java.io.IOException;35import java.util.Iterator;36import java.util.ListResourceBundle;37import java.util.Locale;38import java.util.MissingResourceException;39import java.util.Vector;4041import javax.imageio.IIOException;42import javax.imageio.ImageReadParam;43import javax.imageio.ImageReader;44import javax.imageio.ImageTypeSpecifier;45import javax.imageio.event.IIOReadWarningListener;46import javax.imageio.metadata.IIOInvalidTreeException;47import javax.imageio.metadata.IIOMetadata;48import javax.imageio.spi.ImageReaderSpi;4950import org.w3c.dom.Node;5152public class AppletResourceTest {5354public static void main(String[] argv) {55new AppletResourceTest().init();56}5758public void init() {59DummyImageReaderImpl reader;60MyReadWarningListener listener = new MyReadWarningListener();61Locale[] locales = {new Locale("ru"),62new Locale("fr"),63new Locale("uk")};6465reader = new DummyImageReaderImpl(new DummyImageReaderSpiImpl());66reader.setAvailableLocales(locales);67reader.setLocale(new Locale("fr"));68reader.addIIOReadWarningListener(listener);6970String baseName = "AppletResourceTest$BugStats";71try {72reader.processWarningOccurred("WarningMessage");73reader.processWarningOccurred(baseName, "water");74} catch (MissingResourceException mre) {75throw new RuntimeException("Test failed: couldn't load resource");76}777879}8081private class MyReadWarningListener implements IIOReadWarningListener {82public void warningOccurred(ImageReader source,83String warning)84{85System.out.println("warning occurred: " + warning);86}87}8889public static class BugStats extends ListResourceBundle {9091public Object[][] getContents(){92return contents;93}9495private Object[][] contents = {96{"coffee", new String("coffee from Stats class")},97{"tea", new String("tea from Stats class")},98{"water", new String("water from Stats class")}99};100}101102103public static class DummyImageReaderImpl extends ImageReader {104105public DummyImageReaderImpl(ImageReaderSpi originatingProvider) {106super(originatingProvider);107}108109public int getNumImages(boolean allowSearch) throws IOException {110return 5;111}112113public int getWidth(int imageIndex) throws IOException {114if (input == null)115throw new IllegalStateException();116if (imageIndex >= 5 || imageIndex < 0)117throw new IndexOutOfBoundsException();118119return 10;120}121122public int getHeight(int imageIndex) throws IOException {123if (input == null)124throw new IllegalStateException();125if (imageIndex >= 5 || imageIndex < 0)126throw new IndexOutOfBoundsException();127128return 15;129}130131public Iterator getImageTypes(int imageIndex) throws IOException {132if (input == null)133throw new IllegalStateException();134if (imageIndex >= 5 || imageIndex < 0)135throw new IndexOutOfBoundsException();136137Vector imageTypes = new Vector();138imageTypes.add(ImageTypeSpecifier.createFromBufferedImageType139(BufferedImage.TYPE_BYTE_GRAY ));140return imageTypes.iterator();141}142143public IIOMetadata getStreamMetadata() throws IOException {144return new DummyIIOMetadataImpl(true, null, null, null, null);145}146147public IIOMetadata getImageMetadata(int imageIndex)148throws IOException {149150if (input == null)151throw new IllegalStateException();152if (imageIndex >= 5 || imageIndex < 0)153throw new IndexOutOfBoundsException();154if (seekForwardOnly) {155if (imageIndex < minIndex)156throw new IndexOutOfBoundsException();157minIndex = imageIndex;158}159return new DummyIIOMetadataImpl(true, null, null, null, null);160}161162163public BufferedImage read(int imageIndex, ImageReadParam param)164throws IOException {165if (input == null)166throw new IllegalStateException();167if (imageIndex >= 5 || imageIndex < 0)168throw new IndexOutOfBoundsException();169if (seekForwardOnly) {170if (imageIndex < minIndex)171throw new IndexOutOfBoundsException();172minIndex = imageIndex;173}174175return getDestination(param, getImageTypes(imageIndex), 10, 15);176}177178// protected methods - now public179180public boolean abortRequested() {181return super.abortRequested();182}183184public void clearAbortRequest() {185super.clearAbortRequest();186}187188public void processImageComplete() {189super.processImageComplete();190}191192public void processImageProgress(float percentageDone) {193super.processImageProgress(percentageDone);194}195196public void processImageStarted(int imageIndex) {197super.processImageStarted(imageIndex);198}199200public void processImageUpdate(BufferedImage theImage,201int minX,202int minY,203int width,204int height,205int periodX,206int periodY,207int[] bands) {208super.processImageUpdate(theImage,209minX,210minY,211width,212height,213periodX,214periodY,215bands);216}217218public void processPassComplete(BufferedImage theImage) {219super. processPassComplete(theImage);220}221222public void processPassStarted(BufferedImage theImage,223int pass, int minPass,224int maxPass,225int minX,226int minY,227int periodX,228int periodY,229int[] bands) {230super.processPassStarted(theImage,231pass,232minPass,233maxPass,234minX,235minY,236periodX,237periodY,238bands);239}240241public void processReadAborted() {242super.processReadAborted();243}244245public void processSequenceComplete() {246super.processSequenceComplete();247}248249public void processSequenceStarted(int minIndex) {250super.processSequenceStarted(minIndex);251}252253public void processThumbnailComplete() {254super.processThumbnailComplete();255}256257public void processThumbnailPassComplete(BufferedImage theThumbnail) {258super.processThumbnailPassComplete(theThumbnail);259}260261public void processThumbnailPassStarted(BufferedImage theThumbnail,262int pass,263int minPass,264int maxPass,265int minX,266int minY,267int periodX,268int periodY,269int[] bands) {270super.processThumbnailPassStarted(theThumbnail,271pass,272minPass,273maxPass,274minX,275minY,276periodX,277periodY,278bands);279}280281public void processThumbnailProgress(float percentageDone) {282super.processThumbnailProgress(percentageDone);283}284285public void processThumbnailStarted(int imageIndex, int thumbnailIndex) {286super.processThumbnailStarted(imageIndex, thumbnailIndex);287}288289public void processThumbnailUpdate(BufferedImage theThumbnail,290int minX,291int minY,292int width,293int height,294int periodX,295int periodY,296int[] bands) {297super.processThumbnailUpdate(theThumbnail,298minX,299minY,300width,301height,302periodX,303periodY,304bands);305}306307public void processWarningOccurred(String warning) {308super.processWarningOccurred(warning);309}310311312313public static Rectangle getSourceRegion(ImageReadParam param,314int srcWidth,315int srcHeight) {316return ImageReader.getSourceRegion(param, srcWidth, srcHeight);317}318319public static void computeRegions(ImageReadParam param,320int srcWidth,321int srcHeight,322BufferedImage image,323Rectangle srcRegion,324Rectangle destRegion) {325ImageReader.computeRegions(param,326srcWidth,327srcHeight,328image,329srcRegion,330destRegion);331}332333public static void checkReadParamBandSettings(ImageReadParam param,334int numSrcBands,335int numDstBands) {336ImageReader.checkReadParamBandSettings( param,337numSrcBands,338numDstBands);339}340341public static BufferedImage getDestination(ImageReadParam param,342Iterator imageTypes,343int width,344int height) throws IIOException {345return ImageReader.getDestination(param,346imageTypes,347width,348height);349}350351public void setAvailableLocales(Locale[] locales) {352if (locales == null || locales.length == 0)353availableLocales = null;354else355availableLocales = (Locale[])locales.clone();356}357358public void processWarningOccurred(String baseName, String keyword) {359super.processWarningOccurred(baseName, keyword);360}361}362363public static class DummyIIOMetadataImpl extends IIOMetadata {364365public DummyIIOMetadataImpl() {366super();367}368369public DummyIIOMetadataImpl(boolean standardMetadataFormatSupported,370String nativeMetadataFormatName,371String nativeMetadataFormatClassName,372String[] extraMetadataFormatNames,373String[] extraMetadataFormatClassNames) {374super(standardMetadataFormatSupported,375nativeMetadataFormatName,376nativeMetadataFormatClassName,377extraMetadataFormatNames,378extraMetadataFormatClassNames);379}380381public boolean isReadOnly() {382return true;383}384385public Node getAsTree(String formatName) {386return null;387}388389public void mergeTree(String formatName, Node root)390throws IIOInvalidTreeException {391throw new IllegalStateException();392}393394public void reset() {395throw new IllegalStateException();396}397}398399public static class DummyImageReaderSpiImpl extends ImageReaderSpi {400401static final String[] names ={ "myformat" };402403public DummyImageReaderSpiImpl() {404super("vendorName",405"version",406names,407null,408null,409"DummyImageReaderImpl",410STANDARD_INPUT_TYPE,411null,412true,413null,414null,415null,416null,417true,418null,419null,420null,421null);422}423public boolean canDecodeInput(Object source)424throws IOException {425return true;426}427public ImageReader createReaderInstance(Object extension)428throws IOException {429return new DummyImageReaderImpl(this);430}431public String getDescription(Locale locale) {432return "DummyImageReaderSpiImpl";433}434}435}436437438