Skip to content

Commit 9b5cd9d

Browse files
author
Andrew Lu
committed
8315594: Open source few headless Swing misc tests
Backport-of: 806ef0897b42c8f3cb3b4d7bd904af9ed18a543e
1 parent f57f2cb commit 9b5cd9d

File tree

4 files changed

+256
-0
lines changed

4 files changed

+256
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 1999, 2023, 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+
/* @test
25+
@bug 4267840
26+
@summary Tests how HTMLEditorKit.write() works on small documents
27+
@run main bug4267840
28+
*/
29+
30+
import javax.swing.JTextPane;
31+
import javax.swing.text.EditorKit;
32+
import javax.swing.SwingUtilities;
33+
import java.io.File;
34+
import java.io.FileOutputStream;
35+
36+
public class bug4267840 {
37+
public static void main(String[] args) throws Exception {
38+
SwingUtilities.invokeAndWait(() -> {
39+
final JTextPane textpane = new JTextPane();
40+
textpane.setContentType("text/html");
41+
final EditorKit kit = textpane.getEditorKit();
42+
43+
textpane.setText("A word");
44+
File file = new File("bug4267840.out");
45+
try {
46+
FileOutputStream out = new FileOutputStream(file);
47+
kit.write(out, textpane.getDocument(), 0,
48+
textpane.getDocument().getLength());
49+
out.close();
50+
} catch (Exception e) {}
51+
try {
52+
if (file.length() < 6) { // simply can't be
53+
throw new RuntimeException("Failed: " +
54+
" HTMLEditorKit.write() is broken");
55+
}
56+
} finally {
57+
file.delete();
58+
}
59+
});
60+
}
61+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 1999, 2023, 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+
/* @test
25+
@bug 4218254
26+
@summary Serialization Bug on StyleSheet.
27+
@run main bug4218254
28+
*/
29+
30+
import javax.swing.text.html.StyleSheet;
31+
import java.io.ByteArrayInputStream;
32+
import java.io.ByteArrayOutputStream;
33+
import java.io.ObjectInputStream;
34+
import java.io.ObjectOutputStream;
35+
36+
37+
public class bug4218254 {
38+
39+
public static void main(String[] args) throws Exception {
40+
StyleSheet ssw = new StyleSheet();
41+
StyleSheet ssr = null;
42+
43+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
44+
ObjectOutputStream oos = new ObjectOutputStream(baos);
45+
oos.writeObject(ssw);
46+
byte[] buf = baos.toByteArray();
47+
oos.close();
48+
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
49+
ObjectInputStream ois = new ObjectInputStream(bais);
50+
ssr = (StyleSheet)ois.readObject();
51+
ois.close();
52+
}
53+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 1999, 2023, 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+
/* @test
25+
@bug 4243463
26+
@summary Tests that StyleSheet has following methods:
27+
public void addStyleSheet(StyleSheet ss);
28+
public void removeStyleSheet(StyleSheet ss);
29+
public Enumeration getStyleSheets()
30+
@run main bug4243463
31+
*/
32+
33+
import javax.swing.text.html.StyleSheet;
34+
35+
public class bug4243463 {
36+
37+
public static void main(String[] argv) throws Exception {
38+
StyleSheet main = new StyleSheet();
39+
StyleSheet ss = new StyleSheet();
40+
ss.addRule("p {color:red;}");
41+
42+
main.addStyleSheet(ss);
43+
StyleSheet[] sheets = main.getStyleSheets();
44+
if (sheets.length != 1 || sheets[0] != ss) {
45+
throw new RuntimeException("getStyleSheets failed");
46+
}
47+
48+
main.removeStyleSheet(ss);
49+
sheets = main.getStyleSheets();
50+
if (sheets != null) {
51+
throw new RuntimeException("StyleSheet is not removed");
52+
}
53+
}
54+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2002, 2023, 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+
/* @test
25+
@bug 4485322
26+
@summary DefaultTreeSelectionModel.insureRowContinuity is broken for CONTIGUOUS_TREE_SELECTION
27+
@run main bug4485322
28+
*/
29+
30+
import javax.swing.SwingUtilities;
31+
import javax.swing.tree.DefaultTreeSelectionModel;
32+
import javax.swing.tree.TreeSelectionModel;
33+
import javax.swing.tree.RowMapper;
34+
import javax.swing.tree.TreePath;
35+
36+
import java.util.Arrays;
37+
38+
public class bug4485322 {
39+
40+
Object obj1[] = {"9", "2", "5", "3", "1"};
41+
Object obj2[] = {"1", "2", "3"};
42+
43+
public void init() {
44+
DummyDefaultTreeSelectionModel model = new DummyDefaultTreeSelectionModel();
45+
46+
TreePath sPaths[] = new TreePath[obj1.length];
47+
for (int i=0; i<obj1.length; i++) {
48+
sPaths[i] = new TreePath(obj1[i]);
49+
};
50+
model.setSelectionPaths(sPaths);
51+
52+
model.setRowMapper(new DummyRowMapper());
53+
model.setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
54+
model.insureRowContinuity();
55+
56+
TreePath real[] = model.getSelectionPaths();
57+
TreePath expected[] = new TreePath[obj2.length];
58+
for (int i=0; i<obj2.length; i++) {
59+
expected[i] = new TreePath(obj2[i]);
60+
};
61+
62+
if ( !Arrays.equals(real, expected) ) {
63+
throw new RuntimeException("The tree selection path is wrong.");
64+
}
65+
}
66+
67+
public static class DummyDefaultTreeSelectionModel extends DefaultTreeSelectionModel {
68+
public void insureRowContinuity() {
69+
super.insureRowContinuity();
70+
}
71+
}
72+
73+
public static class DummyRowMapper implements RowMapper {
74+
public int[] getRowsForPaths(TreePath[] path) {
75+
int rows[] = new int[path.length];
76+
for (int i = 0;i < path.length; i++) {
77+
String userObject = path[i].getPathComponent(0).toString();
78+
rows[i] = Integer.valueOf(userObject);
79+
}
80+
return rows;
81+
}
82+
}
83+
84+
public static void main(String[] argv) throws Exception {
85+
bug4485322 b = new bug4485322();
86+
SwingUtilities.invokeAndWait(() -> b.init());
87+
}
88+
}

0 commit comments

Comments
 (0)