Path: blob/master/test/jdk/javax/swing/JFormattedTextField/Test6462562.java
41149 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 646256225@summary Tests text input into JFormattedTextField26with an InternationalFormatter27@author Peter Zhelezniakov28@run main Test646256229*/3031import java.awt.event.ActionEvent;32import java.text.DateFormat;33import java.text.NumberFormat;34import java.text.ParseException;35import java.text.SimpleDateFormat;36import java.util.Date;37import java.util.Locale;38import javax.swing.Action;39import javax.swing.JFormattedTextField;40import javax.swing.SwingUtilities;41import javax.swing.text.Caret;42import javax.swing.text.DateFormatter;43import javax.swing.text.DefaultEditorKit;44import javax.swing.text.InternationalFormatter;45import javax.swing.text.NumberFormatter;464748public class Test646256249{50static final String BACKSPACE = new String("backspace");51static final String DELETE = new String("delete");5253boolean failed = false;5455void test() {56testPercentFormat();57testCurrencyFormat();58testIntegerFormat();59testDateFormat();6061if (failed) {62throw new RuntimeException("Some testcases failed, see output above");63}64System.err.println("(-; All testcases passed ;-)");65}6667TestFormattedTextField create(NumberFormat format) {68format.setMaximumFractionDigits(0);69NumberFormatter fmt = new NumberFormatter(format);70return new TestFormattedTextField(fmt);71}7273TestFormattedTextField create(DateFormat format) {74DateFormatter fmt = new DateFormatter(format);75return new TestFormattedTextField(fmt);76}7778public static void main(String[] args) throws Exception {79SwingUtilities.invokeAndWait(new Runnable() {80public void run() {81new Test6462562().test();82}83});84}8586class TestFormattedTextField extends JFormattedTextField87{88final Action backspace;89final Action delete;90final Action insert;9192final ActionEvent dummyEvent;9394public TestFormattedTextField(InternationalFormatter fmt) {95super(fmt);96fmt.setAllowsInvalid(false);97fmt.setOverwriteMode(true);9899backspace = getActionMap().get(DefaultEditorKit.deletePrevCharAction);100delete = getActionMap().get(DefaultEditorKit.deleteNextCharAction);101insert = getActionMap().get(DefaultEditorKit.insertContentAction);102dummyEvent = new ActionEvent(this, 0, null);103}104105public boolean test(int pos, int selectionLength, String todo, Object expectedResult) {106Object v0 = getValue();107108Caret caret = getCaret();109caret.setDot(pos);110if (selectionLength > 0) {111caret.moveDot(pos + selectionLength);112}113114String desc = todo;115if (todo == BACKSPACE) {116backspace.actionPerformed(dummyEvent);117} else if (todo == DELETE) {118delete.actionPerformed(dummyEvent);119} else {120desc = "insert('" + todo + "')";121insert.actionPerformed(new ActionEvent(this, 0, todo));122}123124try {125commitEdit();126} catch (ParseException e) {127e.printStackTrace();128failed = true;129return false;130}131132Object v1 = getValue();133if (! v1.equals(expectedResult)) {134System.err.printf("Failure: value='%s', mark=%d, dot=%d, action=%s\n",135v0, pos, pos + selectionLength, desc);136System.err.printf(" Result: '%s', expected: '%s'\n", v1, expectedResult);137failed = true;138return false;139}140return true;141}142}143144void testPercentFormat() {145NumberFormat format = NumberFormat.getPercentInstance(Locale.US);146TestFormattedTextField ftf = create(format);147ftf.setValue(.34);148149System.err.println("Testing NumberFormat.getPercentInstance(Locale.US)");150151// test inserting individual characters152ftf.test(0, 0, "1", .14);153ftf.test(2, 0, "2", 1.42);154ftf.test(1, 0, "0", 1.02);155156// test inserting several characters at once - e.g. from clipboard157ftf.test(0, 0, "1024", 10.24);158ftf.test(3, 0, "333", 103.33);159ftf.test(6, 0, "77", 10333.77);160ftf.test(4, 0, "99", 10399.77);161ftf.test(6, 0, "00", 10390.07);162163// test inserting strings that contain some formatting164ftf.test(0, 0, "2,2", 2290.07);165ftf.test(2, 0, "2,2", 222.27);166ftf.test(4, 0, "2,2", 222.22);167ftf.test(6, 0, "33,33", 2222233.33);168169// test delete170ftf.test(0, 0, DELETE, 222233.33);171ftf.test(10, 0, DELETE, 222233.33);172ftf.test(5, 0, DELETE, 22223.33);173ftf.test(6, 0, DELETE, 2222.33);174175// test backspace176ftf.test(0, 0, BACKSPACE, 2222.33);177ftf.test(7, 0, BACKSPACE, 222.23);178ftf.test(4, 0, BACKSPACE, 22.23);179ftf.test(2, 0, BACKSPACE, 2.23);180181// test replacing selection182ftf.test(0, 1, "555", 555.23);183ftf.test(4, 2, "555", 5555.55);184ftf.test(2, 3, "1", 551.55);185ftf.test(3, 2, "6", 55.65);186ftf.test(4, 2, "12", 556.12);187ftf.test(3, 4, "0", 5.5);188ftf.test(0, 3, "111222333444555", 1112223334445.55);189190// test deleting selection191ftf.test(0, 2, DELETE, 12223334445.55);192ftf.test(0, 3, BACKSPACE, 223334445.55);193ftf.test(12, 2, DELETE, 2233344.45);194ftf.test(9, 2, BACKSPACE, 22333.44);195ftf.test(4, 3, DELETE, 223.44);196ftf.test(1, 2, BACKSPACE, 23.44);197ftf.test(3, 3, DELETE, .23);198ftf.test(1, 2, BACKSPACE, .02);199}200201void testCurrencyFormat() {202NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);203TestFormattedTextField ftf = create(format);204ftf.setValue(56L);205206System.err.println("Testing NumberFormat.getCurrencyInstance(Locale.US)");207208// test inserting individual characters209ftf.test(1, 0, "1", 16L);210ftf.test(3, 0, "2", 162L);211ftf.test(2, 0, "0", 102L);212213// test inserting several characters at once - e.g. from clipboard214ftf.test(1, 0, "1024", 1024L);215ftf.test(4, 0, "333", 10333L);216ftf.test(7, 0, "77", 1033377L);217ftf.test(5, 0, "99", 1039977L);218ftf.test(7, 0, "00", 1039007L);219220// test inserting strings that contain some formatting221ftf.test(1, 0, "2,2", 229007L);222ftf.test(3, 0, "2,2", 22227L);223ftf.test(4, 0, "2,2", 2222L);224ftf.test(6, 0, "33,33", 22223333L);225226// test delete227ftf.test(1, 0, DELETE, 2223333L);228ftf.test(10, 0, DELETE, 2223333L);229ftf.test(5, 0, DELETE, 222333L);230ftf.test(5, 0, DELETE, 22233L);231232// test backspace233ftf.test(1, 0, BACKSPACE, 22233L);234ftf.test(7, 0, BACKSPACE, 2223L);235ftf.test(4, 0, BACKSPACE, 223L);236ftf.test(2, 0, BACKSPACE, 23L);237238// test replacing selection239ftf.test(1, 1, "555", 5553L);240ftf.test(4, 2, "555", 55555L);241ftf.test(2, 3, "1", 5155L);242ftf.test(3, 2, "6", 565L);243ftf.test(1, 3, "111222333444555", 111222333444555L);244245// test deleting selection246ftf.test(1, 2, DELETE, 1222333444555L);247ftf.test(1, 3, BACKSPACE, 22333444555L);248ftf.test(13, 2, DELETE, 223334445L);249ftf.test(10, 2, BACKSPACE, 2233344L);250ftf.test(4, 4, DELETE, 2244L);251ftf.test(1, 4, BACKSPACE, 4L);252}253254void testIntegerFormat() {255NumberFormat format = NumberFormat.getIntegerInstance(Locale.US);256TestFormattedTextField ftf = create(format);257ftf.setValue(56L);258259System.err.println("Testing NumberFormat.getIntegerInstance(Locale.US)");260261// test inserting individual characters262ftf.test(0, 0, "1", 16L);263ftf.test(2, 0, "2", 162L);264ftf.test(1, 0, "0", 102L);265266// test inserting several characters at once - e.g. from clipboard267ftf.test(0, 0, "1024", 1024L);268ftf.test(3, 0, "333", 10333L);269ftf.test(6, 0, "77", 1033377L);270ftf.test(4, 0, "99", 1039977L);271ftf.test(6, 0, "00", 1039007L);272273// test inserting strings that contain some formatting274ftf.test(0, 0, "2,2", 229007L);275ftf.test(2, 0, "2,2", 22227L);276ftf.test(3, 0, "2,2", 2222L);277ftf.test(5, 0, "33,33", 22223333L);278279// test delete280ftf.test(0, 0, DELETE, 2223333L);281ftf.test(9, 0, DELETE, 2223333L);282ftf.test(4, 0, DELETE, 222333L);283ftf.test(4, 0, DELETE, 22233L);284285// test backspace286ftf.test(0, 0, BACKSPACE, 22233L);287ftf.test(6, 0, BACKSPACE, 2223L);288ftf.test(2, 0, BACKSPACE, 223L);289ftf.test(2, 0, BACKSPACE, 23L);290291// test replacing selection292ftf.test(0, 1, "555", 5553L);293ftf.test(3, 2, "555", 55555L);294ftf.test(1, 3, "1", 5155L);295ftf.test(2, 2, "6", 565L);296ftf.test(0, 3, "111222333444555", 111222333444555L);297298// test deleting selection299ftf.test(0, 2, DELETE, 1222333444555L);300ftf.test(0, 3, BACKSPACE, 22333444555L);301ftf.test(12, 2, DELETE, 223334445L);302ftf.test(9, 2, BACKSPACE, 2233344L);303ftf.test(3, 4, DELETE, 2244L);304ftf.test(0, 4, BACKSPACE, 4L);305}306307Date date(DateFormat format, String spec) {308try {309return format.parse(spec);310} catch (ParseException e) {311throw new Error("Error in test");312}313}314315void testDateFormat() {316DateFormat format = new SimpleDateFormat("MM/dd/yyyy", Locale.US);317TestFormattedTextField ftf = create(format);318ftf.setValue(date(format, "12/05/2005"));319320System.err.println("Testing SimpleDateFormat(\"MM/dd/yyyy\", Locale.US)");321322// test inserting individual characters323ftf.test(0, 0, "0", date(format, "02/05/2005"));324ftf.test(4, 0, "4", date(format, "02/04/2005"));325ftf.test(6, 0, "1", date(format, "02/04/1005"));326ftf.test(9, 0, "9", date(format, "02/04/1009"));327328// test inserting several characters at once - e.g. from clipboard329ftf.test(0, 0, "11", date(format, "11/04/1009"));330ftf.test(3, 0, "23", date(format, "11/23/1009"));331ftf.test(6, 0, "191", date(format, "11/23/1919"));332333// test delete334ftf.test(0, 0, DELETE, date(format, "01/23/1919"));335ftf.test(3, 0, DELETE, date(format, "01/03/1919"));336ftf.test(10, 0, DELETE, date(format, "01/03/1919"));337ftf.test(1, 0, DELETE, date(format, "12/03/1918"));338ftf.test(4, 0, DELETE, date(format, "11/30/1918"));339340// test backspace341ftf.test(0, 0, BACKSPACE, date(format, "11/30/1918"));342ftf.test(1, 0, BACKSPACE, date(format, "01/30/1918"));343ftf.test(4, 0, BACKSPACE, date(format, "12/31/1917"));344ftf.test(10, 0, BACKSPACE, date(format, "12/31/0191"));345ftf.test(3, 0, BACKSPACE, date(format, "01/31/0191"));346ftf.test(5, 0, BACKSPACE, date(format, "01/03/0191"));347348// test replacing selection349ftf.test(0, 1, "1", date(format, "11/03/0191"));350ftf.test(3, 1, "2", date(format, "11/23/0191"));351ftf.test(6, 2, "20", date(format, "11/23/2091"));352353// test deleting selection354ftf.test(0, 1, BACKSPACE, date(format, "01/23/2091"));355ftf.test(3, 1, DELETE, date(format, "01/03/2091"));356ftf.test(6, 2, BACKSPACE, date(format, "01/03/0091"));357ftf.test(8, 1, DELETE, date(format, "01/03/0001"));358}359}360361362