Skip to content

Commit

Permalink
Merge pull request apache#2910 from JaroslavTulach/jtulach/BetterVirt…
Browse files Browse the repository at this point in the history
…ualFO

More consistency for virtual file objects
  • Loading branch information
geertjanw authored Apr 29, 2021
2 parents a8bfc8a + 0be610e commit 729a291
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,13 @@ public final FileObject getParent() {
retVal = FileBasedFileSystem.getInstance().getRoot();
} else {
retVal = factory.getCachedOnly(file);
retVal = (retVal == null) ? factory.getFileObject(new FileInfo(file), FileObjectFactory.Caller.GetParent, true) : retVal;
if (retVal == null) {
if (this.isValid()) {
retVal = factory.getFileObject(new FileInfo(file), FileObjectFactory.Caller.GetParent, true);
} else {
retVal = factory.getFileObject(new FileInfo(file), FileObjectFactory.Caller.Refresh, false);
}
}
}
}
assert retVal != null : "getParent should not return null for " + this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,14 @@ public void run() {
}
}

public void testVirtualFOs() throws IOException {
final FileObject wd = FileBasedFileSystem.getFileObject(getWorkDir());
FileObject nonExisting = wd.getFileObject("non-existing-folder/non-existing-folder/non-existing-child.xyz", false);
assertFalse(nonExisting.isValid());
assertFalse(nonExisting.getParent().isValid());
assertFalse(nonExisting.getParent().getParent().isValid());
}

private class EventsEvaluator extends FileChangeAdapter {
private int folderCreatedCount;
private int dataCreatedCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -776,13 +776,27 @@ public List<String> asLines(final String encoding) throws IOException {
public abstract OutputStream getOutputStream(FileLock lock)
throws IOException;

/** Get output stream.
/** Get output stream. This method does its best even
* when this file object is {@linkplain #isValid() invalid} - since
* version 9.23 it tries to recreate the parent hierarchy
* and really open the stream.
*
* @return output stream to overwrite the contents of this file
* @throws IOException if an error occurs (the file is invalid, etc.)
* @throws IOException if an error occurs
* @throws FileAlreadyLockedException if the file is already locked
* @since 6.6
*/
public final OutputStream getOutputStream() throws FileAlreadyLockedException, IOException {
if (!isValid()) {
final FileObject recreate = FileUtil.createData(getFileSystem().getRoot(), getPath());
if (recreate != null) {
return recreate.getOutputStreamImpl();
}
}
return getOutputStreamImpl();
}

private OutputStream getOutputStreamImpl() throws IOException {
final FileLock lock = lock();
final OutputStream os;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,53 @@ public void testGetNameExt() {

fsAssert("getNameExt problem",fo1.getNameExt().equals(fo1.getName() + "." +fo1.getExt()));
}


public void testManualRecreateOfInvalidFileObject() throws IOException {
checkSetUp();
FileObject fold1 = getTestFolder1(root);

if (fold1.getFileSystem().isReadOnly()) {
return;
}

FileObject ch = fold1.createData("a-child");
ch.delete();
assertFalse("Not valid", ch.isValid());

FileObject newCh = ch.getParent().createData(ch.getNameExt());
assertEquals("Same path", ch.getPath(), newCh.getPath());

try (OutputStream os = newCh.getOutputStream()) {
os.write("Ahoj".getBytes("UTF-8"));
}
assertEquals("Ahoj", newCh.asText("UTF-8"));
assertEquals("Parents are same", ch.getParent(), newCh.getParent());
}

public void testRecreateOfInvalidFileObjectViaGetOutputStream() throws IOException {
checkSetUp();
FileObject fold1 = getTestFolder1(root);

if (fold1.getFileSystem().isReadOnly()) {
return;
}

FileObject ch = fold1.createData("a-child");
ch.delete();
assertFalse("Not valid", ch.isValid());

try (OutputStream os = ch.getOutputStream()) {
os.write("Ahoj".getBytes("UTF-8"));
}

if (!ch.isValid()) {
ch = ch.getFileSystem().findResource(ch.getPath());
}

assertEquals("Ahoj", ch.asText("UTF-8"));
assertEquals("Parents are same", ch.getParent(), ch.getParent());
}

/** Test of existsExt method, of class org.openide.filesystems.FileObject. */
public void testExistsExt() {
checkSetUp();
Expand Down Expand Up @@ -3467,14 +3513,18 @@ public void testCreateDataWithBackSlash() throws Exception {
}

public void testNonExistingFileObject() throws Exception {
nonExistingFileObject("non-existing-child.xyz");
nonExistingFileObject("non-existing-child.xyz", 0);
}

public void testNonExistingFileObjectInFolder() throws Exception {
nonExistingFileObject("non-existing-folder/non-existing-child.xyz");
nonExistingFileObject("non-existing-folder/non-existing-child.xyz", 1);
}

public void testNonExistingDoubleFileObjectInFolder() throws Exception {
nonExistingFileObject("non-existing-folder/non-existing-folder/non-existing-child.xyz", 2);
}

private void nonExistingFileObject(String childName) throws Exception {
private void nonExistingFileObject(String childName, int depth) throws Exception {
checkSetUp();
final FileObject fold = getTestFolder1(root);

Expand All @@ -3486,12 +3536,35 @@ private void nonExistingFileObject(String childName) throws Exception {
assertEquals("non-existing-child.xyz", ch2.getNameExt());
assertFalse("It is not valid to begin with", ch2.isValid());

{
FileObject p = ch2.getParent();
while (depth-- > 0) {
assertFalse("Parent isn't valid either", p.isValid());
p = p.getParent();
}
}

URI foldUri = fold.toURI();
URI ch2Uri = ch2.toURI();

if (!ch2Uri.toString().startsWith(foldUri.toString())) {
fail("Expecting the child url:\n" + ch2Uri + "\nto begin with folder URL:\n" + foldUri);
}

if (!ch2.getFileSystem().isReadOnly()) {
try (Writer os = new OutputStreamWriter(ch2.getOutputStream())) {
os.write("Ahoj");
}
FileObject ch3;
if (ch2.isValid()) {
ch3 = ch2;
} else {
ch3 = ch2.getFileSystem().findResource(ch2.getPath());
assertNotNull("Found recreated file object for " + ch2, ch3);
}
assertEquals("Ahoj", ch3.asText("UTF-8"));
assertTrue("This file object is valid", ch3.isValid());
}
}

/*#46885: File not refreshed in editor if modified externally the first time after an internal modification*/
Expand Down

0 comments on commit 729a291

Please sign in to comment.