AddFlavorTest.java revision 10490:067bc9c74a1d
150477Speter/*
238465Smsmith * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
3109498Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
439857Sjkh *
548351Speter * This code is free software; you can redistribute it and/or modify it
639851Speter * under the terms of the GNU General Public License version 2 only, as
783368Sru * published by the Free Software Foundation.  Oracle designates this
838465Smsmith * particular file as subject to the "Classpath" exception as provided
939441Smsmith * by Oracle in the LICENSE file that accompanied this code.
1059473Sps *
1139441Smsmith * This code is distributed in the hope that it will be useful, but WITHOUT
1296306Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1359087Sps * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1468310Sps * version 2 for more details (a copy is included in the LICENSE file that
1568310Sps * accompanied this code).
1668310Sps *
1759087Sps * You should have received a copy of the GNU General Public License version
1868310Sps * 2 along with this work; if not, write to the Free Software Foundation,
1959087Sps * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2039178Smsmith *
2140555Smsmith * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2240555Smsmith * or visit www.oracle.com if you need additional information or have any
2339178Smsmith * questions.
2456992Sluigi */
2540877Smsmith
2656903Sjhbimport java.awt.datatransfer.DataFlavor;
2756903Sjhbimport java.awt.datatransfer.SystemFlavorMap;
2841107Sjkhimport java.util.*;
2941107Sjkhimport java.nio.charset.Charset;
3041107Sjkh
3141107Sjkh/*
3241107Sjkh * @test
3356992Sluigi * @summary To test SystemFlavorMap method:
3440877Smsmith *          addFlavorForUnencodedNative(String nat, DataFlavor flav)
3583616Ssobomax *          with valid natives and DataFlavors. This stress test will
3683616Ssobomax *          define numerous mappings of valid String natives and
3783616Ssobomax *          DataFlavors.  The mappings will be verified by examining
3883616Ssobomax *          that all entries are present, and order is maintained.
3983616Ssobomax * @author Rick Reynaga (rick.reynaga@eng.sun.com) area=Clipboard
4083616Ssobomax * @author dmitriy.ermashov@oracle.com
4183616Ssobomax * @run main AddFlavorTest
4238465Smsmith */
4338465Smsmith
4438465Smsmithpublic class AddFlavorTest {
4540338Speter
4640338Speter    SystemFlavorMap flavorMap;
4738465Smsmith    Hashtable<String, List<DataFlavor>> hashVerify;
48109498Sobrien
4938465Smsmith    public static void main (String[] args) throws Exception {
5038465Smsmith        new AddFlavorTest().doTest();
5144243Smsmith    }
5238465Smsmith
5338465Smsmith    void doTest() throws Exception {
5438465Smsmith        flavorMap = (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();
5538465Smsmith
5638465Smsmith        // Test addFlavorForUnencodedNative(String nat, DataFlavor flav);
5739474Smsmith        //
5865598Simp        // Enumerate through all the system defined String natives,
5965598Simp        // and for each String native, define it again to the SystemFlavorMap
6065598Simp        // with a slightly modified String native (name).
6165598Simp        //
6265598Simp        // As a list of DataFlavors will be returned for each String native,
6339474Smsmith        // the method addFlavorForUnencodedNative will be called for each
6465598Simp        // DataFlavor in the list.
6565598Simp        hashVerify = new Hashtable();
6659087Sps
6739474Smsmith        for (String key : flavorMap.getFlavorsForNatives(null).keySet()) {
6839441Smsmith            Set<DataFlavor> flavorsSet = new HashSet<>(flavorMap.getFlavorsForNative(key));
6939646Speter            // construct a unique String native
7039646Speter            key = key.concat("TEST");
7139646Speter
7239441Smsmith            for (DataFlavor element : flavorsSet) {
7339646Speter                flavorMap.addFlavorForUnencodedNative(key, element);
7439441Smsmith            }
7539441Smsmith            hashVerify.put(key, new Vector(flavorsSet));
7639441Smsmith        }
7739646Speter
7839441Smsmith        // Assertions: After establishing "new" mappings, verify that the defined
7939441Smsmith        //             DataFlavors can be retrieved and that the List is preserved.
8039441Smsmith        verifyNewMappings();
8139441Smsmith    }
8239664Smsmith
8340884Smsmith    // Verify getFlavorsForNative(String nat) is returning the correct list
8440884Smsmith    // of DataFlavors (for the new mappings).
8539664Smsmith    void verifyNewMappings() {
8648351Speter        // Enumerate through all natives
8748351Speter        System.out.println("*** native size = " + hashVerify.size());
8838465Smsmith        for (Enumeration e = hashVerify.keys() ; e.hasMoreElements() ;) {
8938465Smsmith            String key = (String)e.nextElement();
90109498Sobrien            compareFlavors(hashVerify.get(key), flavorMap.getFlavorsForNative(key), key);
91102623Sjhb            compareFlavors(flavorMap.getFlavorsForNative(key), hashVerify.get(key), key);
92109498Sobrien        }
9342493Smsmith    }
9442493Smsmith
9538465Smsmith    void compareFlavors(List<DataFlavor> flavors1, List<DataFlavor> flavors2, String key){
96109498Sobrien        for (DataFlavor flavor1 : flavors1) {
9740555Smsmith            boolean result = false;
9869985Srnordier            for (DataFlavor flavor2 : flavors2) {
9940555Smsmith                if (flavor1.equals(flavor2)) result = true;
100109498Sobrien            }
10142807Smsmith            if (!result)
10241821Smsmith                throw new RuntimeException("\n*** Error in verifyNewMappings()" +
10394956Sru                        "\nmethod1: addFlavorForUnencodedNative(String nat, DataFlavor flav)"  +
104109498Sobrien                        "\nmethod2: List getFlavorsForNative(String nat)" +
105115410Sscottl                        "\nString native: " + key +
10694956Sru                        "\nAfter adding several mappings with addFlavorForUnencodedNative," +
10794956Sru                        "\nthe returned list did not match the mappings that were added." +
10845759Sdcs                        "\nEither the mapping was not included in the list, or the order was incorect.");
10994956Sru        }
11045759Sdcs
11141821Smsmith    }
11258713Sjhb
11358713Sjhb    Set<DataFlavor> convertMimeTypeToDataFlavors(String baseType) throws Exception {
11440555Smsmith        Set<DataFlavor> result = new LinkedHashSet<>();
11540555Smsmith
11640555Smsmith        for (String charset : getStandardEncodings()) {
117109498Sobrien            for (String txtClass : new String[]{"java.io.InputStream", "java.nio.ByteBuffer", "\"[B\""}) {
11840555Smsmith                String mimeType = baseType + ";charset=" + charset + ";class=" + txtClass;
11998556Sphk
12039441Smsmith                if ("text/html".equals(baseType)) {
12140555Smsmith                    for (String documentType : new String[]{"all", "selection", "fragment"})
12240555Smsmith                        result.add(new DataFlavor(mimeType + ";document=" + documentType));
12340555Smsmith                } else {
12439441Smsmith                    DataFlavor df = new DataFlavor(mimeType);
12540338Speter                    if (df.equals(DataFlavor.plainTextFlavor))
12640338Speter                        df = DataFlavor.plainTextFlavor;
12740338Speter                    result.add(df);
12840555Smsmith                }
12940555Smsmith            }
13040338Speter        }
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
147