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 
  30 /*
  31  * @test
  32  * @summary To test SystemFlavorMap methods
  33  *          List getFlavorsForNative(String nat)
  34  *          List getNativesForFlavor(DataFlavor flav)
  35  *          with valid natives and DataFlavors, including null.
  36  *          This test will verify that the returned mappings
  37  *          include all entries and that the correct order is
  38  *          maintained.
  39  * @author Rick Reynaga (rick.reynaga@eng.sun.com) area=Clipboard
  40  * @modules java.datatransfer
  41  * @run main ManyFlavorMapTest
  42  */
  43 
  44 public class ManyFlavorMapTest {
  45 
  46     SystemFlavorMap flavorMap;
  47 
  48     Map mapFlavors;
  49     Map mapNatives;
  50 
  51     Hashtable hashFlavors;
  52     Hashtable hashNatives;
  53 
  54     public static void main (String[] args) {
  55         new ManyFlavorMapTest().doTest();
  56     }
  57 
  58     public void doTest() {
  59         flavorMap = (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();
  60 
  61         // Get SystemFlavorMap Maps of String Natives and DataFlavors
  62         mapFlavors = flavorMap.getNativesForFlavors(null);
  63         mapNatives = flavorMap.getFlavorsForNatives(null);
  64 
  65         hashFlavors = new Hashtable(mapFlavors);
  66         hashNatives = new Hashtable(mapNatives);
  67 
  68         // Assertion: Verify getNativesForFlavors(null) is returning the full list
  69         //            of String Native entries
  70         List listNatives = flavorMap.getNativesForFlavor(null);
  71         verifyListAllNativeEntries(listNatives);
  72 
  73         // Assertion: Verify getFlavorsForNatives(null) is returning the full list
  74         //            of DataFlavor entries
  75         List listFlavors = flavorMap.getFlavorsForNative(null);
  76         verifyListAllDataFlavorEntries(listFlavors);
  77 
  78         // Assertion: Verify getNativesForFlavor(DataFlavor flav) is returning the list
  79         //            of Native Strings (for all supported DataFlavors)
  80         //
  81         //            Verify that the first list item is the most preferred Native
  82         verifyListNativeEntries();
  83 
  84         // Assertion: Verify getFlavorsForNative(String nat) is returning the list
  85         //            of DataFlavors(for all supported natives)
  86         //
  87         //            Verify that the first list item is the most preferred DataFlavor
  88         verifyListDataFlavorEntries();
  89     }
  90 
  91     // Verify getFlavorsForNative(String nat) is returning the list
  92     // of DataFlavors(for all supported natives)
  93     public void verifyListDataFlavorEntries() {
  94         // Enumerate through all natives
  95         for (Enumeration e = hashNatives.keys() ; e.hasMoreElements() ;) {
  96             String key = (String)e.nextElement();
  97 
  98             // SystemFlavorMap preferred value
  99             DataFlavor value = (DataFlavor)hashNatives.get(key);
 100 
 101             java.util.List listFlavors = flavorMap.getFlavorsForNative(key);
 102             Vector vectorFlavors = new Vector(listFlavors);
 103 
 104             // First element should be preferred value
 105             DataFlavor prefFlavor = (DataFlavor)vectorFlavors.firstElement();
 106             if ( value != prefFlavor ) {
 107                 throw new RuntimeException("\n*** Error in verifyListDataFlavorEntries()" +
 108                     "\nAPI Test: List getFlavorsForNative(String nat)" +
 109                     "\native: " + key +
 110                     "\nSystemFlavorMap preferred native: " + value.getMimeType() +
 111                     "\nList first entry: " + prefFlavor.getMimeType() +
 112                     "\nTest failed because List first entry does not match preferred");
 113             }
 114         }
 115         System.out.println("*** native size = " + hashNatives.size());
 116     }
 117 
 118     // Verify getNativesForFlavor(DataFlavor flav) is returning the list
 119     // of Native Strings (for all supported DataFlavors)
 120     public void verifyListNativeEntries() {
 121         // Enumerate through all DataFlavors
 122         for (Enumeration e = hashFlavors.keys() ; e.hasMoreElements() ;) {
 123             DataFlavor key = (DataFlavor)e.nextElement();
 124 
 125             // SystemFlavorMap preferred value
 126             String value = (String)hashFlavors.get(key);
 127 
 128             java.util.List listNatives = flavorMap.getNativesForFlavor(key);
 129             Vector vectorNatives = new Vector(listNatives);
 130 
 131             // First element should be preferred value
 132             String prefNative = (String)vectorNatives.firstElement();
 133             if ( value != prefNative ) {
 134                 throw new RuntimeException("\n*** Error in verifyListNativeEntries()" +
 135                     "\nAPI Test: List getNativesForFlavor(DataFlavor flav)" +
 136                     "\nDataFlavor: " + key.getMimeType() +
 137                     "\nSystemFlavorMap preferred native: " + value +
 138                     "\nList first entry: " + prefNative +
 139                     "\nTest failed because List first entry does not match preferred");
 140             }
 141         }
 142         System.out.println("*** DataFlavor size = " + hashFlavors.size());
 143     }
 144 
 145     // Compare List of Natives with list from SystemFlavorMap
 146     //
 147     // Verification will be done by comparing the two results as sets
 148     public void verifyListAllNativeEntries(java.util.List listNatives) {
 149 
 150         HashSet hashSetMap = new HashSet(mapNatives.keySet());
 151         HashSet hashSetList = new HashSet(listNatives);
 152 
 153         System.out.println("*** hashSetMap size = " + hashSetMap.size());
 154         System.out.println("*** hashSetList size = " + hashSetList.size());
 155 
 156         if (!hashSetMap.equals(hashSetList)) {
 157             throw new RuntimeException("\n*** Error in verifyListAllNativeEntries()" +
 158                 "\nAPI Test: List getNativesForFlavor(null)" +
 159                 "\nTest failed because the returned List does not exactly" +
 160                 "\nmatch objects returned from SystemFlavorMap.");
 161         }
 162     }
 163 
 164     // Compare List of DataFlavors with list from SystemFlavorMap
 165     //
 166     // Verification will be done by comparing the two results as sets
 167     public void verifyListAllDataFlavorEntries(java.util.List listFlavors) {
 168 
 169         HashSet hashSetMap = new HashSet(mapFlavors.keySet());
 170         HashSet hashSetList = new HashSet(listFlavors);
 171 
 172         System.out.println("*** hashSetMap size = " + hashSetMap.size());
 173         System.out.println("*** hashSetList size = " + hashSetList.size());
 174 
 175         if (!hashSetMap.equals(hashSetList)) {
 176             throw new RuntimeException("\n*** Error in verifyListAllDataFlavorEntries()" +
 177                 "\nAPI Test: List getFlavorsForNative(null)" +
 178                 "\nTest failed because the returned List does not exactly" +
 179                 "\nmatch objects returned from SystemFlavorMap.");
 180         }
 181     }
 182 }
 183