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 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 {
  50         new AddFlavorTest().doTest();
  51     }
  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