8040322: TextArea.replaceRange() and insert() are broken with setText(null)
authoralexsch
Wed, 30 Sep 2015 17:46:11 +0400
changeset 33245 cba1681fd040
parent 33244 fe43269f8575
child 33246 d54eb15e19eb
8040322: TextArea.replaceRange() and insert() are broken with setText(null) Reviewed-by: serb, azvegint Contributed-by: Ambarish Rapte <ambarish.rapte@oracle.com>
jdk/src/java.desktop/share/classes/java/awt/TextArea.java
jdk/test/java/awt/TextArea/TextAreaEditing/TextAreaEditing.java
--- a/jdk/src/java.desktop/share/classes/java/awt/TextArea.java	Wed Sep 30 13:31:01 2015 +0300
+++ b/jdk/src/java.desktop/share/classes/java/awt/TextArea.java	Wed Sep 30 17:46:11 2015 +0400
@@ -327,9 +327,8 @@
         TextAreaPeer peer = (TextAreaPeer)this.peer;
         if (peer != null) {
             peer.insert(str, pos);
-        } else {
-            text = text.substring(0, pos) + str + text.substring(pos);
         }
+        text = text.substring(0, pos) + str + text.substring(pos);
     }
 
     /**
@@ -355,11 +354,7 @@
      */
     @Deprecated
     public synchronized void appendText(String str) {
-        if (peer != null) {
             insertText(str, getText().length());
-        } else {
-            text = text + str;
-        }
     }
 
     /**
@@ -403,9 +398,8 @@
         TextAreaPeer peer = (TextAreaPeer)this.peer;
         if (peer != null) {
             peer.replaceRange(str, start, end);
-        } else {
-            text = text.substring(0, start) + str + text.substring(end);
         }
+        text = text.substring(0, start) + str + text.substring(end);
     }
 
     /**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/TextArea/TextAreaEditing/TextAreaEditing.java	Wed Sep 30 17:46:11 2015 +0400
@@ -0,0 +1,145 @@
+/*
+ * 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 8040322
+ @summary Test TextArea APIs replaceRange, insert, append & setText
+ @run main TextAreaEditing
+ */
+
+import java.awt.Frame;
+import java.awt.TextArea;
+
+public class TextAreaEditing {
+
+    private int testFailCount;
+    private boolean isTestFail;
+    private StringBuilder testFailMessage;
+
+    private Frame mainFrame;
+    private TextArea textArea;
+
+    private TextAreaEditing() {
+        testFailMessage = new StringBuilder();
+        mainFrame = new Frame();
+        mainFrame.setSize(200, 200);
+
+        textArea = new TextArea();
+        mainFrame.add(textArea);
+        mainFrame.setVisible(true);
+    }
+
+    private void dispose() {
+        if (mainFrame != null) {
+            mainFrame.dispose();
+        }
+    }
+
+    public static void main(String[] s) {
+        TextAreaEditing textArea = new TextAreaEditing();
+        textArea.testReplaceRange();
+        textArea.testInsert();
+        textArea.testAppend();
+        textArea.checkFailures();
+        textArea.dispose();
+    }
+
+    private void testReplaceRange() {
+        textArea.setText(null);
+        textArea.replaceRange("Replace", 0, 0);
+        textArea.setText(null);
+        checkTest("");
+
+        textArea.setText("SetText");
+        textArea.replaceRange("Replace", 0, 3);
+        checkTest("ReplaceText");
+
+        textArea.replaceRange("String", textArea.getText().length(),
+                textArea.getText().length());
+        checkTest("ReplaceTextString");
+
+        textArea.replaceRange("String", 0, 0);
+        checkTest("StringReplaceTextString");
+
+        textArea.replaceRange("replaceRange", 0, textArea.getText().length());
+        checkTest("replaceRange");
+    }
+
+    private void testInsert() {
+        textArea.setText(null);
+        textArea.insert("Insert", 0);
+        textArea.setText("");
+        checkTest("");
+
+        textArea.setText("SetText");
+        textArea.insert("Insert", 3);
+        checkTest("SetInsertText");
+
+        textArea.insert("Insert", 0);
+        checkTest("InsertSetInsertText");
+
+        textArea.insert("Insert", textArea.getText().length());
+        checkTest("InsertSetInsertTextInsert");
+    }
+
+    private void testAppend() {
+        textArea.setText(null);
+        textArea.append("Append");
+        textArea.setText(null);
+        checkTest("");
+
+        textArea.setText("SetText");
+        textArea.append("Append");
+        checkTest("SetTextAppend");
+
+        textArea.append("");
+        checkTest("SetTextAppend");
+        textArea.setText("");
+        checkTest("");
+    }
+
+    private void checkTest(String str) {
+        if (str != null && !str.equals(textArea.getText())) {
+            testFailMessage.append("TestFail line : ");
+            testFailMessage.append(Thread.currentThread().getStackTrace()[2].
+                    getLineNumber());
+            testFailMessage.append(" TextArea string : \"");
+            testFailMessage.append(textArea.getText());
+            testFailMessage.append("\" does not match expected string : \"");
+            testFailMessage.append(str).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());
+        }
+    }
+}