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 25 /* 26 @test 27 @bug 4718897 28 @summary tests that a Unicode string can be transferred between JVMs. 29 @author das@sparc.spb.su area=datatransfer 30 @library ../../regtesthelpers/process 31 @build ProcessResults ProcessCommunicator 32 @run main UnicodeTransferTest 33 */ 34 35 import java.awt.datatransfer.*; 36 import java.awt.*; 37 import java.text.Normalizer; 38 39 import test.java.awt.regtesthelpers.process.ProcessResults; 40 import test.java.awt.regtesthelpers.process.ProcessCommunicator; 41 42 public class UnicodeTransferTest { 43 private static final Toolkit tk = Toolkit.getDefaultToolkit(); 44 private static final Clipboard clipboard = tk.getSystemClipboard(); 45 private static final Transferable t = new StringSelection(Util.getTestString()); 46 47 public static void main(String[] args) throws Exception { 48 Util.setClipboardContents(clipboard, t, null); 49 ProcessResults result = ProcessCommunicator.executeChildProcess( 50 UnicodeTransferTestChild.class, new String[0]); 51 verifyTestResults(result); 52 } 53 54 private static void verifyTestResults(ProcessResults processResults) { 55 if (processResults.getExitValue() != 0) { 56 processResults.printProcessErrorOutput(System.err); 57 throw new RuntimeException("TEST IS FAILED. See child stderr"); 58 } 59 processResults.verifyStdErr(System.err); 60 processResults.verifyProcessExitValue(System.err); 61 processResults.printProcessStandartOutput(System.out); 62 } 63 64 } 65 66 class Util { 67 private static String testString = null; 68 69 static { 70 StringBuilder buf = new StringBuilder(); 71 for (int i = 1; i < 0x10000; i++) { 72 // Skip surrogates. 73 if (i < 0xD800 || (i > 0xDFFF && i < 0xFFF0)) { 74 buf.append((char) i); 75 } else { 76 buf.append(0x20); 77 } 78 } 79 // On OS X the unicode string is normalized but the clipboard, 80 // so we need to use normalized strings as well to be able to 81 // check the result 82 testString = Normalizer.normalize(buf.toString(), Normalizer.Form.NFC); 83 } 84 85 public static String getTestString() { 86 return testString; 87 } 88 89 public static void setClipboardContents(Clipboard cb, 90 Transferable contents, 91 ClipboardOwner owner) { 92 93 boolean set = false; 94 while (!set) { 95 try { 96 cb.setContents(contents, owner); 97 set = true; 98 } catch (IllegalStateException ise) { 99 try { 100 Thread.sleep(100); 101 } catch (InterruptedException e) { 102 e.printStackTrace(); 103 } 104 } 105 } 106 } 107 108 public static Transferable getClipboardContents(Clipboard cb, 109 Object requestor) { 110 while (true) { 111 try { 112 return cb.getContents(requestor); 113 } catch (IllegalStateException ise) { 114 try { 115 Thread.sleep(100); 116 } catch (InterruptedException e) { 117 e.printStackTrace(); 118 } 119 } 120 } 121 } 122 } 123 124 class UnicodeTransferTestChild { 125 private static final Toolkit tk = Toolkit.getDefaultToolkit(); 126 private static final Clipboard clipboard = tk.getSystemClipboard(); 127 128 public static void main(String[] args) { 129 Transferable t = Util.getClipboardContents(clipboard, null); 130 131 if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) { 132 Object o = null; 133 try { 134 o = t.getTransferData(DataFlavor.stringFlavor); 135 } catch (Exception e) { 136 e.printStackTrace(); 137 } 138 String testStr = Util.getTestString(); 139 140 if (!testStr.equals(o)) { 141 if (o instanceof String) { 142 String s = (String)o; 143 if (s.length() != testStr.length()) { 144 System.err.println("Received length:" + s.length() + 145 " Expected length: " + 146 testStr.length()); 147 } else { 148 for (int i = 0; i < s.length(); i++) { 149 char ch = s.charAt(i); 150 char expected = testStr.charAt(i); 151 if (ch != expected) { 152 System.err.println("i=" + i + 153 " char=" + 154 Integer.toHexString((int)ch) + 155 " expected=" + 156 Integer.toHexString(expected)); 157 } 158 } 159 } 160 } else { 161 System.err.println("Received object:" + o); 162 } 163 throw new RuntimeException("String doesn't match."); 164 } 165 } else { 166 throw new RuntimeException("Clipboard content was not set"); 167 } 168 } 169 }