Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/awt/datatransfer/DataFlavorComparatorTest1.java
38839 views
/*1* Copyright (c) 2014, 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 DataTransferer.DataFlavorComparator conforms to Comparator contract27@author Anton Nashatyrev28@run main DataFlavorComparatorTest129*/30import sun.awt.datatransfer.DataTransferer;3132import java.awt.datatransfer.DataFlavor;33import java.util.Comparator;3435public class DataFlavorComparatorTest1 {3637public static void main(String[] args) throws Exception {38String[] mimes = new String[] {39"text/plain;class=java.nio.ByteBuffer;charset=UTF-8",40"text/uri-list;class=java.nio.ByteBuffer;charset=UTF-8",41"text/plain;class=java.nio.ByteBuffer;charset=UTF-16LE",42"text/uri-list;class=java.nio.ByteBuffer;charset=UTF-16LE",43"application/x-java-text-encoding",44"application/x-java-serialized-object;class=java.lang.String",45"text/plain;class=java.io.InputStream;charset=UTF-8",46"text/uri-list;class=java.io.InputStream;charset=UTF-8",47"text/plain;class=java.io.InputStream;charset=windows-1252",48"text/uri-list;class=java.io.InputStream;charset=windows-1252",49"application/x-java-url;class=java.net.URL",50"text/plain;class=java.io.Reader",51"text/plain;charset=windows-1252",52"text/uri-list;class=java.io.Reader",53"text/uri-list;charset=windows-1252",54"text/plain;charset=UTF-8",55"text/uri-list;charset=UTF-8",56"text/plain;class=java.io.InputStream;charset=US-ASCII",57"text/uri-list;class=java.io.InputStream;charset=US-ASCII",58"text/plain;class=java.io.InputStream;charset=UTF-16LE",59"text/plain;charset=US-ASCII",60"text/uri-list;class=java.io.InputStream;charset=UTF-16LE",61"text/uri-list;charset=US-ASCII",62"text/plain;charset=UTF-16LE",63"text/uri-list;charset=UTF-16LE",64"text/plain;class=java.nio.ByteBuffer;charset=UTF-16BE",65"text/uri-list;class=java.nio.ByteBuffer;charset=UTF-16BE",66"text/plain;class=java.nio.ByteBuffer;charset=ISO-8859-1",67"text/uri-list;class=java.nio.ByteBuffer;charset=ISO-8859-1",68"text/plain",69"text/uri-list",70"text/plain;class=java.nio.ByteBuffer;charset=UTF-16",71"text/uri-list;class=java.nio.ByteBuffer;charset=UTF-16",72"text/plain;class=java.io.InputStream;charset=unicode",73"text/uri-list;class=java.io.InputStream;charset=UTF-16",74"text/plain;class=java.nio.CharBuffer",75"text/uri-list;class=java.nio.CharBuffer",76"text/plain;class=java.lang.String",77"text/plain;charset=UTF-16BE",78"text/uri-list;class=java.lang.String",79"text/uri-list;charset=UTF-16BE",80"text/plain;charset=ISO-8859-1",81"text/uri-list;charset=ISO-8859-1",82"text/plain;class=java.io.InputStream;charset=UTF-16BE",83"text/uri-list;class=java.io.InputStream;charset=UTF-16BE",84"text/plain;class=java.nio.ByteBuffer;charset=US-ASCII",85"text/uri-list;class=java.nio.ByteBuffer;charset=US-ASCII",86"text/plain;class=java.io.InputStream;charset=ISO-8859-1",87"text/uri-list;class=java.io.InputStream;charset=ISO-8859-1",88"text/plain;charset=UTF-16",89"text/plain;class=java.nio.ByteBuffer;charset=windows-1252",90"text/uri-list;charset=UTF-16",91"text/uri-list;class=java.nio.ByteBuffer;charset=windows-1252",92"text/plain;class=java.io.InputStream;charset=windows-1252",93"text/uri-list;class=java.io.InputStream;charset=windows-1252",94};9596DataFlavor[] flavors = new DataFlavor[mimes.length];97for (int i = 0; i < flavors.length; i++) {98flavors[i] = new DataFlavor(mimes[i]);99}100101testComparator(new DataTransferer.DataFlavorComparator(true), flavors);102testComparator(new DataTransferer.DataFlavorComparator(false), flavors);103104}105106private static void testComparator(Comparator cmp, DataFlavor[] flavs)107throws ClassNotFoundException {108109for (DataFlavor x: flavs) {110for (DataFlavor y: flavs) {111if (Math.signum(cmp.compare(x,y)) != -Math.signum(cmp.compare(y,x))) {112throw new RuntimeException("Antisymmetry violated: " + x + ", " + y);113}114if (cmp.compare(x,y) == 0 && !x.equals(y)) {115throw new RuntimeException("Equals rule violated: " + x + ", " + y);116}117for (DataFlavor z: flavs) {118if (cmp.compare(x,y) == 0) {119if (Math.signum(cmp.compare(x, z)) != Math.signum(cmp.compare(y, z))) {120throw new RuntimeException("Transitivity (1) violated: " + x + ", " + y + ", " + z);121}122} else {123if (Math.signum(cmp.compare(x, y)) == Math.signum(cmp.compare(y, z))) {124if (Math.signum(cmp.compare(x, y)) != Math.signum(cmp.compare(x, z))) {125throw new RuntimeException("Transitivity (2) violated: " + x + ", " + y + ", " + z);126}127}128}129}130}131}132}133}134135136