Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/imageio/AppletResourceTest.java
38833 views
/*1* Copyright (c) 2003, 2017, 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* @key headful27* @summary Tests that applet-supplied ImageReader, ImageWriter, and28* IIOMetadataFormat implementations do not throw unexpected exceptions29* when indirectly attempting to access ResourceBundles30* @run main AppletResourceTest31* @run applet AppletResourceTest.html32*/3334import java.applet.Applet;35import java.awt.Rectangle;36import java.awt.image.BufferedImage;37import java.io.IOException;38import java.util.Iterator;39import java.util.ListResourceBundle;40import java.util.Locale;41import java.util.MissingResourceException;42import java.util.Vector;4344import javax.imageio.IIOException;45import javax.imageio.ImageReadParam;46import javax.imageio.ImageReader;47import javax.imageio.ImageTypeSpecifier;48import javax.imageio.event.IIOReadWarningListener;49import javax.imageio.metadata.IIOInvalidTreeException;50import javax.imageio.metadata.IIOMetadata;51import javax.imageio.spi.ImageReaderSpi;5253import org.w3c.dom.Node;5455public class AppletResourceTest extends Applet {5657public static void main(String[] argv) {58new AppletResourceTest().init();59}6061public void init() {62DummyImageReaderImpl reader;63MyReadWarningListener listener = new MyReadWarningListener();64Locale[] locales = {new Locale("ru"),65new Locale("fr"),66new Locale("uk")};6768reader = new DummyImageReaderImpl(new DummyImageReaderSpiImpl());69reader.setAvailableLocales(locales);70reader.setLocale(new Locale("fr"));71reader.addIIOReadWarningListener(listener);7273String baseName = "AppletResourceTest$BugStats";74try {75reader.processWarningOccurred("WarningMessage");76reader.processWarningOccurred(baseName, "water");77} catch (MissingResourceException mre) {78throw new RuntimeException("Test failed: couldn't load resource");79}808182}8384private class MyReadWarningListener implements IIOReadWarningListener {85public void warningOccurred(ImageReader source,86String warning)87{88System.out.println("warning occurred: " + warning);89}90}9192public static class BugStats extends ListResourceBundle {9394public Object[][] getContents(){95return contents;96}9798private Object[][] contents = {99{"coffee", new String("coffee from Stats class")},100{"tea", new String("tea from Stats class")},101{"water", new String("water from Stats class")}102};103}104105106public static class DummyImageReaderImpl extends ImageReader {107108public DummyImageReaderImpl(ImageReaderSpi originatingProvider) {109super(originatingProvider);110}111112public int getNumImages(boolean allowSearch) throws IOException {113return 5;114}115116public int getWidth(int imageIndex) throws IOException {117if (input == null)118throw new IllegalStateException();119if (imageIndex >= 5 || imageIndex < 0)120throw new IndexOutOfBoundsException();121122return 10;123}124125public int getHeight(int imageIndex) throws IOException {126if (input == null)127throw new IllegalStateException();128if (imageIndex >= 5 || imageIndex < 0)129throw new IndexOutOfBoundsException();130131return 15;132}133134public Iterator getImageTypes(int imageIndex) throws IOException {135if (input == null)136throw new IllegalStateException();137if (imageIndex >= 5 || imageIndex < 0)138throw new IndexOutOfBoundsException();139140Vector imageTypes = new Vector();141imageTypes.add(ImageTypeSpecifier.createFromBufferedImageType142(BufferedImage.TYPE_BYTE_GRAY ));143return imageTypes.iterator();144}145146public IIOMetadata getStreamMetadata() throws IOException {147return new DummyIIOMetadataImpl(true, null, null, null, null);148}149150public IIOMetadata getImageMetadata(int imageIndex)151throws IOException {152153if (input == null)154throw new IllegalStateException();155if (imageIndex >= 5 || imageIndex < 0)156throw new IndexOutOfBoundsException();157if (seekForwardOnly) {158if (imageIndex < minIndex)159throw new IndexOutOfBoundsException();160minIndex = imageIndex;161}162return new DummyIIOMetadataImpl(true, null, null, null, null);163}164165166public BufferedImage read(int imageIndex, ImageReadParam param)167throws IOException {168if (input == null)169throw new IllegalStateException();170if (imageIndex >= 5 || imageIndex < 0)171throw new IndexOutOfBoundsException();172if (seekForwardOnly) {173if (imageIndex < minIndex)174throw new IndexOutOfBoundsException();175minIndex = imageIndex;176}177178return getDestination(param, getImageTypes(imageIndex), 10, 15);179}180181// protected methods - now public182183public boolean abortRequested() {184return super.abortRequested();185}186187public void clearAbortRequest() {188super.clearAbortRequest();189}190191public void processImageComplete() {192super.processImageComplete();193}194195public void processImageProgress(float percentageDone) {196super.processImageProgress(percentageDone);197}198199public void processImageStarted(int imageIndex) {200super.processImageStarted(imageIndex);201}202203public void processImageUpdate(BufferedImage theImage,204int minX,205int minY,206int width,207int height,208int periodX,209int periodY,210int[] bands) {211super.processImageUpdate(theImage,212minX,213minY,214width,215height,216periodX,217periodY,218bands);219}220221public void processPassComplete(BufferedImage theImage) {222super. processPassComplete(theImage);223}224225public void processPassStarted(BufferedImage theImage,226int pass, int minPass,227int maxPass,228int minX,229int minY,230int periodX,231int periodY,232int[] bands) {233super.processPassStarted(theImage,234pass,235minPass,236maxPass,237minX,238minY,239periodX,240periodY,241bands);242}243244public void processReadAborted() {245super.processReadAborted();246}247248public void processSequenceComplete() {249super.processSequenceComplete();250}251252public void processSequenceStarted(int minIndex) {253super.processSequenceStarted(minIndex);254}255256public void processThumbnailComplete() {257super.processThumbnailComplete();258}259260public void processThumbnailPassComplete(BufferedImage theThumbnail) {261super.processThumbnailPassComplete(theThumbnail);262}263264public void processThumbnailPassStarted(BufferedImage theThumbnail,265int pass,266int minPass,267int maxPass,268int minX,269int minY,270int periodX,271int periodY,272int[] bands) {273super.processThumbnailPassStarted(theThumbnail,274pass,275minPass,276maxPass,277minX,278minY,279periodX,280periodY,281bands);282}283284public void processThumbnailProgress(float percentageDone) {285super.processThumbnailProgress(percentageDone);286}287288public void processThumbnailStarted(int imageIndex, int thumbnailIndex) {289super.processThumbnailStarted(imageIndex, thumbnailIndex);290}291292public void processThumbnailUpdate(BufferedImage theThumbnail,293int minX,294int minY,295int width,296int height,297int periodX,298int periodY,299int[] bands) {300super.processThumbnailUpdate(theThumbnail,301minX,302minY,303width,304height,305periodX,306periodY,307bands);308}309310public void processWarningOccurred(String warning) {311super.processWarningOccurred(warning);312}313314315316public static Rectangle getSourceRegion(ImageReadParam param,317int srcWidth,318int srcHeight) {319return ImageReader.getSourceRegion(param, srcWidth, srcHeight);320}321322public static void computeRegions(ImageReadParam param,323int srcWidth,324int srcHeight,325BufferedImage image,326Rectangle srcRegion,327Rectangle destRegion) {328ImageReader.computeRegions(param,329srcWidth,330srcHeight,331image,332srcRegion,333destRegion);334}335336public static void checkReadParamBandSettings(ImageReadParam param,337int numSrcBands,338int numDstBands) {339ImageReader.checkReadParamBandSettings( param,340numSrcBands,341numDstBands);342}343344public static BufferedImage getDestination(ImageReadParam param,345Iterator imageTypes,346int width,347int height) throws IIOException {348return ImageReader.getDestination(param,349imageTypes,350width,351height);352}353354public void setAvailableLocales(Locale[] locales) {355if (locales == null || locales.length == 0)356availableLocales = null;357else358availableLocales = (Locale[])locales.clone();359}360361public void processWarningOccurred(String baseName, String keyword) {362super.processWarningOccurred(baseName, keyword);363}364}365366public static class DummyIIOMetadataImpl extends IIOMetadata {367368public DummyIIOMetadataImpl() {369super();370}371372public DummyIIOMetadataImpl(boolean standardMetadataFormatSupported,373String nativeMetadataFormatName,374String nativeMetadataFormatClassName,375String[] extraMetadataFormatNames,376String[] extraMetadataFormatClassNames) {377super(standardMetadataFormatSupported,378nativeMetadataFormatName,379nativeMetadataFormatClassName,380extraMetadataFormatNames,381extraMetadataFormatClassNames);382}383384public boolean isReadOnly() {385return true;386}387388public Node getAsTree(String formatName) {389return null;390}391392public void mergeTree(String formatName, Node root)393throws IIOInvalidTreeException {394throw new IllegalStateException();395}396397public void reset() {398throw new IllegalStateException();399}400}401402public static class DummyImageReaderSpiImpl extends ImageReaderSpi {403404static final String[] names ={ "myformat" };405406public DummyImageReaderSpiImpl() {407super("vendorName",408"version",409names,410null,411null,412"DummyImageReaderImpl",413STANDARD_INPUT_TYPE,414null,415true,416null,417null,418null,419null,420true,421null,422null,423null,424null);425}426public boolean canDecodeInput(Object source)427throws IOException {428return true;429}430public ImageReader createReaderInstance(Object extension)431throws IOException {432return new DummyImageReaderImpl(this);433}434public String getDescription(Locale locale) {435return "DummyImageReaderSpiImpl";436}437}438}439440441