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/*
25  @test
26  @bug 4493178
27  @summary tests that getNativesForFlavor() synthesizes an encoded String native
28           only if there are no mappings for the DataFlavor and the mappings
29           were not explicitly removed
30  @author das@sparc.spb.su area=datatransfer
31  @modules java.datatransfer
32  @run main GetNativesForFlavorTest
33*/
34
35import java.awt.datatransfer.DataFlavor;
36import java.awt.datatransfer.SystemFlavorMap;
37import java.util.Iterator;
38
39public class GetNativesForFlavorTest {
40
41    final static SystemFlavorMap fm =
42            (SystemFlavorMap) SystemFlavorMap.getDefaultFlavorMap();
43
44    public static void main(String[] args) throws Exception {
45        // 1.Check that the encoded native is not added if there are other
46        // natives for this DataFlavor.
47        test1();
48
49        // 2.Check that the encoded native is not added if all mappings were
50        // explicitly removed for this DataFlavor.
51        test2();
52
53        // 3.Check that only the encoded native is added for text DataFlavors
54        // that has no mappings and that DataFlavor is properly encoded.
55        test3();
56
57        // 4.Verifies that the encoded native is added only for DataFlavors
58        // that has no mappings and that DataFlavor is properly encoded.
59        test4();
60    }
61
62    /**
63     * Verifies that the encoded native is not added if there are other
64     * natives mapped to this DataFlavor.
65     */
66    public static void test1() throws ClassNotFoundException {
67        final DataFlavor flavor =
68                new DataFlavor("text/plain-TEST; charset=Unicode");
69
70        final java.util.List natives = fm.getNativesForFlavor(flavor);
71
72        if (natives.size() > 1) {
73            for (final Iterator i = natives.iterator(); i.hasNext(); ) {
74                String element = (String) i.next();
75                if (SystemFlavorMap.isJavaMIMEType(element)) {
76                    throw new RuntimeException("getFlavorsForNative() returns: "
77                            + natives);
78                }
79            }
80        }
81    }
82
83    /**
84     * Verifies that the encoded native is not added if all mappings were
85     * explicitly removed for this DataFlavor.
86     */
87    public static void test2() throws ClassNotFoundException {
88        final DataFlavor flavor =
89                new DataFlavor("text/plain-TEST; charset=Unicode");
90
91        fm.setNativesForFlavor(flavor, new String[0]);
92
93        final java.util.List natives = fm.getNativesForFlavor(flavor);
94
95        if (!natives.isEmpty()) {
96            throw new RuntimeException("getFlavorsForNative() returns:" +
97                    natives);
98        }
99    }
100
101    /**
102     * Verifies that only the encoded native is added for text DataFlavors
103     * that has no mappings and that DataFlavor is properly encoded.
104     */
105    public static void test3() throws ClassNotFoundException {
106        //
107        final DataFlavor flavor =
108                new DataFlavor("text/plain-TEST-nocharset; class=java.nio.ByteBuffer");
109
110        final java.util.List natives = fm.getNativesForFlavor(flavor);
111        boolean encodedNativeFound = false;
112
113        if (natives.size() == 0) {
114            throw new RuntimeException("getFlavorsForNative() returns:" +
115                    natives);
116        }
117
118        if (natives.size() == 1) {
119            String element = (String) natives.get(0);
120            if (SystemFlavorMap.isJavaMIMEType(element)) {
121                final DataFlavor decodedFlavor =
122                        SystemFlavorMap.decodeDataFlavor(element);
123                if (!flavor.equals(decodedFlavor)) {
124                    System.err.println("DataFlavor is not properly incoded:");
125                    System.err.println("    encoded flavor: " + flavor);
126                    System.err.println("    decoded flavor: " + decodedFlavor);
127                    throw new RuntimeException("getFlavorsForNative() returns:"
128                            + natives);
129                }
130            }
131        } else {
132            for (final Iterator i = natives.iterator(); i.hasNext(); ) {
133                String element = (String) i.next();
134                if (SystemFlavorMap.isJavaMIMEType(element)) {
135                    throw new RuntimeException("getFlavorsForNative() returns:"
136                            + natives);
137                }
138            }
139        }
140    }
141
142    /**
143     * Verifies that the encoded native is added only for DataFlavors
144     * that has no mappings and that DataFlavor is properly encoded.
145     */
146    public static void test4() throws ClassNotFoundException {
147        final DataFlavor flavor =
148                new DataFlavor("unknown/unknown");
149
150        final java.util.List natives = fm.getNativesForFlavor(flavor);
151
152        if (natives.size() == 1) {
153            String element = (String) natives.get(0);
154            if (SystemFlavorMap.isJavaMIMEType(element)) {
155                final DataFlavor decodedFlavor =
156                        SystemFlavorMap.decodeDataFlavor(element);
157                if (!flavor.equals(decodedFlavor)) {
158                    System.err.println("DataFlavor is not properly incoded:");
159                    System.err.println("    encoded flavor: " + flavor);
160                    System.err.println("    decoded flavor: " + decodedFlavor);
161                    throw new RuntimeException("getFlavorsForNative() returns:"
162                            + natives);
163                }
164            }
165        } else {
166            throw new RuntimeException("getFlavorsForNative() returns:"
167                    + natives);
168        }
169    }
170}
171