This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-vfs.git
The following commit(s) were added to refs/heads/master by this push: new 746106c [VFS-712] Add null-safe org.apache.commons.vfs2.util.FileObjectUtils.exists(FileObject). 746106c is described below commit 746106cc6f78db21fcd4481cf8ff754f9ed15429 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Tue May 7 13:03:49 2019 -0400 [VFS-712] Add null-safe org.apache.commons.vfs2.util.FileObjectUtils.exists(FileObject). --- .../apache/commons/vfs2/impl/VFSClassLoader.java | 3 +- .../commons/vfs2/provider/AbstractFileObject.java | 2 +- .../commons/vfs2/provider/AbstractFileSystem.java | 3 +- .../commons/vfs2/tasks/AbstractSyncTask.java | 6 ++- .../apache/commons/vfs2/util/FileObjectUtils.java | 13 ++++++ .../commons/vfs2/util/FileObjectUtilsTest.java | 47 ++++++++++++++++++++++ src/changes/changes.xml | 3 ++ 7 files changed, 73 insertions(+), 4 deletions(-) diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/VFSClassLoader.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/VFSClassLoader.java index c394cf5..613894e 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/VFSClassLoader.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/VFSClassLoader.java @@ -36,6 +36,7 @@ import org.apache.commons.vfs2.FileObject; import org.apache.commons.vfs2.FileSystemException; import org.apache.commons.vfs2.FileSystemManager; import org.apache.commons.vfs2.NameScope; +import org.apache.commons.vfs2.util.FileObjectUtils; /** * A class loader that can load classes and resources from a search path. @@ -119,7 +120,7 @@ public class VFSClassLoader extends SecureClassLoader { */ private void addFileObjects(final FileSystemManager manager, final FileObject[] files) throws FileSystemException { for (FileObject file : files) { - if (!file.exists()) { + if (!FileObjectUtils.exists(file)) { // Does not exist - skip continue; } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java index 4ae6e1e..ae229b5 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java @@ -264,7 +264,7 @@ public abstract class AbstractFileObject<AFS extends AbstractFileSystem> impleme */ @Override public void copyFrom(final FileObject file, final FileSelector selector) throws FileSystemException { - if (!file.exists()) { + if (!FileObjectUtils.exists(file)) { throw new FileSystemException("vfs.provider/copy-missing-file.error", file); } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileSystem.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileSystem.java index 21a1f2d..f1c19b7 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileSystem.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileSystem.java @@ -47,6 +47,7 @@ import org.apache.commons.vfs2.events.ChangedEvent; import org.apache.commons.vfs2.events.CreateEvent; import org.apache.commons.vfs2.events.DeleteEvent; import org.apache.commons.vfs2.impl.DefaultFileSystemConfigBuilder; +import org.apache.commons.vfs2.util.FileObjectUtils; import org.apache.commons.vfs2.util.Messages; /** @@ -376,7 +377,7 @@ public abstract class AbstractFileSystem extends AbstractVfsComponent implements */ @Override public File replicateFile(final FileObject file, final FileSelector selector) throws FileSystemException { - if (!file.exists()) { + if (!FileObjectUtils.exists(file)) { throw new FileSystemException("vfs.provider/replicate-missing-file.error", file.getName()); } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/tasks/AbstractSyncTask.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/tasks/AbstractSyncTask.java index 2567b01..3f1b820 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/tasks/AbstractSyncTask.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/tasks/AbstractSyncTask.java @@ -25,6 +25,7 @@ import org.apache.commons.vfs2.FileName; import org.apache.commons.vfs2.FileObject; import org.apache.commons.vfs2.NameScope; import org.apache.commons.vfs2.Selectors; +import org.apache.commons.vfs2.util.FileObjectUtils; import org.apache.commons.vfs2.util.Messages; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; @@ -340,7 +341,7 @@ public abstract class AbstractSyncTask extends VfsTask { * Handles a single source file. */ private void handleFile(final FileObject srcFile, final FileObject destFile) throws Exception { - if (!destFile.exists() + if (!FileObjectUtils.exists(destFile) || srcFile.getContent().getLastModifiedTime() > destFile.getContent().getLastModifiedTime()) { // Destination file is out-of-date handleOutOfDateFile(srcFile, destFile); @@ -362,6 +363,7 @@ public abstract class AbstractSyncTask extends VfsTask { * @throws Exception Implementation can throw any Exception. */ protected void handleOutOfDateFile(final FileObject srcFile, final FileObject destFile) throws Exception { + // noop } /** @@ -376,6 +378,7 @@ public abstract class AbstractSyncTask extends VfsTask { * @throws Exception Implementation can throw any Exception. */ protected void handleUpToDateFile(final FileObject srcFile, final FileObject destFile) throws Exception { + // noop } /** @@ -387,6 +390,7 @@ public abstract class AbstractSyncTask extends VfsTask { * @throws Exception Implementation can throw any Exception. */ protected void handleMissingSourceFile(final FileObject destFile) throws Exception { + // noop } /** diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/FileObjectUtils.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/FileObjectUtils.java index 8783a04..3f626ed 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/FileObjectUtils.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/FileObjectUtils.java @@ -25,7 +25,20 @@ import org.apache.commons.vfs2.provider.AbstractFileObject; * Stuff to get some strange things from an FileObject. */ public final class FileObjectUtils { + private FileObjectUtils() { + // noop + } + + /** + * Null-safe call to {@link FileObject#exists()}. + * + * @param fileObject + * @return false if {@code fileObject} is null, otherwise, see {@link FileObject#exists()}. + * @throws FileSystemException On error determining if this file exists. + */ + public static boolean exists(final FileObject fileObject) throws FileSystemException { + return fileObject != null && fileObject.exists(); } /** diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/FileObjectUtilsTest.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/FileObjectUtilsTest.java new file mode 100644 index 0000000..f041037 --- /dev/null +++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/FileObjectUtilsTest.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.vfs2.util; + +import org.apache.commons.vfs2.FileSystemException; +import org.apache.commons.vfs2.VFS; +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests {@link FileObjectUtils}. + * + * @since 2.4 + */ +public class FileObjectUtilsTest { + + @Test + public void testExistsNotNull() throws FileSystemException { + Assert.assertFalse(FileObjectUtils.exists(VFS.getManager().getBaseFile())); + } + + @Test + public void testNotExistsNotNull() throws FileSystemException { + Assert.assertFalse(FileObjectUtils + .exists(VFS.getManager().resolveFile("file:///C:/This file can't possibly exist, right?"))); + } + + @Test + public void testNotExistsNull() throws FileSystemException { + Assert.assertFalse(FileObjectUtils.exists(null)); + } +} diff --git a/src/changes/changes.xml b/src/changes/changes.xml index d2ba03f..02c13da 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -110,6 +110,9 @@ The <action> type attribute can be add,update,fix,remove. <action issue="VFS-700" dev="ggregory" type="fix" due-to="Gary Gregory, Matthias Krueger"> Some tests fail on Java 11 and above. </action> + <action issue="VFS-712" dev="ggregory" type="add" due-to="Gary Gregory"> + Add null-safe org.apache.commons.vfs2.util.FileObjectUtils.exists(FileObject). + </action> </release> <release version="2.3" date="2019-02-01" description="New features and bug fix release."> <action issue="VFS-645" dev="ggregory" type="fix">