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 4493178 27 @summary tests that getNativesForFlavor() synthesizes an encoded String native 28 only if there are no mappings for the DataFlavor and the mappings 29 were not explicitly removed 30 @author das@sparc.spb.su area=datatransfer 31 @run main GetNativesForFlavorTest 32 */ 33 34 import java.awt.datatransfer.DataFlavor; 35 import java.awt.datatransfer.SystemFlavorMap; 36 import java.util.Iterator; 37 38 public class GetNativesForFlavorTest { 39 40 final static SystemFlavorMap fm = 41 (SystemFlavorMap) SystemFlavorMap.getDefaultFlavorMap(); 42 43 public static void main(String[] args) throws Exception { 44 // 1.Check that the encoded native is not added if there are other 45 // natives for this DataFlavor. 46 test1(); 47 48 // 2.Check that the encoded native is not added if all mappings were 49 // explicitly removed for this DataFlavor. 50 test2(); 51 52 // 3.Check that only the encoded native is added for text DataFlavors 53 // that has no mappings and that DataFlavor is properly encoded. 54 test3(); 55 56 // 4.Verifies that the encoded native is added only for DataFlavors 57 // that has no mappings and that DataFlavor is properly encoded. 58 test4(); 59 } 60 61 /** 62 * Verifies that the encoded native is not added if there are other 63 * natives mapped to this DataFlavor. 64 */ 65 public static void test1() throws ClassNotFoundException { 66 final DataFlavor flavor = 67 new DataFlavor("text/plain-TEST; charset=Unicode"); 68 69 final java.util.List natives = fm.getNativesForFlavor(flavor); 70 71 if (natives.size() > 1) { 72 for (final Iterator i = natives.iterator(); i.hasNext(); ) { 73 String element = (String) i.next(); 74 if (SystemFlavorMap.isJavaMIMEType(element)) { 75 throw new RuntimeException("getFlavorsForNative() returns: " 76 + natives); 77 } 78 } 79 } 80 } 81 82 /** 83 * Verifies that the encoded native is not added if all mappings were 84 * explicitly removed for this DataFlavor. 85 */ 86 public static void test2() throws ClassNotFoundException { 87 final DataFlavor flavor = 88 new DataFlavor("text/plain-TEST; charset=Unicode"); 89 90 fm.setNativesForFlavor(flavor, new String[0]); 91 92 final java.util.List natives = fm.getNativesForFlavor(flavor); 93 94 if (!natives.isEmpty()) { 95 throw new RuntimeException("getFlavorsForNative() returns:" + 96 natives); 97 } 98 } 99 100 /** 101 * Verifies that only the encoded native is added for text DataFlavors 102 * that has no mappings and that DataFlavor is properly encoded. 103 */ 104 public static void test3() throws ClassNotFoundException { 105 // 106 final DataFlavor flavor = 107 new DataFlavor("text/plain-TEST-nocharset; class=java.nio.ByteBuffer"); 108 109 final java.util.List natives = fm.getNativesForFlavor(flavor); 110 boolean encodedNativeFound = false; 111 112 if (natives.size() == 0) { 113 throw new RuntimeException("getFlavorsForNative() returns:" + 114 natives); 115 } 116 117 if (natives.size() == 1) { 118 String element = (String) natives.get(0); 119 if (SystemFlavorMap.isJavaMIMEType(element)) { 120 final DataFlavor decodedFlavor = 121 SystemFlavorMap.decodeDataFlavor(element); 122 if (!flavor.equals(decodedFlavor)) { 123 System.err.println("DataFlavor is not properly incoded:"); 124 System.err.println(" encoded flavor: " + flavor); 125 System.err.println(" decoded flavor: " + decodedFlavor); 126 throw new RuntimeException("getFlavorsForNative() returns:" 127 + natives); 128 } 129 } 130 } else { 131 for (final Iterator i = natives.iterator(); i.hasNext(); ) { 132 String element = (String) i.next(); 133 if (SystemFlavorMap.isJavaMIMEType(element)) { 134 throw new RuntimeException("getFlavorsForNative() returns:" 135 + natives); 136 } 137 } 138 } 139 } 140 141 /** 142 * Verifies that the encoded native is added only for DataFlavors 143 * that has no mappings and that DataFlavor is properly encoded. 144 */ 145 public static void test4() throws ClassNotFoundException { 146 final DataFlavor flavor = 147 new DataFlavor("unknown/unknown"); 148 149 final java.util.List natives = fm.getNativesForFlavor(flavor); 150 151 if (natives.size() == 1) { 152 String element = (String) natives.get(0); 153 if (SystemFlavorMap.isJavaMIMEType(element)) { 154 final DataFlavor decodedFlavor = 155 SystemFlavorMap.decodeDataFlavor(element); 156 if (!flavor.equals(decodedFlavor)) { 157 System.err.println("DataFlavor is not properly incoded:"); 158 System.err.println(" encoded flavor: " + flavor); 159 System.err.println(" decoded flavor: " + decodedFlavor); 160 throw new RuntimeException("getFlavorsForNative() returns:" 161 + natives); 162 } 163 } 164 } else { 165 throw new RuntimeException("getFlavorsForNative() returns:" 166 + natives); 167 } 168 } 169 }