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 4493189
27  @summary tests that addUnencodedNativeForFlavor()/addFlavorForUnencodedNative()
28           do not allow to duplicate mappings
29  @author das@sparc.spb.su area=datatransfer
30  @modules java.datatransfer
31  @run main DuplicateMappingTest
32*/
33
34import java.awt.*;
35import java.awt.datatransfer.DataFlavor;
36import java.awt.datatransfer.SystemFlavorMap;
37import java.util.Iterator;
38
39public class DuplicateMappingTest {
40
41    public static void main(String[] args) throws Exception {
42
43        final String nativeString = "NATIVE";
44        final DataFlavor dataFlavor = new DataFlavor();
45
46        final SystemFlavorMap fm =
47                (SystemFlavorMap) SystemFlavorMap.getDefaultFlavorMap();
48
49        fm.addUnencodedNativeForFlavor(dataFlavor, nativeString);
50        fm.addUnencodedNativeForFlavor(dataFlavor, nativeString);
51
52        final java.util.List natives =
53                fm.getNativesForFlavor(dataFlavor);
54        boolean found = false;
55
56        for (final Iterator i = natives.iterator(); i.hasNext(); ) {
57            if (nativeString.equals(i.next())) {
58                if (found) {
59                    throw new RuntimeException("getNativesForFlavor() returns:" +
60                            natives);
61                } else {
62                    found = true;
63                }
64            }
65        }
66
67        if (!found) {
68            throw new RuntimeException("getNativesForFlavor() returns:" +
69                    natives);
70        }
71
72        fm.addFlavorForUnencodedNative(nativeString, dataFlavor);
73        fm.addFlavorForUnencodedNative(nativeString, dataFlavor);
74
75        final java.util.List flavors =
76                fm.getFlavorsForNative(nativeString);
77        found = false;
78
79        for (final Iterator i = flavors.iterator(); i.hasNext(); ) {
80            if (dataFlavor.equals(i.next())) {
81                if (found) {
82                    throw new RuntimeException("getFlavorsForNative() returns:" +
83                            flavors);
84                } else {
85                    found = true;
86                }
87            }
88        }
89
90        if (!found) {
91            throw new RuntimeException("getFlavorsForNative() returns:" +
92                    natives);
93        }
94    }
95}
96