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