1 /*
   2  * Copyright (c) 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.awt.datatransfer.DataFlavor;
  25 import java.awt.datatransfer.SystemFlavorMap;
  26 import java.util.List;
  27 
  28 /*
  29   @test
  30   @bug 4512530 8027148
  31   @summary tests that mappings for text flavors are generated properly
  32   @author das@sparc.spb.su area=datatransfer
  33 */
  34 
  35 public class MappingGenerationTest {
  36 
  37     private static final SystemFlavorMap fm =
  38         (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();
  39 
  40     public static void main(String[] args)  {
  41         test1();
  42         test2();
  43         test3();
  44         test4();
  45         test5();
  46         test6();
  47     }
  48 
  49     /**
  50      * Verifies that Lists returned from getNativesForFlavor() and
  51      * getFlavorsForNative() are not modified with a subsequent call
  52      * to addUnencodedNativeForFlavor() and addFlavorForUnencodedNative()
  53      * respectively.
  54      */
  55     public static void test1() {
  56         DataFlavor df = new DataFlavor("text/plain-test1", null);
  57         String nat = "native1";
  58 
  59         List<String> natives = fm.getNativesForFlavor(df);
  60         fm.addUnencodedNativeForFlavor(df, nat);
  61         List<String> nativesNew = fm.getNativesForFlavor(df);
  62         if (natives.equals(nativesNew)) {
  63             System.err.println("orig=" + natives);
  64             System.err.println("new=" + nativesNew);
  65             throw new RuntimeException("Test failed");
  66         }
  67 
  68         List<DataFlavor> flavors = fm.getFlavorsForNative(nat);
  69         fm.addFlavorForUnencodedNative(nat, df);
  70         List<DataFlavor> flavorsNew = fm.getFlavorsForNative(nat);
  71         if (flavors.equals(flavorsNew)) {
  72             System.err.println("orig=" + flavors);
  73             System.err.println("new=" + flavorsNew);
  74             throw new RuntimeException("Test failed");
  75         }
  76     }
  77 
  78     /**
  79      * Verifies that SystemFlavorMap is not affected by modification of
  80      * the Lists returned from getNativesForFlavor() and
  81      * getFlavorsForNative().
  82      */
  83     public static void test2() {
  84         DataFlavor df = new DataFlavor("text/plain-test2", null);
  85         String nat = "native2";
  86         DataFlavor extraDf = new DataFlavor("text/test", null);
  87 
  88         List<String> natives = fm.getNativesForFlavor(df);
  89         natives.add("Should not be here");
  90         java.util.List nativesNew = fm.getNativesForFlavor(df);
  91         if (natives.equals(nativesNew)) {
  92             System.err.println("orig=" + natives);
  93             System.err.println("new=" + nativesNew);
  94             throw new RuntimeException("Test failed");
  95         }
  96 
  97         List<DataFlavor> flavors = fm.getFlavorsForNative(nat);
  98         flavors.add(extraDf);
  99         java.util.List flavorsNew = fm.getFlavorsForNative(nat);
 100         if (flavors.equals(flavorsNew)) {
 101             System.err.println("orig=" + flavors);
 102             System.err.println("new=" + flavorsNew);
 103             throw new RuntimeException("Test failed");
 104         }
 105     }
 106 
 107     /**
 108      * Verifies that addUnencodedNativeForFlavor() for a particular text flavor
 109      * doesn't affect mappings for other flavors.
 110      */
 111     public static void test3() {
 112         DataFlavor df1 = new DataFlavor("text/plain-test3", null);
 113         DataFlavor df2 = new DataFlavor("text/plain-test3; charset=Unicode; class=java.io.Reader", null);
 114         String nat = "native3";
 115         List<String> natives = fm.getNativesForFlavor(df2);
 116         fm.addUnencodedNativeForFlavor(df1, nat);
 117         List<String> nativesNew = fm.getNativesForFlavor(df2);
 118         if (!natives.equals(nativesNew)) {
 119             System.err.println("orig=" + natives);
 120             System.err.println("new=" + nativesNew);
 121             throw new RuntimeException("Test failed");
 122         }
 123     }
 124 
 125     /**
 126      * Verifies that addUnencodedNativeForFlavor() really adds the specified
 127      * flavor-to-native mapping to the existing mappings.
 128      */
 129     public static void test4() {
 130         DataFlavor df = new DataFlavor("text/plain-test4; charset=Unicode; class=java.io.Reader", null);
 131         String nat = "native4";
 132         List<String> natives = fm.getNativesForFlavor(df);
 133         if (!natives.contains(nat)) {
 134             fm.addUnencodedNativeForFlavor(df, nat);
 135             List<String> nativesNew = fm.getNativesForFlavor(df);
 136             natives.add(nat);
 137             if (!natives.equals(nativesNew)) {
 138                 System.err.println("orig=" + natives);
 139                 System.err.println("new=" + nativesNew);
 140                 throw new RuntimeException("Test failed");
 141             }
 142         }
 143     }
 144 
 145     /**
 146      * Verifies that a flavor doesn't have any flavor-to-native mappings after
 147      * a call to setNativesForFlavor() with this flavor and an empty native
 148      * array as arguments.
 149      */
 150     public static void test5() {
 151         final DataFlavor flavor =
 152             new DataFlavor("text/plain-TEST5; charset=Unicode", null);
 153 
 154         fm.getNativesForFlavor(flavor);
 155 
 156         fm.setNativesForFlavor(flavor, new String[0]);
 157 
 158         List<String> natives = fm.getNativesForFlavor(flavor);
 159 
 160         if (!natives.isEmpty()) {
 161             System.err.println("natives=" + natives);
 162             throw new RuntimeException("Test failed");
 163         }
 164     }
 165 
 166     /**
 167      * Verifies that a native doesn't have any native-to-flavor mappings after
 168      * a call to setFlavorsForNative() with this native and an empty flavor
 169      * array as arguments.
 170      */
 171     public static void test6() {
 172         final String nat = "STRING";
 173         fm.getFlavorsForNative(nat);
 174         fm.setFlavorsForNative(nat, new DataFlavor[0]);
 175 
 176         List<DataFlavor> flavors = fm.getFlavorsForNative(nat);
 177 
 178         if (!flavors.isEmpty()) {
 179             System.err.println("flavors=" + flavors);
 180             throw new RuntimeException("Test failed");
 181         }
 182     }
 183 }