1 /* 2 * Copyright (c) 2001, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 import sun.awt.datatransfer.DataTransferer; 27 28 import java.awt.datatransfer.DataFlavor; 29 import java.awt.datatransfer.SystemFlavorMap; 30 import java.util.*; 31 32 /* 33 * @test 34 * @summary To test SystemFlavorMap method: 35 * addFlavorForUnencodedNative(String nat, DataFlavor flav) 36 * with valid natives and DataFlavors. This stress test will 37 * define numerous mappings of valid String natives and 38 * DataFlavors. The mappings will be verified by examining 39 * that all entries are present, and order is maintained. 40 * @author Rick Reynaga (rick.reynaga@eng.sun.com) area=Clipboard 41 * @author dmitriy.ermashov@oracle.com 42 * @run main AddFlavorTest 43 */ 44 45 public class AddFlavorTest { 46 47 SystemFlavorMap flavorMap; 48 Hashtable<String, List<DataFlavor>> hashVerify; 49 50 public static void main (String[] args) throws Exception { 51 new AddFlavorTest().doTest(); 52 } 53 54 void doTest() throws Exception { 55 flavorMap = (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap(); 56 57 // Test addFlavorForUnencodedNative(String nat, DataFlavor flav); 58 // 59 // Enumerate through all the system defined String natives, 60 // and for each String native, define it again to the SystemFlavorMap 61 // with a slightly modified String native (name). 62 // 63 // As a list of DataFlavors will be returned for each String native, 64 // the method addFlavorForUnencodedNative will be called for each 65 // DataFlavor in the list. 66 hashVerify = new Hashtable(); 67 68 for (String key : flavorMap.getFlavorsForNatives(null).keySet()) { 69 Set<DataFlavor> flavorsSet = new HashSet<>(flavorMap.getFlavorsForNative(key)); 70 // construct a unique String native 71 key = key.concat("TEST"); 72 73 for (DataFlavor element : flavorsSet) 74 flavorMap.addFlavorForUnencodedNative(key, element); 75 76 // This part is valid only for X-based graphical systems 77 if (!System.getProperty("os.name").startsWith("Win") && !System.getProperty("os.name").startsWith("Mac") ) { 78 if (key.contains("/")) { 79 Set<DataFlavor> fls = DataTransferer.getInstance().getPlatformMappingsForNative(key); 80 flavorsSet.addAll(fls); 81 if (!fls.isEmpty() && key.startsWith("text/")) 82 flavorsSet.addAll(convertMimeTypeToDataFlavors(key)); 83 } 84 } 85 hashVerify.put(key, new Vector(flavorsSet)); 86 } 87 88 // Assertions: After establishing "new" mappings, verify that the defined 89 // DataFlavors can be retrieved and that the List is preserved. 90 verifyNewMappings(); 91 } 92 93 // Verify getFlavorsForNative(String nat) is returning the correct list 94 // of DataFlavors (for the new mappings). 95 void verifyNewMappings() { 96 // Enumerate through all natives 97 System.out.println("*** native size = " + hashVerify.size()); 98 for (Enumeration e = hashVerify.keys() ; e.hasMoreElements() ;) { 99 String key = (String)e.nextElement(); 100 compareFlavors(hashVerify.get(key), flavorMap.getFlavorsForNative(key), key); 101 compareFlavors(flavorMap.getFlavorsForNative(key), hashVerify.get(key), key); 102 } 103 } 104 105 void compareFlavors(List<DataFlavor> flavors1, List<DataFlavor> flavors2, String key){ 106 DataTransferer.DataFlavorComparator comparator = new DataTransferer.DataFlavorComparator(); 107 for (DataFlavor flavor1 : flavors1) { 108 boolean result = false; 109 for (DataFlavor flavor2 : flavors2) { 110 if (comparator.compare(flavor1, flavor2) == 0) 111 result = true; 112 } 113 if (!result) 114 throw new RuntimeException("\n*** Error in verifyNewMappings()" + 115 "\nmethod1: addFlavorForUnencodedNative(String nat, DataFlavor flav)" + 116 "\nmethod2: List getFlavorsForNative(String nat)" + 117 "\nString native: " + key + 118 "\nAfter adding several mappings with addFlavorForUnencodedNative," + 119 "\nthe returned list did not match the mappings that were added." + 120 "\nEither the mapping was not included in the list, or the order was incorect."); 121 } 122 123 } 124 125 Set<DataFlavor> convertMimeTypeToDataFlavors(String baseType) throws Exception { 126 Set<DataFlavor> result = new LinkedHashSet<>(); 127 128 for (String charset : DataTransferer.standardEncodings()) { 129 for (String txtClass : new String[]{"java.io.InputStream", "java.nio.ByteBuffer", "\"[B\""}) { 130 String mimeType = baseType + ";charset=" + charset + ";class=" + txtClass; 131 132 if ("text/html".equals(baseType)) { 133 for (String documentType : new String[]{"all", "selection", "fragment"}) 134 result.add(new DataFlavor(mimeType + ";document=" + documentType)); 135 } else { 136 DataFlavor df = new DataFlavor(mimeType); 137 if (df.equals(DataFlavor.plainTextFlavor)) 138 df = DataFlavor.plainTextFlavor; 139 result.add(df); 140 } 141 } 142 } 143 return result; 144 } 145 } 146