43
43
import java .nio .file .Path ;
44
44
import java .nio .file .Paths ;
45
45
import java .nio .file .attribute .FileTime ;
46
- import java .util .ArrayList ;
47
46
import java .util .Arrays ;
48
47
import java .util .Date ;
49
48
import java .util .Iterator ;
50
49
import java .util .LinkedList ;
51
50
import java .util .List ;
52
51
import java .util .Map ;
53
52
import java .util .Set ;
53
+ import java .util .stream .Collectors ;
54
54
55
55
import org .apache .commons .lang3 .time .DateUtils ;
56
56
import org .eclipse .jgit .api .Git ;
@@ -1013,9 +1013,12 @@ void testGetLastHistoryEntry() throws Exception {
1013
1013
* getting history cache entries for directories.
1014
1014
*/
1015
1015
@ Test
1016
- void testGetLastHistoryEntries () throws Exception {
1016
+ void testFillLastHistoryEntries () throws Exception {
1017
1017
File repositoryRoot = new File (repositories .getSourceRoot (), "git" );
1018
1018
Repository repository = RepositoryFactory .getRepository (repositoryRoot );
1019
+
1020
+ // Create non-empty directory without repository involvement. This will be used to check
1021
+ // that fillLastHistoryEntries() does not attempt to get history entry for it.
1019
1022
File subDir = new File (repositoryRoot , "subdir" );
1020
1023
assertTrue (subDir .mkdir ());
1021
1024
File subFile = new File (subDir , "subfile.txt" );
@@ -1031,14 +1034,51 @@ void testGetLastHistoryEntries() throws Exception {
1031
1034
assertNotNull (files );
1032
1035
assertTrue (files .length > 0 );
1033
1036
assertTrue (Arrays .stream (files ).anyMatch (File ::isDirectory ));
1034
- List <DirectoryEntry > directoryEntries = new ArrayList <>();
1035
- for (File file : files ) {
1036
- directoryEntries .add (new DirectoryEntry (file ));
1037
- }
1037
+ List <DirectoryEntry > directoryEntries = Arrays .stream (files ).map (DirectoryEntry ::new ).
1038
+ collect (Collectors .toList ());
1038
1039
1039
- spyCache .fillLastHistoryEntries (directoryEntries );
1040
+ assertTrue ( spyCache .fillLastHistoryEntries (directoryEntries ) );
1040
1041
Mockito .verify (spyCache , never ()).getLastHistoryEntry (ArgumentMatchers .eq (subDir ));
1041
1042
1043
+ assertEquals (directoryEntries .size () - 3 ,
1044
+ (int ) directoryEntries .stream ().filter (e -> e .getDate () != null ).count ());
1045
+ assertEquals (directoryEntries .size (),
1046
+ (int ) directoryEntries .stream ().filter (e -> e .getDescription () != null ).count ());
1047
+
1048
+ // Cleanup.
1049
+ cache .clear (repository );
1050
+ }
1051
+
1052
+ /**
1053
+ * Test {@link FileHistoryCache#fillLastHistoryEntries(List)}, in particular that it
1054
+ * returns {@code false} and resets date/descriptions if some entries cannot be filled.
1055
+ */
1056
+ @ Test
1057
+ void testFillLastHistoryEntriesAllOrNothing () throws Exception {
1058
+ File repositoryRoot = new File (repositories .getSourceRoot (), "git" );
1059
+ Repository repository = RepositoryFactory .getRepository (repositoryRoot );
1060
+
1061
+ // This file will be created without any repository involvement, therefore it will not be possible
1062
+ // to get history entry for it. This should make fillLastHistoryEntries() to return false.
1063
+ File subFile = new File (repositoryRoot , "file.txt" );
1064
+ assertFalse (subFile .exists ());
1065
+ assertTrue (subFile .createNewFile ());
1066
+
1067
+ FileHistoryCache spyCache = Mockito .spy (cache );
1068
+ spyCache .clear (repository );
1069
+ History historyToStore = repository .getHistory (repositoryRoot );
1070
+ spyCache .store (historyToStore , repository );
1071
+
1072
+ File [] files = repositoryRoot .listFiles ();
1073
+ assertNotNull (files );
1074
+ assertTrue (files .length > 0 );
1075
+ List <DirectoryEntry > directoryEntries = Arrays .stream (files ).map (DirectoryEntry ::new ).
1076
+ collect (Collectors .toList ());
1077
+
1078
+ assertFalse (spyCache .fillLastHistoryEntries (directoryEntries ));
1079
+ assertEquals (0 , (int ) directoryEntries .stream ().filter (e -> e .getDate () != null ).count ());
1080
+ assertEquals (0 , (int ) directoryEntries .stream ().filter (e -> e .getDescription () != null ).count ());
1081
+
1042
1082
// Cleanup.
1043
1083
cache .clear (repository );
1044
1084
}
0 commit comments