Path: blob/master/test/jdk/javax/imageio/metadata/IIOMetadataFormat/UserPluginMetadataFormatTest.java
41154 views
/*1* Copyright (c) 2012, 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 492917026* @summary Tests that user-supplied IIOMetadata implementations loaded by27* system class loader (i.e. corresponding classes are available via28* classpath) is able to load correspnding IIOMetadataFormat29* implementations.30* @run main UserPluginMetadataFormatTest31*/3233import java.awt.Rectangle;34import java.awt.image.BufferedImage;35import java.io.IOException;36import java.io.ByteArrayInputStream;37import java.util.Iterator;38import java.util.ListResourceBundle;39import java.util.Locale;40import java.util.MissingResourceException;41import java.util.Vector;42import javax.imageio.ImageIO;43import javax.imageio.ImageReader;44import javax.imageio.ImageReadParam;45import javax.imageio.IIOException;46import javax.imageio.ImageTypeSpecifier;47import javax.imageio.event.IIOReadWarningListener;48import javax.imageio.metadata.IIOMetadata;49import javax.imageio.metadata.IIOMetadataFormat;50import javax.imageio.metadata.IIOMetadataFormatImpl;51import javax.imageio.metadata.IIOInvalidTreeException;52import javax.imageio.spi.ImageReaderSpi;53import org.w3c.dom.Node;5455public class UserPluginMetadataFormatTest implements MetadataTest {5657public static void main(String[] argv) throws IOException {58new UserPluginMetadataFormatTest().doTest();59}6061public void doTest() throws IOException {6263DummyImageReaderImpl reader;6465reader = new DummyImageReaderImpl(new DummyImageReaderSpiImpl());6667byte[] data = new byte[1024];68ByteArrayInputStream bais =69new ByteArrayInputStream(data);7071reader.setInput(ImageIO.createImageInputStream(bais));72IIOMetadata metadata = reader.getImageMetadata(1);73if(metadata == null) {74throw new RuntimeException("IIOMetada is NULL");75}7677String[] formatNames = metadata.getMetadataFormatNames();7879for(int j=0; j<formatNames.length; j++) {8081String formatName = formatNames[j];82System.out.println("\nFormat Names : " + formatName);8384try {85IIOMetadataFormat metadataFormat =86metadata.getMetadataFormat(formatName);87System.out.println(" Class Name " +88metadataFormat.getClass());89} catch(IllegalStateException ise) {90Throwable t = ise;91t.printStackTrace();92while(t.getCause() != null) {93t = t.getCause();94t.printStackTrace();95}96// test failed!97// stop applet!98System.out.println("Test faied.");99throw new RuntimeException("Test failed.", ise);100}101}102}103104public static class DummyImageReaderImpl extends ImageReader {105106public DummyImageReaderImpl(ImageReaderSpi originatingProvider) {107super(originatingProvider);108}109110public int getNumImages(boolean allowSearch) throws IOException {111return 5;112}113114public int getWidth(int imageIndex) throws IOException {115if (input == null)116throw new IllegalStateException();117if (imageIndex >= 5 || imageIndex < 0)118throw new IndexOutOfBoundsException();119120return 10;121}122123public int getHeight(int imageIndex) throws IOException {124if (input == null)125throw new IllegalStateException();126if (imageIndex >= 5 || imageIndex < 0)127throw new IndexOutOfBoundsException();128129return 15;130}131132public Iterator getImageTypes(int imageIndex) throws IOException {133if (input == null)134throw new IllegalStateException();135if (imageIndex >= 5 || imageIndex < 0)136throw new IndexOutOfBoundsException();137138Vector imageTypes = new Vector();139imageTypes.add(ImageTypeSpecifier.createFromBufferedImageType140(BufferedImage.TYPE_BYTE_GRAY ));141return imageTypes.iterator();142}143144public IIOMetadata getStreamMetadata() throws IOException {145return new DummyIIOMetadataImpl(true, null, null, null, null);146}147148public IIOMetadata getImageMetadata(int imageIndex) throws 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}159System.out.println("Current format class name " + DummyIIOMetadataFormatImpl.class.getName());160return new DummyIIOMetadataImpl(true,161DummyIIOMetadataFormatImpl.nativeMetadataFormatName,162DummyIIOMetadataFormatImpl.class.getName(),163null, null);164}165166167public BufferedImage read(int imageIndex, ImageReadParam param)168throws IOException {169if (input == null)170throw new IllegalStateException();171if (imageIndex >= 5 || imageIndex < 0)172throw new IndexOutOfBoundsException();173if (seekForwardOnly) {174if (imageIndex < minIndex)175throw new IndexOutOfBoundsException();176minIndex = imageIndex;177}178179return getDestination(param, getImageTypes(imageIndex), 10, 15);180}181182// protected methods - now public183184public boolean abortRequested() {185return super.abortRequested();186}187188public void clearAbortRequest() {189super.clearAbortRequest();190}191192public void processImageComplete() {193super.processImageComplete();194}195196public void processImageProgress(float percentageDone) {197super.processImageProgress(percentageDone);198}199200public void processImageStarted(int imageIndex) {201super.processImageStarted(imageIndex);202}203204public void processImageUpdate(BufferedImage theImage,205int minX,206int minY,207int width,208int height,209int periodX,210int periodY,211int[] bands) {212super.processImageUpdate(theImage,213minX,214minY,215width,216height,217periodX,218periodY,219bands);220}221222public void processPassComplete(BufferedImage theImage) {223super. processPassComplete(theImage);224}225226public void processPassStarted(BufferedImage theImage,227int pass, int minPass,228int maxPass,229int minX,230int minY,231int periodX,232int periodY,233int[] bands) {234super.processPassStarted(theImage,235pass,236minPass,237maxPass,238minX,239minY,240periodX,241periodY,242bands);243}244245public void processReadAborted() {246super.processReadAborted();247}248249public void processSequenceComplete() {250super.processSequenceComplete();251}252253public void processSequenceStarted(int minIndex) {254super.processSequenceStarted(minIndex);255}256257public void processThumbnailComplete() {258super.processThumbnailComplete();259}260261public void processThumbnailPassComplete(BufferedImage theThumbnail) {262super.processThumbnailPassComplete(theThumbnail);263}264265public void processThumbnailPassStarted(BufferedImage theThumbnail,266int pass,267int minPass,268int maxPass,269int minX,270int minY,271int periodX,272int periodY,273int[] bands) {274super.processThumbnailPassStarted(theThumbnail,275pass,276minPass,277maxPass,278minX,279minY,280periodX,281periodY,282bands);283}284285public void processThumbnailProgress(float percentageDone) {286super.processThumbnailProgress(percentageDone);287}288289public void processThumbnailStarted(int imageIndex, int thumbnailIndex) {290super.processThumbnailStarted(imageIndex, thumbnailIndex);291}292293public void processThumbnailUpdate(BufferedImage theThumbnail,294int minX,295int minY,296int width,297int height,298int periodX,299int periodY,300int[] bands) {301super.processThumbnailUpdate(theThumbnail,302minX,303minY,304width,305height,306periodX,307periodY,308bands);309}310311public void processWarningOccurred(String warning) {312super.processWarningOccurred(warning);313}314315316317public static Rectangle getSourceRegion(ImageReadParam param,318int srcWidth,319int srcHeight) {320return ImageReader.getSourceRegion(param, srcWidth, srcHeight);321}322323public static void computeRegions(ImageReadParam param,324int srcWidth,325int srcHeight,326BufferedImage image,327Rectangle srcRegion,328Rectangle destRegion) {329ImageReader.computeRegions(param,330srcWidth,331srcHeight,332image,333srcRegion,334destRegion);335}336337public static void checkReadParamBandSettings(ImageReadParam param,338int numSrcBands,339int numDstBands) {340ImageReader.checkReadParamBandSettings( param,341numSrcBands,342numDstBands);343}344345public static BufferedImage getDestination(ImageReadParam param,346Iterator imageTypes,347int width,348int height)349throws IIOException {350return ImageReader.getDestination(param,351imageTypes,352width,353height);354}355356public void setAvailableLocales(Locale[] locales) {357if (locales == null || locales.length == 0)358availableLocales = null;359else360availableLocales = (Locale[])locales.clone();361}362363public void processWarningOccurred(String baseName, String keyword) {364super.processWarningOccurred(baseName, keyword);365}366}367368public static class DummyIIOMetadataFormatImpl369extends IIOMetadataFormatImpl {370public static String nativeMetadataFormatName =371"javax_imageio_dummy_1.0";372373private static IIOMetadataFormat instance = null;374375376private DummyIIOMetadataFormatImpl() {377super(DummyIIOMetadataFormatImpl.nativeMetadataFormatName,378CHILD_POLICY_SOME);379}380381public boolean canNodeAppear(String elementName,382ImageTypeSpecifier imageType) {383return false;384}385386public static synchronized IIOMetadataFormat getInstance() {387if (instance == null) {388instance = new DummyIIOMetadataFormatImpl();389}390return instance;391}392}393394public static class DummyIIOMetadataImpl extends IIOMetadata {395396public DummyIIOMetadataImpl() {397super();398}399400public DummyIIOMetadataImpl(boolean standardMetadataFormatSupported,401String nativeMetadataFormatName,402String nativeMetadataFormatClassName,403String[] extraMetadataFormatNames,404String[] extraMetadataFormatClassNames) {405super(standardMetadataFormatSupported,406nativeMetadataFormatName,407nativeMetadataFormatClassName,408extraMetadataFormatNames,409extraMetadataFormatClassNames);410}411412public boolean isReadOnly() {413return true;414}415416public Node getAsTree(String formatName) {417return null;418}419420public void mergeTree(String formatName, Node root)421throws IIOInvalidTreeException {422throw new IllegalStateException();423}424425public void reset() {426throw new IllegalStateException();427}428}429430public static class DummyImageReaderSpiImpl extends ImageReaderSpi {431432static final String[] names ={ "myformat" };433434public DummyImageReaderSpiImpl() {435super("vendorName",436"version",437names,438null,439null,440"DummyImageReaderImpl",441STANDARD_INPUT_TYPE,442null,443true,444null,445null,446null,447null,448true,449null,450null,451null,452null);453}454public boolean canDecodeInput(Object source)455throws IOException {456return true;457}458public ImageReader createReaderInstance(Object extension)459throws IOException {460return new DummyImageReaderImpl(this);461}462public String getDescription(Locale locale) {463return "DummyImageReaderSpiImpl";464}465}466}467468469