Path: blob/master/test/jdk/javax/swing/JPasswordField/CheckCommonUseCases.java
41152 views
/*1* Copyright (c) 2020, 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*/2223import java.awt.EventQueue;24import java.util.Arrays;25import java.util.concurrent.atomic.AtomicInteger;2627import javax.swing.JPasswordField;28import javax.swing.event.DocumentEvent;29import javax.swing.event.DocumentListener;3031/**32* @test33* @bug 825837334*/35public final class CheckCommonUseCases {3637public static void main(String[] args) throws Exception {38EventQueue.invokeAndWait(() -> {39JPasswordField pf = new JPasswordField();40// check that pf work if the new text is longer/shorter than the old41checkDifferentTextLength(pf);42// count the listeners called by the setText();43countListeners(pf);44});45}4647private static void countListeners(JPasswordField pf) {48AtomicInteger insert = new AtomicInteger();49AtomicInteger update = new AtomicInteger();50AtomicInteger remove = new AtomicInteger();51pf.getDocument().addDocumentListener(new DocumentListener() {52@Override53public void insertUpdate(DocumentEvent e) {54insert.incrementAndGet();55System.err.println("e = " + e);56}5758@Override59public void removeUpdate(DocumentEvent e) {60remove.incrementAndGet();61System.err.println("e = " + e);62}6364@Override65public void changedUpdate(DocumentEvent e) {66update.incrementAndGet();67System.err.println("e = " + e);68}69});70// set the new text71pf.setText("aaa");72if (remove.get() != 0 || update.get() != 0 || insert.get() > 1) {73System.err.println("remove = " + remove);74System.err.println("update = " + update);75System.err.println("insert = " + insert);76throw new RuntimeException("Unexpected number of listeners");77}78insert.set(0);79update.set(0);80remove.set(0);8182// replace the old text83pf.setText("bbb");84if (remove.get() > 1 || update.get() > 1 || insert.get() > 1) {85System.err.println("remove = " + remove);86System.err.println("update = " + update);87System.err.println("insert = " + insert);88throw new RuntimeException("Unexpected number of listeners");89}90insert.set(0);91update.set(0);92remove.set(0);9394// remove the text95pf.setText("");96if (remove.get() > 1 || update.get() > 0 || insert.get() > 0) {97System.err.println("remove = " + remove);98System.err.println("update = " + update);99System.err.println("insert = " + insert);100throw new RuntimeException("Unexpected number of listeners");101}102}103104private static void checkDifferentTextLength(JPasswordField pf) {105// forward106for (int i = 0 ; i < 100; ++i){107String expected = ("" + i).repeat(i);108pf.setText(expected);109String actual = Arrays.toString(pf.getPassword());110if (actual.equals(expected)){111System.err.println("Expected: " + expected);112System.err.println("Actual: " + actual);113throw new RuntimeException();114}115}116// backward117for (int i = 99; i >= 0; --i){118String expected = ("" + i).repeat(i);119pf.setText(expected);120String actual = Arrays.toString(pf.getPassword());121if (actual.equals(expected)){122System.err.println("Expected: " + expected);123System.err.println("Actual: " + actual);124throw new RuntimeException();125}126}127}128}129130131