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 import java.awt.Toolkit;
  24 import java.awt.datatransfer.Clipboard;
  25 import java.awt.datatransfer.DataFlavor;
  26 import java.awt.datatransfer.SystemFlavorMap;
  27 import java.io.IOException;
  28 import java.io.Reader;
  29 import javax.swing.JLabel;
  30 import javax.swing.TransferHandler;
  31 /*
  32  * @test
  33  * @bug 8130329
  34  * @summary  Audit Core Reflection in module java.desktop AWT/Miscellaneous area
  35  *           for places that will require changes to work with modules
  36  * @author Alexander Scherbatiy
  37  */
  38 public class ConstructFlavoredObjectTest {
  39 
  40     private static final String TEST_MIME_TYPE = "text/plain;class="
  41             + MyStringReader.class.getName();
  42 
  43     public static void main(String[] args) throws Exception {
  44 
  45         final DataFlavor dataFlavor = new DataFlavor(TEST_MIME_TYPE);
  46         SystemFlavorMap systemFlavorMap = (SystemFlavorMap) SystemFlavorMap.
  47                 getDefaultFlavorMap();
  48         systemFlavorMap.addUnencodedNativeForFlavor(dataFlavor, "TEXT");
  49         systemFlavorMap.addFlavorForUnencodedNative("TEXT", dataFlavor);
  50 
  51         TransferHandler transferHandler = new TransferHandler("Test Handler");
  52 
  53         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
  54         transferHandler.exportToClipboard(new JLabel("Test"), clipboard,
  55                 TransferHandler.COPY);
  56 
  57         Object clipboardData = clipboard.getData(dataFlavor);
  58 
  59         if (!(clipboardData instanceof MyStringReader)) {
  60             throw new RuntimeException("Wrong clipboard data!");
  61         }
  62     }
  63 
  64     public static class MyStringReader extends Reader {
  65 
  66         public MyStringReader(Reader reader) {
  67         }
  68 
  69         @Override
  70         public int read(char[] cbuf, int off, int len) throws IOException {
  71             throw new UnsupportedOperationException("Not supported yet.");
  72         }
  73 
  74         @Override
  75         public void close() throws IOException {
  76             throw new UnsupportedOperationException("Not supported yet.");
  77         }
  78     }
  79 }