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