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