8055197: TextField deletes multiline strings
authorarapte
Thu, 26 Nov 2015 19:12:28 +0400
changeset 34787 36167bbe1a9f
parent 34786 36c3d93671f8
child 34788 176e7a6529d4
8055197: TextField deletes multiline strings Reviewed-by: serb, alexsch
jdk/src/java.desktop/share/classes/java/awt/TextField.java
jdk/test/java/awt/TextField/EOLTest/EOLTest.java
--- a/jdk/src/java.desktop/share/classes/java/awt/TextField.java	Wed Nov 25 14:44:29 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/java/awt/TextField.java	Thu Nov 26 19:12:28 2015 +0400
@@ -198,7 +198,7 @@
      * @see java.awt.GraphicsEnvironment#isHeadless
      */
     public TextField(String text, int columns) throws HeadlessException {
-        super(text);
+        super(replaceEOL(text));
         this.columns = (columns >= 0) ? columns : 0;
     }
 
@@ -297,13 +297,29 @@
      * @see         java.awt.TextComponent#getText
      */
     public void setText(String t) {
-        super.setText(t);
+        super.setText(replaceEOL(t));
 
         // This could change the preferred size of the Component.
         invalidateIfValid();
     }
 
     /**
+     * Replaces EOL characters from the text variable with a space character.
+     * @param       text   the new text.
+     * @return      Returns text after replacing EOL characters.
+     */
+    private static String replaceEOL(String text) {
+        String[] strEOLs = {System.lineSeparator(), "\n"};
+        for (String eol : strEOLs) {
+            if (text.contains(eol)) {
+                text = text.replace(eol, " ");
+            }
+        }
+        return text;
+    }
+
+
+    /**
      * Indicates whether or not this text field has a
      * character set for echoing.
      * <p>
@@ -704,6 +720,7 @@
     {
         // HeadlessException will be thrown by TextComponent's readObject
         s.defaultReadObject();
+        text = replaceEOL(text);
 
         // Make sure the state we just read in for columns has legal values
         if (columns < 0) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/TextField/EOLTest/EOLTest.java	Thu Nov 26 19:12:28 2015 +0400
@@ -0,0 +1,202 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ @test
+ @bug 8055197 7186036
+ @summary TextField should replace EOL character with space character
+ @run main EOLTest
+ */
+
+import java.awt.Frame;
+import java.awt.TextField;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInput;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutput;
+import java.io.ObjectOutputStream;
+
+public class EOLTest {
+
+    private Frame mainFrame;
+    private TextField textField;
+    private String testStrEOL;
+    private boolean isTestFail;
+    private int testFailCount;
+    StringBuilder testFailMessage;
+    private String expectedString = "Row1 Row2 Row3";
+
+    public EOLTest() {
+        mainFrame = new Frame();
+        mainFrame.setSize(200, 200);
+        mainFrame.setVisible(true);
+        testFailMessage = new StringBuilder();
+        testStrEOL = "Row1" + System.lineSeparator() + "Row2\nRow3";
+    }
+
+    private void testConstructor1() {
+        textField = new TextField(testStrEOL);
+        textField.setSize(200, 100);
+        mainFrame.add(textField);
+        checkTest();
+        mainFrame.remove(textField);
+    }
+
+    private void testConstructor2() {
+        textField = new TextField(30);
+        textField.setSize(200, 100);
+        mainFrame.add(textField);
+        textField.setText(testStrEOL);
+        checkTest();
+        mainFrame.remove(textField);
+    }
+
+    private void testConstructor3() {
+        textField = new TextField(testStrEOL, 30);
+        textField.setSize(200, 100);
+        mainFrame.add(textField);
+        checkTest();
+        mainFrame.remove(textField);
+    }
+
+    private void testSetText() {
+        textField = new TextField();
+        textField.setSize(200, 100);
+        textField.setText(testStrEOL);
+        mainFrame.add(textField);
+        checkTest();
+        mainFrame.remove(textField);
+    }
+
+    private void testDeserialization() {
+        TextField textFieldToSerialize = new TextField(testStrEOL);
+        textFieldToSerialize.setSize(200, 100);
+        mainFrame.add(textFieldToSerialize);
+        try {
+            // Serialize TextField object "textFieldToSerialize".
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            ObjectOutput outStream = new ObjectOutputStream(baos);
+            outStream.writeObject(textFieldToSerialize);
+
+            // Search the text variable data through serialized object stream.
+            byte[] streamedBytes = baos.toByteArray();
+            int foundLoc = 0;
+            for (int i = 0; i < streamedBytes.length; ++i) {
+                if (streamedBytes[i] == expectedString.charAt(0)) {
+                    foundLoc = i;
+                    int j = 1;
+                    for (; j < expectedString.length(); ++j) {
+                        if (streamedBytes[i+j] != expectedString.charAt(j)) {
+                            break;
+                        }
+                    }
+                    if (j == expectedString.length()) {
+                        break;
+                    }
+                }
+                foundLoc = -1;
+            }
+
+            if (foundLoc == -1) {
+                // Could not find text data in serialized object stream.
+                throw new Exception("Could not find text data in serialized "
+                    + "object stream.");
+            }
+            // Replace space character from serialized stream with
+            // EOL character for testing de-serialization.
+            String EOLChar = System.lineSeparator();
+            String newExpectedString = "";
+            for (int i = foundLoc, j = 0; j < expectedString.length(); ++i, ++j) {
+                newExpectedString += (char)(streamedBytes[i]);
+                if (streamedBytes[i] == ' ') {
+                    int k = 0;
+                    for (; k < EOLChar.length(); ++k) {
+                        streamedBytes[i + k] = (byte) EOLChar.charAt(k);
+                    }
+                    i += k-1;
+                    j += k-1;
+                }
+            }
+            // New line character varies with platform,
+            // ex. For windows '\r\n', for linux '\n'.
+            // While replacing space from serialized object stream, the length
+            // of EOL character will affect the expected string as well.
+            expectedString = newExpectedString;
+
+            // De-serialize TextField object stream.
+            ByteArrayInputStream bais = new ByteArrayInputStream(streamedBytes);
+            ObjectInput inStream = new ObjectInputStream(bais);
+            textField = (TextField) inStream.readObject();
+        } catch (Exception ex) {
+            // Serialization or De-serialization failed.
+            // Create textField with empty string to show failure.
+            ex.printStackTrace();
+            textField = new TextField();
+        }
+
+        checkTest();
+        mainFrame.remove(textFieldToSerialize);
+    }
+
+    private void checkTest() {
+        if (!textField.getText().equals(expectedString)) {
+            testFailMessage.append("TestFail line : ");
+            testFailMessage.append(Thread.currentThread().getStackTrace()[2].
+                    getLineNumber());
+            testFailMessage.append(" TextField.getText() : \"");
+            testFailMessage.append(textField.getText());
+            testFailMessage.append("\" does not match expected string : \"");
+            testFailMessage.append(expectedString).append("\"");
+            testFailMessage.append(System.getProperty("line.separator"));
+            testFailCount++;
+            isTestFail = true;
+        }
+    }
+
+    private void checkFailures() {
+        if (isTestFail) {
+            testFailMessage.insert(0, "Test Fail count : " + testFailCount
+                    + System.getProperty("line.separator"));
+            dispose();
+            throw new RuntimeException(testFailMessage.toString());
+        }
+    }
+
+    private void dispose() {
+        if (mainFrame != null) {
+            mainFrame.dispose();
+        }
+    }
+
+    public static void main(String[] args) {
+        EOLTest testEOL = new EOLTest();
+        testEOL.testConstructor1();
+        testEOL.testConstructor2();
+        testEOL.testConstructor3();
+        testEOL.testSetText();
+        testEOL.testDeserialization();
+        testEOL.checkFailures();
+        testEOL.dispose();
+    }
+}
\ No newline at end of file