2222 */
2323
2424import java .awt .BorderLayout ;
25-
25+ import java .awt .Component ;
26+ import java .awt .Container ;
27+ import java .awt .Robot ;
2628import java .io .File ;
27- import java .lang . reflect . InvocationTargetException ;
29+ import java .util . function . Predicate ;
2830
29- import javax .swing .JFrame ;
31+ import javax .swing .JComboBox ;
3032import javax .swing .JFileChooser ;
33+ import javax .swing .JFrame ;
3134import javax .swing .SwingUtilities ;
35+ import javax .swing .UIManager ;
36+ import javax .swing .UnsupportedLookAndFeelException ;
3237import javax .swing .WindowConstants ;
33-
3438import javax .swing .filechooser .FileView ;
39+ import javax .swing .plaf .metal .MetalLookAndFeel ;
3540
3641/*
3742 * @test
3843 * @bug 6616245
3944 * @key headful
40- * @requires (os.family == "windows" | os.family == "linux")
41- * @library /java/awt/regtesthelpers
42- * @build PassFailJFrame
4345 * @summary Test to check if NPE occurs when using custom FileView.
44- * @run main/manual FileViewNPETest
46+ * @run main FileViewNPETest
4547 */
4648public class FileViewNPETest {
47- static PassFailJFrame passFailJFrame ;
49+
50+ private static JFrame frame ;
51+ private static JFileChooser jfc ;
52+ private static File path ;
53+
4854 public static void main (String [] args ) throws Exception {
49- SwingUtilities .invokeAndWait (new Runnable () {
50- public void run () {
51- try {
52- initialize ();
53- } catch (InterruptedException | InvocationTargetException e ) {
54- throw new RuntimeException (e );
55+ final Robot robot = new Robot ();
56+ try {
57+ SwingUtilities .invokeAndWait (FileViewNPETest ::initialize );
58+ robot .waitForIdle ();
59+
60+ SwingUtilities .invokeAndWait (() -> {
61+ jfc .setCurrentDirectory (path .getParentFile ());
62+ if (null != jfc .getCurrentDirectory ()) {
63+ // The current directory to become null because
64+ // the parent directory is not traversable
65+ throw new RuntimeException ("Current directory is not null" );
5566 }
56- }
57- });
58- passFailJFrame .awaitAndCheck ();
67+ });
68+ robot .waitForIdle ();
69+
70+ SwingUtilities .invokeAndWait (() -> {
71+ JComboBox <?> dirs = findDirectoryComboBox (jfc );
72+ // No NPE is expected
73+ dirs .setSelectedIndex (dirs .getSelectedIndex ());
74+ if (!jfc .getCurrentDirectory ().equals (path )) {
75+ throw new RuntimeException ("The current directory is not restored" );
76+ }
77+ });
78+ } finally {
79+ SwingUtilities .invokeAndWait (() -> {
80+ if (frame != null ) {
81+ frame .dispose ();
82+ }
83+ });
84+ }
5985 }
6086
61- static void initialize () throws InterruptedException , InvocationTargetException {
62- JFrame frame ;
63- JFileChooser jfc ;
64-
65- //Initialize the components
66- final String INSTRUCTIONS = """
67- Instructions to Test:
68- 1. The traversable folder is set to the Documents folder,
69- if it exists, in the user's home folder, otherwise
70- it's the user's home. Other folders are non-traversable.
71- 2. When the file chooser appears on the screen, select any
72- non-traversable folder from "Look-In" combo box,
73- for example the user's folder or a folder above it.
74- (The folder will not be opened since it's non-traversable).
75- 3. Select the Documents folder again.
76- 4. If NullPointerException does not occur in the step 3,
77- click Pass, otherwise the test fails automatically.
78- """ ;
79- frame = new JFrame ("JFileChooser File View NPE test" );
80- passFailJFrame = new PassFailJFrame ("Test Instructions" , INSTRUCTIONS ,
81- 5L , 13 , 40 );
82- jfc = new JFileChooser ();
87+ private static void initialize () {
88+ try {
89+ UIManager .setLookAndFeel (new MetalLookAndFeel ());
90+ } catch (UnsupportedLookAndFeelException e ) {
91+ throw new RuntimeException (e );
92+ }
8393
8494 String userHome = System .getProperty ("user.home" );
8595 String docs = userHome + File .separator + "Documents" ;
86- String path = ( new File (docs ).exists ()) ? docs : userHome ;
96+ path = new File (( new File (docs ).exists ()) ? docs : userHome ) ;
8797
88- jfc .setCurrentDirectory (new File (path ));
89- jfc .setFileView (new CustomFileView (path ));
98+ jfc = new JFileChooser ();
99+ jfc .setCurrentDirectory (path );
100+ jfc .setFileView (new CustomFileView (path .getPath ()));
90101 jfc .setControlButtonsAreShown (false );
91102
92- PassFailJFrame .addTestWindow (frame );
93- PassFailJFrame .positionTestWindow (frame , PassFailJFrame .Position .HORIZONTAL );
103+ frame = new JFrame ("JFileChooser FileView NPE test" );
94104 frame .setDefaultCloseOperation (WindowConstants .EXIT_ON_CLOSE );
95-
96105 frame .add (jfc , BorderLayout .CENTER );
97106 frame .pack ();
107+ frame .setLocationRelativeTo (null );
98108 frame .setVisible (true );
99109 }
110+
111+ private static JComboBox <?> findDirectoryComboBox (final Container container ) {
112+ Component result = findComponent (container ,
113+ c -> c instanceof JComboBox <?>);
114+ return (JComboBox <?>) result ;
115+ }
116+
117+ private static Component findComponent (final Container container ,
118+ final Predicate <Component > predicate ) {
119+ for (Component child : container .getComponents ()) {
120+ if (predicate .test (child )) {
121+ return child ;
122+ }
123+ if (child instanceof Container cont && cont .getComponentCount () > 0 ) {
124+ Component result = findComponent (cont , predicate );
125+ if (result != null ) {
126+ return result ;
127+ }
128+ }
129+ }
130+ return null ;
131+ }
100132}
101133
102134class CustomFileView extends FileView {
@@ -106,6 +138,7 @@ public CustomFileView(String path) {
106138 basePath = path ;
107139 }
108140
141+ @ Override
109142 public Boolean isTraversable (File filePath ) {
110143 return ((filePath != null ) && (filePath .isDirectory ()))
111144 && filePath .getAbsolutePath ().startsWith (basePath );
0 commit comments