|
| 1 | +/* |
| 2 | + * Copyright (c) 2002, 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 4701398 4652358 4659958 4697796 4666876 |
| 27 | + @summary REGRESSION: TextArea.append does not work consistently with \r. |
| 28 | + @key headful |
| 29 | +*/ |
| 30 | + |
| 31 | +import java.awt.BorderLayout; |
| 32 | +import java.awt.Color; |
| 33 | +import java.awt.Dialog; |
| 34 | +import java.awt.EventQueue; |
| 35 | +import java.awt.Font; |
| 36 | +import java.awt.Frame; |
| 37 | +import java.awt.Panel; |
| 38 | +import java.awt.TextArea; |
| 39 | + |
| 40 | +public class TextAreaCRLFTest { |
| 41 | + private static final char[] DIGITS = { |
| 42 | + '0', '1', '2', '3', '4', '5', '6', '7', |
| 43 | + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' |
| 44 | + }; |
| 45 | + |
| 46 | + public static Dialog aDialog; |
| 47 | + public static TextArea area; |
| 48 | + public static boolean passed = true; |
| 49 | + public static boolean res; |
| 50 | + public static String atext = ""; |
| 51 | + |
| 52 | + public static void main(String[] args) throws Exception { |
| 53 | + String atextCRLF = "row1\r\nrow2\r\nrow3"; |
| 54 | + |
| 55 | + try { |
| 56 | + EventQueue.invokeAndWait(() -> { |
| 57 | + aDialog = new Dialog(new Frame()); |
| 58 | + aDialog.setTitle("ADialog"); |
| 59 | + aDialog.setBackground(Color.lightGray); |
| 60 | + aDialog.setLayout(new BorderLayout()); |
| 61 | + Panel mainPanel = new Panel(); |
| 62 | + mainPanel.setLayout(new BorderLayout(6, 6)); |
| 63 | + area = new TextArea(atextCRLF, 25, 68, |
| 64 | + TextArea.SCROLLBARS_VERTICAL_ONLY); |
| 65 | + area.setFont(new Font("Monospaced", Font.PLAIN, 11)); |
| 66 | + mainPanel.add(area, "Center"); |
| 67 | + aDialog.add(mainPanel, "Center"); |
| 68 | + aDialog.pack(); |
| 69 | + System.out.println("before: "+hexEncode(atextCRLF)); |
| 70 | + System.out.println(" after: "+hexEncode(area.getText())); |
| 71 | + res = area.getText().equals(atextCRLF); |
| 72 | + System.out.println("01: " + res + "\n"); |
| 73 | + passed = passed && res; |
| 74 | + area.setText(atextCRLF); |
| 75 | + System.out.println("before: "+hexEncode(atextCRLF)); |
| 76 | + System.out.println(" after: "+hexEncode(area.getText())); |
| 77 | + res = area.getText().equals(atextCRLF); |
| 78 | + System.out.println("02: " + res + "\n"); |
| 79 | + passed = passed && res; |
| 80 | + |
| 81 | + area.setText(""); |
| 82 | + atext = "row1"; |
| 83 | + area.append(atext+"\r"); |
| 84 | + area.append(atext+"\r"); |
| 85 | + System.out.println("before: " |
| 86 | + +hexEncode(atext+"\r" + atext+"\r")); |
| 87 | + System.out.println(" after: "+hexEncode(area.getText())); |
| 88 | + res = area.getText().equals(atext + atext); |
| 89 | + System.out.println("03: " + res + "\n"); |
| 90 | + passed = passed && res; |
| 91 | + |
| 92 | + area.setText(""); |
| 93 | + String atext1 = "fine."; |
| 94 | + String atext2 = "messed up."; |
| 95 | + atext = atext1 +"\r\n"+ atext2; |
| 96 | + for (int i = 0; i < atext.length(); i++) { |
| 97 | + area.append(atext.substring(i, i+1)); |
| 98 | + } |
| 99 | + System.out.println("before: " |
| 100 | + +hexEncode(atext1 +"\r\n"+ atext2)); |
| 101 | + System.out.println(" after: "+hexEncode(area.getText())); |
| 102 | + String s = area.getText(); |
| 103 | + String t = s.substring(s.length()-atext2.length()); |
| 104 | + res = t.equals(atext2); |
| 105 | + System.out.println("04: " + res); |
| 106 | + passed = passed && res; |
| 107 | + |
| 108 | + area.setText(""); |
| 109 | + atext = "\r"; |
| 110 | + area.append(atext); |
| 111 | + System.out.println("before: "+hexEncode(atext)); |
| 112 | + System.out.println(" after: "+hexEncode(area.getText())); |
| 113 | + res = area.getText().equals(""); |
| 114 | + System.out.println("05: " + res + "\n"); |
| 115 | + passed = passed && res; |
| 116 | + |
| 117 | + if (System.getProperty("os.name").toUpperCase(). |
| 118 | + startsWith("WIN")) { |
| 119 | + if (!passed) { |
| 120 | + throw new RuntimeException("TextAreaCRLFTest FAILED."); |
| 121 | + } else { |
| 122 | + System.out.println("TextAreaCRLFTest PASSED"); |
| 123 | + } |
| 124 | + } else { |
| 125 | + System.out.println("This is a Windows oriented testcase."); |
| 126 | + } |
| 127 | + }); |
| 128 | + } finally { |
| 129 | + EventQueue.invokeAndWait(() -> { |
| 130 | + if (aDialog != null) { |
| 131 | + aDialog.dispose(); |
| 132 | + } |
| 133 | + }); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private static String hexEncode(String str) { |
| 138 | + return hexEncode(str.getBytes()); |
| 139 | + } |
| 140 | + |
| 141 | + private static String hexEncode(byte[] bytes) { |
| 142 | + StringBuffer buffer = new StringBuffer(bytes.length * 2); |
| 143 | + for (int i = 0; i < bytes.length; i++) { |
| 144 | + byte b = bytes[i]; |
| 145 | + buffer.append(DIGITS[(b & 0xF0) >> 4]); |
| 146 | + buffer.append(DIGITS[b & 0x0F]); |
| 147 | + } |
| 148 | + return buffer.toString(); |
| 149 | + } |
| 150 | +} |
0 commit comments