|
| 1 | +/* |
| 2 | + * Copyright (c) 2002, 2025, 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 4638351 |
| 27 | + * @summary tests that HTML transfer doesn't cause console output |
| 28 | + * @key headful |
| 29 | + * @library /test/lib |
| 30 | + * @run main HTMLTransferConsoleOutputTest |
| 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.Transferable; |
| 38 | +import java.awt.datatransfer.UnsupportedFlavorException; |
| 39 | +import java.io.ByteArrayOutputStream; |
| 40 | +import java.io.IOException; |
| 41 | +import java.io.OutputStream; |
| 42 | +import java.io.PrintStream; |
| 43 | +import java.io.UnsupportedEncodingException; |
| 44 | +import java.util.concurrent.TimeUnit; |
| 45 | +import java.util.concurrent.TimeoutException; |
| 46 | + |
| 47 | +import jdk.test.lib.process.OutputAnalyzer; |
| 48 | +import jdk.test.lib.process.ProcessTools; |
| 49 | + |
| 50 | +public class HTMLTransferConsoleOutputTest implements ClipboardOwner { |
| 51 | + static final Clipboard clipboard = |
| 52 | + Toolkit.getDefaultToolkit().getSystemClipboard(); |
| 53 | + static final DataFlavor dataFlavor = |
| 54 | + new DataFlavor("text/html; class=java.lang.String", null); |
| 55 | + static final String magic = "TESTMAGICSTRING"; |
| 56 | + static final Transferable transferable = new Transferable() { |
| 57 | + final DataFlavor[] flavors = new DataFlavor[]{dataFlavor}; |
| 58 | + final String data = "<html><body>" + magic + "</html></body>"; |
| 59 | + |
| 60 | + public DataFlavor[] getTransferDataFlavors() { |
| 61 | + return flavors; |
| 62 | + } |
| 63 | + |
| 64 | + public boolean isDataFlavorSupported(DataFlavor df) { |
| 65 | + return dataFlavor.equals(df); |
| 66 | + } |
| 67 | + |
| 68 | + public Object getTransferData(DataFlavor df) |
| 69 | + throws UnsupportedFlavorException { |
| 70 | + if (!isDataFlavorSupported(df)) { |
| 71 | + throw new UnsupportedFlavorException(df); |
| 72 | + } |
| 73 | + return data; |
| 74 | + } |
| 75 | + }; |
| 76 | + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 77 | + public static final int CLIPBOARD_DELAY = 1000; |
| 78 | + |
| 79 | + public static void main(String[] args) throws Exception { |
| 80 | + if (args.length == 0) { |
| 81 | + HTMLTransferConsoleOutputTest htmlTransferConsoleOutputTest = new HTMLTransferConsoleOutputTest(); |
| 82 | + htmlTransferConsoleOutputTest.initialize(); |
| 83 | + return; |
| 84 | + } |
| 85 | + final ClipboardOwner clipboardOwner = new ClipboardOwner() { |
| 86 | + public void lostOwnership(Clipboard clip, |
| 87 | + Transferable contents) { |
| 88 | + System.exit(0); |
| 89 | + } |
| 90 | + }; |
| 91 | + clipboard.setContents(transferable, clipboardOwner); |
| 92 | + final Object o = new Object(); |
| 93 | + synchronized (o) { |
| 94 | + try { |
| 95 | + o.wait(); |
| 96 | + } catch (InterruptedException ie) { |
| 97 | + ie.printStackTrace(); |
| 98 | + } |
| 99 | + } |
| 100 | + System.out.println("Test Pass!"); |
| 101 | + } |
| 102 | + |
| 103 | + public void initialize() throws Exception { |
| 104 | + clipboard.setContents(transferable, this); |
| 105 | + ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder( |
| 106 | + HTMLTransferConsoleOutputTest.class.getName(), |
| 107 | + "child" |
| 108 | + ); |
| 109 | + |
| 110 | + Process process = ProcessTools.startProcess("Child", pb); |
| 111 | + OutputAnalyzer outputAnalyzer = new OutputAnalyzer(process); |
| 112 | + |
| 113 | + if (!process.waitFor(15, TimeUnit.SECONDS)) { |
| 114 | + process.destroyForcibly(); |
| 115 | + throw new TimeoutException("Timed out waiting for Child"); |
| 116 | + } |
| 117 | + |
| 118 | + byte[] bytes = baos.toByteArray(); |
| 119 | + String string = null; |
| 120 | + try { |
| 121 | + string = new String(bytes, "ASCII"); |
| 122 | + } catch (UnsupportedEncodingException uee) { |
| 123 | + uee.printStackTrace(); |
| 124 | + } |
| 125 | + if (string.lastIndexOf(magic) != -1) { |
| 126 | + throw new RuntimeException("Test failed. Output contains:" + |
| 127 | + string); |
| 128 | + } |
| 129 | + |
| 130 | + outputAnalyzer.shouldHaveExitValue(0); |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + static class ForkOutputStream extends OutputStream { |
| 135 | + final OutputStream outputStream1; |
| 136 | + final OutputStream outputStream2; |
| 137 | + |
| 138 | + public ForkOutputStream(OutputStream os1, OutputStream os2) { |
| 139 | + outputStream1 = os1; |
| 140 | + outputStream2 = os2; |
| 141 | + } |
| 142 | + |
| 143 | + public void write(int b) throws IOException { |
| 144 | + outputStream1.write(b); |
| 145 | + outputStream2.write(b); |
| 146 | + } |
| 147 | + |
| 148 | + public void flush() throws IOException { |
| 149 | + outputStream1.flush(); |
| 150 | + outputStream2.flush(); |
| 151 | + } |
| 152 | + |
| 153 | + public void close() throws IOException { |
| 154 | + outputStream1.close(); |
| 155 | + outputStream2.close(); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + public void lostOwnership(Clipboard clip, Transferable contents) { |
| 160 | + final Runnable r = () -> { |
| 161 | + try { |
| 162 | + Thread.sleep(CLIPBOARD_DELAY); |
| 163 | + } catch (InterruptedException e) { |
| 164 | + e.printStackTrace(); |
| 165 | + } |
| 166 | + final PrintStream oldOut = System.out; |
| 167 | + final PrintStream newOut = |
| 168 | + new PrintStream(new ForkOutputStream(oldOut, baos)); |
| 169 | + Transferable t = clipboard.getContents(null); |
| 170 | + try { |
| 171 | + System.setOut(newOut); |
| 172 | + t.getTransferData(dataFlavor); |
| 173 | + System.setOut(oldOut); |
| 174 | + } catch (IOException | UnsupportedFlavorException ioe) { |
| 175 | + ioe.printStackTrace(); |
| 176 | + } |
| 177 | + clipboard.setContents(transferable, null); |
| 178 | + }; |
| 179 | + final Thread t = new Thread(r); |
| 180 | + t.start(); |
| 181 | + } |
| 182 | +} |
0 commit comments