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