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