|  | 
|  | 1 | +package org.codehaus.plexus.components.io.resources; | 
|  | 2 | + | 
|  | 3 | +import java.io.File; | 
|  | 4 | +import java.util.ArrayList; | 
|  | 5 | +import java.util.Iterator; | 
|  | 6 | +import java.util.List; | 
|  | 7 | + | 
|  | 8 | +import org.codehaus.plexus.components.io.functions.SymlinkDestinationSupplier; | 
|  | 9 | +import org.junit.jupiter.api.Test; | 
|  | 10 | +import org.junit.jupiter.api.condition.DisabledOnOs; | 
|  | 11 | +import org.junit.jupiter.api.condition.OS; | 
|  | 12 | + | 
|  | 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; | 
|  | 14 | +import static org.junit.jupiter.api.Assertions.assertFalse; | 
|  | 15 | +import static org.junit.jupiter.api.Assertions.assertTrue; | 
|  | 16 | + | 
|  | 17 | +/** | 
|  | 18 | + * Test for symlink handling in PlexusIoFileResourceCollection | 
|  | 19 | + */ | 
|  | 20 | +public class PlexusIoFileResourceCollectionSymlinkTest { | 
|  | 21 | + | 
|  | 22 | +    @Test | 
|  | 23 | +    @DisabledOnOs(OS.WINDOWS) | 
|  | 24 | +    void testFollowSymlinksToFilesEnabled() throws Exception { | 
|  | 25 | +        PlexusIoFileResourceCollection collection = new PlexusIoFileResourceCollection(); | 
|  | 26 | +        collection.setBaseDir(new File("src/test/resources/symlinks/src")); | 
|  | 27 | +        collection.setIncludes(new String[] {"symR"}); | 
|  | 28 | +        collection.setFollowingSymLinks(true); | 
|  | 29 | + | 
|  | 30 | +        List<PlexusIoResource> resources = collectResources(collection); | 
|  | 31 | +        assertEquals(1, resources.size()); | 
|  | 32 | + | 
|  | 33 | +        PlexusIoResource resource = resources.get(0); | 
|  | 34 | +        assertEquals("symR", resource.getName()); | 
|  | 35 | +        // When following symlinks, the resource should NOT be detected as a symlink | 
|  | 36 | +        assertFalse(resource.isSymbolicLink(), "Resource should not be a symlink when followSymlinks=true"); | 
|  | 37 | +        assertTrue(resource.isFile(), "Resource should be a file"); | 
|  | 38 | +        assertEquals(38, resource.getSize(), "Should have size of target file"); | 
|  | 39 | +    } | 
|  | 40 | + | 
|  | 41 | +    @Test | 
|  | 42 | +    @DisabledOnOs(OS.WINDOWS) | 
|  | 43 | +    void testFollowSymlinksToFilesDisabled() throws Exception { | 
|  | 44 | +        PlexusIoFileResourceCollection collection = new PlexusIoFileResourceCollection(); | 
|  | 45 | +        collection.setBaseDir(new File("src/test/resources/symlinks/src")); | 
|  | 46 | +        collection.setIncludes(new String[] {"symR"}); | 
|  | 47 | +        collection.setFollowingSymLinks(false); | 
|  | 48 | + | 
|  | 49 | +        List<PlexusIoResource> resources = collectResources(collection); | 
|  | 50 | +        assertEquals(1, resources.size()); | 
|  | 51 | + | 
|  | 52 | +        PlexusIoResource resource = resources.get(0); | 
|  | 53 | +        assertEquals("symR", resource.getName()); | 
|  | 54 | +        // When not following symlinks, the resource should be detected as a symlink | 
|  | 55 | +        assertTrue(resource.isSymbolicLink(), "Resource should be a symlink when followSymlinks=false"); | 
|  | 56 | +        assertTrue(resource instanceof SymlinkDestinationSupplier); | 
|  | 57 | +        assertEquals("fileR.txt", ((SymlinkDestinationSupplier) resource).getSymlinkDestination()); | 
|  | 58 | +    } | 
|  | 59 | + | 
|  | 60 | +    @Test | 
|  | 61 | +    @DisabledOnOs(OS.WINDOWS) | 
|  | 62 | +    void testFollowSymlinksToDirsEnabled() throws Exception { | 
|  | 63 | +        PlexusIoFileResourceCollection collection = new PlexusIoFileResourceCollection(); | 
|  | 64 | +        collection.setBaseDir(new File("src/test/resources/symlinks/src")); | 
|  | 65 | +        collection.setIncludes(new String[] {"symDir/**"}); | 
|  | 66 | +        collection.setFollowingSymLinks(true); | 
|  | 67 | + | 
|  | 68 | +        List<PlexusIoResource> resources = collectResources(collection); | 
|  | 69 | +        // Should include the symDir itself and files inside targetDir | 
|  | 70 | +        assertTrue(resources.size() >= 1, "Should find resources when following directory symlinks"); | 
|  | 71 | +    } | 
|  | 72 | + | 
|  | 73 | +    @Test | 
|  | 74 | +    @DisabledOnOs(OS.WINDOWS) | 
|  | 75 | +    void testFollowSymlinksToDirsDisabled() throws Exception { | 
|  | 76 | +        PlexusIoFileResourceCollection collection = new PlexusIoFileResourceCollection(); | 
|  | 77 | +        collection.setBaseDir(new File("src/test/resources/symlinks/src")); | 
|  | 78 | +        collection.setIncludes(new String[] {"symDir/**"}); | 
|  | 79 | +        collection.setFollowingSymLinks(false); | 
|  | 80 | + | 
|  | 81 | +        List<PlexusIoResource> resources = collectResources(collection); | 
|  | 82 | +        // Should not follow the directory symlink, so fewer or no resources | 
|  | 83 | +        // The exact behavior depends on DirectoryScanner | 
|  | 84 | +        assertTrue(resources.size() >= 0); | 
|  | 85 | +    } | 
|  | 86 | + | 
|  | 87 | +    @Test | 
|  | 88 | +    @DisabledOnOs(OS.WINDOWS) | 
|  | 89 | +    void testMultipleSymlinksFollowEnabled() throws Exception { | 
|  | 90 | +        PlexusIoFileResourceCollection collection = new PlexusIoFileResourceCollection(); | 
|  | 91 | +        collection.setBaseDir(new File("src/test/resources/symlinks/src")); | 
|  | 92 | +        collection.setIncludes(new String[] {"symR", "symW", "symX"}); | 
|  | 93 | +        collection.setFollowingSymLinks(true); | 
|  | 94 | + | 
|  | 95 | +        List<PlexusIoResource> resources = collectResources(collection); | 
|  | 96 | +        assertEquals(3, resources.size()); | 
|  | 97 | + | 
|  | 98 | +        for (PlexusIoResource resource : resources) { | 
|  | 99 | +            assertFalse( | 
|  | 100 | +                    resource.isSymbolicLink(), | 
|  | 101 | +                    "Resource " + resource.getName() + " should not be a symlink when followSymlinks=true"); | 
|  | 102 | +        } | 
|  | 103 | +    } | 
|  | 104 | + | 
|  | 105 | +    @Test | 
|  | 106 | +    @DisabledOnOs(OS.WINDOWS) | 
|  | 107 | +    void testMultipleSymlinksFollowDisabled() throws Exception { | 
|  | 108 | +        PlexusIoFileResourceCollection collection = new PlexusIoFileResourceCollection(); | 
|  | 109 | +        collection.setBaseDir(new File("src/test/resources/symlinks/src")); | 
|  | 110 | +        collection.setIncludes(new String[] {"symR", "symW", "symX"}); | 
|  | 111 | +        collection.setFollowingSymLinks(false); | 
|  | 112 | + | 
|  | 113 | +        List<PlexusIoResource> resources = collectResources(collection); | 
|  | 114 | +        assertEquals(3, resources.size()); | 
|  | 115 | + | 
|  | 116 | +        for (PlexusIoResource resource : resources) { | 
|  | 117 | +            assertTrue( | 
|  | 118 | +                    resource.isSymbolicLink(), | 
|  | 119 | +                    "Resource " + resource.getName() + " should be a symlink when followSymlinks=false"); | 
|  | 120 | +        } | 
|  | 121 | +    } | 
|  | 122 | + | 
|  | 123 | +    private List<PlexusIoResource> collectResources(PlexusIoFileResourceCollection collection) throws Exception { | 
|  | 124 | +        List<PlexusIoResource> result = new ArrayList<>(); | 
|  | 125 | +        Iterator<PlexusIoResource> resources = collection.getResources(); | 
|  | 126 | +        while (resources.hasNext()) { | 
|  | 127 | +            result.add(resources.next()); | 
|  | 128 | +        } | 
|  | 129 | +        return result; | 
|  | 130 | +    } | 
|  | 131 | +} | 
0 commit comments