Skip to content

Commit 9842fd2

Browse files
committed
Add org.apache.commons.io.file.PathUtils.getPath(String, String)
1 parent 2594bb5 commit 9842fd2

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ The <action> type attribute can be add,update,fix,remove.
6262
<action dev="ggregory" type="add" due-to="Gary Gregory">Add org.apache.commons.io.output.ProxyOutputStream.writeRepeat(int, long).</action>
6363
<action dev="pkarwasz" type="add" due-to="Piotr P. Karwasz">Add length unit support in FileSystem limits.</action>
6464
<action dev="pkarwasz" type="add" due-to="Piotr P. Karwasz">Add IOUtils.toByteArray(InputStream, int, int) for safer chunked reading with size validation.</action>
65+
<action dev="pkarwasz" type="add" due-to="Piotr P. Karwasz">Add org.apache.commons.io.file.PathUtils.getPath(String, String).</action>
6566
<!-- UPDATE -->
6667
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 85 to 87 #774.</action>
6768
<action type="update" dev="ggregory" due-to="Gary Gregory">[test] Bump commons-codec:commons-codec from 1.18.0 to 1.19.0.</action>

src/main/java/org/apache/commons/io/file/PathUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,19 @@ private static Path getParent(final Path path) {
10961096
return path == null ? null : path.getParent();
10971097
}
10981098

1099+
/**
1100+
* Gets the system property with the specified name as a Path, or the default value as a Path if there is no property with that key.
1101+
*
1102+
* @param key the name of the system property.
1103+
* @param defaultPath a default path, may be null.
1104+
* @return the resulting {@code Path}, or the default value as a Path if there is no property with that key.
1105+
* @since 2.21.0
1106+
*/
1107+
public static Path getPath(final String key, final String defaultPath) {
1108+
final String property = System.getProperty(Objects.toString(key, defaultPath), defaultPath);
1109+
return property != null ? Paths.get(property) : null;
1110+
}
1111+
10991112
/**
11001113
* Shorthand for {@code Files.getFileAttributeView(path, PosixFileAttributeView.class)}.
11011114
*

src/test/java/org/apache/commons/io/file/PathUtilsTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,18 @@ void testGetLastModifiedFileTime_URL_Present() throws IOException, URISyntaxExce
351351
assertNotNull(PathUtils.getLastModifiedFileTime(current().toUri().toURL()));
352352
}
353353

354+
@Test
355+
void testGetPath() {
356+
final String validKey = "user.dir";
357+
final Path value = Paths.get(System.getProperty(validKey));
358+
assertEquals(value, PathUtils.getPath(validKey, null));
359+
assertEquals(value, PathUtils.getPath(validKey, validKey));
360+
final String invalidKey = "this property key does not exist";
361+
assertEquals(value, PathUtils.getPath(invalidKey, value.toString()));
362+
assertNull(PathUtils.getPath(invalidKey, null));
363+
assertEquals(value, PathUtils.getPath(null, value.toString()));
364+
}
365+
354366
@Test
355367
void testGetTempDirectory() {
356368
final Path tempDirectory = Paths.get(SystemProperties.getJavaIoTmpdir());

src/test/java/org/apache/commons/io/filefilter/AbstractConditionalFileFilterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public abstract class AbstractConditionalFileFilterTest extends AbstractIOFileFi
6060

6161
@BeforeEach
6262
public void setUp() {
63-
this.workingPath = determineWorkingDirectoryPath(getWorkingPathNamePropertyKey(), getDefaultWorkingPath());
63+
this.workingPath = getWorkingDirectoryPath(getWorkingPathNamePropertyKey(), getDefaultWorkingPath());
6464
this.file = new File(this.workingPath, TEST_FILE_NAME_PREFIX + 1 + TEST_FILE_TYPE);
6565
this.trueFilters = new TesterTrueFileFilter[4];
6666
this.falseFilters = new TesterFalseFileFilter[4];

src/test/java/org/apache/commons/io/filefilter/AbstractIOFileFilterTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.Objects;
2323
import java.util.stream.Stream;
2424

25+
import org.apache.commons.io.file.PathUtils;
26+
2527
public abstract class AbstractIOFileFilterTest {
2628

2729
final class TesterFalseFileFilter extends FalseFileFilter {
@@ -125,10 +127,9 @@ public static void assertTrueFiltersInvoked(final int testNumber, final TesterTr
125127
}
126128
}
127129

128-
public static File determineWorkingDirectoryPath(final String key, final String defaultPath) {
130+
public static File getWorkingDirectoryPath(final String key, final String defaultPath) {
129131
// Look for a system property to specify the working directory
130-
final String workingPathName = System.getProperty(key, defaultPath);
131-
return new File(workingPathName);
132+
return PathUtils.getPath(key, defaultPath).toFile();
132133
}
133134

134135
public static void resetFalseFilters(final TesterFalseFileFilter[] filters) {

0 commit comments

Comments
 (0)