< prev index next >

test/java/awt/datatransfer/ConstructFlavoredObjectTest/ConstructFlavoredObjectTest.java

Print this page


   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  * @key headful
  34  * @bug 8130329
  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  */
  39 public class ConstructFlavoredObjectTest {
  40 
  41     private static final String TEST_MIME_TYPE = "text/plain;class="
  42             + MyStringReader.class.getName();
  43 
  44     public static void main(String[] args) throws Exception {
  45 











  46         final DataFlavor dataFlavor = new DataFlavor(TEST_MIME_TYPE);
  47         SystemFlavorMap systemFlavorMap = (SystemFlavorMap) SystemFlavorMap.
  48                 getDefaultFlavorMap();
  49         systemFlavorMap.addUnencodedNativeForFlavor(dataFlavor, "TEXT");
  50         systemFlavorMap.addFlavorForUnencodedNative("TEXT", dataFlavor);
  51 
  52         TransferHandler transferHandler = new TransferHandler("Test Handler");
  53 

  54         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
  55         transferHandler.exportToClipboard(new JLabel("Test"), clipboard,
  56                 TransferHandler.COPY);
  57 
  58         Object clipboardData = clipboard.getData(dataFlavor);
  59 
  60         if (!(clipboardData instanceof MyStringReader)) {
  61             throw new RuntimeException("Wrong clipboard data!");


  62         }
  63     }
  64 
  65     public static class MyStringReader extends Reader {


  66 
  67         public MyStringReader(Reader reader) {
  68         }



  69 
  70         @Override
  71         public int read(char[] cbuf, int off, int len) throws IOException {
  72             throw new UnsupportedOperationException("Not supported yet.");
  73         }



  74 
  75         @Override
  76         public void close() throws IOException {
  77             throw new UnsupportedOperationException("Not supported yet.");
  78         }
  79     }
  80 }

   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 javax.swing.JColorChooser;

  28 import javax.swing.JLabel;
  29 import javax.swing.TransferHandler;
  30 import java.awt.Color;
  31 
  32 /*
  33  * @test
  34  * @key headful
  35  * @bug 8130329 8134612
  36  * @summary  Audit Core Reflection in module java.desktop AWT/Miscellaneous area
  37  *           for places that will require changes to work with modules
  38  * @author Alexander Scherbatiy
  39  * @run main ConstructFlavoredObjectTest
  40  */
  41 public class ConstructFlavoredObjectTest {
  42 



  43     public static void main(String[] args) throws Exception {
  44 
  45         ConstructFlavoredObjectTest testObject = new ConstructFlavoredObjectTest();
  46 
  47         testObject.testTextFlavor();
  48 
  49         testObject.testColorFlavor();
  50     }
  51 
  52     public void testTextFlavor() throws Exception {
  53         final String TEST_MIME_TYPE = DataFlavor.javaJVMLocalObjectMimeType +
  54                 ";class=" + String.class.getName();
  55 
  56         final DataFlavor dataFlavor = new DataFlavor(TEST_MIME_TYPE);
  57         SystemFlavorMap systemFlavorMap =
  58                 (SystemFlavorMap) SystemFlavorMap.getDefaultFlavorMap();
  59         systemFlavorMap.addUnencodedNativeForFlavor(dataFlavor, "TEXT");
  60         systemFlavorMap.addFlavorForUnencodedNative("TEXT", dataFlavor);
  61 
  62         TransferHandler transferHandler = new TransferHandler("text");
  63 
  64         String text = "This is sample export text";
  65         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
  66         transferHandler.exportToClipboard(new JLabel(text), clipboard,
  67                 TransferHandler.COPY);
  68 
  69         Object clipboardData = clipboard.getData(dataFlavor);
  70 
  71         String textFromClipboard = (String) clipboardData;
  72 
  73         if (!textFromClipboard.equals(text)) {
  74             throw new RuntimeException("Wrong clipboard data : Text");
  75         }
  76     }
  77 
  78     public void testColorFlavor() throws Exception {
  79         final String TEST_MIME_TYPE = DataFlavor.javaJVMLocalObjectMimeType +
  80                 ";class=" + Color.class.getName();
  81 
  82         final DataFlavor dataFlavor = new DataFlavor(TEST_MIME_TYPE);
  83         SystemFlavorMap systemFlavorMap =
  84                 (SystemFlavorMap) SystemFlavorMap.getDefaultFlavorMap();
  85         systemFlavorMap.addUnencodedNativeForFlavor(dataFlavor, "COLOR");
  86         systemFlavorMap.addFlavorForUnencodedNative("COLOR", dataFlavor);
  87 
  88         TransferHandler transferHandler = new TransferHandler("color");
  89 
  90         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
  91         transferHandler.exportToClipboard(new JColorChooser(Color.BLUE),
  92                 clipboard, TransferHandler.COPY);
  93 
  94         Object clipboardData = clipboard.getData(dataFlavor);
  95 
  96         Color colorFromClipboard = (Color) clipboardData;
  97         if (!colorFromClipboard.equals(Color.BLUE)) {
  98             throw new RuntimeException("Wrong clipboard data : Color");
  99         }
 100     }
 101 }
 102 
< prev index next >