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 8055197 7186036 27 @summary TextField should replace EOL character with space character 28 @run main EOLTest 29 */ 30 31 import java.awt.Frame; 32 import java.awt.TextField; 33 34 public class EOLTest { 35 36 private Frame mainFrame; 37 private TextField textField; 38 private String testStrEOL; 39 private boolean isTestFail; 40 private int testFailCount; 41 StringBuilder testFailMessage; 42 private String expectedString = "Row1 Row2 Row3"; 43 44 public EOLTest() { 45 mainFrame = new Frame(); 46 mainFrame.setSize(200, 200); 47 mainFrame.setVisible(true); 48 testFailMessage = new StringBuilder(); 49 testStrEOL = "Row1" + System.lineSeparator() + "Row2\nRow3"; 50 } 51 52 private void testConstructor1() { 53 textField = new TextField(testStrEOL); 54 textField.setSize(200, 100); 55 mainFrame.add(textField); 56 checkTest(); 57 mainFrame.remove(textField); 58 textField = null; 59 } 60 61 private void testConstructor2() { 62 textField = new TextField(30); 63 textField.setSize(200, 100); 64 mainFrame.add(textField); 65 textField.setText(testStrEOL); 66 checkTest(); 67 mainFrame.remove(textField); 68 textField = null; 69 } 70 71 private void testConstructor3() { 72 textField = new TextField(testStrEOL, 30); 73 textField.setSize(200, 100); 74 mainFrame.add(textField); 75 checkTest(); 76 mainFrame.remove(textField); 77 textField = null; 78 } 79 80 private void testSetText() { 81 textField = new TextField(); 82 textField.setSize(200, 100); 83 textField.setText(testStrEOL); 84 mainFrame.add(textField); 85 checkTest(); 86 mainFrame.remove(textField); 87 textField = null; 88 } 89 90 private void checkTest() { 91 if (!textField.getText().equals(expectedString)) { 92 testFailMessage.append("TestFail line : "); 93 testFailMessage.append(Thread.currentThread().getStackTrace()[2]. 94 getLineNumber()); 95 testFailMessage.append(" TextField.getText() : \""); 96 testFailMessage.append(textField.getText()); 97 testFailMessage.append("\" does not match expected string : \""); 98 testFailMessage.append(expectedString).append("\""); 99 testFailMessage.append(System.getProperty("line.separator")); 100 testFailCount++; 101 isTestFail = true; 102 } 103 } 104 105 private void checkFailures() { 106 if (isTestFail) { 107 testFailMessage.insert(0, "Test Fail count : " + testFailCount 108 + System.getProperty("line.separator")); 109 dispose(); 110 throw new RuntimeException(testFailMessage.toString()); 111 } 112 } 113 114 private void dispose() { 115 if (mainFrame != null) { 116 mainFrame.dispose(); 117 } 118 } 119 120 public static void main(String[] args) { 121 EOLTest testEOL = new EOLTest(); 122 testEOL.testConstructor1(); 123 testEOL.testConstructor2(); 124 testEOL.testConstructor3(); 125 testEOL.testSetText(); 126 testEOL.checkFailures(); 127 testEOL.dispose(); 128 } 129 }