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 
  30 /*
  31  * @test
  32  * @summary To test SystemFlavorMap method:
  33  *          addUnencodedNativeForFlavor(DataFlavor flav, String nat)
  34  *          with valid natives and DataFlavors. This stress test will
  35  *          define numerous mappings of valid String natives and
  36  *          DataFlavors.  The mappings will be verified by examining
  37  *          that all entries are present.
  38  * @author Rick Reynaga (rick.reynaga@eng.sun.com) area=Clipboard
  39  * @run main AddNativeTest
  40  */
  41 
  42 public class AddNativeTest {
  43 
  44     SystemFlavorMap flavorMap;
  45     Hashtable hashVerify;
  46 
  47     Map mapFlavors;
  48     Map mapNatives;
  49 
  50     Hashtable hashFlavors;
  51     Hashtable hashNatives;
  52 
  53     public static void main(String[] args) {
  54         new AddNativeTest().doTest();
  55     }
  56 
  57     public void doTest() {
  58         flavorMap = (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();
  59 
  60         // Get SystemFlavorMap Maps of String Natives and DataFlavors
  61         mapFlavors = flavorMap.getNativesForFlavors(null);
  62         mapNatives = flavorMap.getFlavorsForNatives(null);
  63 
  64         hashFlavors = new Hashtable(mapFlavors);
  65         hashNatives = new Hashtable(mapNatives);
  66 
  67         // Test addUnencodedNativeForFlavor(DataFlavor flav, String nat);
  68         //
  69         // Enumerate through all the system defined DataFlavors,
  70         // and for each DataFlavor, define it again to the SystemFlavorMap
  71         // with a slightly modified Mime Type.
  72         //
  73         // As a list of String natives will be returned for each DataFlavor,
  74         // the method addUnencodedNativeForFlavor will be called for each
  75         // String native in the list.
  76         //
  77         // For verification, a seperate Hashtable (Map) will be maintained with changes.
  78         DataFlavor key;
  79         hashVerify = new Hashtable();
  80 
  81         for (Enumeration e = hashFlavors.keys() ; e.hasMoreElements() ;) {
  82             key = (DataFlavor)e.nextElement();
  83 
  84             java.util.List listNatives = flavorMap.getNativesForFlavor(key);
  85             Vector vectorNatives = new Vector(listNatives);
  86 
  87             // Construct a new DataFlavor from an existing DataFlavor's MimeType
  88             // Example:
  89             // Original MimeType: "text/plain; class=java.io.Reader; charset=Unicode"
  90             // Modified MimeType: "text/plain-TEST; class=java.io.Reader; charset=Unicode"
  91 
  92             StringBuffer mimeType = new StringBuffer(key.getMimeType());
  93             mimeType.insert(mimeType.indexOf(";"),"-TEST");
  94 
  95             DataFlavor testFlavor = new DataFlavor(mimeType.toString(), "Test DataFlavor");
  96 
  97             for (ListIterator i = vectorNatives.listIterator() ; i.hasNext() ;) {
  98                 String element = (String)i.next();
  99                 flavorMap.addUnencodedNativeForFlavor(testFlavor, element);
 100             }
 101 
 102             //Added following 4 lines - Aruna Samji - 07/22/2003
 103             Vector existingNatives = new Vector(flavorMap.getNativesForFlavor(testFlavor));
 104             existingNatives.addAll(vectorNatives);
 105             vectorNatives = existingNatives;
 106             hashVerify.put(testFlavor, vectorNatives);
 107         }
 108 
 109         // Assertions: After establishing "new" mappings, verify that the defined
 110         //             DataFlavors can be retrieved.
 111         verifyNewMappings();
 112     }
 113 
 114     // Verify getFlavorsForNative(String nat) is returning the correct list
 115     // of DataFlavors (for the new mappings).
 116     public boolean verifyNewMappings() {
 117         boolean result = true;
 118 
 119         // Enumerate through all DataFlavors
 120         for (Enumeration e = hashVerify.keys() ; e.hasMoreElements() ;) {
 121             DataFlavor key = (DataFlavor)e.nextElement();
 122 
 123             java.util.List listNatives = flavorMap.getNativesForFlavor(key);
 124             Vector vectorNatives = new Vector(listNatives);
 125 
 126             // Compare the list of Natives
 127             //Next line changed by Kanishk to comply with bug 4696522
 128             //if ( !vectorNatives.equals((Vector)hashVerify.get(key))) {
 129             if ( !(vectorNatives.containsAll((Vector)hashVerify.get(key)) && ((Vector)hashVerify.get(key)).containsAll(vectorNatives))) {
 130                 throw new RuntimeException("\n*** Error in verifyNewMappings()" +
 131                     "\nmethod1: addUnencodedNativeForFlavor(DataFlavor flav, String nat)"  +
 132                     "\nmethod2: List getNativesForFlavor(DataFlavor flav)" +
 133                     "\nDataFlavor: " + key.getMimeType() +
 134                     "\nAfter adding several mappings with addUnencodedNativeForFlavor," +
 135                     "\nthe returned list did not match the mappings that were added." +
 136                     "\nThe mapping was not included in the list.");
 137             }
 138         }
 139         System.out.println("*** DataFlavor size = " + hashVerify.size());
 140         System.out.println("*** verifyNewMappings result: " + result + "\n");
 141         return result;
 142     }
 143 }
 144