undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,144 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 7070795
* @summary High contrast colour scheme fails to be applied to JFormattedTextField
* @requires (os.family == "windows")
* @run main/manual JFormattedTextFieldTest
*/
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class JFormattedTextFieldTest implements ActionListener {
private static GridBagLayout layout;
private static JPanel mainControlPanel;
private static JPanel resultButtonPanel;
private static JLabel instructionText;
private static JButton passButton;
private static JButton failButton;
private static JFrame mainFrame;
public static void main(String[] args) throws Exception {
JFormattedTextFieldTest jFormattedTextFieldTest = new JFormattedTextFieldTest();
}
public JFormattedTextFieldTest() throws Exception {
createUI();
}
public final void createUI() throws Exception {
UIManager.setLookAndFeel("com.sun.java.swing.plaf."
+ "windows.WindowsLookAndFeel");
SwingUtilities.invokeAndWait(() -> {
mainFrame = new JFrame("Window LAF JFormattedTextField Test");
layout = new GridBagLayout();
mainControlPanel = new JPanel(layout);
resultButtonPanel = new JPanel(layout);
GridBagConstraints gbc = new GridBagConstraints();
String instructions
= "<html>INSTRUCTIONS:<br>"
+ "Set Windows Theme to HighContrast#1.<br><br>"
+ "(ControlPanel->Personalization->High Contrast#1)<br><br>"
+ "If TextFiled colors are same test"
+ " passes else failed.<br><br></html>";
instructionText = new JLabel();
instructionText.setText(instructions);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
mainControlPanel.add(instructionText, gbc);
passButton = new JButton("Pass");
passButton.setActionCommand("Pass");
passButton.addActionListener(JFormattedTextFieldTest.this);
failButton = new JButton("Fail");
failButton.setActionCommand("Fail");
failButton.addActionListener(JFormattedTextFieldTest.this);
gbc.gridx = 0;
gbc.gridy = 0;
resultButtonPanel.add(passButton, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
resultButtonPanel.add(failButton, gbc);
gbc.gridx = 3;
gbc.gridy = 0;
resultButtonPanel.add(new JTextField("12345"), gbc);
NumberFormat format = NumberFormat.getIntegerInstance();
format.setMaximumIntegerDigits(5);
JFormattedTextField formatted = new JFormattedTextField(format);
formatted.setText("67891");
gbc.gridx = 5;
gbc.gridy = 0;
resultButtonPanel.add(formatted, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
mainControlPanel.add(resultButtonPanel, gbc);
mainFrame.add(mainControlPanel);
mainFrame.setSize(400, 200);
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
});
}
@Override
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() instanceof JButton) {
JButton btn = (JButton) evt.getSource();
cleanUp();
switch (btn.getActionCommand()) {
case "Pass":
break;
case "Fail":
throw new AssertionError("User Clicked Fail!");
}
}
}
private static void cleanUp() {
mainFrame.dispose();
}
}

View file

@ -0,0 +1,73 @@
/*
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.text.ParseException;
import javax.swing.SwingUtilities;
import javax.swing.text.DefaultFormatter;
/*
* @test
* @bug 8080972
* @run main TestDefaultFormatter
* @summary Audit Core Reflection in module java.desktop for places that will
* require changes to work with modules
*/
public class TestDefaultFormatter {
public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(TestDefaultFormatter::testDefaultFormatter);
}
private static void testDefaultFormatter() {
testDefaultFormatter(new DefaultFormatter() {
});
testDefaultFormatter(new DefaultFormatter());
}
private static void testDefaultFormatter(DefaultFormatter formatter ) {
try {
System.out.println("formatter: " + formatter.getClass());
formatter.setValueClass(UserValueClass.class);
UserValueClass userValue = (UserValueClass) formatter.stringToValue("test");
if (!userValue.str.equals("test")) {
throw new RuntimeException("String value is wrong!");
}
} catch (ParseException ex) {
throw new RuntimeException(ex);
}
}
public static class UserValueClass {
String str;
public UserValueClass(String str) {
this.str = str;
}
@Override
public String toString() {
return "UserValueClass: " + str;
}
}
}

View file

@ -0,0 +1,360 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
@bug 6462562
@summary Tests text input into JFormattedTextField
with an InternationalFormatter
@author Peter Zhelezniakov
@run main Test6462562
*/
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.Action;
import javax.swing.JFormattedTextField;
import javax.swing.SwingUtilities;
import javax.swing.text.Caret;
import javax.swing.text.DateFormatter;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.InternationalFormatter;
import javax.swing.text.NumberFormatter;
public class Test6462562
{
static final String BACKSPACE = new String("backspace");
static final String DELETE = new String("delete");
boolean failed = false;
void test() {
testPercentFormat();
testCurrencyFormat();
testIntegerFormat();
testDateFormat();
if (failed) {
throw new RuntimeException("Some testcases failed, see output above");
}
System.err.println("(-; All testcases passed ;-)");
}
TestFormattedTextField create(NumberFormat format) {
format.setMaximumFractionDigits(0);
NumberFormatter fmt = new NumberFormatter(format);
return new TestFormattedTextField(fmt);
}
TestFormattedTextField create(DateFormat format) {
DateFormatter fmt = new DateFormatter(format);
return new TestFormattedTextField(fmt);
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
new Test6462562().test();
}
});
}
class TestFormattedTextField extends JFormattedTextField
{
final Action backspace;
final Action delete;
final Action insert;
final ActionEvent dummyEvent;
public TestFormattedTextField(InternationalFormatter fmt) {
super(fmt);
fmt.setAllowsInvalid(false);
fmt.setOverwriteMode(true);
backspace = getActionMap().get(DefaultEditorKit.deletePrevCharAction);
delete = getActionMap().get(DefaultEditorKit.deleteNextCharAction);
insert = getActionMap().get(DefaultEditorKit.insertContentAction);
dummyEvent = new ActionEvent(this, 0, null);
}
public boolean test(int pos, int selectionLength, String todo, Object expectedResult) {
Object v0 = getValue();
Caret caret = getCaret();
caret.setDot(pos);
if (selectionLength > 0) {
caret.moveDot(pos + selectionLength);
}
String desc = todo;
if (todo == BACKSPACE) {
backspace.actionPerformed(dummyEvent);
} else if (todo == DELETE) {
delete.actionPerformed(dummyEvent);
} else {
desc = "insert('" + todo + "')";
insert.actionPerformed(new ActionEvent(this, 0, todo));
}
try {
commitEdit();
} catch (ParseException e) {
e.printStackTrace();
failed = true;
return false;
}
Object v1 = getValue();
if (! v1.equals(expectedResult)) {
System.err.printf("Failure: value='%s', mark=%d, dot=%d, action=%s\n",
v0, pos, pos + selectionLength, desc);
System.err.printf(" Result: '%s', expected: '%s'\n", v1, expectedResult);
failed = true;
return false;
}
return true;
}
}
void testPercentFormat() {
NumberFormat format = NumberFormat.getPercentInstance(Locale.US);
TestFormattedTextField ftf = create(format);
ftf.setValue(.34);
System.err.println("Testing NumberFormat.getPercentInstance(Locale.US)");
// test inserting individual characters
ftf.test(0, 0, "1", .14);
ftf.test(2, 0, "2", 1.42);
ftf.test(1, 0, "0", 1.02);
// test inserting several characters at once - e.g. from clipboard
ftf.test(0, 0, "1024", 10.24);
ftf.test(3, 0, "333", 103.33);
ftf.test(6, 0, "77", 10333.77);
ftf.test(4, 0, "99", 10399.77);
ftf.test(6, 0, "00", 10390.07);
// test inserting strings that contain some formatting
ftf.test(0, 0, "2,2", 2290.07);
ftf.test(2, 0, "2,2", 222.27);
ftf.test(4, 0, "2,2", 222.22);
ftf.test(6, 0, "33,33", 2222233.33);
// test delete
ftf.test(0, 0, DELETE, 222233.33);
ftf.test(10, 0, DELETE, 222233.33);
ftf.test(5, 0, DELETE, 22223.33);
ftf.test(6, 0, DELETE, 2222.33);
// test backspace
ftf.test(0, 0, BACKSPACE, 2222.33);
ftf.test(7, 0, BACKSPACE, 222.23);
ftf.test(4, 0, BACKSPACE, 22.23);
ftf.test(2, 0, BACKSPACE, 2.23);
// test replacing selection
ftf.test(0, 1, "555", 555.23);
ftf.test(4, 2, "555", 5555.55);
ftf.test(2, 3, "1", 551.55);
ftf.test(3, 2, "6", 55.65);
ftf.test(4, 2, "12", 556.12);
ftf.test(3, 4, "0", 5.5);
ftf.test(0, 3, "111222333444555", 1112223334445.55);
// test deleting selection
ftf.test(0, 2, DELETE, 12223334445.55);
ftf.test(0, 3, BACKSPACE, 223334445.55);
ftf.test(12, 2, DELETE, 2233344.45);
ftf.test(9, 2, BACKSPACE, 22333.44);
ftf.test(4, 3, DELETE, 223.44);
ftf.test(1, 2, BACKSPACE, 23.44);
ftf.test(3, 3, DELETE, .23);
ftf.test(1, 2, BACKSPACE, .02);
}
void testCurrencyFormat() {
NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
TestFormattedTextField ftf = create(format);
ftf.setValue(56L);
System.err.println("Testing NumberFormat.getCurrencyInstance(Locale.US)");
// test inserting individual characters
ftf.test(1, 0, "1", 16L);
ftf.test(3, 0, "2", 162L);
ftf.test(2, 0, "0", 102L);
// test inserting several characters at once - e.g. from clipboard
ftf.test(1, 0, "1024", 1024L);
ftf.test(4, 0, "333", 10333L);
ftf.test(7, 0, "77", 1033377L);
ftf.test(5, 0, "99", 1039977L);
ftf.test(7, 0, "00", 1039007L);
// test inserting strings that contain some formatting
ftf.test(1, 0, "2,2", 229007L);
ftf.test(3, 0, "2,2", 22227L);
ftf.test(4, 0, "2,2", 2222L);
ftf.test(6, 0, "33,33", 22223333L);
// test delete
ftf.test(1, 0, DELETE, 2223333L);
ftf.test(10, 0, DELETE, 2223333L);
ftf.test(5, 0, DELETE, 222333L);
ftf.test(5, 0, DELETE, 22233L);
// test backspace
ftf.test(1, 0, BACKSPACE, 22233L);
ftf.test(7, 0, BACKSPACE, 2223L);
ftf.test(4, 0, BACKSPACE, 223L);
ftf.test(2, 0, BACKSPACE, 23L);
// test replacing selection
ftf.test(1, 1, "555", 5553L);
ftf.test(4, 2, "555", 55555L);
ftf.test(2, 3, "1", 5155L);
ftf.test(3, 2, "6", 565L);
ftf.test(1, 3, "111222333444555", 111222333444555L);
// test deleting selection
ftf.test(1, 2, DELETE, 1222333444555L);
ftf.test(1, 3, BACKSPACE, 22333444555L);
ftf.test(13, 2, DELETE, 223334445L);
ftf.test(10, 2, BACKSPACE, 2233344L);
ftf.test(4, 4, DELETE, 2244L);
ftf.test(1, 4, BACKSPACE, 4L);
}
void testIntegerFormat() {
NumberFormat format = NumberFormat.getIntegerInstance(Locale.US);
TestFormattedTextField ftf = create(format);
ftf.setValue(56L);
System.err.println("Testing NumberFormat.getIntegerInstance(Locale.US)");
// test inserting individual characters
ftf.test(0, 0, "1", 16L);
ftf.test(2, 0, "2", 162L);
ftf.test(1, 0, "0", 102L);
// test inserting several characters at once - e.g. from clipboard
ftf.test(0, 0, "1024", 1024L);
ftf.test(3, 0, "333", 10333L);
ftf.test(6, 0, "77", 1033377L);
ftf.test(4, 0, "99", 1039977L);
ftf.test(6, 0, "00", 1039007L);
// test inserting strings that contain some formatting
ftf.test(0, 0, "2,2", 229007L);
ftf.test(2, 0, "2,2", 22227L);
ftf.test(3, 0, "2,2", 2222L);
ftf.test(5, 0, "33,33", 22223333L);
// test delete
ftf.test(0, 0, DELETE, 2223333L);
ftf.test(9, 0, DELETE, 2223333L);
ftf.test(4, 0, DELETE, 222333L);
ftf.test(4, 0, DELETE, 22233L);
// test backspace
ftf.test(0, 0, BACKSPACE, 22233L);
ftf.test(6, 0, BACKSPACE, 2223L);
ftf.test(2, 0, BACKSPACE, 223L);
ftf.test(2, 0, BACKSPACE, 23L);
// test replacing selection
ftf.test(0, 1, "555", 5553L);
ftf.test(3, 2, "555", 55555L);
ftf.test(1, 3, "1", 5155L);
ftf.test(2, 2, "6", 565L);
ftf.test(0, 3, "111222333444555", 111222333444555L);
// test deleting selection
ftf.test(0, 2, DELETE, 1222333444555L);
ftf.test(0, 3, BACKSPACE, 22333444555L);
ftf.test(12, 2, DELETE, 223334445L);
ftf.test(9, 2, BACKSPACE, 2233344L);
ftf.test(3, 4, DELETE, 2244L);
ftf.test(0, 4, BACKSPACE, 4L);
}
Date date(DateFormat format, String spec) {
try {
return format.parse(spec);
} catch (ParseException e) {
throw new Error("Error in test");
}
}
void testDateFormat() {
DateFormat format = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
TestFormattedTextField ftf = create(format);
ftf.setValue(date(format, "12/05/2005"));
System.err.println("Testing SimpleDateFormat(\"MM/dd/yyyy\", Locale.US)");
// test inserting individual characters
ftf.test(0, 0, "0", date(format, "02/05/2005"));
ftf.test(4, 0, "4", date(format, "02/04/2005"));
ftf.test(6, 0, "1", date(format, "02/04/1005"));
ftf.test(9, 0, "9", date(format, "02/04/1009"));
// test inserting several characters at once - e.g. from clipboard
ftf.test(0, 0, "11", date(format, "11/04/1009"));
ftf.test(3, 0, "23", date(format, "11/23/1009"));
ftf.test(6, 0, "191", date(format, "11/23/1919"));
// test delete
ftf.test(0, 0, DELETE, date(format, "01/23/1919"));
ftf.test(3, 0, DELETE, date(format, "01/03/1919"));
ftf.test(10, 0, DELETE, date(format, "01/03/1919"));
ftf.test(1, 0, DELETE, date(format, "12/03/1918"));
ftf.test(4, 0, DELETE, date(format, "11/30/1918"));
// test backspace
ftf.test(0, 0, BACKSPACE, date(format, "11/30/1918"));
ftf.test(1, 0, BACKSPACE, date(format, "01/30/1918"));
ftf.test(4, 0, BACKSPACE, date(format, "12/31/1917"));
ftf.test(10, 0, BACKSPACE, date(format, "12/31/0191"));
ftf.test(3, 0, BACKSPACE, date(format, "01/31/0191"));
ftf.test(5, 0, BACKSPACE, date(format, "01/03/0191"));
// test replacing selection
ftf.test(0, 1, "1", date(format, "11/03/0191"));
ftf.test(3, 1, "2", date(format, "11/23/0191"));
ftf.test(6, 2, "20", date(format, "11/23/2091"));
// test deleting selection
ftf.test(0, 1, BACKSPACE, date(format, "01/23/2091"));
ftf.test(3, 1, DELETE, date(format, "01/03/2091"));
ftf.test(6, 2, BACKSPACE, date(format, "01/03/0091"));
ftf.test(8, 1, DELETE, date(format, "01/03/0001"));
}
}

View file

@ -0,0 +1,151 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @requires (os.family == "linux")
* @key headful
* @bug 8214112
* @summary Tests JFormattedTextField selected Text background color
* @run main TestSelectedTextBackgroundColor
*/
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.MaskFormatter;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
public class TestSelectedTextBackgroundColor {
private static JFrame frame;
private static JFormattedTextField formattedTextField;
private static Point point;
private static Rectangle rect;
private static Robot robot;
private static final String GTK_LAF_CLASS = "GTKLookAndFeel";
private static int minColorDifference = 100;
private static void blockTillDisplayed(Component comp) {
Point p = null;
while (p == null) {
try {
p = comp.getLocationOnScreen();
} catch (IllegalStateException e) {
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
}
}
}
}
private static int getMaxColorDiff(Color c1, Color c2) {
return Math.max(Math.abs(c1.getRed() - c2.getRed()),
Math.max(Math.abs(c1.getGreen() - c2.getGreen()),
Math.abs(c1.getBlue() - c2.getBlue())));
}
public static void main(String[] args) throws Exception {
if (!System.getProperty("os.name").startsWith("Linux")) {
System.out.println("This test is meant for Linux platform only");
return;
}
for (UIManager.LookAndFeelInfo lookAndFeelInfo :
UIManager.getInstalledLookAndFeels()) {
if (lookAndFeelInfo.getClassName().contains(GTK_LAF_CLASS)) {
try {
UIManager.setLookAndFeel(lookAndFeelInfo.getClassName());
} catch (final UnsupportedLookAndFeelException ignored) {
System.out.println("GTK L&F could not be set, so this " +
"test can not be run in this scenario ");
return;
}
}
}
robot = new Robot();
robot.setAutoDelay(100);
MaskFormatter formatter = new MaskFormatter("###-##-####");
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
JPanel panel = new JPanel();
formattedTextField = new JFormattedTextField(formatter);
panel.add(formattedTextField, BorderLayout.CENTER);
frame = new JFrame("TestSelectedTextBackgroundColor");
frame.add(panel);
frame.setSize(200, 200);
frame.setAlwaysOnTop(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
robot.waitForIdle();
robot.delay(500);
blockTillDisplayed(formattedTextField);
SwingUtilities.invokeAndWait(() -> {
point = formattedTextField.getLocationOnScreen();
rect = formattedTextField.getBounds();
});
robot.waitForIdle();
robot.delay(500);
Color backgroundColor = robot
.getPixelColor(point.x+rect.width/2, point.y+rect.height/2);
robot.waitForIdle();
robot.delay(500);
formattedTextField.selectAll();
robot.waitForIdle();
robot.delay(500);
Color highlightColor = robot
.getPixelColor(point.x+rect.width/2, point.y+rect.height/2);
robot.waitForIdle();
robot.delay(500);
int actualColorDifference = getMaxColorDiff(backgroundColor, highlightColor);
if (actualColorDifference < minColorDifference) {
throw new RuntimeException("The expected background color for " +
"Selected Text was not found");
}
} finally {
if (frame != null) {
SwingUtilities.invokeAndWait(frame::dispose);
}
}
}
}

View file

@ -0,0 +1,134 @@
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4741926
* @summary JFormattedTextField/JSpinner always consumes certain key events
* @key headful
* @run main bug4741926
*/
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.util.Date;
import javax.swing.AbstractAction;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class bug4741926 {
static MyFormattedTextField ftf;
static JFrame fr;
static Robot robot;
static volatile boolean passed_enter = false;
static volatile boolean passed_escape = false;
static volatile boolean ftfFocused = false;
static volatile boolean keyProcessed = false;
public static void main(String[] args) throws Exception {
try {
robot = new Robot();
robot.setAutoDelay(100);
SwingUtilities.invokeAndWait(() -> {
fr = new JFrame("Test");
ftf = new MyFormattedTextField();
ftf.setValue("JFormattedTextField");
JPanel p = (JPanel) fr.getContentPane();
p.add(ftf);
ftf.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
ftfFocused = true;
}
});
InputMap map = p.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
map.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
"enter-action");
p.getActionMap().put("enter-action", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
passed_enter = true;
keyProcessed = true;
}
});
map.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
"escape-action");
p.getActionMap().put("escape-action", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
passed_escape = true;
keyProcessed = true;
}
});
fr.pack();
fr.setLocationRelativeTo(null);
fr.setVisible(true);
});
robot.waitForIdle();
robot.delay(1000);
test();
if (!(passed_enter && passed_escape)) {
throw new RuntimeException("JFormattedTextField consume " +
"Enter/Escape key event");
}
} finally {
SwingUtilities.invokeAndWait(() -> {
if (fr != null) {
fr.dispose();
}
});
}
}
public static void test() throws Exception {
SwingUtilities.invokeAndWait(() -> {
ftf.requestFocus();
});
robot.delay(500);
doTest(KeyEvent.VK_ENTER);
doTest(KeyEvent.VK_ESCAPE);
}
static void doTest(int keyCode) throws InterruptedException {
keyProcessed = false;
KeyEvent key = new KeyEvent(ftf, KeyEvent.KEY_PRESSED,
new Date().getTime(), 0,
keyCode,
KeyEvent.CHAR_UNDEFINED);
ftf.processKey(key);
}
static class MyFormattedTextField extends JFormattedTextField {
public void processKey(KeyEvent e) {
processKeyEvent(e);
}
}
}

View file

@ -0,0 +1,101 @@
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4863121
* @summary JFormattedTextField's NotifyAction should invoke invalidEdit if
commit fails
* @key headful
* @run main bug4863121
*/
import java.awt.Robot;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.text.Format;
import java.text.DecimalFormat;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class bug4863121 {
static TestFormattedTextField ftf;
static JFrame fr;
static Robot robot;
private static volatile boolean focused = false;
private static volatile boolean passed = false;
public static void main(String[] args) throws Exception {
try {
robot = new Robot();
robot.setAutoDelay(100);
SwingUtilities.invokeAndWait(() -> {
fr = new JFrame("Test");
ftf = new TestFormattedTextField(new DecimalFormat("####"));
ftf.setText("q");
fr.getContentPane().add(ftf);
ftf.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
focused = true;
}
});
fr.pack();
fr.setLocationRelativeTo(null);
fr.setVisible(true);
});
robot.waitForIdle();
robot.delay(1000);
SwingUtilities.invokeAndWait(() -> {
ftf.requestFocus();
});
robot.waitForIdle();
robot.delay(500);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
if (!passed) {
throw new RuntimeException("JFormattedTextField's NotifyAction " +
"should invoke invalidEdit if commit fails");
}
} finally {
SwingUtilities.invokeAndWait(() -> {
if (fr != null) {
fr.dispose();
}
});
}
}
public static class TestFormattedTextField extends JFormattedTextField {
public TestFormattedTextField(Format f) {
super(f);
}
protected void invalidEdit() {
passed = true;
}
}
}

View file

@ -0,0 +1,75 @@
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4886538
* @summary JFormattedTextField not returning correct value (class)
* @run main bug4886538
*/
import javax.swing.JFormattedTextField;
import javax.swing.SwingUtilities;
import javax.swing.text.DefaultFormatterFactory;
public class bug4886538 {
public static void main(String[] args) throws Exception {
// test default display formatter
TestFormattedTextField field = new TestFormattedTextField(0.0);
field.setFormatter(((DefaultFormatterFactory) field.
getFormatterFactory()).getDisplayFormatter());
field.setText("10");
field.commitEdit();
Object dblValue = field.getValue();
if (!(dblValue instanceof Double)) {
throw new RuntimeException("The JFormattedTextField's value " +
"should be instanceof Double");
}
// test default editor formatter
field = new TestFormattedTextField(0.0);
field.setFormatter(((DefaultFormatterFactory) field.
getFormatterFactory()).getEditFormatter());
field.setText("10");
field.commitEdit();
dblValue = field.getValue();
if (!(dblValue instanceof Double)) {
throw new RuntimeException("The JFormattedTextField's value " +
"should be instanceof Double");
}
}
static class TestFormattedTextField extends JFormattedTextField {
public TestFormattedTextField(Object value) {
super(value);
}
public void setFormatter(JFormattedTextField.AbstractFormatter formatter) {
super.setFormatter(formatter);
}
}
}