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 /* @test
  25    @bug 8058473
  26    @summary "Comparison method violates its general contract" when using Clipboard
  27             Ensure that DataFlavorComparator conforms to Comparator contract
  28    @author Anton Nashatyrev
  29    @modules java.datatransfer/sun.datatransfer
  30    @run main DataFlavorComparatorTest1
  31 */
  32 import sun.datatransfer.DataFlavorUtil;
  33 
  34 import java.awt.datatransfer.DataFlavor;
  35 import java.util.Comparator;
  36 
  37 public class DataFlavorComparatorTest1 {
  38 
  39     public static void main(String[] args) throws Exception {
  40         String[] mimes = new String[] {
  41                 "text/plain",
  42                 "text/plain; charset=unicode",
  43                 "text/plain; charset=cp1251",
  44                 "text/plain; charset=unicode; class=java.io.InputStream",
  45                 "text/plain; charset=unicode; class=java.io.Serializable",
  46                 "text/plain; charset=unicode; class=java.lang.Object",
  47                 "text/plain; class=java.lang.String",
  48                 "text/plain; class=java.io.Reader",
  49                 "text/plain; class=java.lang.Object",
  50                 "text/html",
  51                 "text/html; charset=unicode",
  52                 "text/html; charset=cp1251",
  53                 "text/html; charset=unicode; class=java.io.InputStream",
  54                 "text/html; charset=unicode; class=java.io.Serializable",
  55                 "text/html; charset=unicode; class=java.lang.Object",
  56                 "text/html; class=java.lang.String",
  57                 "text/html; class=java.io.Reader",
  58                 "text/html; class=java.lang.Object",
  59                 "text/unknown",
  60                 "text/unknown; charset=unicode",
  61                 "text/unknown; charset=cp1251",
  62                 "text/unknown; charset=unicode; class=java.io.InputStream",
  63                 "text/unknown; charset=unicode; class=java.io.Serializable",
  64                 "text/unknown; charset=unicode; class=java.lang.Object",
  65                 "text/unknown; class=java.lang.String",
  66                 "text/unknown; class=java.io.Reader",
  67                 "text/unknown; class=java.lang.Object",
  68                 "application/unknown; class=java.io.InputStream",
  69                 "application/unknown; class=java.lang.Object",
  70                 "application/unknown",
  71                 "application/x-java-jvm-local-objectref; class=java.io.InputStream",
  72                 "application/x-java-jvm-local-objectref; class=java.lang.Object",
  73                 "application/x-java-jvm-local-objectref",
  74                 "unknown/flavor",
  75                 "unknown/flavor; class=java.io.InputStream",
  76                 "unknown/flavor; class=java.lang.Object",
  77         };
  78 
  79         DataFlavor[] flavors = new DataFlavor[mimes.length];
  80         for (int i = 0; i < flavors.length; i++) {
  81             flavors[i] = new DataFlavor(mimes[i]);
  82         }
  83 
  84         testComparator(DataFlavorUtil.getDataFlavorComparator(), flavors);
  85 
  86         System.out.println("Passed.");
  87     }
  88 
  89     private static void testComparator(Comparator cmp, DataFlavor[] flavs)
  90             throws ClassNotFoundException {
  91 
  92         for (DataFlavor x: flavs) {
  93             for (DataFlavor y: flavs) {
  94                 if (Math.signum(cmp.compare(x,y)) != -Math.signum(cmp.compare(y,x))) {
  95                     throw new RuntimeException("Antisymmetry violated: " + x + ", " + y);
  96                 }
  97                 if (cmp.compare(x,y) == 0 && !x.equals(y)) {
  98                     throw new RuntimeException("Equals rule violated: " + x + ", " + y);
  99                 }
 100                 for (DataFlavor z: flavs) {
 101                     if (cmp.compare(x,y) == 0) {
 102                         if (Math.signum(cmp.compare(x, z)) != Math.signum(cmp.compare(y, z))) {
 103                             throw new RuntimeException("Transitivity (1) violated: " + x + ", " + y + ", " + z);
 104                         }
 105                     } else {
 106                         if (Math.signum(cmp.compare(x, y)) == Math.signum(cmp.compare(y, z))) {
 107                             if (Math.signum(cmp.compare(x, y)) != Math.signum(cmp.compare(x, z))) {
 108                                 throw new RuntimeException("Transitivity (2) violated: " + x + ", " + y + ", " + z);
 109                             }
 110                         }
 111                     }
 112                 }
 113             }
 114         }
 115     }
 116 }