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 /* @test
  25    @bug 8058473
  26    @summary "Comparison method violates its general contract" when using Clipboard
  27             Ensure that DataTransferer.DataFlavorComparator conforms to Comparator contract
  28    @author Anton Nashatyrev
  29    @run main DataFlavorComparatorTest1
  30 */
  31 import sun.awt.datatransfer.DataTransferer;
  32 
  33 import java.awt.datatransfer.DataFlavor;
  34 import java.util.Comparator;
  35 
  36 public class DataFlavorComparatorTest1 {
  37 
  38     public static void main(String[] args) throws Exception {
  39         String[] mimes = new String[] {
  40                 "text/plain;class=java.nio.ByteBuffer;charset=UTF-8",
  41                 "text/uri-list;class=java.nio.ByteBuffer;charset=UTF-8",
  42                 "text/plain;class=java.nio.ByteBuffer;charset=UTF-16LE",
  43                 "text/uri-list;class=java.nio.ByteBuffer;charset=UTF-16LE",
  44                 "application/x-java-text-encoding",
  45                 "application/x-java-serialized-object;class=java.lang.String",
  46                 "text/plain;class=java.io.InputStream;charset=UTF-8",
  47                 "text/uri-list;class=java.io.InputStream;charset=UTF-8",
  48                 "text/plain;class=java.io.InputStream;charset=windows-1252",
  49                 "text/uri-list;class=java.io.InputStream;charset=windows-1252",
  50                 "application/x-java-url;class=java.net.URL",
  51                 "text/plain;class=java.io.Reader",
  52                 "text/plain;charset=windows-1252",
  53                 "text/uri-list;class=java.io.Reader",
  54                 "text/uri-list;charset=windows-1252",
  55                 "text/plain;charset=UTF-8",
  56                 "text/uri-list;charset=UTF-8",
  57                 "text/plain;class=java.io.InputStream;charset=US-ASCII",
  58                 "text/uri-list;class=java.io.InputStream;charset=US-ASCII",
  59                 "text/plain;class=java.io.InputStream;charset=UTF-16LE",
  60                 "text/plain;charset=US-ASCII",
  61                 "text/uri-list;class=java.io.InputStream;charset=UTF-16LE",
  62                 "text/uri-list;charset=US-ASCII",
  63                 "text/plain;charset=UTF-16LE",
  64                 "text/uri-list;charset=UTF-16LE",
  65                 "text/plain;class=java.nio.ByteBuffer;charset=UTF-16BE",
  66                 "text/uri-list;class=java.nio.ByteBuffer;charset=UTF-16BE",
  67                 "text/plain;class=java.nio.ByteBuffer;charset=ISO-8859-1",
  68                 "text/uri-list;class=java.nio.ByteBuffer;charset=ISO-8859-1",
  69                 "text/plain",
  70                 "text/uri-list",
  71                 "text/plain;class=java.nio.ByteBuffer;charset=UTF-16",
  72                 "text/uri-list;class=java.nio.ByteBuffer;charset=UTF-16",
  73                 "text/plain;class=java.io.InputStream;charset=unicode",
  74                 "text/uri-list;class=java.io.InputStream;charset=UTF-16",
  75                 "text/plain;class=java.nio.CharBuffer",
  76                 "text/uri-list;class=java.nio.CharBuffer",
  77                 "text/plain;class=java.lang.String",
  78                 "text/plain;charset=UTF-16BE",
  79                 "text/uri-list;class=java.lang.String",
  80                 "text/uri-list;charset=UTF-16BE",
  81                 "text/plain;charset=ISO-8859-1",
  82                 "text/uri-list;charset=ISO-8859-1",
  83                 "text/plain;class=java.io.InputStream;charset=UTF-16BE",
  84                 "text/uri-list;class=java.io.InputStream;charset=UTF-16BE",
  85                 "text/plain;class=java.nio.ByteBuffer;charset=US-ASCII",
  86                 "text/uri-list;class=java.nio.ByteBuffer;charset=US-ASCII",
  87                 "text/plain;class=java.io.InputStream;charset=ISO-8859-1",
  88                 "text/uri-list;class=java.io.InputStream;charset=ISO-8859-1",
  89                 "text/plain;charset=UTF-16",
  90                 "text/plain;class=java.nio.ByteBuffer;charset=windows-1252",
  91                 "text/uri-list;charset=UTF-16",
  92                 "text/uri-list;class=java.nio.ByteBuffer;charset=windows-1252",
  93                 "text/plain;class=java.io.InputStream;charset=windows-1252",
  94                 "text/uri-list;class=java.io.InputStream;charset=windows-1252",
  95         };
  96 
  97         DataFlavor[] flavors = new DataFlavor[mimes.length];
  98         for (int i = 0; i < flavors.length; i++) {
  99             flavors[i] = new DataFlavor(mimes[i]);
 100         }
 101 
 102         testComparator(new DataTransferer.DataFlavorComparator(true), flavors);
 103         testComparator(new DataTransferer.DataFlavorComparator(false), flavors);
 104 
 105     }
 106 
 107     private static void testComparator(Comparator cmp, DataFlavor[] flavs)
 108             throws ClassNotFoundException {
 109 
 110         for (DataFlavor x: flavs) {
 111             for (DataFlavor y: flavs) {
 112                 if (Math.signum(cmp.compare(x,y)) != -Math.signum(cmp.compare(y,x))) {
 113                     throw new RuntimeException("Antisymmetry violated: " + x + ", " + y);
 114                 }
 115                 if (cmp.compare(x,y) == 0 && !x.equals(y)) {
 116                     throw new RuntimeException("Equals rule violated: " + x + ", " + y);
 117                 }
 118                 for (DataFlavor z: flavs) {
 119                     if (cmp.compare(x,y) == 0) {
 120                         if (Math.signum(cmp.compare(x, z)) != Math.signum(cmp.compare(y, z))) {
 121                             throw new RuntimeException("Transitivity (1) violated: " + x + ", " + y + ", " + z);
 122                         }
 123                     } else {
 124                         if (Math.signum(cmp.compare(x, y)) == Math.signum(cmp.compare(y, z))) {
 125                             if (Math.signum(cmp.compare(x, y)) != Math.signum(cmp.compare(x, z))) {
 126                                 throw new RuntimeException("Transitivity (2) violated: " + x + ", " + y + ", " + z);
 127                             }
 128                         }
 129                     }
 130                 }
 131             }
 132         }
 133     }
 134 }