1 /*
   2  * Copyright (c) 2016, 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  * @key headful
  34  * @bug 8130329 8134612
  35  * @summary  Audit Core Reflection in module java.desktop AWT/Miscellaneous area
  36  *           for places that will require changes to work with modules
  37  * @author Alexander Scherbatiy
  38  * @run main/othervm ConstructFlavoredObjectTest COPY
  39  * @run main/othervm ConstructFlavoredObjectTest PASTE
  40  */
  41 public class ConstructFlavoredObjectTest {
  42 
  43     public static void main(String[] args) throws Exception {
  44 
  45         if (args[0].equals("COPY")) {
  46 
  47             // Copy a simple text string on to the System clipboard
  48 
  49             final String TEXT_MIME_TYPE = DataFlavor.javaJVMLocalObjectMimeType +
  50                     ";class=" + String.class.getName();
  51    
  52             final DataFlavor dataFlavor = new DataFlavor(TEXT_MIME_TYPE);
  53             SystemFlavorMap systemFlavorMap =
  54                    (SystemFlavorMap) SystemFlavorMap.getDefaultFlavorMap();
  55             systemFlavorMap.addUnencodedNativeForFlavor(dataFlavor, "TEXT");
  56             systemFlavorMap.addFlavorForUnencodedNative("TEXT", dataFlavor);
  57    
  58             TransferHandler transferHandler = new TransferHandler("text");
  59    
  60             String text = "This is sample export text";
  61             Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
  62             transferHandler.exportToClipboard(new JLabel(text), clipboard,
  63                     TransferHandler.COPY);
  64         }
  65         else if (args[0].equals("PASTE")) {
  66 
  67             // Try to read text data from the System clipboard
  68 
  69             final String TEST_MIME_TYPE = "text/plain;class=" +
  70                     MyStringReader.class.getName();
  71             
  72             final DataFlavor dataFlavor = new DataFlavor(TEST_MIME_TYPE);
  73             SystemFlavorMap systemFlavorMap = (SystemFlavorMap) SystemFlavorMap.
  74                     getDefaultFlavorMap();
  75             systemFlavorMap.addUnencodedNativeForFlavor(dataFlavor, "TEXT");
  76             systemFlavorMap.addFlavorForUnencodedNative("TEXT", dataFlavor);
  77 
  78             Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
  79 
  80             Object clipboardData = clipboard.getData(dataFlavor);
  81            
  82             if (!(clipboardData instanceof MyStringReader)) {
  83                 throw new RuntimeException("Wrong clipboard data!");
  84             }
  85         }
  86     }
  87 
  88     public static class MyStringReader extends Reader {
  89 
  90         public MyStringReader(Reader reader) {
  91         }
  92 
  93         @Override
  94         public int read(char[] cbuf, int off, int len) throws IOException {
  95             throw new UnsupportedOperationException("Not supported yet.");
  96         }
  97 
  98         @Override
  99         public void close() throws IOException {
 100             throw new UnsupportedOperationException("Not supported yet.");
 101         }
 102     }
 103 }
 104