changeset 3531:b45ea2c3bd6d

6735293: javax.swing.text.NavigationFilter.getNextVisualPositionFrom() not always throws BadLocationException Reviewed-by: peterz
author rupashka
date Mon, 24 Jan 2011 18:04:37 +0300
parents e93106dc798b
children 1f3ecbfa0c29 69fac04eda38
files src/share/classes/javax/swing/text/View.java test/javax/swing/text/NavigationFilter/6735293/bug6735293.java
diffstat 2 files changed, 92 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/javax/swing/text/View.java	Wed Jan 19 21:51:10 2011 -0800
+++ b/src/share/classes/javax/swing/text/View.java	Mon Jan 24 18:04:37 2011 +0300
@@ -497,6 +497,10 @@
     public int getNextVisualPositionFrom(int pos, Position.Bias b, Shape a,
                                          int direction, Position.Bias[] biasRet)
       throws BadLocationException {
+        if (pos < -1) {
+            // -1 is a reserved value, see the code below
+            throw new BadLocationException("Invalid position", pos);
+        }
 
         biasRet[0] = Position.Bias.Forward;
         switch (direction) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/javax/swing/text/NavigationFilter/6735293/bug6735293.java	Mon Jan 24 18:04:37 2011 +0300
@@ -0,0 +1,88 @@
+/*
+ * 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 6735293
+ * @summary javax.swing.text.NavigationFilter.getNextVisualPositionFrom() not always throws BadLocationException
+ * @author Pavel Porvatov
+ */
+
+import javax.swing.*;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.NavigationFilter;
+import javax.swing.text.Position;
+
+public class bug6735293 {
+    private static volatile JFormattedTextField jtf;
+
+    private static volatile NavigationFilter nf;
+
+    private static volatile JFrame jFrame;
+
+    public static void main(String[] args) throws Exception {
+        SwingUtilities.invokeAndWait(new Runnable() {
+            public void run() {
+                jtf = new JFormattedTextField();
+                nf = new NavigationFilter();
+                jtf.setText("A text message");
+
+                jFrame = new JFrame();
+                jFrame.getContentPane().add(jtf);
+                jFrame.pack();
+                jFrame.setVisible(true);
+            }
+        });
+
+        Thread.sleep(1000);
+
+        SwingUtilities.invokeAndWait(new Runnable() {
+            public void run() {
+                Position.Bias[] biasRet = {Position.Bias.Forward};
+
+                for (int direction : new int[]{
+                        SwingConstants.EAST,
+                        SwingConstants.WEST,
+                        //  the following constants still will lead to "BadLocationException: Length must be positive"
+                        SwingConstants.SOUTH,
+                        SwingConstants.NORTH,
+                }) {
+                    for (int position : new int[]{-100, Integer.MIN_VALUE}) {
+                        for (Position.Bias bias : new Position.Bias[]{Position.Bias.Backward, Position.Bias.Forward}) {
+                            try {
+                                nf.getNextVisualPositionFrom(jtf, position, bias, direction, biasRet);
+
+                                throw new RuntimeException("BadLocationException was not thrown: position = " +
+                                        position + ", bias = " + bias + ", direction = " + direction);
+                            } catch (BadLocationException e) {
+                                // Ok
+                            }
+                        }
+                    }
+                }
+
+                jFrame.dispose();
+            }
+        });
+    }
+}