|
| 1 | +/* |
| 2 | + * Copyright (c) 2003, 2023, 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 | + @bug 4859006 |
| 27 | + @summary tests that MIME formats are mapped to flavors properly on X11 |
| 28 | + @requires (os.family == "linux") |
| 29 | + @key headful |
| 30 | + @run main MimeFormatsTest |
| 31 | +*/ |
| 32 | + |
| 33 | +import java.awt.Toolkit; |
| 34 | +import java.awt.datatransfer.Clipboard; |
| 35 | +import java.awt.datatransfer.ClipboardOwner; |
| 36 | +import java.awt.datatransfer.DataFlavor; |
| 37 | +import java.awt.datatransfer.FlavorMap; |
| 38 | +import java.awt.datatransfer.StringSelection; |
| 39 | +import java.awt.datatransfer.SystemFlavorMap; |
| 40 | +import java.awt.datatransfer.Transferable; |
| 41 | +import java.awt.datatransfer.UnsupportedFlavorException; |
| 42 | +import java.io.ByteArrayInputStream; |
| 43 | +import java.io.File; |
| 44 | +import java.io.IOException; |
| 45 | +import java.io.InputStream; |
| 46 | +import java.io.InputStreamReader; |
| 47 | +import java.io.Reader; |
| 48 | +import java.nio.charset.StandardCharsets; |
| 49 | + |
| 50 | + |
| 51 | +public class MimeFormatsTest implements ClipboardOwner { |
| 52 | + public static final DataFlavor TEST_FLAVOR = |
| 53 | + new DataFlavor( |
| 54 | + "text/test;charset=UTF-8;class=java.io.InputStream", |
| 55 | + null); |
| 56 | + |
| 57 | + public static class TextTransferable implements Transferable { |
| 58 | + private final String text; |
| 59 | + |
| 60 | + public TextTransferable(String text) { |
| 61 | + this.text = text; |
| 62 | + } |
| 63 | + |
| 64 | + public Object getTransferData(DataFlavor flavor) |
| 65 | + throws UnsupportedFlavorException, IOException { |
| 66 | + if (!isDataFlavorSupported(TEST_FLAVOR)) { |
| 67 | + throw new UnsupportedFlavorException(flavor); |
| 68 | + } |
| 69 | + |
| 70 | + return new ByteArrayInputStream( |
| 71 | + text.getBytes(StandardCharsets.UTF_8)); |
| 72 | + } |
| 73 | + |
| 74 | + public DataFlavor[] getTransferDataFlavors(){ |
| 75 | + return new DataFlavor[] { TEST_FLAVOR }; |
| 76 | + } |
| 77 | + |
| 78 | + public boolean isDataFlavorSupported(DataFlavor flavor) { |
| 79 | + return TEST_FLAVOR.equals(flavor); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public static final String DATA = |
| 84 | + "\u0440\u0443\u0441\u0441\u043a\u0438\u0439"; |
| 85 | + |
| 86 | + private String testData = null; |
| 87 | + |
| 88 | + private static final Clipboard clipboard = |
| 89 | + Toolkit.getDefaultToolkit().getSystemClipboard(); |
| 90 | + |
| 91 | + public void childRun() { |
| 92 | + Transferable t = clipboard.getContents(null); |
| 93 | + String data = ""; |
| 94 | + try { |
| 95 | + data = (String)t.getTransferData(DataFlavor.stringFlavor); |
| 96 | + } catch (Exception e) { |
| 97 | + e.printStackTrace(); |
| 98 | + } |
| 99 | + System.err.println("contents size=" + data.length()); |
| 100 | + for (int i = 0; i < data.length(); i++) { |
| 101 | + System.err.println(" char[" + i + "]=" + (int) data.charAt(i)); |
| 102 | + } |
| 103 | + ClipboardOwner owner = new ClipboardOwner() { |
| 104 | + public void lostOwnership(Clipboard clipboard, |
| 105 | + Transferable contents) { |
| 106 | + System.err.println("%d exit".formatted( |
| 107 | + System.currentTimeMillis())); |
| 108 | + System.err.println("Exiting"); |
| 109 | + System.exit(0); |
| 110 | + } |
| 111 | + }; |
| 112 | + clipboard.setContents(new StringSelection(data + data), owner); |
| 113 | + |
| 114 | + Object lock = new Object(); |
| 115 | + synchronized (lock) { |
| 116 | + // Wait to let the parent retrieve the contents. |
| 117 | + try { |
| 118 | + System.err.println("%d wait".formatted( |
| 119 | + System.currentTimeMillis())); |
| 120 | + lock.wait(); |
| 121 | + } catch (InterruptedException e) { |
| 122 | + e.printStackTrace(); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public void start() { |
| 128 | + FlavorMap fm = SystemFlavorMap.getDefaultFlavorMap(); |
| 129 | + if (fm instanceof SystemFlavorMap) { |
| 130 | + SystemFlavorMap sfm = (SystemFlavorMap)fm; |
| 131 | + String mimeNative = "text/plain;charset=UTF-8"; |
| 132 | + sfm.setNativesForFlavor(TEST_FLAVOR, |
| 133 | + new String[] { mimeNative }); |
| 134 | + sfm.setFlavorsForNative(mimeNative, |
| 135 | + new DataFlavor[] { TEST_FLAVOR }); |
| 136 | + } else { |
| 137 | + System.err.println("WARNING: system flavor map: " + fm); |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + clipboard.setContents(new TextTransferable(DATA), this); |
| 142 | + |
| 143 | + try { |
| 144 | + String javaPath = System.getProperty("java.home", ""); |
| 145 | + String[] command = { |
| 146 | + javaPath + File.separator + "bin" + File.separator + "java", |
| 147 | + "-cp", |
| 148 | + System.getProperty("test.classes", "."), |
| 149 | + "Child" |
| 150 | + }; |
| 151 | + |
| 152 | + Process process = Runtime.getRuntime().exec(command); |
| 153 | + ProcessResults pres = ProcessResults.doWaitFor(process); |
| 154 | + |
| 155 | + int returnCode = pres.exitValue; |
| 156 | + |
| 157 | + if (pres.stderr != null && pres.stderr.length() > 0) { |
| 158 | + System.err.println("========= Child VM System.err ========"); |
| 159 | + System.err.print(pres.stderr); |
| 160 | + System.err.println("======================================"); |
| 161 | + } |
| 162 | + |
| 163 | + if (pres.stdout != null && pres.stdout.length() > 0) { |
| 164 | + System.err.println("========= Child VM System.out ========"); |
| 165 | + System.err.print(pres.stdout); |
| 166 | + System.err.println("======================================"); |
| 167 | + } |
| 168 | + |
| 169 | + System.err.println("Child return code=" + returnCode); |
| 170 | + } catch (Throwable e) { |
| 171 | + e.printStackTrace(); |
| 172 | + } |
| 173 | + |
| 174 | + System.err.println("Received data size=" + testData.length()); |
| 175 | + for (int i = 0; i < testData.length(); i++) { |
| 176 | + System.err.println(" char[" + i + "]=" + (int)testData.charAt(i)); |
| 177 | + } |
| 178 | + |
| 179 | + if (!testData.equals(DATA + DATA)) { |
| 180 | + throw new RuntimeException(); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + public void lostOwnership(Clipboard clip, Transferable contents) { |
| 185 | + Runnable r = new Runnable() { |
| 186 | + public void run() { |
| 187 | + try { |
| 188 | + Thread.sleep(100); |
| 189 | + } catch (InterruptedException e) { |
| 190 | + e.printStackTrace(); |
| 191 | + } |
| 192 | + Transferable t = clipboard.getContents(null); |
| 193 | + try { |
| 194 | + InputStream is = |
| 195 | + (InputStream)t.getTransferData(TEST_FLAVOR); |
| 196 | + Reader r = new InputStreamReader(is, |
| 197 | + StandardCharsets.UTF_8); |
| 198 | + StringBuffer sb = new StringBuffer(); |
| 199 | + int ch = 0; |
| 200 | + while ((ch = r.read()) != -1) { |
| 201 | + System.err.println("ch=" + ch); |
| 202 | + sb.append((char)ch); |
| 203 | + } |
| 204 | + testData = sb.toString(); |
| 205 | + } catch (Exception e) { |
| 206 | + e.printStackTrace(); |
| 207 | + } |
| 208 | + clipboard.setContents(new TextTransferable(""), null); |
| 209 | + } |
| 210 | + }; |
| 211 | + new Thread(r).start(); |
| 212 | + } |
| 213 | + |
| 214 | + public static void main(String[] args) { |
| 215 | + if (!System.getProperty("os.name").startsWith("Linux")) { |
| 216 | + return; |
| 217 | + } |
| 218 | + |
| 219 | + MimeFormatsTest mimeFormatsTest = new MimeFormatsTest(); |
| 220 | + mimeFormatsTest.start(); |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +class Child { |
| 225 | + public static void main(String[] args) { |
| 226 | + MimeFormatsTest test = new MimeFormatsTest(); |
| 227 | + test.childRun(); |
| 228 | + } |
| 229 | +} |
| 230 | + |
| 231 | +class ProcessResults { |
| 232 | + public int exitValue; |
| 233 | + public String stdout; |
| 234 | + public String stderr; |
| 235 | + |
| 236 | + public ProcessResults() { |
| 237 | + exitValue = -1; |
| 238 | + stdout = ""; |
| 239 | + stderr = ""; |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * Method to perform a "wait" for a process and return its exit value. |
| 244 | + * This is a workaround for <code>Process.waitFor()</code> never returning. |
| 245 | + */ |
| 246 | + public static ProcessResults doWaitFor(Process p) { |
| 247 | + ProcessResults pres = new ProcessResults(); |
| 248 | + |
| 249 | + InputStream in = null; |
| 250 | + InputStream err = null; |
| 251 | + |
| 252 | + try { |
| 253 | + in = p.getInputStream(); |
| 254 | + err = p.getErrorStream(); |
| 255 | + |
| 256 | + boolean finished = false; |
| 257 | + |
| 258 | + while (!finished) { |
| 259 | + try { |
| 260 | + while (in.available() > 0) { |
| 261 | + pres.stdout += (char)in.read(); |
| 262 | + } |
| 263 | + while (err.available() > 0) { |
| 264 | + pres.stderr += (char)err.read(); |
| 265 | + } |
| 266 | + // Ask the process for its exitValue. If the process |
| 267 | + // is not finished, an IllegalThreadStateException |
| 268 | + // is thrown. If it is finished, we fall through and |
| 269 | + // the variable finished is set to true. |
| 270 | + pres.exitValue = p.exitValue(); |
| 271 | + finished = true; |
| 272 | + } |
| 273 | + catch (IllegalThreadStateException e) { |
| 274 | + // Process is not finished yet; |
| 275 | + // Sleep a little to save on CPU cycles |
| 276 | + Thread.currentThread().sleep(500); |
| 277 | + } |
| 278 | + } |
| 279 | + if (in != null) in.close(); |
| 280 | + if (err != null) err.close(); |
| 281 | + } |
| 282 | + catch (Throwable e) { |
| 283 | + System.err.println("doWaitFor(): unexpected exception"); |
| 284 | + e.printStackTrace(); |
| 285 | + } |
| 286 | + return pres; |
| 287 | + } |
| 288 | +} |
0 commit comments