Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JPasswordField/CleanInternalStorageOnSetText.java
41152 views
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.ArrayList;
26
import java.util.Arrays;
27
28
import javax.swing.JPasswordField;
29
import javax.swing.text.BadLocationException;
30
import javax.swing.text.Document;
31
import javax.swing.text.GapContent;
32
import javax.swing.text.PlainDocument;
33
import javax.swing.text.Segment;
34
import javax.swing.text.StringContent;
35
import javax.swing.text.html.HTMLDocument;
36
import javax.swing.text.html.StyleSheet;
37
38
/**
39
* @test
40
* @bug 8258373
41
* @summary The JPasswordField#setText() should reset the old internal storage
42
*/
43
public final class CleanInternalStorageOnSetText {
44
45
public static void main(String[] args) throws Exception {
46
EventQueue.invokeAndWait(() -> {
47
// default case
48
testStorage(false, new JPasswordField());
49
testStorage(true, new JPasswordField());
50
51
// custom Plain String document
52
Document document = new PlainDocument(new StringContent());
53
testStorage(false, new JPasswordField(document, "", 10));
54
document = new PlainDocument(new StringContent());
55
testStorage(true, new JPasswordField(document, "", 10));
56
57
// custom Plain GAP document
58
document = new PlainDocument(new GapContent());
59
testStorage(false, new JPasswordField(document, "", 10));
60
document = new PlainDocument(new GapContent());
61
testStorage(true, new JPasswordField(document, "", 10));
62
63
// custom HTMLDocument String document
64
document = new HTMLDocument(new StringContent(), new StyleSheet());
65
testStorage(false, new JPasswordField(document, "", 10));
66
document = new HTMLDocument(new StringContent(), new StyleSheet());
67
testStorage(true, new JPasswordField(document, "", 10));
68
69
// custom HTMLDocument GAP document
70
document = new HTMLDocument(new GapContent(), new StyleSheet());
71
testStorage(false, new JPasswordField(document, "", 10));
72
document = new HTMLDocument(new GapContent(), new StyleSheet());
73
testStorage(true, new JPasswordField(document, "", 10));
74
});
75
}
76
77
private static void testStorage(boolean makeGap, JPasswordField pf) {
78
test(pf, "123", makeGap);
79
test(pf, "1234567", makeGap);
80
test(pf, "1234567890", makeGap);
81
test(pf, "1".repeat(100), makeGap);
82
test(pf, "1234567890", makeGap);
83
test(pf, "1234567", makeGap);
84
test(pf, "123", makeGap);
85
test(pf, "", makeGap);
86
}
87
88
private static void test(JPasswordField pf, String text, boolean makeGap) {
89
pf.setText(text);
90
if (makeGap && text.length() > 3) {
91
try {
92
pf.getDocument().remove(1, 2);
93
} catch (BadLocationException e) {
94
throw new RuntimeException(e);
95
}
96
}
97
// if no gaps we can check whole array
98
char[] internalArray = getInternalArray(pf);
99
ArrayList<Segment> segments = new ArrayList<>();
100
if (makeGap) {
101
// if gaps exists we can check only part of the array
102
Document doc = pf.getDocument();
103
int nleft = doc.getLength();
104
Segment sgm = new Segment();
105
sgm.setPartialReturn(true);
106
int offs = 0;
107
try {
108
while (nleft > 0) {
109
doc.getText(offs, nleft, sgm);
110
segments.add(sgm);
111
nleft -= sgm.count;
112
offs += sgm.count;
113
}
114
} catch (BadLocationException e) {
115
throw new RuntimeException(e);
116
}
117
}
118
System.err.println("Before = " + Arrays.toString(internalArray));
119
pf.setText("");
120
System.err.println("After = " + Arrays.toString(internalArray));
121
122
if (!makeGap) {
123
for (char c : internalArray) {
124
if (c != '\u0000' && c != '\n') {
125
throw new RuntimeException(Arrays.toString(internalArray));
126
}
127
}
128
} else {
129
for (Segment sgm : segments) {
130
for (int i = sgm.offset; i < sgm.count + sgm.offset; i++) {
131
char c = sgm.array[i];
132
if (c != '\u0000' && c != '\n') {
133
throw new RuntimeException(Arrays.toString(sgm.array));
134
}
135
}
136
}
137
}
138
}
139
140
/**
141
* This method returns the reference to the internal data stored by the
142
* document inside JPasswordField.
143
*/
144
private static char[] getInternalArray(JPasswordField pf) {
145
Document doc = pf.getDocument();
146
int nleft = doc.getLength();
147
Segment text = new Segment();
148
int offs = 0;
149
text.setPartialReturn(true);
150
try {
151
doc.getText(offs, nleft, text);
152
} catch (BadLocationException e) {
153
throw new RuntimeException(e);
154
}
155
return text.array;
156
}
157
}
158
159