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 {
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
|
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 java.awt.datatransfer.DataFlavor;
27 import java.awt.datatransfer.SystemFlavorMap;
28 import java.util.*;
29 import java.nio.charset.Charset;
30
31 /*
32 * @test
33 * @summary To test SystemFlavorMap method:
34 * addFlavorForUnencodedNative(String nat, DataFlavor flav)
35 * with valid natives and DataFlavors. This stress test will
36 * define numerous mappings of valid String natives and
37 * DataFlavors. The mappings will be verified by examining
38 * that all entries are present, and order is maintained.
39 * @author Rick Reynaga (rick.reynaga@eng.sun.com) area=Clipboard
40 * @author dmitriy.ermashov@oracle.com
41 * @run main AddFlavorTest
42 */
43
44 public class AddFlavorTest {
45
46 SystemFlavorMap flavorMap;
47 Hashtable<String, List<DataFlavor>> hashVerify;
48
49 public static void main (String[] args) throws Exception {
52
53 void doTest() throws Exception {
54 flavorMap = (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();
55
56 // Test addFlavorForUnencodedNative(String nat, DataFlavor flav);
57 //
58 // Enumerate through all the system defined String natives,
59 // and for each String native, define it again to the SystemFlavorMap
60 // with a slightly modified String native (name).
61 //
62 // As a list of DataFlavors will be returned for each String native,
63 // the method addFlavorForUnencodedNative will be called for each
64 // DataFlavor in the list.
65 hashVerify = new Hashtable();
66
67 for (String key : flavorMap.getFlavorsForNatives(null).keySet()) {
68 Set<DataFlavor> flavorsSet = new HashSet<>(flavorMap.getFlavorsForNative(key));
69 // construct a unique String native
70 key = key.concat("TEST");
71
72 for (DataFlavor element : flavorsSet) {
73 flavorMap.addFlavorForUnencodedNative(key, element);
74 }
75 hashVerify.put(key, new Vector(flavorsSet));
76 }
77
78 // Assertions: After establishing "new" mappings, verify that the defined
79 // DataFlavors can be retrieved and that the List is preserved.
80 verifyNewMappings();
81 }
82
83 // Verify getFlavorsForNative(String nat) is returning the correct list
84 // of DataFlavors (for the new mappings).
85 void verifyNewMappings() {
86 // Enumerate through all natives
87 System.out.println("*** native size = " + hashVerify.size());
88 for (Enumeration e = hashVerify.keys() ; e.hasMoreElements() ;) {
89 String key = (String)e.nextElement();
90 compareFlavors(hashVerify.get(key), flavorMap.getFlavorsForNative(key), key);
91 compareFlavors(flavorMap.getFlavorsForNative(key), hashVerify.get(key), key);
92 }
93 }
94
95 void compareFlavors(List<DataFlavor> flavors1, List<DataFlavor> flavors2, String key){
96 for (DataFlavor flavor1 : flavors1) {
97 boolean result = false;
98 for (DataFlavor flavor2 : flavors2) {
99 if (flavor1.equals(flavor2)) result = true;
100 }
101 if (!result)
102 throw new RuntimeException("\n*** Error in verifyNewMappings()" +
103 "\nmethod1: addFlavorForUnencodedNative(String nat, DataFlavor flav)" +
104 "\nmethod2: List getFlavorsForNative(String nat)" +
105 "\nString native: " + key +
106 "\nAfter adding several mappings with addFlavorForUnencodedNative," +
107 "\nthe returned list did not match the mappings that were added." +
108 "\nEither the mapping was not included in the list, or the order was incorect.");
109 }
110
111 }
112
113 Set<DataFlavor> convertMimeTypeToDataFlavors(String baseType) throws Exception {
114 Set<DataFlavor> result = new LinkedHashSet<>();
115
116 for (String charset : getStandardEncodings()) {
117 for (String txtClass : new String[]{"java.io.InputStream", "java.nio.ByteBuffer", "\"[B\""}) {
118 String mimeType = baseType + ";charset=" + charset + ";class=" + txtClass;
119
120 if ("text/html".equals(baseType)) {
121 for (String documentType : new String[]{"all", "selection", "fragment"})
122 result.add(new DataFlavor(mimeType + ";document=" + documentType));
123 } else {
124 DataFlavor df = new DataFlavor(mimeType);
125 if (df.equals(DataFlavor.plainTextFlavor))
126 df = DataFlavor.plainTextFlavor;
127 result.add(df);
128 }
129 }
130 }
131 return result;
132 }
133
134 Set<String> getStandardEncodings() {
135 Set<String> tempSet = new HashSet<>();
136 tempSet.add("US-ASCII");
137 tempSet.add("ISO-8859-1");
138 tempSet.add("UTF-8");
139 tempSet.add("UTF-16BE");
140 tempSet.add("UTF-16LE");
141 tempSet.add("UTF-16");
142 tempSet.add(Charset.defaultCharset().name());
143 return tempSet;
144 }
145 }
146
|