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