1818package org .apache .commons .io .file ;
1919
2020import static org .apache .commons .io .file .CounterAssertions .assertCounts ;
21-
21+ import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
22+ import static org .junit .jupiter .api .Assertions .assertEquals ;
23+ import static org .junit .jupiter .api .Assertions .assertNotEquals ;
2224import java .io .IOException ;
25+ import java .nio .file .CopyOption ;
2326import java .nio .file .Path ;
2427import java .nio .file .Paths ;
2528import java .nio .file .StandardCopyOption ;
29+ import java .util .function .Supplier ;
2630
2731import org .apache .commons .io .file .Counters .PathCounters ;
32+ import org .apache .commons .io .filefilter .TrueFileFilter ;
2833import org .junit .jupiter .api .io .TempDir ;
2934import org .junit .jupiter .params .ParameterizedTest ;
3035import org .junit .jupiter .params .provider .MethodSource ;
3439 */
3540public class CopyDirectoryVisitorTest extends TestArguments {
3641
42+ private static final CopyOption [] EXPECTED_COPY_OPTIONS = {StandardCopyOption .REPLACE_EXISTING };
43+
3744 @ TempDir
3845 private Path targetDir ;
3946
@@ -44,8 +51,43 @@ public class CopyDirectoryVisitorTest extends TestArguments {
4451 @ MethodSource ("pathCounters" )
4552 public void testCopyDirectoryEmptyFolder (final PathCounters pathCounters ) throws IOException {
4653 try (TempDirectory sourceDir = TempDirectory .create (getClass ().getSimpleName ())) {
47- assertCounts (1 , 0 , 0 , PathUtils
48- .visitFileTree (new CopyDirectoryVisitor (pathCounters , sourceDir , targetDir , StandardCopyOption .REPLACE_EXISTING ), sourceDir .get ()));
54+ final Supplier <CopyDirectoryVisitor > supplier = () -> new CopyDirectoryVisitor (pathCounters , sourceDir , targetDir , EXPECTED_COPY_OPTIONS );
55+ final CopyDirectoryVisitor visitFileTree = PathUtils .visitFileTree (supplier .get (), sourceDir .get ());
56+ assertCounts (1 , 0 , 0 , visitFileTree );
57+ assertArrayEquals (EXPECTED_COPY_OPTIONS , visitFileTree .getCopyOptions ());
58+ assertEquals (sourceDir .get (), ((PathWrapper ) visitFileTree .getSourceDirectory ()).get ());
59+ assertEquals (sourceDir , visitFileTree .getSourceDirectory ());
60+ assertEquals (targetDir , visitFileTree .getTargetDirectory ());
61+ assertEquals (targetDir , visitFileTree .getTargetDirectory ());
62+ //
63+ assertEquals (visitFileTree , supplier .get ());
64+ assertEquals (visitFileTree .hashCode (), supplier .get ().hashCode ());
65+ assertEquals (visitFileTree , visitFileTree );
66+ assertEquals (visitFileTree .hashCode (), visitFileTree .hashCode ());
67+ assertNotEquals (visitFileTree , "not" );
68+ assertNotEquals (visitFileTree , CountingPathVisitor .withLongCounters ());
69+ }
70+ }
71+
72+ /**
73+ * Tests an empty folder.
74+ */
75+ @ ParameterizedTest
76+ @ MethodSource ("pathCounters" )
77+ public void testCopyDirectoryEmptyFolderFilters (final PathCounters pathCounters ) throws IOException {
78+ try (TempDirectory sourceDir = TempDirectory .create (getClass ().getSimpleName ())) {
79+ final Supplier <CopyDirectoryVisitor > supplier = () -> new CopyDirectoryVisitor (pathCounters , TrueFileFilter .INSTANCE , TrueFileFilter .INSTANCE ,
80+ sourceDir , targetDir , EXPECTED_COPY_OPTIONS );
81+ final CopyDirectoryVisitor visitFileTree = PathUtils .visitFileTree (supplier .get (), sourceDir .get ());
82+ assertCounts (1 , 0 , 0 , visitFileTree );
83+ assertArrayEquals (EXPECTED_COPY_OPTIONS , visitFileTree .getCopyOptions ());
84+ assertEquals (sourceDir , visitFileTree .getSourceDirectory ());
85+ assertEquals (targetDir , visitFileTree .getTargetDirectory ());
86+ //
87+ assertEquals (visitFileTree , supplier .get ());
88+ assertEquals (visitFileTree .hashCode (), supplier .get ().hashCode ());
89+ assertEquals (visitFileTree , visitFileTree );
90+ assertEquals (visitFileTree .hashCode (), visitFileTree .hashCode ());
4991 }
5092 }
5193
@@ -56,9 +98,17 @@ public void testCopyDirectoryEmptyFolder(final PathCounters pathCounters) throws
5698 @ MethodSource ("pathCounters" )
5799 public void testCopyDirectoryFolders1FileSize0 (final PathCounters pathCounters ) throws IOException {
58100 final Path sourceDir = Paths .get ("src/test/resources/org/apache/commons/io/dirs-1-file-size-0" );
59- assertCounts (1 , 1 , 0 , PathUtils .visitFileTree (
60- new CopyDirectoryVisitor (pathCounters , sourceDir , targetDir , StandardCopyOption .REPLACE_EXISTING ),
61- sourceDir ));
101+ final Supplier <CopyDirectoryVisitor > supplier = () -> new CopyDirectoryVisitor (pathCounters , sourceDir , targetDir , EXPECTED_COPY_OPTIONS );
102+ final CopyDirectoryVisitor visitFileTree = PathUtils .visitFileTree (supplier .get (), sourceDir );
103+ assertCounts (1 , 1 , 0 , visitFileTree );
104+ assertArrayEquals (EXPECTED_COPY_OPTIONS , visitFileTree .getCopyOptions ());
105+ assertEquals (sourceDir , visitFileTree .getSourceDirectory ());
106+ assertEquals (targetDir , visitFileTree .getTargetDirectory ());
107+ //
108+ assertEquals (visitFileTree , supplier .get ());
109+ assertEquals (visitFileTree .hashCode (), supplier .get ().hashCode ());
110+ assertEquals (visitFileTree , visitFileTree );
111+ assertEquals (visitFileTree .hashCode (), visitFileTree .hashCode ());
62112 }
63113
64114 /**
@@ -68,9 +118,12 @@ public void testCopyDirectoryFolders1FileSize0(final PathCounters pathCounters)
68118 @ MethodSource ("pathCounters" )
69119 public void testCopyDirectoryFolders1FileSize1 (final PathCounters pathCounters ) throws IOException {
70120 final Path sourceDir = Paths .get ("src/test/resources/org/apache/commons/io/dirs-1-file-size-1" );
71- assertCounts (1 , 1 , 1 , PathUtils .visitFileTree (
72- new CopyDirectoryVisitor (pathCounters , sourceDir , targetDir , StandardCopyOption .REPLACE_EXISTING ),
73- sourceDir ));
121+ final CopyDirectoryVisitor visitFileTree = PathUtils .visitFileTree (new CopyDirectoryVisitor (pathCounters , sourceDir , targetDir , EXPECTED_COPY_OPTIONS ),
122+ sourceDir );
123+ assertCounts (1 , 1 , 1 , visitFileTree );
124+ assertArrayEquals (EXPECTED_COPY_OPTIONS , visitFileTree .getCopyOptions ());
125+ assertEquals (sourceDir , visitFileTree .getSourceDirectory ());
126+ assertEquals (targetDir , visitFileTree .getTargetDirectory ());
74127 }
75128
76129 /**
@@ -80,9 +133,12 @@ public void testCopyDirectoryFolders1FileSize1(final PathCounters pathCounters)
80133 @ MethodSource ("pathCounters" )
81134 public void testCopyDirectoryFolders2FileSize2 (final PathCounters pathCounters ) throws IOException {
82135 final Path sourceDir = Paths .get ("src/test/resources/org/apache/commons/io/dirs-2-file-size-2" );
83- assertCounts (3 , 2 , 2 , PathUtils .visitFileTree (
84- new CopyDirectoryVisitor (pathCounters , sourceDir , targetDir , StandardCopyOption .REPLACE_EXISTING ),
85- sourceDir ));
136+ final CopyDirectoryVisitor visitFileTree = PathUtils .visitFileTree (new CopyDirectoryVisitor (pathCounters , sourceDir , targetDir , EXPECTED_COPY_OPTIONS ),
137+ sourceDir );
138+ assertCounts (3 , 2 , 2 , visitFileTree );
139+ assertArrayEquals (EXPECTED_COPY_OPTIONS , visitFileTree .getCopyOptions ());
140+ assertEquals (sourceDir , visitFileTree .getSourceDirectory ());
141+ assertEquals (targetDir , visitFileTree .getTargetDirectory ());
86142 }
87143
88144}
0 commit comments