1/*
2 * Copyright (c) 2014, 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 8059739
26   @summary Dragged and Dropped data is corrupted for two data types
27   @author Anton Nashatyrev
28*/
29
30import javax.swing.*;
31import java.awt.datatransfer.Clipboard;
32import java.awt.datatransfer.DataFlavor;
33import java.io.BufferedReader;
34import java.io.InputStream;
35import java.io.InputStreamReader;
36
37public class bug8059739 {
38
39    private static boolean passed = true;
40
41    public static void main(String[] args) throws Exception {
42        SwingUtilities.invokeAndWait(new Runnable() {
43            @Override
44            public void run() {
45                try {
46                    runTest();
47                } catch (Exception e) {
48                    e.printStackTrace();
49                    passed = false;
50                }
51            }
52        });
53
54        if (!passed) {
55            throw new RuntimeException("Test FAILED.");
56        } else {
57            System.out.println("Passed.");
58        }
59    }
60
61    private static void runTest() throws Exception {
62        String testString = "my string";
63        JTextField tf = new JTextField(testString);
64        tf.selectAll();
65        Clipboard clipboard = new Clipboard("clip");
66        tf.getTransferHandler().exportToClipboard(tf, clipboard, TransferHandler.COPY);
67        DataFlavor[] dfs = clipboard.getAvailableDataFlavors();
68        for (DataFlavor df: dfs) {
69            String charset = df.getParameter("charset");
70            if (InputStream.class.isAssignableFrom(df.getRepresentationClass()) &&
71                    charset != null) {
72                BufferedReader br = new BufferedReader(new InputStreamReader(
73                        (InputStream) clipboard.getData(df), charset));
74                String s = br.readLine();
75                System.out.println("Content: '" + s + "'");
76                passed &= s.contains(testString);
77            }
78        }
79    }
80}
81