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