1 /*
   2  * Copyright (c) 2001, 2015, 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 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  * @modules java.datatransfer
  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             hashVerify.put(key, new Vector(flavorsSet));
  77         }
  78 
  79         // Assertions: After establishing "new" mappings, verify that the defined
  80         //             DataFlavors can be retrieved and that the List is preserved.
  81         verifyNewMappings();
  82     }
  83 
  84     // Verify getFlavorsForNative(String nat) is returning the correct list
  85     // of DataFlavors (for the new mappings).
  86     void verifyNewMappings() {
  87         // Enumerate through all natives
  88         System.out.println("*** native size = " + hashVerify.size());
  89         for (Enumeration e = hashVerify.keys() ; e.hasMoreElements() ;) {
  90             String key = (String)e.nextElement();
  91             compareFlavors(hashVerify.get(key), flavorMap.getFlavorsForNative(key), key);
  92             compareFlavors(flavorMap.getFlavorsForNative(key), hashVerify.get(key), key);
  93         }
  94     }
  95 
  96     void compareFlavors(List<DataFlavor> flavors1, List<DataFlavor> flavors2, String key){
  97         for (DataFlavor flavor1 : flavors1) {
  98             boolean result = false;
  99             for (DataFlavor flavor2 : flavors2) {
 100                 if (flavor1.equals(flavor2)) result = true;
 101             }
 102             if (!result)
 103                 throw new RuntimeException("\n*** Error in verifyNewMappings()" +
 104                         "\nmethod1: addFlavorForUnencodedNative(String nat, DataFlavor flav)"  +
 105                         "\nmethod2: List getFlavorsForNative(String nat)" +
 106                         "\nString native: " + key +
 107                         "\nAfter adding several mappings with addFlavorForUnencodedNative," +
 108                         "\nthe returned list did not match the mappings that were added." +
 109                         "\nEither the mapping was not included in the list, or the order was incorect.");
 110         }
 111 
 112     }
 113 
 114     Set<DataFlavor> convertMimeTypeToDataFlavors(String baseType) throws Exception {
 115         Set<DataFlavor> result = new LinkedHashSet<>();
 116 
 117         for (String charset : getStandardEncodings()) {
 118             for (String txtClass : new String[]{"java.io.InputStream", "java.nio.ByteBuffer", "\"[B\""}) {
 119                 String mimeType = baseType + ";charset=" + charset + ";class=" + txtClass;
 120 
 121                 if ("text/html".equals(baseType)) {
 122                     for (String documentType : new String[]{"all", "selection", "fragment"})
 123                         result.add(new DataFlavor(mimeType + ";document=" + documentType));
 124                 } else {
 125                     DataFlavor df = new DataFlavor(mimeType);
 126                     if (df.equals(DataFlavor.plainTextFlavor))
 127                         df = DataFlavor.plainTextFlavor;
 128                     result.add(df);
 129                 }
 130             }
 131         }
 132         return result;
 133     }
 134 
 135     Set<String> getStandardEncodings() {
 136         Set<String> tempSet = new HashSet<>();
 137         tempSet.add("US-ASCII");
 138         tempSet.add("ISO-8859-1");
 139         tempSet.add("UTF-8");
 140         tempSet.add("UTF-16BE");
 141         tempSet.add("UTF-16LE");
 142         tempSet.add("UTF-16");
 143         tempSet.add(Charset.defaultCharset().name());
 144         return tempSet;
 145     }
 146 }
 147