1 /*
   2  * Copyright (c) 2014, 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.
   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   @modules java.datatransfer
  34 */
  35 
  36 public class MappingGenerationTest {
  37 
  38     private static final SystemFlavorMap fm =
  39         (SystemFlavorMap)SystemFlavorMap.getDefaultFlavorMap();
  40 
  41     public static void main(String[] args)  {
  42         test1();
  43         test2();
  44         test3();
  45         test4();
  46         test5();
  47         test6();
  48     }
  49 
  50     /**
  51      * Verifies that Lists returned from getNativesForFlavor() and
  52      * getFlavorsForNative() are not modified with a subsequent call
  53      * to addUnencodedNativeForFlavor() and addFlavorForUnencodedNative()
  54      * respectively.
  55      */
  56     public static void test1() {
  57         DataFlavor df = new DataFlavor("text/plain-test1", null);
  58         String nat = "native1";
  59 
  60         List<String> natives = fm.getNativesForFlavor(df);
  61         fm.addUnencodedNativeForFlavor(df, nat);
  62         List<String> nativesNew = fm.getNativesForFlavor(df);
  63         if (natives.equals(nativesNew)) {
  64             System.err.println("orig=" + natives);
  65             System.err.println("new=" + nativesNew);
  66             throw new RuntimeException("Test failed");
  67         }
  68 
  69         List<DataFlavor> flavors = fm.getFlavorsForNative(nat);
  70         fm.addFlavorForUnencodedNative(nat, df);
  71         List<DataFlavor> flavorsNew = fm.getFlavorsForNative(nat);
  72         if (flavors.equals(flavorsNew)) {
  73             System.err.println("orig=" + flavors);
  74             System.err.println("new=" + flavorsNew);
  75             throw new RuntimeException("Test failed");
  76         }
  77     }
  78 
  79     /**
  80      * Verifies that SystemFlavorMap is not affected by modification of
  81      * the Lists returned from getNativesForFlavor() and
  82      * getFlavorsForNative().
  83      */
  84     public static void test2() {
  85         DataFlavor df = new DataFlavor("text/plain-test2", null);
  86         String nat = "native2";
  87         DataFlavor extraDf = new DataFlavor("text/test", null);
  88 
  89         List<String> natives = fm.getNativesForFlavor(df);
  90         natives.add("Should not be here");
  91         java.util.List nativesNew = fm.getNativesForFlavor(df);
  92         if (natives.equals(nativesNew)) {
  93             System.err.println("orig=" + natives);
  94             System.err.println("new=" + nativesNew);
  95             throw new RuntimeException("Test failed");
  96         }
  97 
  98         List<DataFlavor> flavors = fm.getFlavorsForNative(nat);
  99         flavors.add(extraDf);
 100         java.util.List flavorsNew = fm.getFlavorsForNative(nat);
 101         if (flavors.equals(flavorsNew)) {
 102             System.err.println("orig=" + flavors);
 103             System.err.println("new=" + flavorsNew);
 104             throw new RuntimeException("Test failed");
 105         }
 106     }
 107 
 108     /**
 109      * Verifies that addUnencodedNativeForFlavor() for a particular text flavor
 110      * doesn't affect mappings for other flavors.
 111      */
 112     public static void test3() {
 113         DataFlavor df1 = new DataFlavor("text/plain-test3", null);
 114         DataFlavor df2 = new DataFlavor("text/plain-test3; charset=Unicode; class=java.io.Reader", null);
 115         String nat = "native3";
 116         List<String> natives = fm.getNativesForFlavor(df2);
 117         fm.addUnencodedNativeForFlavor(df1, nat);
 118         List<String> nativesNew = fm.getNativesForFlavor(df2);
 119         if (!natives.equals(nativesNew)) {
 120             System.err.println("orig=" + natives);
 121             System.err.println("new=" + nativesNew);
 122             throw new RuntimeException("Test failed");
 123         }
 124     }
 125 
 126     /**
 127      * Verifies that addUnencodedNativeForFlavor() really adds the specified
 128      * flavor-to-native mapping to the existing mappings.
 129      */
 130     public static void test4() {
 131         DataFlavor df = new DataFlavor("text/plain-test4; charset=Unicode; class=java.io.Reader", null);
 132         String nat = "native4";
 133         List<String> natives = fm.getNativesForFlavor(df);
 134         if (!natives.contains(nat)) {
 135             fm.addUnencodedNativeForFlavor(df, nat);
 136             List<String> nativesNew = fm.getNativesForFlavor(df);
 137             natives.add(nat);
 138             if (!natives.equals(nativesNew)) {
 139                 System.err.println("orig=" + natives);
 140                 System.err.println("new=" + nativesNew);
 141                 throw new RuntimeException("Test failed");
 142             }
 143         }
 144     }
 145 
 146     /**
 147      * Verifies that a flavor doesn't have any flavor-to-native mappings after
 148      * a call to setNativesForFlavor() with this flavor and an empty native
 149      * array as arguments.
 150      */
 151     public static void test5() {
 152         final DataFlavor flavor =
 153             new DataFlavor("text/plain-TEST5; charset=Unicode", null);
 154 
 155         fm.getNativesForFlavor(flavor);
 156 
 157         fm.setNativesForFlavor(flavor, new String[0]);
 158 
 159         List<String> natives = fm.getNativesForFlavor(flavor);
 160 
 161         if (!natives.isEmpty()) {
 162             System.err.println("natives=" + natives);
 163             throw new RuntimeException("Test failed");
 164         }
 165     }
 166 
 167     /**
 168      * Verifies that a native doesn't have any native-to-flavor mappings after
 169      * a call to setFlavorsForNative() with this native and an empty flavor
 170      * array as arguments.
 171      */
 172     public static void test6() {
 173         final String nat = "STRING";
 174         fm.getFlavorsForNative(nat);
 175         fm.setFlavorsForNative(nat, new DataFlavor[0]);
 176 
 177         List<DataFlavor> flavors = fm.getFlavorsForNative(nat);
 178 
 179         if (!flavors.isEmpty()) {
 180             System.err.println("flavors=" + flavors);
 181             throw new RuntimeException("Test failed");
 182         }
 183     }
 184 }