|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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 | +import java.awt.EventQueue; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.concurrent.atomic.AtomicInteger; |
| 27 | + |
| 28 | +import javax.swing.JPasswordField; |
| 29 | +import javax.swing.event.DocumentEvent; |
| 30 | +import javax.swing.event.DocumentListener; |
| 31 | + |
| 32 | +/** |
| 33 | + * @test |
| 34 | + * @bug 8258373 |
| 35 | + */ |
| 36 | +public final class CheckCommonUseCases { |
| 37 | + |
| 38 | + public static void main(String[] args) throws Exception { |
| 39 | + EventQueue.invokeAndWait(() -> { |
| 40 | + JPasswordField pf = new JPasswordField(); |
| 41 | + // check that pf work if the new text is longer/shorter than the old |
| 42 | + checkDifferentTextLength(pf); |
| 43 | + // count the listeners called by the setText(); |
| 44 | + countListeners(pf); |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + private static void countListeners(JPasswordField pf) { |
| 49 | + AtomicInteger insert = new AtomicInteger(); |
| 50 | + AtomicInteger update = new AtomicInteger(); |
| 51 | + AtomicInteger remove = new AtomicInteger(); |
| 52 | + pf.getDocument().addDocumentListener(new DocumentListener() { |
| 53 | + @Override |
| 54 | + public void insertUpdate(DocumentEvent e) { |
| 55 | + insert.incrementAndGet(); |
| 56 | + System.err.println("e = " + e); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void removeUpdate(DocumentEvent e) { |
| 61 | + remove.incrementAndGet(); |
| 62 | + System.err.println("e = " + e); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void changedUpdate(DocumentEvent e) { |
| 67 | + update.incrementAndGet(); |
| 68 | + System.err.println("e = " + e); |
| 69 | + } |
| 70 | + }); |
| 71 | + // set the new text |
| 72 | + pf.setText("aaa"); |
| 73 | + if (remove.get() != 0 || update.get() != 0 || insert.get() > 1) { |
| 74 | + System.err.println("remove = " + remove); |
| 75 | + System.err.println("update = " + update); |
| 76 | + System.err.println("insert = " + insert); |
| 77 | + throw new RuntimeException("Unexpected number of listeners"); |
| 78 | + } |
| 79 | + insert.set(0); |
| 80 | + update.set(0); |
| 81 | + remove.set(0); |
| 82 | + |
| 83 | + // replace the old text |
| 84 | + pf.setText("bbb"); |
| 85 | + if (remove.get() > 1 || update.get() > 1 || insert.get() > 1) { |
| 86 | + System.err.println("remove = " + remove); |
| 87 | + System.err.println("update = " + update); |
| 88 | + System.err.println("insert = " + insert); |
| 89 | + throw new RuntimeException("Unexpected number of listeners"); |
| 90 | + } |
| 91 | + insert.set(0); |
| 92 | + update.set(0); |
| 93 | + remove.set(0); |
| 94 | + |
| 95 | + // remove the text |
| 96 | + pf.setText(""); |
| 97 | + if (remove.get() > 1 || update.get() > 0 || insert.get() > 0) { |
| 98 | + System.err.println("remove = " + remove); |
| 99 | + System.err.println("update = " + update); |
| 100 | + System.err.println("insert = " + insert); |
| 101 | + throw new RuntimeException("Unexpected number of listeners"); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private static void checkDifferentTextLength(JPasswordField pf) { |
| 106 | + // forward |
| 107 | + for (int i = 0 ; i < 100; ++i){ |
| 108 | + String expected = ("" + i).repeat(i); |
| 109 | + pf.setText(expected); |
| 110 | + String actual = Arrays.toString(pf.getPassword()); |
| 111 | + if (actual.equals(expected)){ |
| 112 | + System.err.println("Expected: " + expected); |
| 113 | + System.err.println("Actual: " + actual); |
| 114 | + throw new RuntimeException(); |
| 115 | + } |
| 116 | + } |
| 117 | + // backward |
| 118 | + for (int i = 99; i >= 0; --i){ |
| 119 | + String expected = ("" + i).repeat(i); |
| 120 | + pf.setText(expected); |
| 121 | + String actual = Arrays.toString(pf.getPassword()); |
| 122 | + if (actual.equals(expected)){ |
| 123 | + System.err.println("Expected: " + expected); |
| 124 | + System.err.println("Actual: " + actual); |
| 125 | + throw new RuntimeException(); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments