Path: blob/master/test/jdk/sun/awt/datatransfer/DataFlavorComparatorTest1.java
41152 views
/*1* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 805847325@summary "Comparison method violates its general contract" when using Clipboard26Ensure that DataFlavorComparator conforms to Comparator contract27@author Anton Nashatyrev28@modules java.datatransfer/sun.datatransfer29@run main DataFlavorComparatorTest130*/31import sun.datatransfer.DataFlavorUtil;3233import java.awt.datatransfer.DataFlavor;34import java.util.Comparator;3536public class DataFlavorComparatorTest1 {3738public static void main(String[] args) throws Exception {39String[] 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};9697DataFlavor[] flavors = new DataFlavor[mimes.length];98for (int i = 0; i < flavors.length; i++) {99flavors[i] = new DataFlavor(mimes[i]);100}101102testComparator(DataFlavorUtil.getDataFlavorComparator(), flavors);103104System.out.println("Passed.");105}106107private static void testComparator(Comparator cmp, DataFlavor[] flavs)108throws ClassNotFoundException {109110for (DataFlavor x: flavs) {111for (DataFlavor y: flavs) {112if (Math.signum(cmp.compare(x,y)) != -Math.signum(cmp.compare(y,x))) {113throw new RuntimeException("Antisymmetry violated: " + x + ", " + y);114}115if (cmp.compare(x,y) == 0 && !x.equals(y)) {116throw new RuntimeException("Equals rule violated: " + x + ", " + y);117}118for (DataFlavor z: flavs) {119if (cmp.compare(x,y) == 0) {120if (Math.signum(cmp.compare(x, z)) != Math.signum(cmp.compare(y, z))) {121throw new RuntimeException("Transitivity (1) violated: " + x + ", " + y + ", " + z);122}123} else {124if (Math.signum(cmp.compare(x, y)) == Math.signum(cmp.compare(y, z))) {125if (Math.signum(cmp.compare(x, y)) != Math.signum(cmp.compare(x, z))) {126throw new RuntimeException("Transitivity (2) violated: " + x + ", " + y + ", " + z);127}128}129}130}131}132}133}134}135136137