1 /* 2 * Copyright (c) 2015, 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 8040322 27 @summary Test TextArea APIs replaceRange, insert, append & setText 28 @run main TextAreaEditing 29 */ 30 31 import java.awt.Frame; 32 import java.awt.TextArea; 33 34 public class TextAreaEditing { 35 36 private int testFailCount; 37 private boolean isTestFail; 38 private StringBuilder testFailMessage; 39 40 private Frame mainFrame; 41 private TextArea textArea; 42 43 private TextAreaEditing() { 44 testFailMessage = new StringBuilder(); 45 mainFrame = new Frame(); 46 mainFrame.setSize(200, 200); 47 48 textArea = new TextArea(); 49 mainFrame.add(textArea); 50 mainFrame.setVisible(true); 51 } 52 53 private void dispose() { 54 if (mainFrame != null) { 55 mainFrame.dispose(); 56 } 57 } 58 59 public static void main(String[] s) { 60 TextAreaEditing textArea = new TextAreaEditing(); 61 textArea.testReplaceRange(); 62 textArea.testInsert(); 63 textArea.testAppend(); 64 textArea.checkFailures(); 65 textArea.dispose(); 66 } 67 68 private void testReplaceRange() { 69 textArea.setText(null); 70 textArea.replaceRange("Replace", 0, 0); 71 textArea.setText(null); 72 checkTest(""); 73 74 textArea.setText("SetText"); 75 textArea.replaceRange("Replace", 0, 3); 76 checkTest("ReplaceText"); 77 78 textArea.replaceRange("String", textArea.getText().length(), 79 textArea.getText().length()); 80 checkTest("ReplaceTextString"); 81 82 textArea.replaceRange("String", 0, 0); 83 checkTest("StringReplaceTextString"); 84 85 textArea.replaceRange("replaceRange", 0, textArea.getText().length()); 86 checkTest("replaceRange"); 87 } 88 89 private void testInsert() { 90 textArea.setText(null); 91 textArea.insert("Insert", 0); 92 textArea.setText(""); 93 checkTest(""); 94 95 textArea.setText("SetText"); 96 textArea.insert("Insert", 3); 97 checkTest("SetInsertText"); 98 99 textArea.insert("Insert", 0); 100 checkTest("InsertSetInsertText"); 101 102 textArea.insert("Insert", textArea.getText().length()); 103 checkTest("InsertSetInsertTextInsert"); 104 } 105 106 private void testAppend() { 107 textArea.setText(null); 108 textArea.append("Append"); 109 textArea.setText(null); 110 checkTest(""); 111 112 textArea.setText("SetText"); 113 textArea.append("Append"); 114 checkTest("SetTextAppend"); 115 116 textArea.append(""); 117 checkTest("SetTextAppend"); 118 textArea.setText(""); 119 checkTest(""); 120 } 121 122 private void checkTest(String str) { 123 if (str != null && !str.equals(textArea.getText())) { 124 testFailMessage.append("TestFail line : "); 125 testFailMessage.append(Thread.currentThread().getStackTrace()[2]. 126 getLineNumber()); 127 testFailMessage.append(" TextArea string : \""); 128 testFailMessage.append(textArea.getText()); 129 testFailMessage.append("\" does not match expected string : \""); 130 testFailMessage.append(str).append("\""); 131 testFailMessage.append(System.getProperty("line.separator")); 132 testFailCount++; 133 isTestFail = true; 134 } 135 } 136 137 private void checkFailures() { 138 if (isTestFail) { 139 testFailMessage.insert(0, "Test Fail count : " + testFailCount 140 + System.getProperty("line.separator")); 141 dispose(); 142 throw new RuntimeException(testFailMessage.toString()); 143 } 144 } 145 }