Skip to content

Commit 84b8d6e

Browse files
Copilotslachiewicz
andcommitted
Add tests for new Path-based methods
Co-authored-by: slachiewicz <[email protected]>
1 parent 78063ba commit 84b8d6e

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed

src/test/java/org/codehaus/plexus/components/io/attributes/AttributeUtilsTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.codehaus.plexus.components.io.attributes;
22

33
import java.io.File;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
46
import java.nio.file.attribute.PosixFilePermission;
57
import java.util.Set;
68

@@ -62,4 +64,20 @@ void chmodBackAndForth() throws Exception {
6264
assertTrue(secondAttrs.isOwnerWritable());
6365
assertTrue(secondAttrs.isOwnerExecutable());
6466
}
67+
68+
@Test
69+
@DisabledOnOs(OS.WINDOWS)
70+
void chmodBackAndForthWithPath() throws Exception {
71+
final Path bxx = Files.createTempFile("bxx", "ff");
72+
AttributeUtils.chmod(bxx, 0422);
73+
PlexusIoResourceAttributes firstAttrs = new FileAttributes(bxx);
74+
assertTrue(firstAttrs.isOwnerReadable());
75+
assertFalse(firstAttrs.isOwnerWritable());
76+
assertFalse(firstAttrs.isOwnerExecutable());
77+
AttributeUtils.chmod(bxx, 0777);
78+
PlexusIoResourceAttributes secondAttrs = new FileAttributes(bxx);
79+
assertTrue(secondAttrs.isOwnerReadable());
80+
assertTrue(secondAttrs.isOwnerWritable());
81+
assertTrue(secondAttrs.isOwnerExecutable());
82+
}
6583
}

src/test/java/org/codehaus/plexus/components/io/attributes/FileAttributesTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.nio.file.Files;
21+
import java.nio.file.Path;
2122

2223
import org.junit.jupiter.api.Test;
2324
import org.junit.jupiter.api.condition.DisabledOnOs;
@@ -52,4 +53,28 @@ void fileAttributesHandlesIOException() throws Exception {
5253
tempFile.delete();
5354
}
5455
}
56+
57+
@Test
58+
@DisabledOnOs(OS.WINDOWS)
59+
void getPosixFileAttributesWithPath() throws Exception {
60+
Path path = java.nio.file.Paths.get(".");
61+
PlexusIoResourceAttributes fa = new FileAttributes(path);
62+
assertNotNull(fa);
63+
}
64+
65+
@Test
66+
void fileAttributesHandlesIOExceptionWithPath() throws Exception {
67+
// Test that FileAttributes can be constructed for a regular file using Path
68+
// even if ownership information is not available (e.g., WSL2 mapped network drives)
69+
Path tempPath = Files.createTempFile("plexus-io-test", ".tmp");
70+
try {
71+
// This should not throw even if ownership info is unavailable
72+
PlexusIoResourceAttributes fa = new FileAttributes(tempPath);
73+
assertNotNull(fa);
74+
// The attributes object should be usable even if userName/groupName are null
75+
assertNotNull(fa.toString());
76+
} finally {
77+
Files.deleteIfExists(tempPath);
78+
}
79+
}
5580
}

src/test/java/org/codehaus/plexus/components/io/attributes/PlexusIoResourceAttributeUtilsTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.File;
2020
import java.net.URL;
2121
import java.nio.file.NoSuchFileException;
22+
import java.nio.file.Path;
2223
import java.util.Map;
2324

2425
import org.codehaus.plexus.util.StringUtils;
@@ -326,4 +327,50 @@ void mergeAttributesDefault() {
326327
assertNull(attributes.getGroupName());
327328
assertEquals(0, attributes.getOctalMode());
328329
}
330+
331+
@Test
332+
@DisabledOnOs(OS.WINDOWS)
333+
void getAttributesForThisTestClassWithPath() throws Exception {
334+
URL resource = Thread.currentThread()
335+
.getContextClassLoader()
336+
.getResource(getClass().getName().replace('.', '/') + ".class");
337+
338+
if (resource == null) {
339+
throw new IllegalStateException("SOMETHING IS VERY WRONG. CANNOT FIND THIS TEST CLASS IN THE CLASSLOADER.");
340+
}
341+
342+
Path path = java.nio.file.Paths.get(resource.getPath().replaceAll("%20", " "));
343+
344+
Map<String, PlexusIoResourceAttributes> attrs =
345+
PlexusIoResourceAttributeUtils.getFileAttributesByPath(path, true);
346+
347+
PlexusIoResourceAttributes fileAttrs = attrs.get(path.toFile().getAbsolutePath());
348+
349+
assertNotNull(fileAttrs);
350+
assertTrue(fileAttrs.isOwnerReadable());
351+
assertEquals(System.getProperty("user.name"), fileAttrs.getUserName());
352+
}
353+
354+
@Test
355+
@DisabledOnOs(OS.WINDOWS)
356+
void srcResourceWithPath() throws Exception {
357+
Path dir = java.nio.file.Paths.get("src/test/resources/symlinks");
358+
final Map<String, PlexusIoResourceAttributes> fileAttributesByPathScreenScrape =
359+
PlexusIoResourceAttributeUtils.getFileAttributesByPath(dir, true);
360+
assertNotNull(fileAttributesByPathScreenScrape);
361+
PlexusIoResourceAttributes pr = null;
362+
for (String s : fileAttributesByPathScreenScrape.keySet()) {
363+
if (s.endsWith("targetFile.txt")) pr = fileAttributesByPathScreenScrape.get(s);
364+
}
365+
assertNotNull(pr);
366+
367+
assertTrue(pr.getOctalMode() > 0);
368+
}
369+
370+
@Test
371+
void nonExistingDirectoryWithPath() {
372+
Path dir = java.nio.file.Paths.get("src/test/noSuchDirectory");
373+
assertThrows(
374+
NoSuchFileException.class, () -> PlexusIoResourceAttributeUtils.getFileAttributesByPath(dir, true));
375+
}
329376
}

src/test/java/org/codehaus/plexus/components/io/attributes/SymlinkUtilsTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.File;
2020
import java.io.IOException;
2121
import java.nio.file.Files;
22+
import java.nio.file.Path;
2223

2324
import org.apache.commons.io.FileUtils;
2425
import org.junit.jupiter.api.BeforeEach;
@@ -57,6 +58,27 @@ void create_read_symbolic_link_to_directory() throws Exception {
5758
assertEquals(new File("aSubDir"), SymlinkUtils.readSymbolicLink(new File(target, "symlinkToDir")));
5859
}
5960

61+
@Test
62+
void create_read_symbolic_link_to_file_with_path() throws Exception {
63+
Path symlink = target.toPath().resolve("symlinkToTarget");
64+
File relativePath = createTargetFile(target);
65+
SymlinkUtils.createSymbolicLink(symlink, relativePath.toPath());
66+
assertEquals(expected, FileUtils.readFileToString(symlink.toFile(), UTF_8));
67+
assertEquals(java.nio.file.Paths.get("actualFile"), SymlinkUtils.readSymbolicLink(symlink));
68+
}
69+
70+
@Test
71+
void create_read_symbolic_link_to_directory_with_path() throws Exception {
72+
File subDir = new File(target, "aSubDir");
73+
createTargetFile(subDir);
74+
Path symlink = target.toPath().resolve("symlinkToDir");
75+
SymlinkUtils.createSymbolicLink(symlink, java.nio.file.Paths.get("aSubDir"));
76+
assertEquals(
77+
expected,
78+
FileUtils.readFileToString(symlink.resolve("actualFile").toFile(), UTF_8));
79+
assertEquals(java.nio.file.Paths.get("aSubDir"), SymlinkUtils.readSymbolicLink(symlink));
80+
}
81+
6082
private File createTargetFile(File target) throws IOException {
6183
File relativePath = new File("actualFile");
6284
File actualFile = new File(target, relativePath.getPath());
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.codehaus.plexus.components.io.resources;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
7+
import org.apache.commons.io.FileUtils;
8+
import org.junit.jupiter.api.Test;
9+
10+
import static java.nio.charset.StandardCharsets.UTF_8;
11+
import static org.junit.jupiter.api.Assertions.assertNotNull;
12+
import static org.junit.jupiter.api.Assertions.assertTrue;
13+
14+
class ResourceFactoryPathTest {
15+
16+
@Test
17+
void testCreateResourceWithPath() throws IOException {
18+
Path tempPath = Files.createTempFile("test", ".txt");
19+
try {
20+
FileUtils.write(tempPath.toFile(), "test content", UTF_8);
21+
PlexusIoResource resource = ResourceFactory.createResource(tempPath);
22+
assertNotNull(resource);
23+
assertTrue(resource.isExisting());
24+
} finally {
25+
Files.deleteIfExists(tempPath);
26+
}
27+
}
28+
29+
@Test
30+
void testCreateResourceWithPathAndName() throws IOException {
31+
Path tempPath = Files.createTempFile("test", ".txt");
32+
try {
33+
FileUtils.write(tempPath.toFile(), "test content", UTF_8);
34+
PlexusIoResource resource = ResourceFactory.createResource(tempPath, "custom-name.txt");
35+
assertNotNull(resource);
36+
assertTrue(resource.isExisting());
37+
} finally {
38+
Files.deleteIfExists(tempPath);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)