Skip to content

Commit 4a63eb0

Browse files
author
Tejesh R
committedSep 15, 2023
8315834: Open source several Swing JSpinner related tests
Reviewed-by: psadhukhan, abhiscxk
1 parent 8dc2d92 commit 4a63eb0

File tree

5 files changed

+370
-0
lines changed

5 files changed

+370
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
import javax.swing.JComponent;
25+
import javax.swing.JSpinner;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.ObjectOutputStream;
28+
29+
/*
30+
* @test
31+
* @bug 4522737
32+
* @summary Cannot serialize JSpinner twice.
33+
* @run main bug4522737
34+
*/
35+
36+
public class bug4522737 {
37+
public static void main(String[] args) throws Exception {
38+
final JComponent originalComponent = new JSpinner();
39+
40+
ByteArrayOutputStream byteArrayOutputStream =
41+
new ByteArrayOutputStream();
42+
ObjectOutputStream objectOutputStream =
43+
new ObjectOutputStream(byteArrayOutputStream);
44+
objectOutputStream.writeObject(originalComponent);
45+
46+
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
47+
objectOutputStream.writeObject(originalComponent);
48+
49+
System.out.println("Test Passed!");
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (c) 2003, 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+
import javax.swing.JFormattedTextField;
25+
import javax.swing.JFrame;
26+
import javax.swing.JSpinner;
27+
import javax.swing.SpinnerDateModel;
28+
import javax.swing.SpinnerListModel;
29+
import javax.swing.SpinnerNumberModel;
30+
import javax.swing.SwingUtilities;
31+
import java.awt.Font;
32+
import java.awt.GridLayout;
33+
import java.awt.Robot;
34+
35+
/*
36+
* @test
37+
* @bug 4656590
38+
* @summary JSpinner.setFont() does nothing
39+
* @key headful
40+
* @run main bug4656590
41+
*/
42+
43+
public class bug4656590 {
44+
private static JSpinner[] spinner = new JSpinner[6];
45+
private static Font font = new Font("Arial", Font.BOLD, 24);
46+
private static volatile boolean failed = false;
47+
private static JFrame frame;
48+
private static Robot robot;
49+
50+
public static void main(String[] args) throws Exception {
51+
try {
52+
robot = new Robot();
53+
SwingUtilities.invokeAndWait(() -> {
54+
frame = new JFrame();
55+
frame.getContentPane().setLayout(new GridLayout(3, 2));
56+
spinner[0] = new JSpinner();
57+
spinner[0].setModel(new SpinnerNumberModel());
58+
spinner[0].setFont(font);
59+
frame.getContentPane().add(spinner[0]);
60+
61+
spinner[1] = new JSpinner();
62+
spinner[1].setModel(new SpinnerDateModel());
63+
spinner[1].setFont(font);
64+
frame.getContentPane().add(spinner[1]);
65+
66+
spinner[2] = new JSpinner();
67+
spinner[2].setModel(new SpinnerListModel
68+
(new Object[]{"one", "two", "three"}));
69+
spinner[2].setFont(font);
70+
frame.getContentPane().add(spinner[2]);
71+
72+
spinner[3] = new JSpinner();
73+
spinner[3].setFont(font);
74+
spinner[3].setModel(new SpinnerNumberModel());
75+
frame.getContentPane().add(spinner[3]);
76+
77+
spinner[4] = new JSpinner();
78+
spinner[4].setFont(font);
79+
spinner[4].setModel(new SpinnerDateModel());
80+
frame.getContentPane().add(spinner[4]);
81+
82+
spinner[5] = new JSpinner();
83+
spinner[5].setFont(font);
84+
spinner[5].setModel(new SpinnerListModel
85+
(new Object[]{"one", "two", "three"}));
86+
frame.getContentPane().add(spinner[5]);
87+
frame.pack();
88+
frame.setVisible(true);
89+
});
90+
robot.waitForIdle();
91+
robot.delay(1000);
92+
SwingUtilities.invokeAndWait(() -> {
93+
JFormattedTextField ftf;
94+
for (int i = 1; i < 6; i++) {
95+
ftf = ((JSpinner.DefaultEditor)
96+
spinner[i].getEditor()).getTextField();
97+
if (!ftf.getFont().equals(font)) {
98+
failed = true;
99+
}
100+
}
101+
});
102+
robot.waitForIdle();
103+
robot.delay(1000);
104+
if (failed) {
105+
throw new RuntimeException("JSpinner.setFont() " +
106+
"doesn't set the font properly");
107+
}
108+
System.out.println("Test Passed!");
109+
} finally {
110+
SwingUtilities.invokeAndWait(() -> {
111+
if (frame != null) {
112+
frame.dispose();
113+
}
114+
});
115+
}
116+
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) 2003, 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+
import javax.swing.JComponent;
25+
import javax.swing.JFrame;
26+
import javax.swing.JSpinner;
27+
import javax.swing.JTextField;
28+
import javax.swing.SpinnerNumberModel;
29+
import javax.swing.SwingUtilities;
30+
import java.awt.Component;
31+
import java.awt.FlowLayout;
32+
import java.awt.Robot;
33+
34+
/*
35+
* @test
36+
* @bug 4680204
37+
* @summary JSpinner shows ToolTipText only on it's border
38+
* @key headful
39+
* @run main bug4680204
40+
*/
41+
42+
public class bug4680204 {
43+
44+
private static JSpinner sp1, sp2;
45+
private static final String TOOL_TIP_TEXT = "ToolTipText";
46+
private static JFrame frame;
47+
private static Robot robot;
48+
private static volatile boolean failed = false;
49+
50+
public static void main(String[] args) throws Exception {
51+
try {
52+
robot = new Robot();
53+
SwingUtilities.invokeAndWait(() -> {
54+
frame = new JFrame();
55+
frame.getContentPane().setLayout(new FlowLayout());
56+
57+
sp1 = new JSpinner(new SpinnerNumberModel(1, 1, 100, 1));
58+
sp1.setToolTipText(TOOL_TIP_TEXT);
59+
frame.getContentPane().add(sp1);
60+
61+
sp2 = new JSpinner();
62+
sp2.setToolTipText(TOOL_TIP_TEXT);
63+
frame.getContentPane().add(sp2);
64+
sp2.setModel(new SpinnerNumberModel(1, 1, 100, 1));
65+
frame.setLocationRelativeTo(null);
66+
frame.pack();
67+
frame.setVisible(true);
68+
});
69+
robot.waitForIdle();
70+
robot.delay(1000);
71+
SwingUtilities.invokeAndWait(() -> {
72+
Component[] children = sp1.getComponents();
73+
for (int i = 0; i < children.length; i++) {
74+
if (children[i] instanceof JSpinner.DefaultEditor) {
75+
JTextField tf = ((JSpinner.DefaultEditor) children[i]).getTextField();
76+
if (!TOOL_TIP_TEXT.equals(tf.getToolTipText())) {
77+
failed = true;
78+
}
79+
} else if (children[i] instanceof JComponent) {
80+
String text = ((JComponent) children[i]).getToolTipText();
81+
if (!TOOL_TIP_TEXT.equals(text)) {
82+
failed = true;
83+
}
84+
}
85+
}
86+
87+
children = sp2.getComponents();
88+
for (int i = 0; i < children.length; i++) {
89+
if (children[i] instanceof JSpinner.DefaultEditor) {
90+
JTextField tf = ((JSpinner.DefaultEditor) children[i]).getTextField();
91+
if (!TOOL_TIP_TEXT.equals(tf.getToolTipText())) {
92+
failed = true;
93+
}
94+
} else if (children[i] instanceof JComponent) {
95+
String text = ((JComponent) children[i]).getToolTipText();
96+
if (!TOOL_TIP_TEXT.equals(text)) {
97+
failed = true;
98+
}
99+
}
100+
}
101+
});
102+
robot.waitForIdle();
103+
robot.delay(1000);
104+
if (failed) {
105+
throw new RuntimeException("The tooltip text is not correctly set for JSpinner");
106+
}
107+
} finally {
108+
SwingUtilities.invokeAndWait(() -> {
109+
if (frame != null) {
110+
frame.dispose();
111+
}
112+
});
113+
}
114+
System.out.println("Test Passed!");
115+
}
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2003, 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+
import javax.swing.JButton;
25+
import javax.swing.JSpinner;
26+
27+
/*
28+
* @test
29+
* @bug 4862257
30+
* @summary Class cast Exception occurred when JButton is set to JSpinner
31+
* @run main bug4862257
32+
*/
33+
34+
public class bug4862257 {
35+
public static void main(String[] argv) {
36+
JSpinner spinner = new JSpinner();
37+
spinner.setEditor(new JButton("JButton"));
38+
System.out.println("Test Passed!");
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2004, 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+
import javax.swing.SpinnerDateModel;
25+
26+
/*
27+
* @test
28+
* @bug 5104421
29+
* @summary SpinnerDateModel.setValue(Object) throws exception with incorrect message
30+
* @run main bug5104421
31+
*/
32+
33+
public class bug5104421 {
34+
public static void main(String[] args) {
35+
SpinnerDateModel model = new SpinnerDateModel();
36+
try {
37+
model.setValue(Integer.valueOf(42));
38+
} catch (IllegalArgumentException e) {
39+
if (e.getMessage().toLowerCase().indexOf("null value") != -1) {
40+
throw new RuntimeException("SpinnerDateModel.setValue(Object) throws " +
41+
"exception with incorrect message");
42+
}
43+
}
44+
System.out.println("Test Passed!");
45+
}
46+
}

0 commit comments

Comments
 (0)
Please sign in to comment.