1/*
2 * Copyright (c) 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  @key headful
27  @bug 8051636
28  @summary DataTransferer optional dependency on RMI
29  @author Semyon Sadetsky
30  @library ../../regtesthelpers/process
31  @build ProcessResults ProcessCommunicator
32  @run main DataFlavorRemoteTest
33*/
34
35import test.java.awt.regtesthelpers.process.ProcessCommunicator;
36import test.java.awt.regtesthelpers.process.ProcessResults;
37
38import java.awt.*;
39import java.awt.datatransfer.Clipboard;
40import java.awt.datatransfer.DataFlavor;
41import java.awt.datatransfer.Transferable;
42import java.awt.datatransfer.UnsupportedFlavorException;
43import java.io.IOException;
44import java.io.Serializable;
45
46interface Hello extends java.rmi.Remote {
47    String sayHello();
48}
49
50public class DataFlavorRemoteTest {
51
52    public static void main(String[] args) throws Exception {
53        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
54        Producer contents = new Producer();
55        clipboard.setContents(contents, null);
56        ProcessResults processResults =
57                ProcessCommunicator
58                        .executeChildProcess(Consumer.class, new String[0]);
59        if (!"Hello".equals(processResults.getStdErr())) {
60            throw new RuntimeException("transfer of remote object failed");
61        }
62        System.out.println("ok");
63    }
64
65    static class Consumer {
66        public static void main(String[] args) throws Exception {
67            Clipboard clipboard =
68                    Toolkit.getDefaultToolkit().getSystemClipboard();
69            DataFlavor dataFlavor = new DataFlavor(DataFlavor.javaRemoteObjectMimeType +
70                    ";class=Hello" );
71            Object data = clipboard.getData(dataFlavor);
72            System.err.print(((Hello) data).sayHello());
73        }
74
75    }
76}
77
78class Producer implements Transferable {
79
80    private final DataFlavor dataFlavor;
81    private final HelloImpl impl;
82
83    private static class HelloImpl implements Hello, Serializable {
84        @Override
85        public String sayHello() {
86            return "Hello";
87        }
88    }
89
90    public Producer() throws Exception {
91        dataFlavor = new DataFlavor(DataFlavor.javaRemoteObjectMimeType +
92                ";class=Hello" );
93        impl = new HelloImpl();
94        System.out.println(impl.hashCode());
95    }
96
97    Hello getImpl() {
98        return impl;
99    }
100
101    @Override
102    public DataFlavor[] getTransferDataFlavors() {
103        return new DataFlavor[]{dataFlavor};
104    }
105
106    @Override
107    public boolean isDataFlavorSupported(DataFlavor flavor) {
108        return flavor.equals(dataFlavor);
109    }
110
111    @Override
112    public Object getTransferData(DataFlavor flavor)
113            throws UnsupportedFlavorException, IOException {
114        return impl;
115    }
116
117}
118