On Thu, 23 Nov 2023 11:53:00 GMT, Abhishek Kumar <abhis...@openjdk.org> wrote:

>>> But the files gets deselected after selection which returns as no file 
>>> selected and test failed.
>> 
>> How is it? It sounds like magic… The working directory shouldn't matter; if 
>> it does, the test isn't as stable as it should be.
>
> Surprise to me also. It failed every single time when temporary files are 
> created in `scratch`. 
> 
>>The test isn't as stable as it should be.
> 
> Unable to trace what is missing in the test.

It took a long while for me to figure it out. What matters is whether the 
`File` object is created with an absolute path or not. By using 
`System.getProperty("java.io.tmpdir")` as the base path, you create an absolute 
path for the parent directory.

Yet if you use pass `null` as the first parameter (as I said, there's no system 
property named ".", so `tmpDir` is `null`) or use the constructor with one 
parameter `new File("testDir")`, the file has relative path. This causes some 
issues…

When you create `JFileChooser` object, you *always* pass `testDir` as its 
starting directory. Then, for each case, you set it once again. Here's what I 
see in a `PropertyChangeListener`:


>> setCurrentDirectory
directoryChanged: /home/aivanov/jdk-dev/testDir/testDir -> testDir
<< setCurrentDirectory

SelectedFileChangedProperty: /home/aivanov/jdk-dev/testDir/subDir_3 -> null
SelectedFilesChangedProperty: [Ljava.io.File;@3fed803b -> null
directoryChanged: testDir -> /home/aivanov/jdk-dev/testDir
SelectedFileChangedProperty: null -> null
SelectedFilesChangedProperty: null -> [Ljava.io.File;@1bf88ed6


This is why see a directory gets selected but then becomes unselected again.

Getting the absolute file — `testDir = new File("testDir").getAbsoluteFile()` — 
resolves this problem, and the test starts to work in the current directory. I 
tested it by running with jtreg, then the current directory is the `scratch` 
directory, and as a stand-alone app, in this case the current directory is 
whatever the current directory is.

So either way works as long as the starting `File` object is created with _an 
absolute path_.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/16674#discussion_r1414467819

Reply via email to