1/* 2 * Copyright (c) 2014, 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 @key headful 27 @bug 4682039 28 @summary Tests that DataTransferer.getFormatsForFlavors() does not throw 29 NullPointerException if some of given as parameter data flavors 30 are null. 31 @author gas@sparc.spb.su area=datatransfer 32 @run main NullDataFlavorTest 33*/ 34 35import java.awt.*; 36import java.awt.datatransfer.Clipboard; 37import java.awt.datatransfer.DataFlavor; 38import java.awt.datatransfer.Transferable; 39import java.awt.datatransfer.UnsupportedFlavorException; 40 41public class NullDataFlavorTest { 42 43 private final static Clipboard clipboard = 44 Toolkit.getDefaultToolkit().getSystemClipboard(); 45 46 public static void main(String[] args) throws Exception { 47 boolean failed = false; 48 49 try { 50 clipboard.setContents(new NullSelection("DATA1", 51 new DataFlavor[] { null, null, null }), null); 52 clipboard.setContents(new NullSelection("DATA2", 53 new DataFlavor[] { null, DataFlavor.stringFlavor, null }), null); 54 clipboard.setContents(new NullSelection("DATA3", null), null); 55 } catch (NullPointerException e) { 56 failed = true; 57 e.printStackTrace(); 58 } catch (Throwable e) { 59 e.printStackTrace(); 60 } 61 62 if (failed) { 63 throw new RuntimeException("test failed: NullPointerException " + 64 "has been thrown"); 65 } else { 66 System.err.println("test passed"); 67 } 68 } 69} 70 71class NullSelection implements Transferable { 72 73 private final DataFlavor[] flavors; 74 75 private final String data; 76 77 public NullSelection(String data, DataFlavor[] flavors) { 78 this.data = data; 79 this.flavors = flavors; 80 } 81 82 @Override 83 public DataFlavor[] getTransferDataFlavors() { 84 return flavors; 85 } 86 87 @Override 88 public boolean isDataFlavorSupported(DataFlavor flavor) { 89 for (DataFlavor fl : flavors) { 90 if (flavor.equals(fl)) { 91 return true; 92 } 93 } 94 return false; 95 } 96 97 @Override 98 public Object getTransferData(DataFlavor flavor) 99 throws UnsupportedFlavorException, java.io.IOException 100 { 101 for (DataFlavor fl : flavors) { 102 if (flavor.equals(fl)) { 103 return data; 104 } 105 } 106 throw new UnsupportedFlavorException(flavor); 107 } 108 109} 110