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 8055197 7186036 28 @summary TextField should replace EOL character with space character 29 @run main EOLTest 30 */ 31 32 import java.awt.Frame; 33 import java.awt.TextField; 34 import java.io.ByteArrayInputStream; 35 import java.io.ByteArrayOutputStream; 36 import java.io.ObjectInput; 37 import java.io.ObjectInputStream; 38 import java.io.ObjectOutput; 39 import java.io.ObjectOutputStream; 40 41 public class EOLTest { 42 43 private Frame mainFrame; 44 private TextField textField; 45 private String testStrEOL; 46 private boolean isTestFail; 47 private int testFailCount; 48 StringBuilder testFailMessage; 49 private String expectedString = "Row1 Row2 Row3"; 50 51 public EOLTest() { 52 mainFrame = new Frame(); 53 mainFrame.setSize(200, 200); 54 mainFrame.setVisible(true); 55 testFailMessage = new StringBuilder(); 56 testStrEOL = "Row1" + System.lineSeparator() + "Row2\nRow3"; 57 } 58 59 private void testConstructor1() { 60 textField = new TextField(testStrEOL); 61 textField.setSize(200, 100); 62 mainFrame.add(textField); 63 checkTest(); 64 mainFrame.remove(textField); 65 } 66 67 private void testConstructor2() { 68 textField = new TextField(30); 69 textField.setSize(200, 100); 70 mainFrame.add(textField); 71 textField.setText(testStrEOL); 72 checkTest(); 73 mainFrame.remove(textField); 74 } 75 76 private void testConstructor3() { 77 textField = new TextField(testStrEOL, 30); 78 textField.setSize(200, 100); 79 mainFrame.add(textField); 80 checkTest(); 81 mainFrame.remove(textField); 82 } 83 84 private void testSetText() { 85 textField = new TextField(); 86 textField.setSize(200, 100); 87 textField.setText(testStrEOL); 88 mainFrame.add(textField); 89 checkTest(); 90 mainFrame.remove(textField); 91 } 92 93 private void testDeserialization() { 94 TextField textFieldToSerialize = new TextField(testStrEOL); 95 textFieldToSerialize.setSize(200, 100); 96 mainFrame.add(textFieldToSerialize); 97 try { 98 // Serialize TextField object "textFieldToSerialize". 99 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 100 ObjectOutput outStream = new ObjectOutputStream(baos); 101 outStream.writeObject(textFieldToSerialize); 102 103 // Search the text variable data through serialized object stream. 104 byte[] streamedBytes = baos.toByteArray(); 105 int foundLoc = 0; 106 for (int i = 0; i < streamedBytes.length; ++i) { 107 if (streamedBytes[i] == expectedString.charAt(0)) { 108 foundLoc = i; 109 int j = 1; 110 for (; j < expectedString.length(); ++j) { 111 if (streamedBytes[i+j] != expectedString.charAt(j)) { 112 break; 113 } 114 } 115 if (j == expectedString.length()) { 116 break; 117 } 118 } 119 foundLoc = -1; 120 } 121 122 if (foundLoc == -1) { 123 // Could not find text data in serialized object stream. 124 throw new Exception("Could not find text data in serialized " 125 + "object stream."); 126 } 127 // Replace space character from serialized stream with 128 // EOL character for testing de-serialization. 129 String EOLChar = System.lineSeparator(); 130 String newExpectedString = ""; 131 for (int i = foundLoc, j = 0; j < expectedString.length(); ++i, ++j) { 132 newExpectedString += (char)(streamedBytes[i]); 133 if (streamedBytes[i] == ' ') { 134 int k = 0; 135 for (; k < EOLChar.length(); ++k) { 136 streamedBytes[i + k] = (byte) EOLChar.charAt(k); 137 } 138 i += k-1; 139 j += k-1; 140 } 141 } 142 // New line character varies with platform, 143 // ex. For windows '\r\n', for linux '\n'. 144 // While replacing space from serialized object stream, the length 145 // of EOL character will affect the expected string as well. 146 expectedString = newExpectedString; 147 148 // De-serialize TextField object stream. 149 ByteArrayInputStream bais = new ByteArrayInputStream(streamedBytes); 150 ObjectInput inStream = new ObjectInputStream(bais); 151 textField = (TextField) inStream.readObject(); 152 } catch (Exception ex) { 153 // Serialization or De-serialization failed. 154 // Create textField with empty string to show failure. 155 ex.printStackTrace(); 156 textField = new TextField(); 157 } 158 159 checkTest(); 160 mainFrame.remove(textFieldToSerialize); 161 } 162 163 private void checkTest() { 164 if (!textField.getText().equals(expectedString)) { 165 testFailMessage.append("TestFail line : "); 166 testFailMessage.append(Thread.currentThread().getStackTrace()[2]. 167 getLineNumber()); 168 testFailMessage.append(" TextField.getText() : \""); 169 testFailMessage.append(textField.getText()); 170 testFailMessage.append("\" does not match expected string : \""); 171 testFailMessage.append(expectedString).append("\""); 172 testFailMessage.append(System.getProperty("line.separator")); 173 testFailCount++; 174 isTestFail = true; 175 } 176 } 177 178 private void checkFailures() { 179 if (isTestFail) { 180 testFailMessage.insert(0, "Test Fail count : " + testFailCount 181 + System.getProperty("line.separator")); 182 dispose(); 183 throw new RuntimeException(testFailMessage.toString()); 184 } 185 } 186 187 private void dispose() { 188 if (mainFrame != null) { 189 mainFrame.dispose(); 190 } 191 } 192 193 public static void main(String[] args) { 194 EOLTest testEOL = new EOLTest(); 195 testEOL.testConstructor1(); 196 testEOL.testConstructor2(); 197 testEOL.testConstructor3(); 198 testEOL.testSetText(); 199 testEOL.testDeserialization(); 200 testEOL.checkFailures(); 201 testEOL.dispose(); 202 } 203 }