1 /*
   2  * Copyright (c) 2010, 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 /*
  25  * @test
  26  * @bug 6735293
  27  * @summary javax.swing.text.NavigationFilter.getNextVisualPositionFrom() not always throws BadLocationException
  28  * @author Pavel Porvatov
  29  */
  30 
  31 import javax.swing.*;
  32 import javax.swing.text.BadLocationException;
  33 import javax.swing.text.NavigationFilter;
  34 import javax.swing.text.Position;
  35 
  36 public class bug6735293 {
  37     private static volatile JFormattedTextField jtf;
  38 
  39     private static volatile NavigationFilter nf;
  40 
  41     private static volatile JFrame jFrame;
  42 
  43     public static void main(String[] args) throws Exception {
  44         SwingUtilities.invokeAndWait(new Runnable() {
  45             public void run() {
  46                 jtf = new JFormattedTextField();
  47                 nf = new NavigationFilter();
  48                 jtf.setText("A text message");
  49 
  50                 jFrame = new JFrame();
  51                 jFrame.getContentPane().add(jtf);
  52                 jFrame.pack();
  53                 jFrame.setVisible(true);
  54             }
  55         });
  56 
  57         Thread.sleep(1000);
  58 
  59         SwingUtilities.invokeAndWait(new Runnable() {
  60             public void run() {
  61                 Position.Bias[] biasRet = {Position.Bias.Forward};
  62 
  63                 for (int direction : new int[]{
  64                         SwingConstants.EAST,
  65                         SwingConstants.WEST,
  66                         //  the following constants still will lead to "BadLocationException: Length must be positive"
  67                         SwingConstants.SOUTH,
  68                         SwingConstants.NORTH,
  69                 }) {
  70                     for (int position : new int[]{-100, Integer.MIN_VALUE}) {
  71                         for (Position.Bias bias : new Position.Bias[]{Position.Bias.Backward, Position.Bias.Forward}) {
  72                             try {
  73                                 nf.getNextVisualPositionFrom(jtf, position, bias, direction, biasRet);
  74 
  75                                 throw new RuntimeException("BadLocationException was not thrown: position = " +
  76                                         position + ", bias = " + bias + ", direction = " + direction);
  77                             } catch (BadLocationException e) {
  78                                 // Ok
  79                             }
  80                         }
  81                     }
  82                 }
  83 
  84                 jFrame.dispose();
  85             }
  86         });
  87     }
  88 }